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