1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
57: public class CompositeName implements Name, Cloneable, Serializable
58: {
59: private static final long serialVersionUID = 1667768148915813118L;
60:
61: private transient Vector<String> elts;
62:
63: public CompositeName ()
64: {
65: elts = new Vector<String> ();
66: }
67:
68: protected CompositeName (Enumeration<String> comps)
69: {
70: elts = new Vector<String> ();
71: try
72: {
73: while (comps.hasMoreElements ())
74: elts.add (comps.nextElement ());
75: }
76: catch (NoSuchElementException ignore)
77: {
78: }
79: }
80:
81: public CompositeName (String n) throws InvalidNameException
82: {
83: elts = new Vector<String> ();
84:
85: final char no_quote = 'x';
86: char quote = no_quote;
87: boolean escaped = false;
88: StringBuffer new_element = new StringBuffer ();
89: for (int i = 0; i < n.length (); ++i)
90: {
91: char c = n.charAt (i);
92: if (escaped)
93: escaped = false;
94: else if (c == '\\')
95: {
96: escaped = true;
97: continue;
98: }
99: else if (quote != no_quote)
100: {
101: if (quote == c)
102: {
103:
104: if (i + 1 < n.length () && n.charAt (i + 1) != '/')
105: throw new InvalidNameException ("close quote before end of component");
106: elts.add (new_element.toString ());
107: new_element.setLength (0);
108: quote = no_quote;
109: continue;
110: }
111:
112: }
113:
114: else if (new_element.length () == 0
115: && (c == '\'' || c == '"'))
116: {
117: quote = c;
118: continue;
119: }
120: else if (c == '/')
121: {
122: elts.add (new_element.toString ());
123: new_element.setLength (0);
124: continue;
125: }
126:
127: new_element.append (c);
128: }
129:
130: if (new_element.length () != 0)
131: elts.add (new_element.toString ());
132:
133:
134: if (quote != no_quote)
135: throw new InvalidNameException ("unterminated quote");
136: if (escaped)
137: throw new InvalidNameException ("trailing escape character");
138: }
139:
140: public Name add (int posn, String comp) throws InvalidNameException
141: {
142: elts.add (posn, comp);
143: return this;
144: }
145:
146: public Name add (String comp) throws InvalidNameException
147: {
148: elts.add (comp);
149: return this;
150: }
151:
152: public Name addAll (int posn, Name n) throws InvalidNameException
153: {
154: Enumeration<String> e = n.getAll ();
155: try
156: {
157: while (e.hasMoreElements ())
158: {
159: elts.add (posn, e.nextElement ());
160: ++posn;
161: }
162: }
163: catch (NoSuchElementException ignore)
164: {
165: }
166: return this;
167: }
168:
169: public Name addAll (Name suffix) throws InvalidNameException
170: {
171: Enumeration<String> e = suffix.getAll ();
172: try
173: {
174: while (e.hasMoreElements ())
175: elts.add (e.nextElement ());
176: }
177: catch (NoSuchElementException ignore)
178: {
179: }
180: return this;
181: }
182:
183: public Object clone ()
184: {
185: return new CompositeName (elts.elements ());
186: }
187:
188: public int compareTo (Object obj)
189: {
190: if (obj == null || ! (obj instanceof CompositeName))
191: throw new ClassCastException ("CompositeName.compareTo() expected CompositeName");
192: CompositeName cn = (CompositeName) obj;
193: int last = Math.min (cn.elts.size (), elts.size ());
194: for (int i = 0; i < last; ++i)
195: {
196: String f = elts.get (i);
197: int comp = f.compareTo (cn.elts.get (i));
198: if (comp != 0)
199: return comp;
200: }
201: return elts.size () - cn.elts.size ();
202: }
203:
204: public boolean endsWith (Name n)
205: {
206: if (! (n instanceof CompositeName))
207: return false;
208: CompositeName cn = (CompositeName) n;
209: if (cn.elts.size () > elts.size ())
210: return false;
211: int delta = elts.size () - cn.elts.size ();
212: for (int i = 0; i < cn.elts.size (); ++i)
213: {
214: if (! cn.elts.get (i).equals (elts.get (delta + i)))
215: return false;
216: }
217: return true;
218: }
219:
220: public boolean equals (Object obj)
221: {
222: if (! (obj instanceof CompositeName))
223: return false;
224: CompositeName cn = (CompositeName) obj;
225: return elts.equals (cn.elts);
226: }
227:
228: public String get (int posn)
229: {
230: return elts.get (posn);
231: }
232:
233: public Enumeration<String> getAll ()
234: {
235: return elts.elements ();
236: }
237:
238: public Name getPrefix (int posn)
239: {
240: CompositeName cn = new CompositeName ();
241: for (int i = 0; i < posn; ++i)
242: cn.elts.add (elts.get (i));
243: return cn;
244: }
245:
246: public Name getSuffix (int posn)
247: {
248: if (posn > elts.size ())
249: throw new ArrayIndexOutOfBoundsException (posn);
250: CompositeName cn = new CompositeName ();
251: for (int i = posn; i < elts.size (); ++i)
252: cn.elts.add (elts.get (i));
253: return cn;
254: }
255:
256: public int hashCode ()
257: {
258:
259: int h = 0;
260: for (int i = 0; i < elts.size (); ++i)
261: h += elts.get (i).hashCode ();
262: return h;
263: }
264:
265: public boolean isEmpty ()
266: {
267: return elts.isEmpty ();
268: }
269:
270: public Object remove (int posn) throws InvalidNameException
271: {
272: return elts.remove (posn);
273: }
274:
275: public int size ()
276: {
277: return elts.size ();
278: }
279:
280: public boolean startsWith (Name n)
281: {
282: if (! (n instanceof CompositeName))
283: return false;
284: CompositeName cn = (CompositeName) n;
285: if (cn.elts.size () > elts.size ())
286: return false;
287: for (int i = 0; i < cn.elts.size (); ++i)
288: {
289: if (! cn.elts.get (i).equals (elts.get (i)))
290: return false;
291: }
292: return true;
293: }
294:
295: public String toString ()
296: {
297: StringBuffer result = new StringBuffer ();
298: for (int i = 0; i < elts.size (); ++i)
299: {
300:
301:
302: String elt = elts.get (i);
303: if (i > 0
304: || (i == elts.size () - 1 && elt.equals ("")))
305: result.append ('/');
306: for (int k = 0; k < elt.length (); ++k)
307: {
308: char c = elt.charAt (k);
309:
310:
311: if ((k == 0 && (c == '"' || c == '\''))
312:
313:
314: || (c == '\\'
315: && (k == elt.length () - 1
316: || "\\'\"/".indexOf (elt.charAt (k + 1)) != -1))
317:
318: || c == '/')
319: result.append ('\\');
320: result.append (c);
321: }
322: }
323: return result.toString ();
324: }
325:
326: private void readObject(ObjectInputStream s)
327: throws IOException, ClassNotFoundException
328: {
329: int size = s.readInt();
330: elts = new Vector<String>(size);
331: for (int i = 0; i < size; i++)
332: elts.add((String) s.readObject());
333: }
334:
335: private void writeObject(ObjectOutputStream s) throws IOException
336: {
337: s.writeInt(elts.size());
338: for (int i = 0; i < elts.size(); i++)
339: s.writeObject(elts.get(i));
340: }
341: }