Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / xml / stream / XMLEventFactory.java
blob456414d6101c52586cc99af8e9500ab79c636550
1 /* XMLEventFactory.java --
2 Copyright (C) 2005 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.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;
64 /**
65 * Factory for XML events.
67 public abstract class XMLEventFactory
70 protected XMLEventFactory()
74 /**
75 * Create a new factory instance.
76 * @see #newInstance(String,ClassLoader)
78 public static XMLEventFactory newInstance()
79 throws FactoryConfigurationError
81 return newInstance(null, null);
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>
99 static XMLEventFactory newInstance(String factoryId, ClassLoader classLoader)
100 throws FactoryConfigurationError
102 ClassLoader loader = classLoader;
103 if (loader == null)
105 loader = Thread.currentThread().getContextClassLoader();
107 if (loader == null)
109 loader = XMLEventFactory.class.getClassLoader();
111 String className = null;
112 int count = 0;
115 className = getFactoryClassName(loader, count++);
116 if (className != null)
120 Class t = (loader != null) ? loader.loadClass(className) :
121 Class.forName(className);
122 return (XMLEventFactory) t.newInstance();
124 catch (ClassNotFoundException e)
126 className = null;
128 catch (Exception e)
130 throw new FactoryConfigurationError(e,
131 "error instantiating class " + className);
135 while (className == null && count < 3);
136 return new gnu.xml.stream.XMLEventFactoryImpl();
139 private static String getFactoryClassName(ClassLoader loader, int attempt)
141 final String propertyName = "javax.xml.stream.XMLEventFactory";
142 switch (attempt)
144 case 0:
145 return System.getProperty(propertyName);
146 case 1:
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);
158 catch (IOException e)
160 return null;
162 case 2:
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)
171 BufferedReader r =
172 new BufferedReader(new InputStreamReader(in));
173 String ret = r.readLine();
174 r.close();
175 return ret;
178 catch (IOException e)
181 return null;
182 default:
183 return null;
188 * Sets the location for each event created by this factory.
190 public abstract void setLocation(Location location);
193 * Create an attribute event.
195 public abstract Attribute createAttribute(String prefix, String namespaceURI,
196 String localName, String value);
199 * Create an attribute event.
201 public abstract Attribute createAttribute(String localName, String value);
204 * Create an attribute event.
206 public abstract Attribute createAttribute(QName name, String value);
209 * Create a namespace declaration event.
211 public abstract Namespace createNamespace(String namespaceURI);
214 * Create a namespace declaration event.
216 public abstract Namespace createNamespace(String prefix, String namespaceUri);
219 * Create a start-element event.
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 public abstract StartElement createStartElement(String prefix,
236 String namespaceUri,
237 String localName,
238 Iterator attributes,
239 Iterator namespaces);
242 * Create a start-element event.
244 public abstract StartElement createStartElement(String prefix,
245 String namespaceUri,
246 String localName,
247 Iterator attributes,
248 Iterator namespaces,
249 NamespaceContext context);
252 * Create an end-element event.
254 public abstract EndElement createEndElement(QName name,
255 Iterator namespaces);
258 * Create an end-element event.
260 public abstract EndElement createEndElement(String prefix,
261 String namespaceUri,
262 String localName);
265 * Create an end-element event.
267 public abstract EndElement createEndElement(String prefix,
268 String namespaceUri,
269 String localName,
270 Iterator namespaces);
273 * Create a text event.
275 public abstract Characters createCharacters(String content);
278 * Create a text event of type CDATA section.
280 public abstract Characters createCData(String content);
283 * Create a text event of type whitespace.
285 public abstract Characters createSpace(String content);
288 * Create a text event of type ignorable whitespace.
290 public abstract Characters createIgnorableSpace(String content);
293 * Create a start-document event.
295 public abstract StartDocument createStartDocument();
298 * Create a start-document event.
300 public abstract StartDocument createStartDocument(String encoding,
301 String version,
302 boolean standalone);
305 * Create a start-document event.
307 public abstract StartDocument createStartDocument(String encoding,
308 String version);
311 * Create a start-document event.
313 public abstract StartDocument createStartDocument(String encoding);
316 * Create an end-document event.
318 public abstract EndDocument createEndDocument();
321 * Create an entity reference event.
323 //public abstract EntityReference createEntityReference(String name,
324 // EntityDeclaration declaration);
325 public abstract EntityReference createEntityReference(String name,
326 String replacementText);
329 * Create a comment event.
331 public abstract Comment createComment(String text);
334 * Create a processing instruction event.
336 public abstract ProcessingInstruction createProcessingInstruction(String target,
337 String data);
340 * Create a DOCTYPE declaration event.
342 public abstract DTD createDTD(String dtd);