1 /*
2  * Copyright (c) 2001-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
9  * PURPOSE.
10  * See W3C License http://www.w3.org/Consortium/Legal/ for more details.
11  */
12 
13 /*
14   $Log: DOM4JTestDocumentBuilderFactory.java,v $
15   Revision 1.2  2004/03/11 01:44:21  dom-ts-4
16   Checkstyle fixes (bug 592)
17 
18   Revision 1.1  2002/02/03 07:47:51  dom-ts-4
19   More missing files
20 
21  */
22 
23 package org.w3c.domts;
24 
25 import java.lang.reflect.Constructor;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 
29 import org.w3c.dom.DOMImplementation;
30 import org.w3c.dom.Document;
31 import org.xml.sax.XMLReader;
32 
33 /**
34  *   This class implements the generic parser and configuation
35  *   abstract class for JAXP supporting parsers.
36  */
37 public class DOM4JTestDocumentBuilderFactory
38     extends DOMTestDocumentBuilderFactory {
39 
40   private final Object domFactory;
41   private final Object saxReader;
42   private final org.xml.sax.XMLReader xmlReader;
43   private org.w3c.dom.DOMImplementation domImpl;
44   private final Method readMethod;
45 
46   /**
47    * Creates a JAXP implementation of DOMTestDocumentBuilderFactory.
48    * @param factory null for default JAXP provider.  If not null,
49    * factory will be mutated in constructor and should be released
50    * by calling code upon return.
51    * @param XMLReader if null use default XMLReader.  If provided,
52    * it may be mutated and should be released by the caller immediately
53    * after the constructor.
54    * @param settings array of settings, may be null.
55    */
DOM4JTestDocumentBuilderFactory(DocumentBuilderSetting[] settings)56   public DOM4JTestDocumentBuilderFactory(DocumentBuilderSetting[] settings) throws
57       DOMTestIncompatibleException {
58     super(settings);
59     try {
60       //
61       //   The following reflection code is trying to accomplish
62       //
63       //domFactory = org.dom4j.dom.DOMDocumentFactory.getInstance();
64       //domImpl = (DOMImplementation) domFactory;
65       //saxReader = new org.dom4j.io.SAXReader(domFactory);
66       //xmlReader = saxReader.getXMLReader();
67 
68       ClassLoader classLoader = ClassLoader.getSystemClassLoader();
69       Class domFactoryClass = classLoader.loadClass(
70           "org.dom4j.dom.DOMDocumentFactory");
71       Method getInstance = domFactoryClass.getMethod("getInstance", new Class[] {});
72       domFactory = getInstance.invoke(null, new Object[] {});
73       domImpl = (DOMImplementation) domFactory;
74       Class saxReaderClass = classLoader.loadClass("org.dom4j.io.SAXReader");
75       Constructor saxReaderConstructor = saxReaderClass.getConstructor(
76           new Class[] {classLoader.loadClass("org.dom4j.DocumentFactory")});
77       saxReader = saxReaderConstructor.newInstance(new Object[] {domFactory});
78 
79       Method getReaderMethod = saxReaderClass.getMethod("getXMLReader",
80           new Class[] {});
81       xmlReader = (XMLReader) getReaderMethod.invoke(saxReader, new Object[0]);
82 
83       readMethod = saxReaderClass.getMethod("read", new Class[] {java.net.URL.class});
84     }
85     catch (InvocationTargetException ex) {
86       throw new DOMTestIncompatibleException(ex.getTargetException(), null);
87     }
88     catch (Exception ex) {
89       throw new DOMTestIncompatibleException(ex, null);
90     }
91     //
92     //   TODO: Process settings
93     //
94   }
95 
newInstance(DocumentBuilderSetting[] newSettings)96   public DOMTestDocumentBuilderFactory newInstance(DocumentBuilderSetting[]
97       newSettings) throws DOMTestIncompatibleException {
98     if (newSettings == null) {
99       return this;
100     }
101     DocumentBuilderSetting[] mergedSettings = mergeSettings(newSettings);
102     return new DOM4JTestDocumentBuilderFactory(mergedSettings);
103   }
104 
load(java.net.URL url)105   public Document load(java.net.URL url) throws DOMTestLoadException {
106     if (url == null) {
107       throw new NullPointerException("url");
108     }
109     if (saxReader == null) {
110       throw new NullPointerException("saxReader");
111     }
112     try {
113       return (org.w3c.dom.Document) readMethod.invoke(saxReader,
114           new Object[] {url});
115     }
116     catch (InvocationTargetException ex) {
117       ex.getTargetException().printStackTrace();
118       throw new DOMTestLoadException(ex.getTargetException());
119     }
120     catch (Exception ex) {
121       ex.printStackTrace();
122       throw new DOMTestLoadException(ex);
123     }
124   }
125 
getDOMImplementation()126   public DOMImplementation getDOMImplementation() {
127     return domImpl;
128   }
129 
hasFeature(String feature, String version)130   public boolean hasFeature(String feature, String version) {
131     return domImpl.hasFeature(feature, version);
132   }
133 
isCoalescing()134   public boolean isCoalescing() {
135     return false;
136   }
137 
isExpandEntityReferences()138   public boolean isExpandEntityReferences() {
139     return false;
140   }
141 
isIgnoringElementContentWhitespace()142   public boolean isIgnoringElementContentWhitespace() {
143     return false;
144   }
145 
isNamespaceAware()146   public boolean isNamespaceAware() {
147     return true;
148   }
149 
isValidating()150   public boolean isValidating() {
151     return false;
152   }
153 
154 }
155