/** * (The MIT License) * * Copyright (c) 2008 - 2011: * * * {Aaron Patterson}[http://tenderlovemaking.com] * * {Mike Dalessio}[http://mike.daless.io] * * {Charles Nutter}[http://blog.headius.com] * * {Sergio Arbeo}[http://www.serabe.com] * * {Patrick Mahoney}[http://polycrystal.org] * * {Yoko Harada}[http://yokolet.blogspot.com] * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * 'Software'), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package nokogiri; import static nokogiri.internals.NokogiriHelpers.getCachedNodeOrCreate; import static nokogiri.internals.NokogiriHelpers.getLocalNameForNamespace; import static nokogiri.internals.NokogiriHelpers.getNokogiriClass; import static nokogiri.internals.NokogiriHelpers.isNamespace; import static nokogiri.internals.NokogiriHelpers.rubyStringToString; import static nokogiri.internals.NokogiriHelpers.stringOrNil; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import nokogiri.internals.NokogiriHelpers; import nokogiri.internals.NokogiriNamespaceCache; import nokogiri.internals.SaveContextVisitor; import nokogiri.internals.XmlDomParserContext; import org.jruby.Ruby; import org.jruby.RubyClass; import org.jruby.RubyFixnum; import org.jruby.RubyNil; import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; import org.jruby.javasupport.JavaUtil; import org.jruby.javasupport.util.RuntimeHelpers; import org.jruby.runtime.Arity; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * Class for Nokogiri::XML::Document * * @author sergio * @author Yoko Harada */ @JRubyClass(name="Nokogiri::XML::Document", parent="Nokogiri::XML::Node") public class XmlDocument extends XmlNode { private NokogiriNamespaceCache nsCache; /* UserData keys for storing extra info in the document node. */ public final static String DTD_RAW_DOCUMENT = "DTD_RAW_DOCUMENT"; public final static String DTD_INTERNAL_SUBSET = "DTD_INTERNAL_SUBSET"; public final static String DTD_EXTERNAL_SUBSET = "DTD_EXTERNAL_SUBSET"; /* DocumentBuilderFactory implementation class name. This needs to set a classloader into it. * Setting an appropriate classloader resolves issue 380. */ private static final String DOCUMENTBUILDERFACTORY_IMPLE_NAME = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"; private static boolean substituteEntities = false; private static boolean loadExternalSubset = false; // TODO: Verify this. /** cache variables */ protected IRubyObject encoding = null; protected IRubyObject url = null; public XmlDocument(Ruby ruby, RubyClass klazz) { super(ruby, klazz, createNewDocument()); } public XmlDocument(Ruby ruby, Document document) { this(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Document"), document); } public XmlDocument(Ruby ruby, RubyClass klass, Document document) { super(ruby, klass, document); nsCache = new NokogiriNamespaceCache(); createAndCacheNamespaces(ruby, document.getDocumentElement()); stabilizeTextContent(document); setInstanceVariable("@decorators", ruby.getNil()); } @Override public void setNode(ThreadContext context, Node node) { super.setNode(context, node); if (nsCache == null) nsCache = new NokogiriNamespaceCache(); Ruby runtime = context.getRuntime(); if (node != null) { Document document = (Document)node; stabilizeTextContent(document); createAndCacheNamespaces(runtime, document.getDocumentElement()); } setInstanceVariable("@decorators", runtime.getNil()); } public void setEncoding(IRubyObject encoding) { this.encoding = encoding; } // not sure, but like attribute values, text value will be lost // unless it is referred once before this document is used. // this seems to happen only when the fragment is parsed from Node#in_context. private void stabilizeTextContent(Document document) { if (document.getDocumentElement() != null) document.getDocumentElement().getTextContent(); } private void createAndCacheNamespaces(Ruby ruby, Node node) { if (node == null) return; if (node.hasAttributes()) { NamedNodeMap nodeMap = node.getAttributes(); for (int i=0; i