1 /* XMLEventFactory.java --
2 Copyright (C) 2005,2006,2009 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
38 package javax.xml.stream;
40 import java.io.BufferedReader;
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;
64 * Factory for XML events.
66 public abstract class XMLEventFactory
69 protected XMLEventFactory()
74 * Create a new factory instance.
75 * @see #newInstance(String,ClassLoader)
77 public static XMLEventFactory newInstance()
78 throws FactoryConfigurationError
80 return newInstance(null, null);
84 * Create a new factory instance.
85 * The implementation class to load is the first found in the following
88 * <li>the <code>javax.xml.stream.XMLEventFactory</code> system
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>
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
103 public static XMLEventFactory newInstance(String factoryId,
104 ClassLoader classLoader)
105 throws FactoryConfigurationError
107 ClassLoader loader = classLoader;
110 loader = Thread.currentThread().getContextClassLoader();
114 loader = XMLEventFactory.class.getClassLoader();
116 String className = null;
120 className = getFactoryClassName(loader, count++);
121 if (className != null)
125 Class<?> t = (loader != null) ? loader.loadClass(className) :
126 Class.forName(className);
127 return (XMLEventFactory) t.newInstance();
129 catch (ClassNotFoundException e)
135 throw new FactoryConfigurationError(e,
136 "error instantiating class " + className);
140 while (className == null && count < 3);
141 return new gnu.xml.stream.XMLEventFactoryImpl();
144 private static String getFactoryClassName(ClassLoader loader, int attempt)
146 final String propertyName = "javax.xml.stream.XMLEventFactory";
150 return System.getProperty(propertyName);
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();
161 return props.getProperty(propertyName);
163 catch (IOException e)
170 String serviceKey = "/META-INF/services/" + propertyName;
171 InputStream in = (loader != null) ?
172 loader.getResourceAsStream(serviceKey) :
173 XMLEventFactory.class.getResourceAsStream(serviceKey);
177 new BufferedReader(new InputStreamReader(in));
178 String ret = r.readLine();
183 catch (IOException e)
193 * Sets the location for each event created by this factory.
195 public abstract void setLocation(Location location);
198 * Create an attribute event.
200 public abstract Attribute createAttribute(String prefix, String namespaceURI,
201 String localName, String value);
204 * Create an attribute event.
206 public abstract Attribute createAttribute(String localName, String value);
209 * Create an attribute event.
211 public abstract Attribute createAttribute(QName name, String value);
214 * Create a namespace declaration event.
216 public abstract Namespace createNamespace(String namespaceURI);
219 * Create a namespace declaration event.
221 public abstract Namespace createNamespace(String prefix, String namespaceUri);
224 * Create a start-element event.
226 @SuppressWarnings("rawtypes")
227 public abstract StartElement createStartElement(QName name,
229 Iterator namespaces);
232 * Create a start-element event.
234 public abstract StartElement createStartElement(String prefix,
239 * Create a start-element event.
241 @SuppressWarnings("rawtypes")
242 public abstract StartElement createStartElement(String prefix,
246 Iterator namespaces);
249 * Create a start-element event.
251 @SuppressWarnings("rawtypes")
252 public abstract StartElement createStartElement(String prefix,
257 NamespaceContext context);
260 * Create an end-element event.
262 @SuppressWarnings("rawtypes")
263 public abstract EndElement createEndElement(QName name,
264 Iterator namespaces);
267 * Create an end-element event.
269 public abstract EndElement createEndElement(String prefix,
274 * Create an end-element event.
276 @SuppressWarnings("rawtypes")
277 public abstract EndElement createEndElement(String prefix,
280 Iterator namespaces);
283 * Create a text event.
285 public abstract Characters createCharacters(String content);
288 * Create a text event of type CDATA section.
290 public abstract Characters createCData(String content);
293 * Create a text event of type whitespace.
295 public abstract Characters createSpace(String content);
298 * Create a text event of type ignorable whitespace.
300 public abstract Characters createIgnorableSpace(String content);
303 * Create a start-document event.
305 public abstract StartDocument createStartDocument();
308 * Create a start-document event.
310 public abstract StartDocument createStartDocument(String encoding,
315 * Create a start-document event.
317 public abstract StartDocument createStartDocument(String encoding,
321 * Create a start-document event.
323 public abstract StartDocument createStartDocument(String encoding);
326 * Create an end-document event.
328 public abstract EndDocument createEndDocument();
331 * Create an entity reference event.
333 public abstract EntityReference createEntityReference(String name,
334 EntityDeclaration declaration);
337 * Create a comment event.
339 public abstract Comment createComment(String text);
342 * Create a processing instruction event.
344 public abstract ProcessingInstruction createProcessingInstruction(String target,
348 * Create a DOCTYPE declaration event.
350 public abstract DTD createDTD(String dtd);