1 package org.jacorb.naming; 2 3 22 23 import java.util.*; 24 import org.omg.CosNaming.*; 25 import org.omg.CosNaming.NamingContextPackage.*; 26 27 34 35 public class Name 36 implements java.io.Serializable 37 { 38 private NameComponent[] fullName; 39 private NameComponent baseName; 40 41 42 private NameComponent[] ctxName; 43 44 public Name() 45 { 46 fullName = null; 47 baseName = null; 48 ctxName = null; 49 } 50 51 55 56 public Name(NameComponent[] n) 57 throws InvalidName 58 { 59 if( n == null || n.length == 0 ) 60 throw new InvalidName(); 61 62 fullName = n; 63 baseName = n[ n.length-1 ]; 64 if( n.length > 1 ) 65 { 66 ctxName = new NameComponent[n.length-1]; 67 for( int i = 0; i< n.length-1; i++ ) 68 ctxName[i] = n[i]; 69 } 70 else 71 ctxName = null; 72 } 73 74 78 79 public Name(String string_name) 80 throws org.omg.CosNaming.NamingContextPackage.InvalidName 81 { 82 this( toName( string_name) ); 83 } 84 85 90 91 public Name(org.omg.CosNaming.NameComponent n) 92 throws org.omg.CosNaming.NamingContextPackage.InvalidName 93 { 94 if( n == null ) 95 throw new org.omg.CosNaming.NamingContextPackage.InvalidName (); 96 baseName = n; 97 fullName = new org.omg.CosNaming.NameComponent [1]; 98 fullName[0] = n; 99 ctxName = null; 100 } 101 102 106 107 public org.omg.CosNaming.NameComponent baseNameComponent() 108 { 109 return baseName; 110 } 111 112 113 public String kind() 114 { 115 return baseName.kind; 116 } 117 118 122 123 public org.omg.CosNaming.NameComponent [] components() 124 { 125 return fullName; 126 } 127 128 131 132 public Name ctxName() 133 { 134 if( ctxName != null ) 136 { 137 try 138 { 139 return new Name(ctxName); 140 } 141 catch ( org.omg.CosNaming.NamingContextPackage.InvalidName im) 142 { 143 im.printStackTrace(); 144 return null; 145 } 146 } 147 else 148 return null; 149 } 150 151 public boolean equals( Object obj ) 152 { 153 if( obj == null ) return false; 154 if( !(obj instanceof Name) ) return false; 155 return( toString().equals( obj.toString() )); 156 } 157 158 159 public Name fullName() 160 throws org.omg.CosNaming.NamingContextPackage.InvalidName 161 { 162 return new Name(fullName); 163 } 164 165 public int hashCode() 166 { 167 return toString().hashCode(); 168 } 169 170 173 174 public String toString() 175 { 176 try 177 { 178 return toString(fullName); 179 } 180 catch( InvalidName in ) 181 { 182 return "<invalid>"; 183 } 184 } 185 186 189 190 private static org.omg.CosNaming.NameComponent getComponent (String sn) 191 throws org.omg.CosNaming.NamingContextPackage.InvalidName 192 { 193 char ch; 194 int len = sn.length (); 195 boolean inKind = false; 196 StringBuffer id = new StringBuffer (); 197 StringBuffer kind = new StringBuffer (); 198 199 for (int i = 0; i < len; i++) 200 { 201 ch = sn.charAt (i); 202 203 if (ch == '\\') 204 { 205 207 i++; 208 if (i >= len) 209 { 210 throw new InvalidName (); 211 } 212 ch = sn.charAt (i); 213 } 214 else if (ch == '.') 215 { 216 218 if (inKind) 219 { 220 throw new InvalidName (); 221 } 222 inKind = true; 223 continue; 224 } 225 if (inKind) 226 { 227 kind.append (ch); 228 } 229 else 230 { 231 id.append (ch); 232 } 233 } 234 235 return (new org.omg.CosNaming.NameComponent (id.toString (), kind.toString ())); 236 } 237 238 243 244 public static org.omg.CosNaming.NameComponent [] toName( String sn ) 245 throws org.omg.CosNaming.NamingContextPackage.InvalidName 246 { 247 if( sn == null || sn.length() == 0 || sn.startsWith("/")) 248 throw new InvalidName(); 249 250 Vector v = new Vector(); 251 252 int start = 0; 253 int i = 0; 254 for( ; i < sn.length(); i++ ) 255 { 256 if( sn.charAt(i) == '/' && sn.charAt(i-1) != '\\') 257 { 258 if( i-start == 0 ) 259 throw new InvalidName(); 260 v.addElement( getComponent( sn.substring( start, i ))); 261 start = i+1; 262 } 263 } 264 if( start < i ) 265 v.addElement( getComponent( sn.substring( start, i ))); 266 267 org.omg.CosNaming.NameComponent [] result = 268 new org.omg.CosNaming.NameComponent [v.size()]; 269 270 for( int j = 0; j < result.length; j++ ) 271 { 272 result[j] = (org.omg.CosNaming.NameComponent )v.elementAt(j); 273 } 274 return result; 275 } 276 277 280 281 public static String toString( org.omg.CosNaming.NameComponent [] n) 282 throws org.omg.CosNaming.NamingContextPackage.InvalidName 283 { 284 if( n == null || n.length == 0 ) 285 throw new org.omg.CosNaming.NamingContextPackage.InvalidName (); 286 287 StringBuffer b = new StringBuffer (); 288 for( int i = 0; i < n.length; i++ ) 289 { 290 if( i > 0 ) 291 b.append("/"); 292 293 if( n[i].id.length() > 0 ) 294 b.append( escape(n[i].id) ); 295 296 if( n[i].kind.length() > 0 || 297 n[i].id.length() == 0 ) 298 b.append("."); 299 300 if( n[i].kind.length() > 0 ) 301 b.append( escape(n[i].kind) ); 302 } 303 return b.toString(); 304 } 305 306 309 310 private static String escape(String s) 311 { 312 StringBuffer sb = new StringBuffer (s); 313 for( int i = 0; i < sb.length(); i++ ) 314 { 315 if( sb.charAt(i) == '/' || 316 sb.charAt(i) == '\\' || 317 sb.charAt(i) == '.' ) 318 { 319 sb.insert(i, '\\'); 320 i++; 321 } 322 } 323 return sb.toString(); 324 } 325 326 } 327 328 | Popular Tags |