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:  * Represents names that may span over several namespaces. For instance,
  51:  * the composite name http://www.gnu.org/software/classpath/index.html spans
  52:  * over three namespaces (the protocol http, the web server location
  53:  * (www.gnu.org) and the index.html location on the server).
  54:  * 
  55:  * @author Tom Tromey (tromey@redhat.com)
  56:  */
  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:     // Parse the string into its components.
  85:     final char no_quote = 'x';    // Use 'x' to mean no quoting.
  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:         // The quotes must surround a complete component.
 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:         // Otherwise, fall through.
 112:       }
 113:     // Quotes are only special at the start of a component.
 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:     // Error checking.
 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:     // Specified in documentation.
 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:     // For simplicity we choose to always quote using escapes and
 301:     // never quotes.
 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:         // We must quote
 310:         //     ... a leading quote,
 311:         if ((k == 0 && (c == '"' || c == '\''))
 312:         // ... an escape preceding a meta character,
 313:         //     or at the end of a component,
 314:         || (c == '\\'
 315:             && (k == elt.length () - 1
 316:             || "\\'\"/".indexOf (elt.charAt (k + 1)) != -1))
 317:         // ... or a component separator.
 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: }