Source for javax.naming.CompositeName

   1: /* CompositeName.java --
   2:    Copyright (C) 2001, 2005, 2006  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.naming;
  40: 
  41: import java.io.IOException;
  42: import java.io.ObjectInputStream;
  43: import java.io.ObjectOutputStream;
  44: import java.io.Serializable;
  45: import java.util.Enumeration;
  46: import java.util.NoSuchElementException;
  47: import java.util.Vector;
  48: 
  49: /**
  50:  * @author Tom Tromey (tromey@redhat.com)
  51:  */
  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:     // Parse the string into its components.
  78:     final char no_quote = 'x';    // Use 'x' to mean no quoting.
  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:         // The quotes must surround a complete component.
  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:         // Otherwise, fall through.
 105:       }
 106:     // Quotes are only special at the start of a component.
 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:     // Error checking.
 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:     // Specified in documentation.
 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:     // For simplicity we choose to always quote using escapes and
 294:     // never quotes.
 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:         // We must quote
 303:         //     ... a leading quote,
 304:         if ((k == 0 && (c == '"' || c == '\''))
 305:         // ... an escape preceding a meta character,
 306:         //     or at the end of a component,
 307:         || (c == '\\'
 308:             && (k == elt.length () - 1
 309:             || "\\'\"/".indexOf (elt.charAt (k + 1)) != -1))
 310:         // ... or a component separator.
 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: }