Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / xml / dom / DomDocumentBuilder.java
blob343f48c13fdfd13be2d33adfec9ca46acf68d25a
1 /* DomDocumentBuilder.java --
2 Copyright (C) 2004,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 gnu.xml.dom;
40 import java.io.File;
41 import java.io.InputStream;
42 import java.io.IOException;
43 import java.io.Reader;
44 import java.net.MalformedURLException;
45 import java.net.URL;
46 import javax.xml.parsers.DocumentBuilder;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.DOMConfiguration;
49 import org.w3c.dom.DOMImplementation;
50 import org.w3c.dom.ls.DOMImplementationLS;
51 import org.w3c.dom.ls.LSInput;
52 import org.w3c.dom.ls.LSParser;
53 import org.xml.sax.EntityResolver;
54 import org.xml.sax.ErrorHandler;
55 import org.xml.sax.InputSource;
56 import org.xml.sax.SAXException;
58 /**
59 * Document builder using the GNU DOM Load & Save implementation.
61 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
63 class DomDocumentBuilder
64 extends DocumentBuilder
67 final DOMImplementation impl;
68 final DOMImplementationLS ls;
69 final LSParser parser;
71 DomDocumentBuilder(DOMImplementation impl,
72 DOMImplementationLS ls,
73 LSParser parser)
75 this.impl = impl;
76 this.ls = ls;
77 this.parser = parser;
80 public boolean isNamespaceAware()
82 DOMConfiguration config = parser.getDomConfig();
83 return ((Boolean) config.getParameter("namespaces")).booleanValue();
86 public boolean isValidating()
88 DOMConfiguration config = parser.getDomConfig();
89 return ((Boolean) config.getParameter("validating")).booleanValue();
92 public boolean isXIncludeAware()
94 DOMConfiguration config = parser.getDomConfig();
95 return ((Boolean) config.getParameter("xinclude-aware")).booleanValue();
98 public void setEntityResolver(EntityResolver resolver)
100 DOMConfiguration config = parser.getDomConfig();
101 config.setParameter("entity-resolver", resolver);
104 public void setErrorHandler(ErrorHandler handler)
106 DOMConfiguration config = parser.getDomConfig();
107 config.setParameter("error-handler", handler);
110 public DOMImplementation getDOMImplementation()
112 return impl;
115 public Document newDocument()
117 return impl.createDocument(null, null, null);
120 public Document parse(InputStream in)
121 throws SAXException, IOException
123 LSInput input = ls.createLSInput();
124 input.setByteStream(in);
125 return parser.parse(input);
128 public Document parse(InputStream in, String systemId)
129 throws SAXException, IOException
131 LSInput input = ls.createLSInput();
132 input.setByteStream(in);
133 input.setSystemId(systemId);
134 return parser.parse(input);
137 public Document parse(String systemId)
138 throws SAXException, IOException
140 return parser.parseURI(systemId);
143 public Document parse(InputSource is)
144 throws SAXException, IOException
146 LSInput input = ls.createLSInput();
147 String systemId = is.getSystemId();
148 InputStream in = is.getByteStream();
149 if (in != null)
151 input.setByteStream(in);
153 else
155 Reader reader = is.getCharacterStream();
156 if (reader != null)
158 input.setCharacterStream(reader);
160 else
164 URL url = new URL(systemId);
165 input.setByteStream(url.openStream());
167 catch (MalformedURLException e)
169 // Maybe this is a relative file URL
170 File cwd = new File(System.getProperty("user.dir"));
171 URL url = new URL(cwd.toURL(), systemId);
172 input.setByteStream(url.openStream());
176 input.setPublicId(is.getPublicId());
177 input.setSystemId(systemId);
178 input.setEncoding(is.getEncoding());
179 return parser.parse(input);