1 45 package org.openejb.core.ivm.naming; 46 47 54 public class ParsedName implements java.io.Serializable { 55 final static int IS_EQUAL = 0; 56 final static int IS_LESS = -1; 57 final static int IS_GREATER = 1; 58 59 String [] components; 60 int pos = 0; 61 int hashcode; 62 63 public ParsedName(String path){ 64 path = normalize(path); 65 if (path == null || path.equals("/") ) { 67 components = new String [1]; 69 components[0] = ""; 70 hashcode = 0; 71 } else if( path.length() > 0) { 72 java.util.StringTokenizer st = new java.util.StringTokenizer (path, "/"); 73 components = new String [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 components = new String [1]; 81 components[0] = ""; 82 hashcode = 0; 83 } 84 } 85 86 public String 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; } 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 other){ 113 int otherHash = other.hashCode(); 114 return compareTo(otherHash); 115 } 116 public static void main(String [] 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 toString() { 124 if( components.length == 0) { 125 return ""; 126 } 127 StringBuffer buffer = new StringBuffer ( 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 137 138 140 private String normalize(String 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 sb = new StringBuffer (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 160 private String normalize(String 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 |