1 7 8 package com.sun.corba.se.impl.naming.cosnaming; 9 10 import org.omg.CosNaming.NamingContextExtPackage.*; 11 import java.io.StringWriter ; 12 13 import org.omg.CORBA.SystemException ; 15 import org.omg.CORBA.Object ; 16 17 import org.omg.CosNaming.NameComponent ; 19 import org.omg.CosNaming.NamingContext ; 20 21 22 29 public class InterOperableNamingImpl 30 { 31 38 public String convertToString( org.omg.CosNaming.NameComponent [] 39 theNameComponents ) 40 { 41 String theConvertedString = 42 convertNameComponentToString( theNameComponents[0] ); 43 String temp; 44 for( int i = 1; i < theNameComponents.length; i++ ) { 45 temp = convertNameComponentToString( theNameComponents[i] ); 46 if( temp != null ) { 47 theConvertedString = 48 theConvertedString + "/" + convertNameComponentToString( 49 theNameComponents[i] ); 50 } 51 } 52 return theConvertedString; 53 } 54 55 58 private String convertNameComponentToString( 59 org.omg.CosNaming.NameComponent theNameComponent ) 60 { 61 if( ( ( theNameComponent.id == null ) 62 ||( theNameComponent.id.length() == 0 ) ) 63 &&( ( theNameComponent.kind == null ) 64 ||( theNameComponent.kind.length() == 0 ) ) ) 65 { 66 return "."; 67 } 68 else if( ( theNameComponent.id == null ) 69 ||( theNameComponent.id.length() == 0 ) ) 70 { 71 String kind = addEscape( theNameComponent.kind ); 72 return "." + kind; 73 } 74 else if( ( theNameComponent.kind == null ) 75 ||( theNameComponent.kind.length() == 0 ) ) 76 { 77 String id = addEscape( theNameComponent.id ); 78 return id; 79 } 80 else { 81 String id = addEscape( theNameComponent.id ); 82 String kind = addEscape( theNameComponent.kind ); 83 return (id + "." + kind); 84 } 85 } 86 87 88 90 private String addEscape( String value ) 91 { 92 StringBuffer theNewValue; 93 if( (value != null) && ( (value.indexOf('.') != -1 ) || 94 (value.indexOf('/') != -1))) 95 { 96 char c; 97 theNewValue = new StringBuffer ( ); 98 for( int i = 0; i < value.length( ); i++ ) { 99 c = value.charAt( i ); 100 if( ( c != '.' ) && (c != '/' ) ) 101 { 102 theNewValue.append( c ); 103 } 104 else { 105 theNewValue.append( '\\' ); 107 theNewValue.append( c ); 108 } 109 } 110 } 111 else { 112 return value; 113 } 114 return new String ( theNewValue ); 115 } 116 117 123 public org.omg.CosNaming.NameComponent [] convertToNameComponent( 124 String theStringifiedName ) 125 throws org.omg.CosNaming.NamingContextPackage.InvalidName 126 { 127 String [] theStringifiedNameComponents = 128 breakStringToNameComponents( theStringifiedName ); 129 if( ( theStringifiedNameComponents == null ) 130 || (theStringifiedNameComponents.length == 0 ) ) 131 { 132 return null; 133 } 134 NameComponent [] theNameComponents = 135 new NameComponent [theStringifiedNameComponents.length]; 136 for( int i = 0; i < theStringifiedNameComponents.length; i++ ) { 137 theNameComponents[i] = createNameComponentFromString( 138 theStringifiedNameComponents[i] ); 139 } 140 return theNameComponents; 141 } 142 143 146 private String [] breakStringToNameComponents( String theStringifiedName ) { 147 int[] theIndices = new int[100]; 148 int theIndicesIndex = 0; 149 150 for(int index = 0; index <= theStringifiedName.length(); ) { 151 theIndices[theIndicesIndex] = theStringifiedName.indexOf( '/', 152 index ); 153 if( theIndices[theIndicesIndex] == -1 ) { 154 index = theStringifiedName.length()+1; 157 } 158 else { 159 if( (theIndices[theIndicesIndex] > 0 ) 164 && (theStringifiedName.charAt( 165 theIndices[theIndicesIndex]-1) == '\\') ) 166 { 167 index = theIndices[theIndicesIndex] + 1; 168 theIndices[theIndicesIndex] = -1; 169 } 170 else { 171 index = theIndices[theIndicesIndex] + 1; 172 theIndicesIndex++; 173 } 174 } 175 } 176 if( theIndicesIndex == 0 ) { 177 String [] tempString = new String [1]; 178 tempString[0] = theStringifiedName; 179 return tempString; 180 } 181 if( theIndicesIndex != 0 ) { 182 theIndicesIndex++; 183 } 184 return StringComponentsFromIndices( theIndices, theIndicesIndex, 185 theStringifiedName ); 186 } 187 188 191 private String [] StringComponentsFromIndices( int[] theIndices, 192 int indicesCount, String theStringifiedName ) 193 { 194 String [] theStringComponents = new String [indicesCount]; 195 int firstIndex = 0; 196 int lastIndex = theIndices[0]; 197 for( int i = 0; i < indicesCount; i++ ) { 198 theStringComponents[i] = theStringifiedName.substring( firstIndex, 199 lastIndex ); 200 if( ( theIndices[i] < theStringifiedName.length() - 1 ) 201 &&( theIndices[i] != -1 ) ) 202 { 203 firstIndex = theIndices[i]+1; 204 } 205 else { 206 firstIndex = 0; 207 i = indicesCount; 208 } 209 if( (i+1 < theIndices.length) 210 && (theIndices[i+1] < (theStringifiedName.length() - 1)) 211 && (theIndices[i+1] != -1) ) 212 { 213 lastIndex = theIndices[i+1]; 214 } 215 else { 216 i = indicesCount; 217 } 218 if( firstIndex != 0 && i == indicesCount ) { 220 theStringComponents[indicesCount-1] = 221 theStringifiedName.substring( firstIndex ); 222 } 223 } 224 return theStringComponents; 225 } 226 227 231 private NameComponent createNameComponentFromString( 232 String theStringifiedNameComponent ) 233 throws org.omg.CosNaming.NamingContextPackage.InvalidName 234 235 { 236 String id = null; 237 String kind = null; 238 if( ( theStringifiedNameComponent == null ) 239 || ( theStringifiedNameComponent.length( ) == 0) 240 || ( theStringifiedNameComponent.endsWith(".") ) ) 241 { 242 throw new org.omg.CosNaming.NamingContextPackage.InvalidName ( ); 245 } 246 247 int index = theStringifiedNameComponent.indexOf( '.', 0 ); 248 if( index == -1 ) { 250 id = theStringifiedNameComponent; 251 } 252 else if( index == 0 ) { 254 if( theStringifiedNameComponent.length( ) != 1 ) { 257 kind = theStringifiedNameComponent.substring(1); 258 } 259 } 260 else 261 { 262 if( theStringifiedNameComponent.charAt(index-1) != '\\' ) { 263 id = theStringifiedNameComponent.substring( 0, index); 264 kind = theStringifiedNameComponent.substring( index + 1 ); 265 } 266 else { 267 boolean kindfound = false; 268 while( (index < theStringifiedNameComponent.length() ) 269 &&( kindfound != true ) ) 270 { 271 index = theStringifiedNameComponent.indexOf( '.',index + 1); 272 if( index > 0 ) { 273 if( theStringifiedNameComponent.charAt( 274 index - 1 ) != '\\' ) 275 { 276 kindfound = true; 277 } 278 } 279 else 280 { 281 index = theStringifiedNameComponent.length(); 283 } 284 } 285 if( kindfound == true ) { 286 id = theStringifiedNameComponent.substring( 0, index); 287 kind = theStringifiedNameComponent.substring(index + 1 ); 288 } 289 else { 290 id = theStringifiedNameComponent; 291 } 292 } 293 } 294 id = cleanEscapeCharacter( id ); 295 kind = cleanEscapeCharacter( kind ); 296 if( id == null ) { 297 id = ""; 298 } 299 if( kind == null ) { 300 kind = ""; 301 } 302 return new NameComponent ( id, kind ); 303 } 304 305 306 309 private String cleanEscapeCharacter( String theString ) 310 { 311 if( ( theString == null ) || (theString.length() == 0 ) ) { 312 return theString; 313 } 314 int index = theString.indexOf( '\\' ); 315 if( index == 0 ) { 316 return theString; 317 } 318 else { 319 StringBuffer src = new StringBuffer ( theString ); 320 StringBuffer dest = new StringBuffer ( ); 321 char c; 322 for( int i = 0; i < theString.length( ); i++ ) { 323 c = src.charAt( i ); 324 if( c != '\\' ) { 325 dest.append( c ); 326 } else { 327 if( i+1 < theString.length() ) { 328 char d = src.charAt( i + 1 ); 329 if( Character.isLetterOrDigit(d) ) { 333 dest.append( c ); 334 } 335 } 336 } 337 } 338 return new String (dest); 339 } 340 } 341 342 350 public String createURLBasedAddress( String address, String name ) 351 throws InvalidAddress 352 { 353 String theurl = null; 354 if( ( address == null ) 355 ||( address.length() == 0 ) ) { 356 throw new InvalidAddress(); 357 } 358 else { 359 theurl = "corbaname:" + address + "#" + encode( name ); 360 } 361 return theurl; 362 } 363 364 366 private String encode( String stringToEncode ) { 367 StringWriter theStringAfterEscape = new StringWriter (); 368 int byteCount = 0; 369 for( int i = 0; i < stringToEncode.length(); i++ ) 370 { 371 char c = stringToEncode.charAt( i ) ; 372 if( Character.isLetterOrDigit( c ) ) { 373 theStringAfterEscape.write( c ); 374 } 375 else if((c == ';') || (c == '/') || (c == '?') 378 || (c == ':') || (c == '@') || (c == '&') || (c == '=') 379 || (c == '+') || (c == '$') || (c == ';') || (c == '-') 380 || (c == '_') || (c == '.') || (c == '!') || (c == '~') 381 || (c == '*') || (c == ' ') || (c == '(') || (c == ')') ) 382 { 383 theStringAfterEscape.write( c ); 384 } 385 else { 386 theStringAfterEscape.write( '%' ); 388 String hexString = Integer.toHexString( (int) c ); 389 theStringAfterEscape.write( hexString ); 390 } 391 } 392 return theStringAfterEscape.toString(); 393 } 394 395 } 396 397 | Popular Tags |