OSDN Git Service

* external/w3c_dom/Makefile.am: New file.
[pf3gnuchains/gcc-fork.git] / libjava / gnu / xml / dom / JAXPFactory.java
1 /* JAXPFactory.java -- 
2    Copyright (C) 2001 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 gnu.xml.dom;
39
40 import java.io.IOException;
41
42 import org.w3c.dom.Document;
43 import org.w3c.dom.DocumentType;
44 import org.w3c.dom.DOMImplementation;
45
46 import org.xml.sax.EntityResolver;
47 import org.xml.sax.ErrorHandler;
48 import org.xml.sax.InputSource;
49 import org.xml.sax.XMLReader;
50 import org.xml.sax.SAXException;
51 import org.xml.sax.SAXParseException;
52
53 import javax.xml.parsers.DocumentBuilder;
54 import javax.xml.parsers.DocumentBuilderFactory;
55 import javax.xml.parsers.ParserConfigurationException;
56 import javax.xml.parsers.SAXParserFactory;
57
58
59 /**
60  * DOM bootstrapping API, for use with JAXP.
61  *
62  * @see Consumer
63  *
64  * @author David Brownell
65  */
66 public final class JAXPFactory
67   extends DocumentBuilderFactory
68 {
69   
70   private static final String   PROPERTY = "http://xml.org/sax/properties/";
71   private static final String   FEATURE = "http://xml.org/sax/features/";
72   
73   private SAXParserFactory      pf;
74   
75   /**
76    * Default constructor.
77    */
78   public JAXPFactory()
79   {
80   }
81   
82   /**
83    * Constructs a JAXP document builder which uses the default
84    * JAXP SAX2 parser and the DOM implementation in this package.
85    */
86   public DocumentBuilder newDocumentBuilder()
87     throws ParserConfigurationException
88   {
89     if (pf == null)
90       {
91         // Force use of AElfred2 since not all JAXP parsers
92         // conform very well to the SAX2 API spec ...
93         pf = new gnu.xml.aelfred2.JAXPFactory();
94         // pf = SAXParserFactory.newInstance ();
95       }
96     
97     // JAXP default: false
98     pf.setValidating(isValidating());
99
100     // FIXME:  this namespace setup may cause errors in some
101     // conformant SAX2 parsers, which we CAN patch up by
102     // splicing a "NSFilter" stage up front ...
103     
104     // JAXP default: false
105     pf.setNamespaceAware(isNamespaceAware());
106
107     try
108       {
109         // undo rude "namespace-prefixes=false" default
110         pf.setFeature(FEATURE + "namespace-prefixes", true);
111
112         return new JAXPBuilder(pf.newSAXParser().getXMLReader(), this);
113       }
114     catch (SAXException e)
115       {
116         String msg = "can't create JAXP DocumentBuilder: " + e.getMessage();
117         throw new ParserConfigurationException(msg);
118       }
119   }
120   
121   /** There seems to be no useful specification for attribute names */
122   public void setAttribute(String name, Object value)
123     throws IllegalArgumentException
124   {
125     if ("http://java.sun.com/xml/jaxp/properties/schemaLanguage".equals(name))
126       {
127         // TODO
128       }
129     else
130       {
131         throw new IllegalArgumentException(name);
132       }
133   }
134
135   /** There seems to be no useful specification for attribute names */
136   public Object getAttribute(String name)
137     throws IllegalArgumentException
138   {
139     throw new IllegalArgumentException(name);
140   }
141
142   static final class JAXPBuilder
143     extends DocumentBuilder
144     implements ErrorHandler
145   {
146
147     private Consumer    consumer;
148     private XMLReader   producer;
149     private DomImpl             impl;
150     
151     JAXPBuilder(XMLReader parser, JAXPFactory factory)
152       throws ParserConfigurationException
153     {
154       impl = new DomImpl();
155       
156       // set up consumer side
157       try
158         {
159           consumer = new Consumer();
160         }
161       catch (SAXException e)
162         {
163           throw new ParserConfigurationException(e.getMessage());
164         }
165
166       // JAXP defaults: true, noise nodes are good (bleech)
167       consumer.setHidingReferences(factory.isExpandEntityReferences());
168       consumer.setHidingComments(factory.isIgnoringComments());
169       consumer.setHidingWhitespace(factory.isIgnoringElementContentWhitespace());
170       consumer.setHidingCDATA(factory.isCoalescing());
171
172       // set up producer side
173       producer = parser;
174       producer.setContentHandler(consumer.getContentHandler());
175       producer.setDTDHandler(consumer.getDTDHandler());
176
177       try
178         {
179           String        id;
180           
181           // if validating, report validity errors, and default
182           // to treating them as fatal
183           if (factory.isValidating ())
184             {
185               producer.setFeature(FEATURE + "validation", true);
186               producer.setErrorHandler(this);
187             }
188           
189           // always save prefix info, maybe do namespace processing
190           producer.setFeature(FEATURE + "namespace-prefixes", true);
191           producer.setFeature(FEATURE + "namespaces",
192                               factory.isNamespaceAware());
193
194           // set important handlers
195           id = PROPERTY + "lexical-handler";
196           producer.setProperty(id, consumer.getProperty(id));
197
198           id = PROPERTY + "declaration-handler";
199           producer.setProperty(id, consumer.getProperty(id));
200           
201         }
202       catch (SAXException e)
203         {
204           throw new ParserConfigurationException(e.getMessage());
205         }
206     }
207     
208     public Document parse(InputSource source) 
209       throws SAXException, IOException
210     {
211       producer.parse(source);
212       Document doc = consumer.getDocument();
213       // TODO inputEncoding
214       doc.setDocumentURI(source.getSystemId());
215       return doc;
216     }
217
218     public boolean isNamespaceAware()
219     {
220       try
221         {
222           return producer.getFeature(FEATURE + "namespaces");
223         }
224       catch (SAXException e)
225         {
226           // "can't happen"
227           throw new RuntimeException(e.getMessage());
228         }
229     }
230
231     public boolean isValidating()
232     {
233       try
234         {
235           return producer.getFeature(FEATURE + "validation");
236         }
237       catch (SAXException e)
238         {
239           // "can't happen"
240           throw new RuntimeException(e.getMessage());
241         }
242     }
243
244     public void setEntityResolver(EntityResolver resolver)
245     {
246       producer.setEntityResolver(resolver);
247     }
248
249     public void setErrorHandler(ErrorHandler handler)
250     {
251       producer.setErrorHandler(handler);
252       consumer.setErrorHandler(handler);
253     }
254
255     public DOMImplementation getDOMImplementation()
256     {
257       return impl;
258     }
259
260     public Document newDocument()
261     {
262       return new DomDocument();
263     }
264         
265     // implementation of error handler that's used when validating
266     public void fatalError(SAXParseException e)
267       throws SAXException
268     {
269       throw e;
270     }
271     
272     public void error(SAXParseException e)
273       throws SAXException
274     {
275       throw e;
276     }
277     
278     public void warning(SAXParseException e)
279       throws SAXException
280     {
281       /* ignore */
282     }
283  
284   }
285
286 }
287