File: XmlDomParserContext.java

package info (click to toggle)
ruby-nokogiri 1.13.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,416 kB
  • sloc: ansic: 38,198; xml: 28,086; ruby: 22,271; java: 15,517; cpp: 7,037; yacc: 244; sh: 148; makefile: 136
file content (264 lines) | stat: -rw-r--r-- 8,409 bytes parent folder | download
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package nokogiri.internals;

import nokogiri.XmlDocument;
import nokogiri.XmlDtd;
import nokogiri.XmlSyntaxError;
import org.apache.xerces.parsers.DOMParser;
import org.jruby.*;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static nokogiri.internals.NokogiriHelpers.isBlank;

/**
 * Parser class for XML DOM processing. This class actually parses XML document
 * and creates DOM tree in Java side. However, DOM tree in Ruby side is not since
 * we delay creating objects for performance.
 *
 * @author sergio
 * @author Yoko Harada <yokolet@gmail.com>
 */
public class XmlDomParserContext extends ParserContext
{

  protected static final String FEATURE_LOAD_EXTERNAL_DTD =
    "http://apache.org/xml/features/nonvalidating/load-external-dtd";
  protected static final String FEATURE_LOAD_DTD_GRAMMAR =
    "http://apache.org/xml/features/nonvalidating/load-dtd-grammar";
  protected static final String FEATURE_INCLUDE_IGNORABLE_WHITESPACE =
    "http://apache.org/xml/features/dom/include-ignorable-whitespace";
  protected static final String CONTINUE_AFTER_FATAL_ERROR =
    "http://apache.org/xml/features/continue-after-fatal-error";
  protected static final String FEATURE_NOT_EXPAND_ENTITY =
    "http://apache.org/xml/features/dom/create-entity-ref-nodes";
  protected static final String FEATURE_VALIDATION = "http://xml.org/sax/features/validation";
  private static final String SECURITY_MANAGER = "http://apache.org/xml/properties/security-manager";

  protected ParserContext.Options options;
  protected DOMParser parser;
  protected NokogiriErrorHandler errorHandler;
  protected IRubyObject ruby_encoding;

  public
  XmlDomParserContext(Ruby runtime, IRubyObject options)
  {
    this(runtime, runtime.getNil(), options);
  }

  public
  XmlDomParserContext(Ruby runtime, IRubyObject encoding, IRubyObject options)
  {
    super(runtime);
    this.options = new ParserContext.Options(RubyFixnum.fix2long(options));
    java_encoding = NokogiriHelpers.getValidEncodingOrNull(encoding);
    ruby_encoding = encoding;
    initErrorHandler(runtime);
    initParser(runtime);
  }

  protected void
  initErrorHandler(Ruby runtime)
  {
    if (options.recover) {
      errorHandler = new NokogiriNonStrictErrorHandler(runtime, options.noError, options.noWarning);
    } else {
      errorHandler = new NokogiriStrictErrorHandler(runtime, options.noError, options.noWarning);
    }
  }

  protected void
  initParser(Ruby runtime)
  {
    if (options.xInclude) {
      System.setProperty("org.apache.xerces.xni.parser.XMLParserConfiguration",
                         "org.apache.xerces.parsers.XIncludeParserConfiguration");
    }

    parser = new NokogiriDomParser(options);
    parser.setErrorHandler(errorHandler);

    // Fix for Issue#586.  This limits entity expansion up to 100000 and nodes up to 3000.
    setProperty(SECURITY_MANAGER, new org.apache.xerces.util.SecurityManager());

    if (options.noBlanks) {
      setFeature(FEATURE_INCLUDE_IGNORABLE_WHITESPACE, false);
    }

    if (options.recover) {
      setFeature(CONTINUE_AFTER_FATAL_ERROR, true);
    }

    if (options.dtdValid) {
      setFeature(FEATURE_VALIDATION, true);
    }

    if (!options.noEnt) {
      setFeature(FEATURE_NOT_EXPAND_ENTITY, true);
    }
    // If we turn off loading of external DTDs complete, we don't
    // getthe publicID.  Instead of turning off completely, we use
    // an entity resolver that returns empty documents.
    if (options.dtdLoad) {
      setFeature(FEATURE_LOAD_EXTERNAL_DTD, true);
      setFeature(FEATURE_LOAD_DTD_GRAMMAR, true);
    }
    parser.setEntityResolver(new NokogiriEntityResolver(runtime, errorHandler, options));
  }

  /**
   * Convenience method that catches and ignores SAXException
   * (unrecognized and unsupported exceptions).
   */
  protected void
  setFeature(String feature, boolean value)
  {
    try {
      parser.setFeature(feature, value);
    } catch (SAXException e) {
      // ignore
    }
  }

  /**
   * Convenience method that catches and ignores SAXException
   * (unrecognized and unsupported exceptions).
   */
  protected void
  setProperty(String property, Object value)
  {
    try {
      parser.setProperty(property, value);
    } catch (SAXException e) {
      // ignore
    }
  }

  public void
  addErrorsIfNecessary(ThreadContext context, XmlDocument doc)
  {
    doc.setInstanceVariable("@errors", mapErrors(context, errorHandler));
  }


  public static RubyArray
  mapErrors(ThreadContext context, NokogiriErrorHandler errorHandler)
  {
    final Ruby runtime = context.runtime;
    final List<RubyException> errors = errorHandler.getErrors();
    final IRubyObject[] errorsAry = new IRubyObject[errors.size()];
    for (int i = 0; i < errors.size(); i++) {
      errorsAry[i] = errors.get(i);
    }
    return runtime.newArrayNoCopy(errorsAry);
  }

  public XmlDocument
  getDocumentWithErrorsOrRaiseException(ThreadContext context, RubyClass klazz, Exception ex)
  {
    if (options.recover) {
      XmlDocument xmlDocument = getInterruptedOrNewXmlDocument(context, klazz);
      this.addErrorsIfNecessary(context, xmlDocument);
      XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
      xmlSyntaxError.setException(ex);
      ((RubyArray) xmlDocument.getInstanceVariable("@errors")).append(xmlSyntaxError);
      return xmlDocument;
    } else {
      XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
      xmlSyntaxError.setException(ex);
      throw xmlSyntaxError.toThrowable();
    }
  }

  private XmlDocument
  getInterruptedOrNewXmlDocument(ThreadContext context, RubyClass klass)
  {
    Document document = parser.getDocument();
    XmlDocument xmlDocument = new XmlDocument(context.runtime, klass, document);
    xmlDocument.setEncoding(ruby_encoding);
    return xmlDocument;
  }

  /**
   * This method is broken out so that HtmlDomParserContext can
   * override it.
   */
  protected XmlDocument
  wrapDocument(ThreadContext context, RubyClass klass, Document doc)
  {
    XmlDocument xmlDocument = new XmlDocument(context.runtime, klass, doc);
    Helpers.invoke(context, xmlDocument, "initialize");
    xmlDocument.setEncoding(ruby_encoding);

    if (options.dtdLoad) {
      IRubyObject dtd = XmlDtd.newFromExternalSubset(context.runtime, doc);
      if (!dtd.isNil()) {
        doc.setUserData(XmlDocument.DTD_EXTERNAL_SUBSET, (XmlDtd) dtd, null);
      }
    }
    return xmlDocument;
  }

  /**
   * Must call setInputSource() before this method.
   */
  public XmlDocument
  parse(ThreadContext context, RubyClass klass, IRubyObject url)
  {
    XmlDocument xmlDoc;
    try {
      Document doc = do_parse();
      xmlDoc = wrapDocument(context, klass, doc);
      xmlDoc.setUrl(url);
      addErrorsIfNecessary(context, xmlDoc);
      return xmlDoc;
    } catch (SAXException e) {
      return getDocumentWithErrorsOrRaiseException(context, klass, e);
    } catch (IOException e) {
      return getDocumentWithErrorsOrRaiseException(context, klass, e);
    }
  }

  protected Document
  do_parse() throws SAXException, IOException
  {
    try {
      parser.parse(getInputSource());
    } catch (NullPointerException ex) {
      // FIXME: this is really a hack to fix #838. Xerces will throw a NullPointerException
      // if we tried to parse '<? ?>'. We should submit a patch to Xerces.
    }
    if (options.noBlanks) {
      List<Node> emptyNodes = new ArrayList<Node>();
      findEmptyTexts(parser.getDocument(), emptyNodes);
      if (emptyNodes.size() > 0) {
        for (Node node : emptyNodes) {
          node.getParentNode().removeChild(node);
        }
      }
    }
    return parser.getDocument();
  }

  private static void
  findEmptyTexts(Node node, List<Node> emptyNodes)
  {
    if (node.getNodeType() == Node.TEXT_NODE && isBlank(node.getTextContent())) {
      emptyNodes.add(node);
    } else {
      NodeList children = node.getChildNodes();
      for (int i = 0; i < children.getLength(); i++) {
        findEmptyTexts(children.item(i), emptyNodes);
      }
    }
  }
}