KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > ivm > naming > ParsedName


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: ParsedName.java 1096 2004-03-26 21:41:16Z dblevins $
44  */

45 package org.openejb.core.ivm.naming;
46
47 /**
48  * This class represents a compound path name; a path made of several atomic names.
49  * It provides an interface for navigating the components sequentially. This class
50  * assumes that the path separator is a '\' character. The "java:" component of a
51  * JNDI ENC path must be striped out of the path before it used to construct an
52  * instance of this class.
53  */

54 public class ParsedName implements java.io.Serializable JavaDoc{
55     final static int IS_EQUAL = 0;
56     final static int IS_LESS = -1;
57     final static int IS_GREATER = 1;
58     
59     String JavaDoc [] components;
60     int pos = 0;
61     int hashcode;
62     
63     public ParsedName(String JavaDoc path){
64         path = normalize(path);
65         //System.out.println("[] path "+path);
66
if (path == null || path.equals("/") ) {
67         // A blank string is a legal name and refers to the current/root context.
68
components = new String JavaDoc[1];
69         components[0] = "";
70         hashcode = 0;
71         } else if( path.length() > 0) {
72         java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(path, "/");
73         components = new String JavaDoc[st.countTokens()];
74         for(int i = 0; st.hasMoreTokens() && i < components.length; i++)
75         components[i] = st.nextToken();
76         hashcode = components[0].hashCode();
77     }
78     else {
79         // A blank string is a legal name and refers to the current/root context.
80
components = new String JavaDoc[1];
81         components[0] = "";
82         hashcode = 0;
83     }
84     }
85     
86     public String JavaDoc getComponent( ){
87         return components[pos];
88     }
89     public boolean next( ){
90         if(components.length > pos+1){
91             hashcode = components[++pos].hashCode();
92             return true;
93         }else{
94             return false;// maintain position
95
}
96     }
97     public void reset(){
98         pos = 0;
99         hashcode = components[0].hashCode();
100     }
101     public int compareTo(int otherHash){
102         if(hashcode == otherHash)
103             return 0;
104         else if(hashcode > otherHash )
105             return 1;
106         else
107             return -1;
108     }
109     public int getComponentHashCode( ){
110         return hashcode;
111     }
112     public int compareTo(String JavaDoc other){
113         int otherHash = other.hashCode();
114         return compareTo(otherHash);
115     }
116     // for testing only
117
public static void main(String JavaDoc [] args){
118         
119         ParsedName name = new ParsedName("comp/env/jdbc/mydatabase");
120         while(name.next())
121             System.out.println(name.getComponent());
122     }
123     public String JavaDoc toString() {
124     if( components.length == 0) {
125         return "";
126     }
127     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc( components[0]);
128     for( int i = 1; i < components.length; ++i) {
129         buffer.append('/');
130         buffer.append( components[i]);
131     }
132     return buffer.toString();
133     }
134
135         /* A normal Unix pathname contains no duplicate slashes and does not end
136        with a slash. It may be the empty string. */

137
138     /* Normalize the given pathname, whose length is len, starting at the given
139        offset; everything before this offset is already normal. */

140     private String JavaDoc normalize(String JavaDoc pathname, int len, int off) {
141     if (len == 0) return pathname;
142     int n = len;
143     while ((n > 0) && (pathname.charAt(n - 1) == '/')) n--;
144     if (n == 0) return "/";
145     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(pathname.length());
146     if (off > 0) sb.append(pathname.substring(0, off));
147     char prevChar = 0;
148     for (int i = off; i < n; i++) {
149         char c = pathname.charAt(i);
150         if ((prevChar == '/') && (c == '/')) continue;
151         sb.append(c);
152         prevChar = c;
153     }
154     return sb.toString();
155     }
156
157     /* Check that the given pathname is normal. If not, invoke the real
158        normalizer on the part of the pathname that requires normalization.
159        This way we iterate through the whole pathname string only once. */

160     private String JavaDoc normalize(String JavaDoc pathname) {
161     int n = pathname.length();
162     char prevChar = 0;
163     for (int i = 0; i < n; i++) {
164         char c = pathname.charAt(i);
165         if ((prevChar == '/') && (c == '/'))
166         return normalize(pathname, n, i - 1);
167         prevChar = c;
168     }
169     if (prevChar == '/') return normalize(pathname, n, n - 1);
170     return pathname;
171     }
172
173 }
174
Popular Tags