@Test public void parsesPathVariablesOutOfATwoLevelPatternWithTwoVars(){ Path r = new SimplePathPattern("/{apple}/{orange}").match("/jane/doe"); assertNotNull(r); assertEquals(2, r.size()); assertEquals("jane", r.valueFor("apple")); assertEquals("doe", r.valueFor("orange")); }
public Path match(String path){ if(path == null) return null; String[] segments = path.split(Pattern.quote("/")); stripQueryString(segments); List<PathParam> params = new LinkedList<PathParam>(); boolean matches = true; Seg lastSeg = null; String wildcardMatch = ""; int limit = Math.max(segs.size(), segments.length); for(int x=0;x<limit;x++){ Seg seg = x<segs.size()?segs.get(x):null; String s = x<segments.length?segments[x]:null; if(seg==null && (lastSeg==null || !lastSeg.hasWildcard)){ matches = false; break; }else if(lastSeg!=null && lastSeg.hasWildcard){ if(s.length()>0 && s!=null){ wildcardMatch += "/"; } wildcardMatch += s; }else if(seg!=null){ if(seg.isVar){ if(seg.hasWildcard && s!=null){ wildcardMatch += s; }else{ params.add(seg.name.withValue(s)); } }else if(!seg.name.equals(s) && !seg.name.equals("*")){ matches = false; } } if(seg!=null) lastSeg = seg; } if(lastSeg!=null && lastSeg.hasWildcard && !wildcardMatch.isEmpty()){ params.add(lastSeg.name.withValue(wildcardMatch)); } if(matches){ return new Path(path, params.toArray(new PathParam[]{})); }else{ return null; } }
SimplePathPattern implements PathPattern { public Path match(String path){ if(path == null) return null; String[] segments = path.split(Pattern.quote("/")); stripQueryString(segments); List<PathParam> params = new LinkedList<PathParam>(); boolean matches = true; Seg lastSeg = null; String wildcardMatch = ""; int limit = Math.max(segs.size(), segments.length); for(int x=0;x<limit;x++){ Seg seg = x<segs.size()?segs.get(x):null; String s = x<segments.length?segments[x]:null; if(seg==null && (lastSeg==null || !lastSeg.hasWildcard)){ matches = false; break; }else if(lastSeg!=null && lastSeg.hasWildcard){ if(s.length()>0 && s!=null){ wildcardMatch += "/"; } wildcardMatch += s; }else if(seg!=null){ if(seg.isVar){ if(seg.hasWildcard && s!=null){ wildcardMatch += s; }else{ params.add(seg.name.withValue(s)); } }else if(!seg.name.equals(s) && !seg.name.equals("*")){ matches = false; } } if(seg!=null) lastSeg = seg; } if(lastSeg!=null && lastSeg.hasWildcard && !wildcardMatch.isEmpty()){ params.add(lastSeg.name.withValue(wildcardMatch)); } if(matches){ return new Path(path, params.toArray(new PathParam[]{})); }else{ return null; } } }
SimplePathPattern implements PathPattern { public Path match(String path){ if(path == null) return null; String[] segments = path.split(Pattern.quote("/")); stripQueryString(segments); List<PathParam> params = new LinkedList<PathParam>(); boolean matches = true; Seg lastSeg = null; String wildcardMatch = ""; int limit = Math.max(segs.size(), segments.length); for(int x=0;x<limit;x++){ Seg seg = x<segs.size()?segs.get(x):null; String s = x<segments.length?segments[x]:null; if(seg==null && (lastSeg==null || !lastSeg.hasWildcard)){ matches = false; break; }else if(lastSeg!=null && lastSeg.hasWildcard){ if(s.length()>0 && s!=null){ wildcardMatch += "/"; } wildcardMatch += s; }else if(seg!=null){ if(seg.isVar){ if(seg.hasWildcard && s!=null){ wildcardMatch += s; }else{ params.add(seg.name.withValue(s)); } }else if(!seg.name.equals(s) && !seg.name.equals("*")){ matches = false; } } if(seg!=null) lastSeg = seg; } if(lastSeg!=null && lastSeg.hasWildcard && !wildcardMatch.isEmpty()){ params.add(lastSeg.name.withValue(wildcardMatch)); } if(matches){ return new Path(path, params.toArray(new PathParam[]{})); }else{ return null; } } SimplePathPattern(String pattern); }
SimplePathPattern implements PathPattern { public Path match(String path){ if(path == null) return null; String[] segments = path.split(Pattern.quote("/")); stripQueryString(segments); List<PathParam> params = new LinkedList<PathParam>(); boolean matches = true; Seg lastSeg = null; String wildcardMatch = ""; int limit = Math.max(segs.size(), segments.length); for(int x=0;x<limit;x++){ Seg seg = x<segs.size()?segs.get(x):null; String s = x<segments.length?segments[x]:null; if(seg==null && (lastSeg==null || !lastSeg.hasWildcard)){ matches = false; break; }else if(lastSeg!=null && lastSeg.hasWildcard){ if(s.length()>0 && s!=null){ wildcardMatch += "/"; } wildcardMatch += s; }else if(seg!=null){ if(seg.isVar){ if(seg.hasWildcard && s!=null){ wildcardMatch += s; }else{ params.add(seg.name.withValue(s)); } }else if(!seg.name.equals(s) && !seg.name.equals("*")){ matches = false; } } if(seg!=null) lastSeg = seg; } if(lastSeg!=null && lastSeg.hasWildcard && !wildcardMatch.isEmpty()){ params.add(lastSeg.name.withValue(wildcardMatch)); } if(matches){ return new Path(path, params.toArray(new PathParam[]{})); }else{ return null; } } SimplePathPattern(String pattern); List<PathParamName> varNames(); boolean matches(String path); Path match(String path); String raw(); }
SimplePathPattern implements PathPattern { public Path match(String path){ if(path == null) return null; String[] segments = path.split(Pattern.quote("/")); stripQueryString(segments); List<PathParam> params = new LinkedList<PathParam>(); boolean matches = true; Seg lastSeg = null; String wildcardMatch = ""; int limit = Math.max(segs.size(), segments.length); for(int x=0;x<limit;x++){ Seg seg = x<segs.size()?segs.get(x):null; String s = x<segments.length?segments[x]:null; if(seg==null && (lastSeg==null || !lastSeg.hasWildcard)){ matches = false; break; }else if(lastSeg!=null && lastSeg.hasWildcard){ if(s.length()>0 && s!=null){ wildcardMatch += "/"; } wildcardMatch += s; }else if(seg!=null){ if(seg.isVar){ if(seg.hasWildcard && s!=null){ wildcardMatch += s; }else{ params.add(seg.name.withValue(s)); } }else if(!seg.name.equals(s) && !seg.name.equals("*")){ matches = false; } } if(seg!=null) lastSeg = seg; } if(lastSeg!=null && lastSeg.hasWildcard && !wildcardMatch.isEmpty()){ params.add(lastSeg.name.withValue(wildcardMatch)); } if(matches){ return new Path(path, params.toArray(new PathParam[]{})); }else{ return null; } } SimplePathPattern(String pattern); List<PathParamName> varNames(); boolean matches(String path); Path match(String path); String raw();  }