1 19 20 package org.netbeans.modules.masterfs; 21 22 import org.openide.util.Utilities; 23 import java.util.Enumeration ; 24 import java.util.StringTokenizer ; 25 import java.io.File ; 26 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo; 27 28 34 final class ResourcePath { 35 private final String resourcePath; 36 37 private static final ResourcePath root = new ResourcePath("/"); 38 39 ResourcePath(StringBuilder sb) { 40 resourcePath = sb.toString(); 41 } 42 43 ResourcePath(final String resourcePath) { 44 assert resourcePath != null; 45 this.resourcePath = getNormalizedPath(resourcePath); 46 assert this.resourcePath.length() > 0 && this.resourcePath.charAt(0) == '/'; 47 } 48 49 static ResourcePath getRoot() { 50 return root; 51 } 52 53 boolean isRoot() { 54 return (getParent() == null) ? true : false; 55 } 56 57 File getFile () { 58 if ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) { 60 File retVal = null; 61 if (!isRoot()) { 63 String filePath = resourcePath.substring(1).replace('/', File.separatorChar); if (getParent().isRoot() && !filePath.endsWith("/")) { filePath = filePath + "/"; } 67 retVal = new File (filePath); 68 } 69 70 return retVal; 71 } else { 72 return new File (resourcePath); 74 } 75 } 76 77 final String getNormalizedPath() { 78 return resourcePath; 79 } 80 81 Enumeration getElements () { 82 StringTokenizer sTokens = new StringTokenizer (resourcePath, "/"); 83 return sTokens; 84 } 85 86 ResourcePath getChild(final String nameExt) { 87 ResourcePath retVal = null; 88 StringBuilder sb = new StringBuilder (resourcePath); 89 if (!resourcePath.endsWith("/")) sb.append('/'); sb.append(nameExt); 91 retVal = new ResourcePath(sb); 92 return retVal; 93 } 94 95 ResourcePath getChild(final String name, final String ext) { 96 final StringBuilder sb = new StringBuilder (resourcePath); 97 if (!resourcePath.endsWith("/")) sb.append('/'); sb.append(name); 99 if (ext != null && ext.length() != 0) 100 sb.append('.').append(ext); return new ResourcePath(sb.toString()); 102 } 103 104 ResourcePath getParent() { 105 final int idx = resourcePath.lastIndexOf('/'); 106 if (idx == 0 && resourcePath.length() == 1) return null; 107 return new ResourcePath((idx <= 0) ? "/" : resourcePath.substring(0, idx)); } 109 110 String getExt() { 111 return FileInfo.getExt(getNameExt()); 112 } 113 114 117 String getName() { 118 return FileInfo.getName(getNameExt()); 119 } 120 121 124 String getNameExt() { 125 int idx0 = resourcePath.lastIndexOf('/'); assert idx0 != -1 : resourcePath; 127 idx0++; 128 return resourcePath.substring(idx0); 129 } 130 131 String getPath() { 132 return resourcePath.substring(1); 133 } 134 135 136 static String getNormalizedPath(String resPath) { 137 if (resPath == null) return resPath; 138 resPath = resPath.replace('\\', '/'); 140 if (!resPath.startsWith("/")) resPath = "/" + resPath; 143 if (resPath.endsWith("/") && resPath.length() != "/".length()) resPath = resPath.substring(0, resPath.length() - 1); 145 146 return resPath; 147 } 148 149 public int hashCode() { 150 return resourcePath.hashCode(); 151 } 152 153 public boolean equals(final Object obj) { 154 String resPath = null; 155 if (obj instanceof String ) { 156 resPath = (String ) obj; 157 } else if (obj instanceof ResourcePath) { 158 resPath = ((ResourcePath) obj).getNormalizedPath(); 159 } 160 return resourcePath.equals(resPath); 161 } 162 163 public String toString() { 164 return getNormalizedPath(); 165 } 166 } 167 | Popular Tags |