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