2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http.pathmap;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 public class RegexPathSpec extends PathSpec
26 protected Pattern pattern;
28 protected RegexPathSpec()
33 public RegexPathSpec(String regex)
35 super.pathSpec = regex;
36 boolean inGrouping = false;
38 this.specLength = pathSpec.length();
39 // build up a simple signature we can use to identify the grouping
40 StringBuilder signature = new StringBuilder();
41 for (char c : pathSpec.toCharArray())
50 signature.append('g'); // glob
53 signature.append('g'); // glob
64 if (Character.isLetterOrDigit(c))
66 signature.append('l'); // literal (exact)
72 this.pattern = Pattern.compile(pathSpec);
74 // Figure out the grouping based on the signature
75 String sig = signature.toString();
77 if (Pattern.matches("^l*$",sig))
79 this.group = PathSpecGroup.EXACT;
81 else if (Pattern.matches("^l*g+",sig))
83 this.group = PathSpecGroup.PREFIX_GLOB;
85 else if (Pattern.matches("^g+l+$",sig))
87 this.group = PathSpecGroup.SUFFIX_GLOB;
91 this.group = PathSpecGroup.MIDDLE_GLOB;
95 public Matcher getMatcher(String path)
97 return this.pattern.matcher(path);
101 public String getPathInfo(String path)
103 // Path Info only valid for PREFIX_GLOB types
104 if (group == PathSpecGroup.PREFIX_GLOB)
106 Matcher matcher = getMatcher(path);
107 if (matcher.matches())
109 if (matcher.groupCount() >= 1)
111 String pathInfo = matcher.group(1);
112 if ("".equals(pathInfo))
127 public String getPathMatch(String path)
129 Matcher matcher = getMatcher(path);
130 if (matcher.matches())
132 if (matcher.groupCount() >= 1)
134 int idx = matcher.start(1);
137 if (path.charAt(idx - 1) == '/')
141 return path.substring(0,idx);
149 public Pattern getPattern()
155 public String getRelativePath(String base, String path)
157 // TODO Auto-generated method stub
162 public boolean matches(final String path)
164 int idx = path.indexOf('?');
167 // match only non-query part
168 return getMatcher(path.substring(0,idx)).matches();
173 return getMatcher(path).matches();