KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > naming > Name


1 package org.jacorb.naming;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.util.*;
24 import org.omg.CosNaming.*;
25 import org.omg.CosNaming.NamingContextPackage.*;
26
27 /**
28  * A convenience class for names and converting
29  * between Names and their string representation
30  *
31  * @author Gerald Brose, FU Berlin
32  * @version $Id: Name.java,v 1.12 2004/05/06 12:39:59 nicolas Exp $
33  */

34
35 public class Name
36     implements java.io.Serializable JavaDoc
37 {
38     private NameComponent[] fullName;
39     private NameComponent baseName;
40
41     /** context part of this Name */
42     private NameComponent[] ctxName;
43
44     public Name()
45     {
46     fullName = null;
47     baseName = null;
48     ctxName = null;
49     }
50
51     /**
52      * create a name from an array of NameComponents
53      * @param org.omg.CosNaming.NameComponent[] n
54      */

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     /**
75      * create a name from a stringified name
76      * @param String structured_name
77      */

78
79     public Name(String JavaDoc string_name)
80     throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
81     {
82     this( toName( string_name) );
83     }
84
85     /**
86      * create a name from a singleNameComponent
87      * @param org.omg.CosNaming.NameComponent n
88      *
89      */

90
91     public Name(org.omg.CosNaming.NameComponent JavaDoc n)
92     throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
93     {
94     if( n == null )
95         throw new org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc();
96     baseName = n;
97     fullName = new org.omg.CosNaming.NameComponent JavaDoc[1];
98     fullName[0] = n;
99     ctxName = null;
100     }
101
102     /**
103      * @return a NameComponent object representing the unstructured
104      * base name of this structured name
105      */

106
107     public org.omg.CosNaming.NameComponent JavaDoc baseNameComponent()
108     {
109     return baseName;
110     }
111
112
113     public String JavaDoc kind()
114     {
115     return baseName.kind;
116     }
117
118     /**
119      * @return this name as an array of org.omg.CosNaming.NameComponent,
120      * neccessary for a number of operations on naming context
121      */

122
123     public org.omg.CosNaming.NameComponent JavaDoc[] components()
124     {
125     return fullName;
126     }
127
128     /**
129      * @return a Name object representing the name of the enclosing context
130      */

131
132     public Name ctxName()
133     {
134     // null if no further context
135
if( ctxName != null )
136     {
137         try
138         {
139         return new Name(ctxName);
140         }
141         catch ( org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc im)
142         {
143         im.printStackTrace();
144         return null;
145         }
146     }
147     else
148         return null;
149     }
150
151     public boolean equals( Object JavaDoc 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 JavaDoc
161     {
162     return new Name(fullName);
163     }
164
165     public int hashCode()
166     {
167     return toString().hashCode();
168     }
169
170     /**
171      * @return the string representation of this name
172      */

173
174     public String JavaDoc toString()
175     {
176     try
177     {
178         return toString(fullName);
179     }
180     catch( InvalidName in )
181     {
182         return "<invalid>";
183     }
184     }
185
186     /**
187      * @return a single NameComponent, parsed from sn
188      */

189
190     private static org.omg.CosNaming.NameComponent JavaDoc getComponent (String JavaDoc sn)
191     throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
192     {
193         char ch;
194         int len = sn.length ();
195         boolean inKind = false;
196         StringBuffer JavaDoc id = new StringBuffer JavaDoc ();
197         StringBuffer JavaDoc kind = new StringBuffer JavaDoc ();
198
199         for (int i = 0; i < len; i++)
200         {
201             ch = sn.charAt (i);
202
203             if (ch == '\\')
204             {
205                 // Escaped character
206

207                 i++;
208                 if (i >= len)
209                 {
210                     throw new InvalidName ();
211                 }
212             ch = sn.charAt (i);
213             }
214             else if (ch == '.')
215             {
216                 // id/kind separator character
217

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 JavaDoc (id.toString (), kind.toString ()));
236     }
237
238     /**
239      *
240      * @return an a array of NameComponents
241      * @throws org.omg.CosNaming.NamingContextPackage.InvalidName
242      */

243
244     public static org.omg.CosNaming.NameComponent JavaDoc[] toName( String JavaDoc sn )
245     throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
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 JavaDoc[] result =
268             new org.omg.CosNaming.NameComponent JavaDoc[v.size()];
269     
270     for( int j = 0; j < result.length; j++ )
271     {
272         result[j] = (org.omg.CosNaming.NameComponent JavaDoc)v.elementAt(j);
273     }
274     return result;
275     }
276
277     /**
278      * @return the string representation of this NameComponent array
279      */

280
281     public static String JavaDoc toString( org.omg.CosNaming.NameComponent JavaDoc[] n)
282     throws org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc
283     {
284     if( n == null || n.length == 0 )
285         throw new org.omg.CosNaming.NamingContextPackage.InvalidName JavaDoc();
286
287     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
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     /**
307      * escape any occurrence of "/", "." and "\"
308      */

309
310     private static String JavaDoc escape(String JavaDoc s)
311     {
312     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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