Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 2858dd7cdcb9235f6fc7b86b24773ea7595d9516 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.xml.jaxp;


import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xerces.dom.DOMImplementationImpl;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.impl.Constants;
import org.eclipse.wst.wsi.internal.core.xml.XMLUtils;
import org.eclipse.wst.wsi.internal.core.xml.dom.DOMParser;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Using this class, we can obtain a Document from XML. This class is a 
 * specialization of javax.xml.parsers.DocumentBuilder.
 * 
 * An instance of this class can be obtained from the 
 * DocumentBuilderFactory.newDocumentBuilder method. Once an instance 
 * of this class is obtained, XML can be parsed from a variety of input 
 * sources. These input sources are InputStreams, Files, URLs, and SAX 
 * InputSources.

This class will javax.xml.parsers.DocumentBuilderFactory...
 * 
 * @author Peter  Brittenham (peterbr@us.ibm.com)
 * @version 1.0.1
 */
public class DocumentBuilderImpl extends DocumentBuilder
{
  protected DOMParser domParser = null;

  protected EntityResolver entityResolver = null;
  protected ErrorHandler errorHandler = new DefaultHandler();

  protected static final String NAMESPACES_FEATURE =
    Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
  protected static final String VALIDATION_FEATURE =
    Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;

  /**
   * Constructor for DocumentBuilderImpl2.
   *
   * @param dbFactory  a DocumentBuilderFactory object.
   * @throws SAXException if any parse errors occur. 
   */
  public DocumentBuilderImpl(DocumentBuilderFactory dbFactory, Hashtable attributes)
    throws SAXException
  {
    // Create parser
    domParser = new DOMParser();

    // Set namespace aware
    domParser.setFeature(NAMESPACES_FEATURE, dbFactory.isNamespaceAware());

    // Set validating
    domParser.setFeature(VALIDATION_FEATURE, dbFactory.isValidating());
    if (dbFactory.isValidating()) domParser.setFeature(XMLUtils.FEATURE_VALIDATION_SCHEMA, true);

    // Do not defer node expansion
    domParser.setFeature(
      Constants.XERCES_FEATURE_PREFIX + Constants.DEFER_NODE_EXPANSION_FEATURE,
      false);

    // Set other features from the document factory builder
    domParser.setFeature(
      Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_IGNORABLE_WHITESPACE,
      !dbFactory.isIgnoringElementContentWhitespace());
    domParser.setFeature(
      Constants.XERCES_FEATURE_PREFIX
        + Constants.CREATE_ENTITY_REF_NODES_FEATURE,
      !dbFactory.isExpandEntityReferences());
    domParser.setFeature(
      Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_COMMENTS_FEATURE,
      !dbFactory.isIgnoringComments());
    domParser.setFeature(
      Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_CDATA_NODES_FEATURE,
      !dbFactory.isCoalescing());
   
    // set features and properties specified at factory level
    if (attributes != null)
    {
      for (Enumeration i=attributes.keys(); i.hasMoreElements();)
      {
        String attribute = (String)i.nextElement();
        if (attribute.equals(XMLUtils.JAXP_SCHEMA_SOURCE))
        {
          // for multiple schema validation may be array of InputSource 
          if(attributes.get(attribute) instanceof InputSource[])
            domParser.setProperty(attribute, (InputSource[])attributes.get(attribute));
          else
            domParser.setProperty(attribute, (InputSource)attributes.get(attribute));
        }
        else
        {
          domParser.setProperty(attribute, attributes.get(attribute));
        }
      }
    }
  }

  /**
   * Parse the content of the given input source as an XML document and return a new DOM.
   * @param is  InputStream containing the content to be parsed. 
   * @throws SAXException if any parse errors occur. 
   * @throws IOException if any IO errors occur. 
   */
  public Document parse(InputSource is) throws SAXException, IOException
  {
	ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();   
	try
	{
	  Thread.currentThread().setContextClassLoader(XMLUtils.class.getClassLoader());   
 	
      // Set entity resolver
      if (this.entityResolver != null)
        domParser.setEntityResolver(this.entityResolver);

      // Set error handler    
      if (this.errorHandler != null)
        domParser.setErrorHandler(this.errorHandler);

      // Parse input source
      domParser.parse(is);

      // Return the document that was created
      return domParser.getDocument();
	}
    finally
    { 
      Thread.currentThread().setContextClassLoader(currentLoader);
    }    
  }

  /**
   * Indicates whether or not this parser is configured to understand namespaces.
   * @return true if this parser is configured to understand namespaces; false otherwise.
   */
  public boolean isNamespaceAware()
  {
    boolean namespaceAware = false;

    try
    {
      namespaceAware = domParser.getFeature(NAMESPACES_FEATURE);
    }

    catch (SAXException se)
    {
      throw new RuntimeException(se.toString());
    }

    return namespaceAware;
  }

  /**
   * Indicates whether or not this parser is configured to validate XML documents.
   * @return true if this parser is configured to validate XML documents; false otherwise.
   */
  public boolean isValidating()
  {
    boolean validating = false;

    try
    {
      validating = domParser.getFeature(VALIDATION_FEATURE);
    }

    catch (SAXException se)
    {
      throw new RuntimeException(se.toString());
    }

    return validating;
  }

  /**
   * Specify the EntityResolver to be used to resolve entities present 
   * in the XML document to be parsed. Setting this to null will result 
   * in the underlying implementation using it's own default 
   * implementation and behavior.
   * 
   * @param er  the EntityResolver to be used to resolve entities 
   *            present in the XML document to be parsed.
   */
  public void setEntityResolver(EntityResolver entityResolver)
  {
    this.entityResolver = entityResolver;
  }

  /**
   * Specify the ErrorHandler to be used to report errors present in 
   * the XML document to be parsed. Setting this to null will result 
   * in the underlying implementation using it's own default 
   * implementation and behavior. 
   * 
   * param eh  the ErrorHandler to be used to report errors present in 
   *           the XML document to be parsed.
   */
  public void setErrorHandler(ErrorHandler errorHandler)
  {
    this.errorHandler = errorHandler;
  }

  /**
   * Obtain a new instance of a DOM Document object to build a DOM 
   * tree with. An alternative way to create a DOM Document object 
   * is to use the getDOMImplementation method to get a DOM Level 2 
   * DOMImplementation object and then use DOM Level 2 methods on 
   * that object to create a DOM Document object.
   * 
   * @return a new instance of a DOM Document object.
   */
  public Document newDocument()
  {
    return new DocumentImpl();
  }

  /**
   * Obtain an instance of a DOMImplementation object. 
   * @return a new instance of a DOMImplementation.
   */
  public DOMImplementation getDOMImplementation()
  {
    return new DOMImplementationImpl();
  }

}

Back to the top