KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > naming > cosnaming > InterOperableNamingImpl


1 /*
2  * @(#)InterOperableNamingImpl.java 1.15 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.naming.cosnaming;
9
10 import org.omg.CosNaming.NamingContextExtPackage.*;
11 import java.io.StringWriter JavaDoc;
12
13 // Import general CORBA classes
14
import org.omg.CORBA.SystemException JavaDoc;
15 import org.omg.CORBA.Object JavaDoc;
16
17 // Import org.omg.CosNaming types
18
import org.omg.CosNaming.NameComponent JavaDoc;
19 import org.omg.CosNaming.NamingContext JavaDoc;
20
21
22 /**
23  * Class InteroperableNamingImpl implements the methods defined
24  * for NamingContextExt which is part of Interoperable Naming
25  * Service specifications. This class is added for doing more
26  * of Parsing and Building of Stringified names according to INS
27  * Spec.
28  */

29 public class InterOperableNamingImpl
30 {
31    /**
32      * Method which stringifies the Name Components given as the input
33      * parameter.
34      *
35      * @param n Array of Name Components (Simple or Compound Names)
36      * @return string which is the stringified reference.
37      */

38     public String JavaDoc convertToString( org.omg.CosNaming.NameComponent JavaDoc[]
39                                    theNameComponents )
40     {
41         String JavaDoc theConvertedString =
42             convertNameComponentToString( theNameComponents[0] );
43         String JavaDoc 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    /** This method converts a single Namecomponent to String, By adding Escapes
56     * If neccessary.
57     */

58     private String JavaDoc convertNameComponentToString(
59         org.omg.CosNaming.NameComponent JavaDoc 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 JavaDoc kind = addEscape( theNameComponent.kind );
72             return "." + kind;
73         }
74         else if( ( theNameComponent.kind == null )
75                ||( theNameComponent.kind.length() == 0 ) )
76         {
77             String JavaDoc id = addEscape( theNameComponent.id );
78             return id;
79         }
80         else {
81             String JavaDoc id = addEscape( theNameComponent.id );
82             String JavaDoc kind = addEscape( theNameComponent.kind );
83             return (id + "." + kind);
84         }
85     }
86
87
88    /** This method adds escape '\' for the Namecomponent if neccessary
89     */

90    private String JavaDoc addEscape( String JavaDoc value )
91    {
92         StringBuffer JavaDoc theNewValue;
93         if( (value != null) && ( (value.indexOf('.') != -1 ) ||
94                                  (value.indexOf('/') != -1)))
95         {
96             char c;
97             theNewValue = new StringBuffer JavaDoc( );
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                     // Adding escape for the "."
106
theNewValue.append( '\\' );
107                     theNewValue.append( c );
108                 }
109             }
110         }
111         else {
112             return value;
113         }
114         return new String JavaDoc( theNewValue );
115    }
116
117    /**
118      * Method which converts the Stringified name into Array of Name Components.
119      *
120      * @param string which is the stringified name.
121      * @return Array of Name Components (Simple or Compound Names)
122      */

123    public org.omg.CosNaming.NameComponent JavaDoc[] convertToNameComponent(
124        String JavaDoc theStringifiedName )
125        throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
126    {
127     String JavaDoc[] theStringifiedNameComponents =
128          breakStringToNameComponents( theStringifiedName );
129         if( ( theStringifiedNameComponents == null )
130          || (theStringifiedNameComponents.length == 0 ) )
131         {
132             return null;
133         }
134         NameComponent JavaDoc[] theNameComponents =
135             new NameComponent JavaDoc[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    /** Step1 in converting Stringified name into array of Name Component
144      * is breaking the String into multiple name components
145      */

146    private String JavaDoc[] breakStringToNameComponents( String JavaDoc 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                // This is the end of all the occurence of '/' and hence come
155
// out of the loop
156
index = theStringifiedName.length()+1;
157            }
158            else {
159                // If the '/' is found, first check whether it is
160
// preceded by escape '\'
161
// If not then set theIndices and increment theIndicesIndex
162
// and also set the index else just ignore the '/'
163
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 JavaDoc[] tempString = new String JavaDoc[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    /** This method breaks one big String into multiple substrings based
189      * on the array of index passed in.
190      */

191    private String JavaDoc[] StringComponentsFromIndices( int[] theIndices,
192           int indicesCount, String JavaDoc theStringifiedName )
193    {
194        String JavaDoc[] theStringComponents = new String JavaDoc[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            // This is done for the last component
219
if( firstIndex != 0 && i == indicesCount ) {
220                theStringComponents[indicesCount-1] =
221                theStringifiedName.substring( firstIndex );
222            }
223        }
224        return theStringComponents;
225    }
226
227    /** Step 2: After Breaking the Stringified name into set of NameComponent
228      * Strings, The next step is to create Namecomponents from the substring
229      * by removing the escapes if there are any.
230      */

231    private NameComponent JavaDoc createNameComponentFromString(
232         String JavaDoc theStringifiedNameComponent )
233         throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
234
235    {
236         String JavaDoc id = null;
237         String JavaDoc kind = null;
238         if( ( theStringifiedNameComponent == null )
239          || ( theStringifiedNameComponent.length( ) == 0)
240          || ( theStringifiedNameComponent.endsWith(".") ) )
241         {
242             // If any of the above is true, then we create an invalid Name
243
// Component to indicate that it is an invalid name.
244
throw new org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc( );
245         }
246
247         int index = theStringifiedNameComponent.indexOf( '.', 0 );
248         // The format could be XYZ (Without kind)
249
if( index == -1 ) {
250             id = theStringifiedNameComponent;
251         }
252         // The format is .XYZ (Without ID)
253
else if( index == 0 ) {
254             // This check is for the Namecomponent which is just "." meaning Id
255
// and Kinds are null
256
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                         // No more '.', which means there is no Kind
282
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 JavaDoc( id, kind );
303    }
304
305   
306    /** This method cleans the escapes in the Stringified name and returns the
307      * correct String
308      */

309    private String JavaDoc cleanEscapeCharacter( String JavaDoc 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 JavaDoc src = new StringBuffer JavaDoc( theString );
320             StringBuffer JavaDoc dest = new StringBuffer JavaDoc( );
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 there is a AlphaNumeric character after a \
330
// then include slash, as it is not intended as an
331
// escape character.
332
if( Character.isLetterOrDigit(d) ) {
333                             dest.append( c );
334                         }
335                     }
336                 }
337             }
338             return new String JavaDoc(dest);
339         }
340    }
341
342    /**
343      * Method which converts the Stringified name and Host Name Address into
344      * a URL based Name
345      *
346      * @param address which is ip based host name
347      * @param name which is the stringified name.
348      * @return url based Name.
349      */

350     public String JavaDoc createURLBasedAddress( String JavaDoc address, String JavaDoc name )
351         throws InvalidAddress
352     {
353     String JavaDoc 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     /** Encodes the string according to RFC 2396 IETF spec required by INS.
365      */

366     private String JavaDoc encode( String JavaDoc stringToEncode ) {
367         StringWriter JavaDoc theStringAfterEscape = new StringWriter JavaDoc();
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             // Do no Escape for characters in this list
376
// RFC 2396
377
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                 // Add escape
387
theStringAfterEscape.write( '%' );
388                 String JavaDoc hexString = Integer.toHexString( (int) c );
389                 theStringAfterEscape.write( hexString );
390             }
391         }
392         return theStringAfterEscape.toString();
393     }
394
395 }
396
397
Popular Tags