2 * Copyright (c) 2002-2004 World Wide Web Consortium,
3 * (Massachusetts Institute of Technology, Institut National de
4 * Recherche en Informatique et en Automatique, Keio University). All
5 * Rights Reserved. This program is distributed under the W3C's Software
6 * Intellectual Property License. This program is distributed in the
7 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even
8 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * See W3C License http://www.w3.org/Consortium/Legal/ for more details.
13 package org.w3c.domts;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
19 import org.w3c.dom.DOMImplementation;
20 import org.w3c.dom.Document;
23 * This class implements the generic parser builder
24 * for JTidy (http://sf.net/projects/JTidy) which reads HTML
25 * and supports the fundamental DOM interfaces but not either HTML L1 DOM
28 public class JTidyDocumentBuilderFactory
29 extends DOMTestDocumentBuilderFactory {
31 private final Constructor tidyConstructor;
32 private final Method parseDOMMethod;
33 private final DOMImplementation domImpl;
34 private static final Class[] NO_CLASSES = new Class[0];
35 private static final Object[] NO_OBJECTS = new Object[0];
38 * Creates a implementation of DOMTestDocumentBuilderFactory
39 * using JTidy's HTML parser and DOM implementation
40 * @param settings array of settings, may be null.
42 public JTidyDocumentBuilderFactory(
43 DocumentBuilderSetting[] settings) throws DOMTestIncompatibleException {
47 ClassLoader classLoader = ClassLoader.getSystemClassLoader();
48 Class tidyClass = classLoader.loadClass("org.w3c.tidy.Tidy");
51 tidyClass.getConstructor(NO_CLASSES);
54 tidyClass.getMethod("parseDOM",
55 new Class[] {java.io.InputStream.class,
56 java.io.OutputStream.class});
59 // JTidy doesn't implement DOMImplementation so
61 domImpl = new JTidyDOMImplementation();
64 catch (Exception ex) {
65 throw new DOMTestIncompatibleException(ex, null);
69 // apply settings to selected document builder
70 // may throw exception if incompatible
71 if (settings != null) {
72 for (int i = 0; i < settings.length; i++) {
73 // settings[i].applySetting(factory);
78 public DOMTestDocumentBuilderFactory newInstance(DocumentBuilderSetting[]
79 newSettings) throws DOMTestIncompatibleException {
80 if (newSettings == null) {
83 DocumentBuilderSetting[] mergedSettings = mergeSettings(newSettings);
84 return new JTidyDocumentBuilderFactory(mergedSettings);
87 public Document load(java.net.URL url) throws DOMTestLoadException {
90 java.io.InputStream stream = url.openStream();
91 Object tidyObj = tidyConstructor.newInstance(new Object[0]);
92 doc = (Document) parseDOMMethod.invoke(tidyObj,
93 new Object[] {stream, null});
95 catch (InvocationTargetException ex) {
96 throw new DOMTestLoadException(ex.getTargetException());
98 catch (Exception ex) {
99 throw new DOMTestLoadException(ex);
104 public DOMImplementation getDOMImplementation() {
108 public boolean hasFeature(String feature, String version) {
109 return domImpl.hasFeature(feature, version);
112 public String getContentType() {
116 public boolean isCoalescing() {
120 public boolean isExpandEntityReferences() {
124 public boolean isIgnoringElementContentWhitespace() {
128 public boolean isNamespaceAware() {
132 public boolean isValidating() {