KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > ResourcePath


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs;
21
22 import org.openide.util.Utilities;
23 import java.util.Enumeration JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.io.File JavaDoc;
26 import org.netbeans.modules.masterfs.filebasedfs.utils.FileInfo;
27
28 /**
29  * Encapsulates independent view of hierarchical resource path.
30  * Implemented as immutable.
31  *
32  * @author Radek Matous
33  */

34 final class ResourcePath {
35     private final String JavaDoc resourcePath;
36
37     private static final ResourcePath root = new ResourcePath("/");
38
39     ResourcePath(StringBuilder JavaDoc sb) {
40         resourcePath = sb.toString();
41     }
42
43     ResourcePath(final String JavaDoc 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 JavaDoc getFile () {
58         // XXX this is ugly and fragile; revisit
59
if ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) {
60             File JavaDoc retVal = null;
61             // Windows: resourcePath is e.g. /d:/temp/foo.txt
62
if (!isRoot()) {
63                 String JavaDoc filePath = resourcePath.substring(1).replace('/', File.separatorChar);//NOI18N
64
if (getParent().isRoot() && !filePath.endsWith("/")) { //NOI18N
65
filePath = filePath + "/";//NOI18N
66
}
67                 retVal = new File JavaDoc(filePath);
68             }
69             
70             return retVal;
71         } else {
72             // Unix (or similar): resourcePath is e.g. /tmp/foo.txt
73
return new File JavaDoc(resourcePath);
74         }
75     }
76     
77     final String JavaDoc getNormalizedPath() {
78         return resourcePath;
79     }
80
81     Enumeration JavaDoc getElements () {
82         StringTokenizer JavaDoc sTokens = new StringTokenizer JavaDoc(resourcePath, "/");
83         return sTokens;
84     }
85
86     ResourcePath getChild(final String JavaDoc nameExt) {
87         ResourcePath retVal = null;
88         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(resourcePath);
89         if (!resourcePath.endsWith("/")) sb.append('/');//NOI18N
90
sb.append(nameExt);
91         retVal = new ResourcePath(sb);
92         return retVal;
93     }
94
95     ResourcePath getChild(final String JavaDoc name, final String JavaDoc ext) {
96         final StringBuilder JavaDoc sb = new StringBuilder JavaDoc(resourcePath);
97         if (!resourcePath.endsWith("/")) sb.append('/');//NOI18N
98
sb.append(name);
99         if (ext != null && ext.length() != 0)
100             sb.append('.').append(ext);//NOI18N
101
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));//NOI18N
108
}
109
110     String JavaDoc getExt() {
111         return FileInfo.getExt(getNameExt());
112     }
113
114     /**
115      * Implements abstract FileObject.getName()
116      */

117     String JavaDoc getName() {
118         return FileInfo.getName(getNameExt());
119     }
120
121     /**
122      * Overrides FileObject.getNameExt() to return only substring rather than newly created String
123      */

124     String JavaDoc getNameExt() {
125         int idx0 = resourcePath.lastIndexOf('/');//NOI18N
126
assert idx0 != -1 : resourcePath;
127         idx0++;
128         return resourcePath.substring(idx0);
129     }
130
131     String JavaDoc getPath() {
132         return resourcePath.substring(1);
133     }
134
135     /** Adds slash at first position if necessary and removes slash from last position */
136     static String JavaDoc getNormalizedPath(String JavaDoc resPath) {
137         if (resPath == null) return resPath;
138         resPath = resPath.replace('\\', '/');//NOI18N
139

140         if (!resPath.startsWith("/")) //NOI18N
141
resPath = "/" + resPath; //NOI18N
142

143         if (resPath.endsWith("/") && resPath.length() != "/".length()) //NOI18N
144
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 JavaDoc obj) {
154         String JavaDoc resPath = null;
155         if (obj instanceof String JavaDoc) {
156             resPath = (String JavaDoc) obj;
157         } else if (obj instanceof ResourcePath) {
158             resPath = ((ResourcePath) obj).getNormalizedPath();
159         }
160         return resourcePath.equals(resPath);
161     }
162
163     public String JavaDoc toString() {
164         return getNormalizedPath();
165     }
166 }
167
Popular Tags