Source for javax.xml.stream.XMLEventFactory

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