OSDN Git Service

libjava/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / xml / stream / XMLEventFactory.java
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   @SuppressWarnings("unchecked")
221   public abstract StartElement createStartElement(QName name,
222                                                   Iterator attributes,
223                                                   Iterator namespaces);
224
225   /**
226    * Create a start-element event.
227    */
228   public abstract StartElement createStartElement(String prefix,
229                                                   String namespaceUri,
230                                                   String localName);
231
232   /**
233    * Create a start-element event.
234    */
235   @SuppressWarnings("unchecked")
236   public abstract StartElement createStartElement(String prefix,
237                                                   String namespaceUri,
238                                                   String localName,
239                                                   Iterator attributes,
240                                                   Iterator namespaces);
241
242   /**
243    * Create a start-element event.
244    */
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);
252   
253   /**
254    * Create an end-element event.
255    */
256   @SuppressWarnings("unchecked")
257   public abstract EndElement createEndElement(QName name,
258                                               Iterator namespaces);
259
260   /**
261    * Create an end-element event.
262    */
263   public abstract EndElement createEndElement(String prefix,
264                                               String namespaceUri,
265                                               String localName);
266
267   /**
268    * Create an end-element event.
269    */
270   @SuppressWarnings("unchecked")
271   public abstract EndElement createEndElement(String prefix,
272                                               String namespaceUri,
273                                               String localName,
274                                               Iterator namespaces);
275
276   /**
277    * Create a text event.
278    */
279   public abstract Characters createCharacters(String content);
280
281   /**
282    * Create a text event of type CDATA section.
283    */
284   public abstract Characters createCData(String content);
285
286   /**
287    * Create a text event of type whitespace.
288    */
289   public abstract Characters createSpace(String content);
290
291   /**
292    * Create a text event of type ignorable whitespace.
293    */
294   public abstract Characters createIgnorableSpace(String content);
295
296   /**
297    * Create a start-document event.
298    */
299   public abstract StartDocument createStartDocument();
300
301   /**
302    * Create a start-document event.
303    */
304   public abstract StartDocument createStartDocument(String encoding,
305                                                     String version,
306                                                     boolean standalone);
307
308   /**
309    * Create a start-document event.
310    */
311   public abstract StartDocument createStartDocument(String encoding,
312                                                     String version);
313
314   /**
315    * Create a start-document event.
316    */
317   public abstract StartDocument createStartDocument(String encoding);
318
319   /**
320    * Create an end-document event.
321    */
322   public abstract EndDocument createEndDocument();
323
324   /**
325    * Create an entity reference event.
326    */
327   public abstract EntityReference createEntityReference(String name,
328                                                         EntityDeclaration declaration);
329
330   /**
331    * Create a comment event.
332    */
333   public abstract Comment createComment(String text);
334
335   /**
336    * Create a processing instruction event.
337    */
338   public abstract ProcessingInstruction createProcessingInstruction(String target,
339                                                                     String data);
340
341   /**
342    * Create a DOCTYPE declaration event.
343    */
344   public abstract DTD createDTD(String dtd);
345   
346 }
347