KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > registry > mergedctx > Resource


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.registry.mergedctx;
21
22 import org.openide.util.Utilities;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 /**
28  * Encapsulates independent view of hierarchical resource path.
29  * Implemented as immutable.
30  *
31  * @author Radek Matous
32  */

33 final class Resource {
34     private final String JavaDoc resourcePath;
35
36     Resource(final String JavaDoc resourcePath) {
37         if (resourcePath == null) throw new IllegalArgumentException JavaDoc();
38         this.resourcePath = getNormalizedPath(resourcePath);
39     }
40
41     boolean isRoot() {
42         return (getParent() == null) ? true : false;
43     }
44
45
46     Enumeration JavaDoc getElements() {
47         final StringTokenizer JavaDoc sTokens = new StringTokenizer JavaDoc(resourcePath, "/");//NOI18N
48
return sTokens;
49     }
50
51     boolean isSuperior(final Resource subordered) {
52         return subordered.getNormalizedPath().startsWith(getNormalizedPath());
53     }
54
55     Resource getChild(final String JavaDoc nameExt) {
56         final Resource retVal;
57         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(resourcePath);
58         if (!resourcePath.endsWith("/")) sb.append("/");//NOI18N
59
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));//NOI18N
68
}
69
70     String JavaDoc getName() {
71         int idx0 = resourcePath.lastIndexOf('/');//NOI18N
72
idx0 = (idx0 == -1) ? 0 : (idx0 + 1);
73         return resourcePath.substring(idx0, resourcePath.length());
74     }
75
76
77     String JavaDoc getPath() {
78         String JavaDoc retValue = resourcePath;
79         //retValue = retValue.replace('/', '/');
80
//retValue = retValue.replace('.', '.');
81
final int idx = (Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2)) ? 1 : 0;
82         return retValue.substring(idx);
83     }
84
85     /** Adds slash at first position if necessary and removes slash from last position */
86     private static String JavaDoc getNormalizedPath(String JavaDoc resPath) {
87         if (resPath == null) return resPath;
88         resPath = resPath.replace('\\', '/');//NOI18N
89

90         if (!resPath.startsWith("/")) //NOI18N
91
resPath = "/" + resPath; //NOI18N
92

93         if (resPath.endsWith("/") && resPath.length() != "/".length()) //NOI18N
94
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 JavaDoc obj) {
104         String JavaDoc resPath = null;
105         if (obj instanceof String JavaDoc) {
106             resPath = (String JavaDoc) obj;
107         } else if (obj instanceof Resource) {
108             resPath = ((Resource) obj).getNormalizedPath();
109         }
110         return resourcePath.equals(resPath);
111     }
112
113     public String JavaDoc toString() {
114         return getNormalizedPath();
115     }
116
117     private String JavaDoc getNormalizedPath() {
118         return resourcePath;
119     }
120
121 }
122
Popular Tags