Source for javax.xml.stream.XMLEventFactory

   1: /* XMLEventFactory.java -- 
   2:    Copyright (C) 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: package javax.xml.stream;
  39: 
  40: import java.io.BufferedReader;
  41: import java.io.File;
  42: import java.io.FileInputStream;
  43: import java.io.InputStream;
  44: import java.io.InputStreamReader;
  45: import java.io.IOException;
  46: import java.io.Reader;
  47: import java.util.Iterator;
  48: import java.util.Properties;
  49: import javax.xml.namespace.NamespaceContext;
  50: import javax.xml.namespace.QName;
  51: import javax.xml.stream.events.Attribute;
  52: import javax.xml.stream.events.Characters;
  53: import javax.xml.stream.events.Comment;
  54: import javax.xml.stream.events.DTD;
  55: import javax.xml.stream.events.EndDocument;
  56: import javax.xml.stream.events.EndElement;
  57: import javax.xml.stream.events.EntityDeclaration;
  58: import javax.xml.stream.events.EntityReference;
  59: import javax.xml.stream.events.Namespace;
  60: import javax.xml.stream.events.ProcessingInstruction;
  61: import javax.xml.stream.events.StartDocument;
  62: import javax.xml.stream.events.StartElement;
  63: 
  64: /**
  65:  * Factory for XML events.
  66:  */
  67: public abstract class XMLEventFactory
  68: {
  69: 
  70:   protected XMLEventFactory()
  71:   {
  72:   }
  73: 
  74:   /**
  75:    * Create a new factory instance.
  76:    * @see #newInstance(String,ClassLoader)
  77:    */
  78:   public static XMLEventFactory newInstance()
  79:     throws FactoryConfigurationError
  80:   {
  81:     return newInstance(null, null);
  82:   }
  83: 
  84:   /**
  85:    * Create a new factory instance.
  86:    * The implementation class to load is the first found in the following
  87:    * locations:
  88:    * <ol>
  89:    * <li>the <code>javax.xml.stream.XMLEventFactory</code> system
  90:    * property</li>
  91:    * <li>the above named property value in the
  92:    * <code><i>$JAVA_HOME</i>/lib/stax.properties</code> file</li>
  93:    * <li>the class name specified in the
  94:    * <code>META-INF/services/javax.xml.stream.XMLEventFactory</code>
  95:    * system resource</li>
  96:    * <li>the default factory class</li>
  97:    * </ol>
  98:    */
  99:   static XMLEventFactory newInstance(String factoryId, ClassLoader classLoader)
 100:     throws FactoryConfigurationError
 101:   {
 102:     ClassLoader loader = classLoader;
 103:     if (loader == null)
 104:       {
 105:         loader = Thread.currentThread().getContextClassLoader();
 106:       }
 107:     if (loader == null)
 108:       {
 109:         loader = XMLEventFactory.class.getClassLoader();
 110:       }
 111:     String className = null;
 112:     int count = 0;
 113:     do
 114:       {
 115:         className = getFactoryClassName(loader, count++);
 116:         if (className != null)
 117:           {
 118:             try
 119:               {
 120:                 Class t = (loader != null) ? loader.loadClass(className) :
 121:                   Class.forName(className);
 122:                 return (XMLEventFactory) t.newInstance();
 123:               }
 124:             catch (ClassNotFoundException e)
 125:               {
 126:                 className = null;
 127:               }
 128:             catch (Exception e)
 129:               {
 130:                 throw new FactoryConfigurationError(e,
 131:                      "error instantiating class " + className);
 132:               }
 133:           }
 134:       }
 135:     while (className == null && count < 3);
 136:     return new gnu.xml.stream.XMLEventFactoryImpl();
 137:   }
 138: 
 139:   private static String getFactoryClassName(ClassLoader loader, int attempt)
 140:   {
 141:     final String propertyName = "javax.xml.stream.XMLEventFactory";
 142:     switch (attempt)
 143:       {
 144:         case 0:
 145:           return System.getProperty(propertyName);
 146:         case 1:
 147:           try
 148:             {
 149:               File file = new File(System.getProperty("java.home"));
 150:               file = new File(file, "lib");
 151:               file = new File(file, "stax.properties");
 152:               InputStream in = new FileInputStream(file);
 153:               Properties props = new Properties();
 154:               props.load(in);
 155:               in.close();
 156:               return props.getProperty(propertyName);
 157:             }
 158:           catch (IOException e)
 159:             {
 160:               return null;
 161:             }
 162:         case 2:
 163:           try
 164:             {
 165:               String serviceKey = "/META-INF/services/" + propertyName;
 166:               InputStream in = (loader != null) ?
 167:                  loader.getResourceAsStream(serviceKey) :
 168:                 XMLEventFactory.class.getResourceAsStream(serviceKey);
 169:               if (in != null)
 170:                 {
 171:                   BufferedReader r =
 172:                      new BufferedReader(new InputStreamReader(in));
 173:                   String ret = r.readLine();
 174:                   r.close();
 175:                   return ret;
 176:                 }
 177:             }
 178:           catch (IOException e)
 179:             {
 180:             }
 181:           return null;
 182:         default:
 183:           return null;
 184:       }
 185:   }
 186: 
 187:   /**
 188:    * Sets the location for each event created by this factory.
 189:    */
 190:   public abstract void setLocation(Location location);
 191: 
 192:   /**
 193:    * Create an attribute event.
 194:    */
 195:   public abstract Attribute createAttribute(String prefix, String namespaceURI,
 196:                                             String localName, String value);
 197:   
 198:   /**
 199:    * Create an attribute event.
 200:    */
 201:   public abstract Attribute createAttribute(String localName, String value);
 202: 
 203:   /**
 204:    * Create an attribute event.
 205:    */
 206:   public abstract Attribute createAttribute(QName name, String value);
 207: 
 208:   /**
 209:    * Create a namespace declaration event.
 210:    */
 211:   public abstract Namespace createNamespace(String namespaceURI);
 212: 
 213:   /**
 214:    * Create a namespace declaration event.
 215:    */
 216:   public abstract Namespace createNamespace(String prefix, String namespaceUri);
 217: 
 218:   /**
 219:    * Create a start-element event.
 220:    */
 221:   public abstract StartElement createStartElement(QName name,
 222:                                                   Iterator attributes,
 223:                                                   Iterator namespaces);
 224: 
 225:   /**
 226:    * Create a start-element event.
 227:    */
 228:   public abstract StartElement createStartElement(String prefix,
 229:                                                   String namespaceUri,
 230:                                                   String localName);
 231: 
 232:   /**
 233:    * Create a start-element event.
 234:    */
 235:   public abstract StartElement createStartElement(String prefix,
 236:                                                   String namespaceUri,
 237:                                                   String localName,
 238:                                                   Iterator attributes,
 239:                                                   Iterator namespaces);
 240: 
 241:   /**
 242:    * Create a start-element event.
 243:    */
 244:   public abstract StartElement createStartElement(String prefix,
 245:                                                   String namespaceUri,
 246:                                                   String localName,
 247:                                                   Iterator attributes,
 248:                                                   Iterator namespaces,
 249:                                                   NamespaceContext context);
 250:   
 251:   /**
 252:    * Create an end-element event.
 253:    */
 254:   public abstract EndElement createEndElement(QName name,
 255:                                               Iterator namespaces);
 256: 
 257:   /**
 258:    * Create an end-element event.
 259:    */
 260:   public abstract EndElement createEndElement(String prefix,
 261:                                               String namespaceUri,
 262:                                               String localName);
 263: 
 264:   /**
 265:    * Create an end-element event.
 266:    */
 267:   public abstract EndElement createEndElement(String prefix,
 268:                                               String namespaceUri,
 269:                                               String localName,
 270:                                               Iterator namespaces);
 271: 
 272:   /**
 273:    * Create a text event.
 274:    */
 275:   public abstract Characters createCharacters(String content);
 276: 
 277:   /**
 278:    * Create a text event of type CDATA section.
 279:    */
 280:   public abstract Characters createCData(String content);
 281: 
 282:   /**
 283:    * Create a text event of type whitespace.
 284:    */
 285:   public abstract Characters createSpace(String content);
 286: 
 287:   /**
 288:    * Create a text event of type ignorable whitespace.
 289:    */
 290:   public abstract Characters createIgnorableSpace(String content);
 291: 
 292:   /**
 293:    * Create a start-document event.
 294:    */
 295:   public abstract StartDocument createStartDocument();
 296: 
 297:   /**
 298:    * Create a start-document event.
 299:    */
 300:   public abstract StartDocument createStartDocument(String encoding,
 301:                                                     String version,
 302:                                                     boolean standalone);
 303: 
 304:   /**
 305:    * Create a start-document event.
 306:    */
 307:   public abstract StartDocument createStartDocument(String encoding,
 308:                                                     String version);
 309: 
 310:   /**
 311:    * Create a start-document event.
 312:    */
 313:   public abstract StartDocument createStartDocument(String encoding);
 314: 
 315:   /**
 316:    * Create an end-document event.
 317:    */
 318:   public abstract EndDocument createEndDocument();
 319: 
 320:   /**
 321:    * Create an entity reference event.
 322:    */
 323:   public abstract EntityReference createEntityReference(String name,
 324:                                                         EntityDeclaration declaration);
 325: 
 326:   /**
 327:    * Create a comment event.
 328:    */
 329:   public abstract Comment createComment(String text);
 330: 
 331:   /**
 332:    * Create a processing instruction event.
 333:    */
 334:   public abstract ProcessingInstruction createProcessingInstruction(String target,
 335:                                                                     String data);
 336: 
 337:   /**
 338:    * Create a DOCTYPE declaration event.
 339:    */
 340:   public abstract DTD createDTD(String dtd);
 341:   
 342: }