Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 37e8210411bf1ac9a321a11c161b264e4988b032 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************
 * Copyright (c) 2007, 2009 SAP AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * SAP AG - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.web.validation;

/**
 * Utility method for URL patterns.
 * 
 * <p>
 * Could be used by components dealing with URL patterns like: Servlet and
 * Filter wizards, web.xml validators, etc.
 * </p>
 * 
 * @author Kaloyan Raev
 */
public class UrlPattern {

	/**
	 * Validates an URL pattern.
	 * 
	 * @param urlPattern
	 *            the string representation of the URL pattern to validate
	 * 
	 * @return <code>true</code> if the given pattern is a valid one,
	 *         <code>false</code> - otherwise.
	 */
	public static boolean isValid(String urlPattern) {
		// URL Pattern must not be empty string
		if (urlPattern.length() == 0)
			return false;

		// URL Pattern must not contain Carriage Return characters
		if (urlPattern.indexOf('\r') != -1)
			return false;

		// URL Pattern must not contain New Line characters
		if (urlPattern.indexOf('\n') != -1)
			return false;

		// Path Mappings must not contain "*." character sequences
		if (urlPattern.startsWith("/")) { //$NON-NLS-1$
			if (urlPattern.indexOf("*.") == -1) { //$NON-NLS-1$
				return true;
			}
			return false;
		}

		// Extension Mappings must not contain '/' characters
		if (urlPattern.startsWith("*.")) { //$NON-NLS-1$
			if (urlPattern.indexOf('/') == -1) {
				return true;
			}
			return false;
		}

		// The URL Pattern is neither a Path Mapping, nor Extension Mapping
		// Therefore, it is invalid
		return false;
	}

}

Back to the top