1 /* GnomeDocumentBuilder.java -
2 Copyright (C) 2004 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)
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
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
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
.libxmlj
.dom
;
40 import java
.io
.InputStream
;
41 import java
.io
.IOException
;
43 import javax
.xml
.parsers
.DocumentBuilder
;
45 import org
.w3c
.dom
.Document
;
46 import org
.w3c
.dom
.DocumentType
;
47 import org
.w3c
.dom
.DOMImplementation
;
48 import org
.xml
.sax
.EntityResolver
;
49 import org
.xml
.sax
.ErrorHandler
;
50 import org
.xml
.sax
.InputSource
;
51 import org
.xml
.sax
.Locator
;
52 import org
.xml
.sax
.SAXException
;
53 import org
.xml
.sax
.SAXParseException
;
55 import gnu
.xml
.libxmlj
.util
.NamedInputStream
;
56 import gnu
.xml
.libxmlj
.util
.StandaloneDocumentType
;
57 import gnu
.xml
.libxmlj
.util
.StandaloneLocator
;
58 import gnu
.xml
.libxmlj
.util
.XMLJ
;
61 * A JAXP DOM implementation that uses Gnome libxml2 as the underlying
62 * parser and node representation.
64 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
66 public class GnomeDocumentBuilder
67 extends DocumentBuilder
68 implements DOMImplementation
76 // -- DocumentBuilder --
78 private boolean validate
;
79 private boolean coalesce
;
80 private boolean expandEntities
;
81 private EntityResolver entityResolver
;
82 private ErrorHandler errorHandler
;
83 private boolean seenFatalError
;
86 * Constructs a new validating document builder.
88 public GnomeDocumentBuilder()
90 this(true, false, false);
94 * Constructs a new document builder.
95 * @param validate whether to validate during parsing
96 * @param coalesce whether to merge CDATA as text nodes
97 * @param expandEntities whether to expand entity references
99 public GnomeDocumentBuilder(boolean validate
,
101 boolean expandEntities
)
103 this.validate
= validate
;
104 this.coalesce
= coalesce
;
105 this.expandEntities
= expandEntities
;
108 public DOMImplementation
getDOMImplementation()
113 public boolean isNamespaceAware()
118 public boolean isValidating()
123 public Document
newDocument()
125 return createDocument(null, null, null);
128 public Document
parse(InputSource input
)
129 throws SAXException
, IOException
131 NamedInputStream in
= XMLJ
.getInputStream(input
);
132 byte[] detectBuffer
= in
.getDetectBuffer();
133 String publicId
= input
.getPublicId();
134 String systemId
= input
.getSystemId();
135 String base
= XMLJ
.getBaseURI(systemId
);
136 // Handle zero-length document
137 if (detectBuffer
== null)
139 throw new SAXParseException("No document element", publicId
,
142 seenFatalError
= false;
143 return parseStream(in
,
151 true, //entityResolver != null,
152 errorHandler
!= null);
155 private native Document
parseStream(InputStream in
,
162 boolean expandEntities
,
163 boolean entityResolver
,
164 boolean errorHandler
);
166 public void setEntityResolver(EntityResolver resolver
)
168 entityResolver
= resolver
;
171 public void setErrorHandler(ErrorHandler handler
)
173 errorHandler
= handler
;
176 // -- DOMImplementation --
178 public boolean hasFeature(String name
, String version
)
180 if (name
.length() == 0)
184 name
= name
.toLowerCase();
185 if (name
.charAt(0) == '+')
187 name
= name
.substring(1);
190 if ("xml".equals(name
) || "core".equals(name
))
192 return (version
== null ||
193 "".equals(version
) ||
194 "1.0".equals(version
) ||
195 "2.0".equals(version
) ||
196 "3.0".equals(version
));
199 else if ("ls".equals(name
) || "ls-async".equals(name
))
203 return (version == null ||
204 "".equals(version) ||
205 "3.0".equals(version));
209 else if ("traversal".equals(name
))
211 return (version
== null ||
212 "".equals(version
) ||
213 "2.0".equals(version
));
215 else if ("xpath".equals(name
))
217 return (version
== null ||
218 "".equals(version
) ||
219 "3.0".equals(version
));
226 public Object
getFeature(String feature
, String version
)
228 if (hasFeature(feature
, version
))
235 public native Document
createDocument(String namespaceURI
,
236 String qualifiedName
,
237 DocumentType doctype
);
239 public DocumentType
createDocumentType(String qualifiedName
,
243 return new StandaloneDocumentType(qualifiedName
, publicId
, systemId
);
246 // Callback hooks from JNI
248 private void setDocumentLocator(Object ctx
, Object loc
)
253 private InputStream
resolveEntity(String publicId
, String systemId
,
255 throws SAXException
, IOException
257 String url
= XMLJ
.getAbsoluteURI(base
, systemId
);
258 InputStream in
= null;
259 if (entityResolver
!= null)
261 InputSource source
= entityResolver
.resolveEntity(publicId
, url
);
264 in
= XMLJ
.getInputStream(source
);
269 in
= XMLJ
.getInputStream(new URL(url
));
274 private void warning(String message
,
281 if (!seenFatalError
&& errorHandler
!= null)
283 Locator l
= new StandaloneLocator(lineNumber
,
287 errorHandler
.warning(new SAXParseException(message
, l
));
291 private void error(String message
,
298 if (!seenFatalError
&& errorHandler
!= null)
300 Locator l
= new StandaloneLocator(lineNumber
,
304 errorHandler
.error(new SAXParseException(message
, l
));
308 private void fatalError(String message
,
315 if (!seenFatalError
&& errorHandler
!= null)
317 seenFatalError
= true;
318 Locator l
= new StandaloneLocator(lineNumber
,
322 errorHandler
.fatalError(new SAXParseException(message
, l
));