📄 pathmap.java
字号:
// prefix search int i=l-1; while((i=path.lastIndexOf('/',i-1))>=0) { entry=_prefixMap.getEntry(path,0,i); if (entry!=null) entries=LazyList.add(entries,entry.getValue()); } // Prefix Default if (_prefixDefault!=null) entries=LazyList.add(entries,_prefixDefault); // Extension search i=0; while ((i=path.indexOf('.',i+1))>0) { entry=_suffixMap.getEntry(path,i+1,l-i-1); if (entry!=null) entries=LazyList.add(entries,entry.getValue()); } // Default if (_default!=null) { // Optimization for just the default if (entries==null) return _defaultSingletonList; entries=LazyList.add(entries,_default); } return entries; } /* --------------------------------------------------------------- */ /** Get all entries matched by the path. * Best match first. * @param path Path to match * @return List of Map.Entry instances key=pathSpec */ public List getMatches(String path) { return LazyList.getList(getLazyMatches(path)); } /* --------------------------------------------------------------- */ /** Return whether the path matches any entries in the PathMap, * excluding the default entry * @param path Path to match * @return Whether the PathMap contains any entries that match this */ public boolean containsMatch(String path) { Entry match = getMatch(path); return match!=null && !match.equals(_default); } /* --------------------------------------------------------------- */ public synchronized Object remove(Object pathSpec) { if (pathSpec!=null) { String spec=(String) pathSpec; if (spec.equals("/*")) _prefixDefault=null; else if (spec.endsWith("/*")) { _prefixMap.remove(spec.substring(0,spec.length()-2)); _exactMap.remove(spec.substring(0,spec.length()-1)); _exactMap.remove(spec.substring(0,spec.length()-2)); } else if (spec.startsWith("*.")) _suffixMap.remove(spec.substring(2)); else if (spec.equals(URIUtil.SLASH)) { _default=null; _defaultSingletonList=null; } else _exactMap.remove(spec); } return super.remove(pathSpec); } /* --------------------------------------------------------------- */ public void clear() { _exactMap=new StringMap(); _prefixMap=new StringMap(); _suffixMap=new StringMap(); _default=null; _defaultSingletonList=null; super.clear(); } /* --------------------------------------------------------------- */ /** * @return true if match. */ public static boolean match(String pathSpec, String path) throws IllegalArgumentException { return match(pathSpec, path, false); } /* --------------------------------------------------------------- */ /** * @return true if match. */ public static boolean match(String pathSpec, String path, boolean noDefault) throws IllegalArgumentException { char c = pathSpec.charAt(0); if (c=='/') { if (!noDefault && pathSpec.length()==1 || pathSpec.equals(path)) return true; if(isPathWildcardMatch(pathSpec, path)) return true; } else if (c=='*') return path.regionMatches(path.length()-pathSpec.length()+1, pathSpec,1,pathSpec.length()-1); return false; } /* --------------------------------------------------------------- */ private static boolean isPathWildcardMatch(String pathSpec, String path) { // For a spec of "/foo/*" match "/foo" , "/foo/..." but not "/foobar" int cpl=pathSpec.length()-2; if (pathSpec.endsWith("/*") && path.regionMatches(0,pathSpec,0,cpl)) { if (path.length()==cpl || '/'==path.charAt(cpl)) return true; } return false; } /* --------------------------------------------------------------- */ /** Return the portion of a path that matches a path spec. * @return null if no match at all. */ public static String pathMatch(String pathSpec, String path) { char c = pathSpec.charAt(0); if (c=='/') { if (pathSpec.length()==1) return path; if (pathSpec.equals(path)) return path; if (isPathWildcardMatch(pathSpec, path)) return path.substring(0,pathSpec.length()-2); } else if (c=='*') { if (path.regionMatches(path.length()-(pathSpec.length()-1), pathSpec,1,pathSpec.length()-1)) return path; } return null; } /* --------------------------------------------------------------- */ /** Return the portion of a path that is after a path spec. * @return The path info string */ public static String pathInfo(String pathSpec, String path) { char c = pathSpec.charAt(0); if (c=='/') { if (pathSpec.length()==1) return null; if (pathSpec.equals(path)) return null; if (isPathWildcardMatch(pathSpec, path)) { if (path.length()==pathSpec.length()-2) return null; return path.substring(pathSpec.length()-2); } } return null; } /* ------------------------------------------------------------ */ /** Relative path. * @param base The base the path is relative to. * @param pathSpec The spec of the path segment to ignore. * @param path the additional path * @return base plus path with pathspec removed */ public static String relativePath(String base, String pathSpec, String path ) { String info=pathInfo(pathSpec,path); if (info==null) info=path; if( info.startsWith( "./")) info = info.substring( 2); if( base.endsWith( URIUtil.SLASH)) if( info.startsWith( URIUtil.SLASH)) path = base + info.substring(1); else path = base + info; else if( info.startsWith( URIUtil.SLASH)) path = base + info; else path = base + URIUtil.SLASH + info; return path; } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ public static class Entry implements Map.Entry { private Object key; private Object value; private String mapped; private transient String string; Entry(Object key, Object value) { this.key=key; this.value=value; } public Object getKey() { return key; } public Object getValue() { return value; } public Object setValue(Object o) { throw new UnsupportedOperationException(); } public String toString() { if (string==null) string=key+"="+value; return string; } public String getMapped() { return mapped; } void setMapped(String mapped) { this.mapped = mapped; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -