Source for javax.naming.CompositeName

   1: /* CompositeName.java --
   2:    Copyright (C) 2001, 2005  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.Serializable;
  42: import java.util.Enumeration;
  43: import java.util.NoSuchElementException;
  44: import 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:  */
  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:     // Parse the string into its components.
  79:     final char no_quote = 'x';    // Use 'x' to mean no quoting.
  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:         // The quotes must surround a complete component.
  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:         // Otherwise, fall through.
 106:       }
 107:     // Quotes are only special at the start of a component.
 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:     // Error checking.
 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:     // Specified in documentation.
 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:     // For simplicity we choose to always quote using escapes and
 295:     // never quotes.
 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:         // We must quote
 304:         //     ... a leading quote,
 305:         if ((k == 0 && (c == '"' || c == '\''))
 306:         // ... an escape preceding a meta character,
 307:         //     or at the end of a component,
 308:         || (c == '\\'
 309:             && (k == elt.length () - 1
 310:             || "\\'\"/".indexOf (elt.charAt (k + 1)) != -1))
 311:         // ... or a component separator.
 312:         || c == '/')
 313:           result.append ('\\');
 314:         result.append (c);
 315:       }
 316:       }
 317:     return result.toString ();
 318:   }
 319: 
 320:   private transient Vector elts;
 321: }