1 19 20 package org.netbeans.modules.registry.mergedctx; 21 22 import org.openide.util.Utilities; 23 24 import java.util.Enumeration ; 25 import java.util.StringTokenizer ; 26 27 33 final class Resource { 34 private final String resourcePath; 35 36 Resource(final String resourcePath) { 37 if (resourcePath == null) throw new IllegalArgumentException (); 38 this.resourcePath = getNormalizedPath(resourcePath); 39 } 40 41 boolean isRoot() { 42 return (getParent() == null) ? true : false; 43 } 44 45 46 Enumeration getElements() { 47 final StringTokenizer sTokens = new StringTokenizer (resourcePath, "/"); return sTokens; 49 } 50 51 boolean isSuperior(final Resource subordered) { 52 return subordered.getNormalizedPath().startsWith(getNormalizedPath()); 53 } 54 55 Resource getChild(final String nameExt) { 56 final Resource retVal; 57 final StringBuffer sb = new StringBuffer (resourcePath); 58 if (!resourcePath.endsWith("/")) sb.append("/"); sb.append(nameExt); 60 retVal = new Resource(sb.toString()); 61 return retVal; 62 } 63 64 Resource getParent() { 65 final int idx = resourcePath.lastIndexOf('/'); 66 if (idx == 0 && resourcePath.length() == 1) return null; 67 return new Resource((idx <= 0) ? "/" : resourcePath.substring(0, idx)); } 69 70 String getName() { 71 int idx0 = resourcePath.lastIndexOf('/'); idx0 = (idx0 == -1) ? 0 : (idx0 + 1); 73 return resourcePath.substring(idx0, resourcePath.length()); 74 } 75 76 77 String getPath() { 78 String retValue = resourcePath; 79 final int idx = (Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2)) ? 1 : 0; 82 return retValue.substring(idx); 83 } 84 85 86 private static String getNormalizedPath(String resPath) { 87 if (resPath == null) return resPath; 88 resPath = resPath.replace('\\', '/'); 90 if (!resPath.startsWith("/")) resPath = "/" + resPath; 93 if (resPath.endsWith("/") && resPath.length() != "/".length()) resPath = resPath.substring(0, resPath.length() - 1); 95 96 return resPath; 97 } 98 99 public int hashCode() { 100 return resourcePath.hashCode(); 101 } 102 103 public boolean equals(final Object obj) { 104 String resPath = null; 105 if (obj instanceof String ) { 106 resPath = (String ) obj; 107 } else if (obj instanceof Resource) { 108 resPath = ((Resource) obj).getNormalizedPath(); 109 } 110 return resourcePath.equals(resPath); 111 } 112 113 public String toString() { 114 return getNormalizedPath(); 115 } 116 117 private String getNormalizedPath() { 118 return resourcePath; 119 } 120 121 } 122 | Popular Tags |