libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / xml / stream / XMLEventFactory.java
blob30e160793b9680ec9545434dbc18b0a5bf7bdb88
1 /* XMLEventFactory.java --
2 Copyright (C) 2005,2006 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)
9 any later version.
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
19 02110-1301 USA.
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.
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;
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;
63 /**
64 * Factory for XML events.
66 public abstract class XMLEventFactory
69 protected XMLEventFactory()
73 /**
74 * Create a new factory instance.
75 * @see #newInstance(String,ClassLoader)
77 public static XMLEventFactory newInstance()
78 throws FactoryConfigurationError
80 return newInstance(null, null);
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>
98 static XMLEventFactory newInstance(String factoryId, ClassLoader classLoader)
99 throws FactoryConfigurationError
101 ClassLoader loader = classLoader;
102 if (loader == null)
104 loader = Thread.currentThread().getContextClassLoader();
106 if (loader == null)
108 loader = XMLEventFactory.class.getClassLoader();
110 String className = null;
111 int count = 0;
114 className = getFactoryClassName(loader, count++);
115 if (className != null)
119 Class<?> t = (loader != null) ? loader.loadClass(className) :
120 Class.forName(className);
121 return (XMLEventFactory) t.newInstance();
123 catch (ClassNotFoundException e)
125 className = null;
127 catch (Exception e)
129 throw new FactoryConfigurationError(e,
130 "error instantiating class " + className);
134 while (className == null && count < 3);
135 return new gnu.xml.stream.XMLEventFactoryImpl();
138 private static String getFactoryClassName(ClassLoader loader, int attempt)
140 final String propertyName = "javax.xml.stream.XMLEventFactory";
141 switch (attempt)
143 case 0:
144 return System.getProperty(propertyName);
145 case 1:
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);
157 catch (IOException e)
159 return null;
161 case 2:
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)
170 BufferedReader r =
171 new BufferedReader(new InputStreamReader(in));
172 String ret = r.readLine();
173 r.close();
174 return ret;
177 catch (IOException e)
180 return null;
181 default:
182 return null;
187 * Sets the location for each event created by this factory.
189 public abstract void setLocation(Location location);
192 * Create an attribute event.
194 public abstract Attribute createAttribute(String prefix, String namespaceURI,
195 String localName, String value);
198 * Create an attribute event.
200 public abstract Attribute createAttribute(String localName, String value);
203 * Create an attribute event.
205 public abstract Attribute createAttribute(QName name, String value);
208 * Create a namespace declaration event.
210 public abstract Namespace createNamespace(String namespaceURI);
213 * Create a namespace declaration event.
215 public abstract Namespace createNamespace(String prefix, String namespaceUri);
218 * Create a start-element event.
220 @SuppressWarnings("unchecked")
221 public abstract StartElement createStartElement(QName name,
222 Iterator attributes,
223 Iterator namespaces);
226 * Create a start-element event.
228 public abstract StartElement createStartElement(String prefix,
229 String namespaceUri,
230 String localName);
233 * Create a start-element event.
235 @SuppressWarnings("unchecked")
236 public abstract StartElement createStartElement(String prefix,
237 String namespaceUri,
238 String localName,
239 Iterator attributes,
240 Iterator namespaces);
243 * Create a start-element event.
245 @SuppressWarnings("unchecked")
246 public abstract StartElement createStartElement(String prefix,
247 String namespaceUri,
248 String localName,
249 Iterator attributes,
250 Iterator namespaces,
251 NamespaceContext context);
254 * Create an end-element event.
256 @SuppressWarnings("unchecked")
257 public abstract EndElement createEndElement(QName name,
258 Iterator namespaces);
261 * Create an end-element event.
263 public abstract EndElement createEndElement(String prefix,
264 String namespaceUri,
265 String localName);
268 * Create an end-element event.
270 @SuppressWarnings("unchecked")
271 public abstract EndElement createEndElement(String prefix,
272 String namespaceUri,
273 String localName,
274 Iterator namespaces);
277 * Create a text event.
279 public abstract Characters createCharacters(String content);
282 * Create a text event of type CDATA section.
284 public abstract Characters createCData(String content);
287 * Create a text event of type whitespace.
289 public abstract Characters createSpace(String content);
292 * Create a text event of type ignorable whitespace.
294 public abstract Characters createIgnorableSpace(String content);
297 * Create a start-document event.
299 public abstract StartDocument createStartDocument();
302 * Create a start-document event.
304 public abstract StartDocument createStartDocument(String encoding,
305 String version,
306 boolean standalone);
309 * Create a start-document event.
311 public abstract StartDocument createStartDocument(String encoding,
312 String version);
315 * Create a start-document event.
317 public abstract StartDocument createStartDocument(String encoding);
320 * Create an end-document event.
322 public abstract EndDocument createEndDocument();
325 * Create an entity reference event.
327 public abstract EntityReference createEntityReference(String name,
328 EntityDeclaration declaration);
331 * Create a comment event.
333 public abstract Comment createComment(String text);
336 * Create a processing instruction event.
338 public abstract ProcessingInstruction createProcessingInstruction(String target,
339 String data);
342 * Create a DOCTYPE declaration event.
344 public abstract DTD createDTD(String dtd);