source: trunk/gcc/libjava/javax/naming/CompositeName.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.9 KB
Line 
1/* CompositeName.java --
2 Copyright (C) 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package javax.naming;
40
41import java.io.Serializable;
42import java.util.Enumeration;
43import java.util.NoSuchElementException;
44import java.util.Vector;
45
46/**
47 * @author Tom Tromey <tromey@redhat.com>
48 * @date May 16, 2001
49 *
50 * FIXME: must write readObject and writeObject to conform to
51 * serialization spec.
52 */
53public class CompositeName implements Name, Cloneable, Serializable
54{
55 public CompositeName ()
56 {
57 elts = new Vector ();
58 }
59
60 protected CompositeName (Enumeration comps)
61 {
62 elts = new Vector ();
63 try
64 {
65 while (comps.hasMoreElements ())
66 elts.add (comps.nextElement ());
67 }
68 catch (NoSuchElementException ignore)
69 {
70 }
71 }
72
73 public CompositeName (String n) throws InvalidNameException
74 {
75 elts = new Vector ();
76 // Parse the string into its components.
77 final char no_quote = 'x'; // Use 'x' to mean no quoting.
78 char quote = no_quote;
79 boolean escaped = false;
80 StringBuffer new_element = new StringBuffer ();
81 for (int i = 0; i < n.length (); ++i)
82 {
83 char c = n.charAt (i);
84 if (escaped)
85 escaped = false;
86 else if (c == '\\')
87 {
88 escaped = true;
89 continue;
90 }
91 else if (quote != no_quote)
92 {
93 if (quote == c)
94 {
95 // The quotes must surround a complete component.
96 if (i + 1 < n.length () && n.charAt (i + 1) != '/')
97 throw new InvalidNameException ("close quote before end of component");
98 elts.add (new_element.toString ());
99 new_element.setLength (0);
100 quote = no_quote;
101 continue;
102 }
103 // Otherwise, fall through.
104 }
105 // Quotes are only special at the start of a component.
106 else if (new_element.length () == 0
107 && (c == '\'' || c == '"'))
108 {
109 quote = c;
110 continue;
111 }
112 else if (c == '/')
113 {
114 elts.add (new_element.toString ());
115 new_element.setLength (0);
116 continue;
117 }
118
119 new_element.append (c);
120 }
121
122 if (new_element.length () != 0)
123 elts.add (new_element.toString ());
124
125 // Error checking.
126 if (quote != no_quote)
127 throw new InvalidNameException ("unterminated quote");
128 if (escaped)
129 throw new InvalidNameException ("trailing escape character");
130 }
131
132 public Name add (int posn, String comp) throws InvalidNameException
133 {
134 elts.add (posn, comp);
135 return this;
136 }
137
138 public Name add (String comp) throws InvalidNameException
139 {
140 elts.add (comp);
141 return this;
142 }
143
144 public Name addAll (int posn, Name n) throws InvalidNameException
145 {
146 Enumeration e = n.getAll ();
147 try
148 {
149 while (e.hasMoreElements ())
150 {
151 elts.add (posn, e.nextElement ());
152 ++posn;
153 }
154 }
155 catch (NoSuchElementException ignore)
156 {
157 }
158 return this;
159 }
160
161 public Name addAll (Name suffix) throws InvalidNameException
162 {
163 Enumeration e = suffix.getAll ();
164 try
165 {
166 while (e.hasMoreElements ())
167 elts.add (e.nextElement ());
168 }
169 catch (NoSuchElementException ignore)
170 {
171 }
172 return this;
173 }
174
175 public Object clone ()
176 {
177 return new CompositeName (elts.elements ());
178 }
179
180 public int compareTo (Object obj)
181 {
182 if (obj == null || ! (obj instanceof CompositeName))
183 throw new ClassCastException ("CompositeName.compareTo() expected CompositeName");
184 CompositeName cn = (CompositeName) obj;
185 int last = Math.min (cn.elts.size (), elts.size ());
186 for (int i = 0; i < last; ++i)
187 {
188 String f = (String) elts.get (i);
189 int comp = f.compareTo ((String) cn.elts.get (i));
190 if (comp != 0)
191 return comp;
192 }
193 return elts.size () - cn.elts.size ();
194 }
195
196 public boolean endsWith (Name n)
197 {
198 if (! (n instanceof CompositeName))
199 return false;
200 CompositeName cn = (CompositeName) n;
201 if (cn.elts.size () > elts.size ())
202 return false;
203 int delta = elts.size () - cn.elts.size ();
204 for (int i = 0; i < cn.elts.size (); ++i)
205 {
206 if (! cn.elts.get (i).equals (elts.get (delta + i)))
207 return false;
208 }
209 return true;
210 }
211
212 public boolean equals (Object obj)
213 {
214 if (! (obj instanceof CompositeName))
215 return false;
216 CompositeName cn = (CompositeName) obj;
217 return elts.equals (cn.elts);
218 }
219
220 public String get (int posn)
221 {
222 return (String) elts.get (posn);
223 }
224
225 public Enumeration getAll ()
226 {
227 return elts.elements ();
228 }
229
230 public Name getPrefix (int posn)
231 {
232 CompositeName cn = new CompositeName ();
233 for (int i = 0; i < posn; ++i)
234 cn.elts.add ((String) elts.get (i));
235 return cn;
236 }
237
238 public Name getSuffix (int posn)
239 {
240 if (posn > elts.size ())
241 throw new ArrayIndexOutOfBoundsException (posn);
242 CompositeName cn = new CompositeName ();
243 for (int i = posn; i < elts.size (); ++i)
244 cn.elts.add ((String) elts.get (i));
245 return cn;
246 }
247
248 public int hashCode ()
249 {
250 // Specified in documentation.
251 int h = 0;
252 for (int i = 0; i < elts.size (); ++i)
253 h += elts.get (i).hashCode ();
254 return h;
255 }
256
257 public boolean isEmpty ()
258 {
259 return elts.isEmpty ();
260 }
261
262 public Object remove (int posn) throws InvalidNameException
263 {
264 return elts.remove (posn);
265 }
266
267 public int size ()
268 {
269 return elts.size ();
270 }
271
272 public boolean startsWith (Name n)
273 {
274 if (! (n instanceof CompositeName))
275 return false;
276 CompositeName cn = (CompositeName) n;
277 if (cn.elts.size () > elts.size ())
278 return false;
279 for (int i = 0; i < cn.elts.size (); ++i)
280 {
281 if (! cn.elts.get (i).equals (elts.get (i)))
282 return false;
283 }
284 return true;
285 }
286
287 public String toString ()
288 {
289 StringBuffer result = new StringBuffer ();
290 for (int i = 0; i < elts.size (); ++i)
291 {
292 // For simplicity we choose to always quote using escapes and
293 // never quotes.
294 String elt = (String) elts.get (i);
295 if (i > 0
296 || (i == elts.size () - 1 && elt.equals ("")))
297 result.append ('/');
298 for (int k = 0; k < elt.length (); ++k)
299 {
300 char c = elt.charAt (k);
301 // We must quote
302 // ... a leading quote,
303 if ((k == 0 && (c == '"' || c == '\''))
304 // ... an escape preceding a meta character,
305 // or at the end of a component,
306 || (c == '\\'
307 && (k == elt.length () - 1
308 || "\\'\"/".indexOf (elt.charAt (k + 1)) != -1))
309 // ... or a component separator.
310 || c == '/')
311 result.append ('\\');
312 result.append (c);
313 }
314 }
315 return result.toString ();
316 }
317
318 private transient Vector elts;
319}
Note: See TracBrowser for help on using the repository browser.