KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > naming > QName


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.naming;
30
31 import com.caucho.util.L10N;
32
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InvalidNameException JavaDoc;
35 import javax.naming.Name JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collections JavaDoc;
39 import java.util.Enumeration JavaDoc;
40
41 /**
42  * Represents a parsed JNDI name.
43  */

44 public class QName implements Name JavaDoc {
45   private static L10N L = new L10N(QName.class);
46
47    // The owning root context.
48
protected Context JavaDoc _context;
49
50   // The name items
51
private ArrayList JavaDoc<String JavaDoc> _items = new ArrayList JavaDoc<String JavaDoc>();
52
53   /**
54    * Creates a root name based on a context.
55    *
56    * @param context the root context
57    */

58   public QName(Context JavaDoc context)
59   {
60     _context = context;
61   }
62
63   /**
64    * Creates a new name with a single component.
65    *
66    * @param context the root context
67    * @param first the first name component
68    */

69   public QName(Context JavaDoc context, String JavaDoc first)
70   {
71     _context = context;
72     
73     if (first != null)
74       _items.add(first);
75   }
76
77
78   /**
79    * Creates a new name with two components
80    *
81    * @param context the root context
82    * @param first the first name component
83    * @param tail the tail name component
84    */

85   public QName(Context JavaDoc context, String JavaDoc first, String JavaDoc rest)
86   {
87     _context = context;
88     
89     if (first != null)
90       _items.add(first);
91     if (rest != null)
92       _items.add(rest);
93   }
94
95   /**
96    * Clones the name.
97    */

98   public Object JavaDoc clone()
99   {
100     QName name = new QName(_context);
101
102     for (int i = 0; i < _items.size(); i++)
103       name._items.add(_items.get(i));
104
105     return name;
106   }
107
108   public int size()
109   {
110     return _items.size();
111   }
112
113   public boolean isEmpty()
114   {
115     return _items.size() == 0;
116   }
117
118   public Enumeration JavaDoc getAll()
119   {
120     return Collections.enumeration(_items);
121   }
122
123   public String JavaDoc get(int pos)
124   {
125     if (pos < _items.size())
126       return (String JavaDoc) _items.get(pos);
127     else
128       return null;
129   }
130
131   public Name JavaDoc getPrefix(int posn)
132   {
133     QName name = new QName(_context);
134
135     for (int i = 0; i < posn; i++)
136       name._items.add(_items.get(i));
137
138     return name;
139   }
140   
141   public Name JavaDoc getSuffix(int posn)
142   {
143     Context JavaDoc subcontext = _context;
144
145     for (int i = 0; i < posn; i++) {
146       String JavaDoc item = (String JavaDoc) _items.get(i);
147       try {
148         Object JavaDoc obj = subcontext.lookup(item);
149         if (obj instanceof Context JavaDoc)
150           subcontext = (Context JavaDoc) obj;
151         else
152           break;
153       } catch (NamingException JavaDoc e) {
154         break;
155       }
156     }
157
158     QName name = new QName(subcontext);
159     for (int i = posn; i < _items.size(); i++) {
160       String JavaDoc item = (String JavaDoc) _items.get(i);
161       
162       name._items.add(_items.get(i));
163     }
164
165     return name;
166   }
167
168   /**
169    * Returns true if the argument is a prefix of the name.
170    *
171    * @param name the Name to start as a prefix.
172    */

173   public boolean startsWith(Name JavaDoc name)
174   {
175     if (name == null)
176       return false;
177     
178     if (size() < name.size())
179       return false;
180
181     for (int i = 0; i < name.size(); i++) {
182       if (! get(i).equals(name.get(i)))
183         return false;
184     }
185
186     return true;
187   }
188   
189   public boolean endsWith(Name JavaDoc name)
190   {
191     if (name == null)
192       return false;
193     
194     int nameSize = name.size();
195     if (size() < nameSize)
196       return false;
197
198     int offset = size() - nameSize;
199     for (int i = 0; i < nameSize; i++)
200       if (! get(i + offset).equals(name.get(i)))
201         return false;
202
203     return true;
204   }
205
206   /**
207    * Append a name to the current name.
208    *
209    * @param suffix the name to add as a suffix
210    *
211    * @return the modified name
212    */

213   public Name JavaDoc addAll(Name JavaDoc suffix)
214     throws InvalidNameException JavaDoc
215   {
216     for (int i = 0; i < suffix.size(); i++)
217       _items.add(suffix.get(i));
218     
219     return this;
220   }
221
222
223   /**
224    * Insert a name to the current name.
225    *
226    * @param suffix the name to add as a suffix
227    *
228    * @return the modified name
229    */

230   public Name JavaDoc addAll(int posn, Name JavaDoc suffix)
231     throws InvalidNameException JavaDoc
232   {
233     for (int i = 0; i < suffix.size(); i++)
234       _items.add(posn, suffix.get(i));
235
236     return this;
237   }
238
239   /**
240    * Add a component to the tail of the name, returning the name.
241    *
242    * @param comp the new component to add.
243    *
244    * @return the modified name
245    */

246   public Name JavaDoc add(String JavaDoc comp)
247     throws InvalidNameException JavaDoc
248   {
249     _items.add(comp);
250
251     return this;
252   }
253
254   /**
255    * Add a component at a specific position, returning the name.
256    *
257    * @return the modified name
258    */

259   public Name JavaDoc add(int posn, String JavaDoc comp)
260     throws InvalidNameException JavaDoc
261   {
262     _items.add(posn, comp);
263
264     return this;
265   }
266
267   public Object JavaDoc remove(int posn)
268     throws InvalidNameException JavaDoc
269   {
270     _items.remove(posn);
271     
272     return this;
273   }
274
275   /**
276    * Returns the name's hash code.
277    */

278   public int hashCode()
279   {
280     int hashCode = 337;
281
282     for (int i = size() - 1; i >= 0; i--)
283       hashCode = 65521 * hashCode + get(i).hashCode();
284
285     return hashCode;
286   }
287
288   /**
289    * Returns true if the object is an equivalent name.
290    *
291    * @param obj the object to test for equality.
292    */

293   public boolean equals(Object JavaDoc obj)
294   {
295     if (! (obj instanceof Name JavaDoc))
296       return false;
297
298     Name JavaDoc name = (Name JavaDoc) obj;
299
300     if (size() != name.size())
301       return false;
302
303     for (int i = size() - 1; i >= 0; i--) {
304       if (! get(i).equals(name.get(i)))
305         return false;
306     }
307
308     return true;
309   }
310
311   /**
312    * Compares the name to another name.
313    *
314    * @return -1 if less than b, 0 if equal, or 1 if greater chan
315    */

316   public int compareTo(Object JavaDoc rawB)
317   {
318     if (! (rawB instanceof Name JavaDoc))
319       return -1;
320
321     Name JavaDoc b = (Name JavaDoc) rawB;
322
323     for (int i = 0; i < size(); i++) {
324       if (i >= b.size())
325         return 1;
326       
327       String JavaDoc sa = (String JavaDoc) get(i);
328       String JavaDoc sb = (String JavaDoc) b.get(i);
329
330       int cmp = sa.compareTo(sb);
331       if (cmp != 0)
332         return cmp;
333     }
334
335     if (size() == b.size())
336       return 0;
337     else
338       return -1;
339   }
340
341   /**
342    * Converts the name to a string.
343    */

344   public String JavaDoc toString()
345   {
346     String JavaDoc name = null;
347
348     for (int i = 0; i < size(); i++) {
349       String JavaDoc str = (String JavaDoc) get(i);
350       
351       if (name != null) {
352         try {
353           name = _context.composeName(str, name);
354         } catch (NamingException JavaDoc e) {
355           name = name + "/" + str;
356         }
357       }
358       else
359         name = str;
360     }
361
362     return name == null ? "" : name;
363   }
364 }
365
Popular Tags