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