Search in sources :

Example 1 with XmlDocument

use of nokogiri.XmlDocument in project nokogiri by sparklemotion.

the class XmlDomParserContext method parse.

/**
     * Must call setInputSource() before this method.
     */
public XmlDocument parse(ThreadContext context, IRubyObject klazz, IRubyObject url) {
    XmlDocument xmlDoc;
    try {
        Document doc = do_parse();
        xmlDoc = wrapDocument(context, (RubyClass) klazz, doc);
        xmlDoc.setUrl(url);
        addErrorsIfNecessary(context, xmlDoc);
        return xmlDoc;
    } catch (SAXException e) {
        return getDocumentWithErrorsOrRaiseException(context, (RubyClass) klazz, e);
    } catch (IOException e) {
        return getDocumentWithErrorsOrRaiseException(context, (RubyClass) klazz, e);
    }
}
Also used : XmlDocument(nokogiri.XmlDocument) RubyClass(org.jruby.RubyClass) IOException(java.io.IOException) Document(org.w3c.dom.Document) XmlDocument(nokogiri.XmlDocument) SAXException(org.xml.sax.SAXException)

Example 2 with XmlDocument

use of nokogiri.XmlDocument in project gocd by gocd.

the class XmlDomParserContext method getDocumentWithErrorsOrRaiseException.

public XmlDocument getDocumentWithErrorsOrRaiseException(ThreadContext context, RubyClass klazz, Exception ex) {
    if (options.recover) {
        XmlDocument xmlDocument = getInterruptedOrNewXmlDocument(context, klazz);
        this.addErrorsIfNecessary(context, xmlDocument);
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        xmlSyntaxError.setException(ex);
        ((RubyArray) xmlDocument.getInstanceVariable("@errors")).append(xmlSyntaxError);
        return xmlDocument;
    } else {
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        xmlSyntaxError.setException(ex);
        throw new RaiseException(xmlSyntaxError);
    }
}
Also used : RubyArray(org.jruby.RubyArray) XmlSyntaxError(nokogiri.XmlSyntaxError) RaiseException(org.jruby.exceptions.RaiseException) XmlDocument(nokogiri.XmlDocument)

Example 3 with XmlDocument

use of nokogiri.XmlDocument in project gocd by gocd.

the class ReaderNode method getAttributesNodes.

public IRubyObject getAttributesNodes() {
    RubyArray array = RubyArray.newArray(ruby);
    if (attributeList != null && attributeList.length > 0) {
        if (document == null) {
            XmlDocument doc = (XmlDocument) XmlDocument.rbNew(ruby.getCurrentContext(), getNokogiriClass(ruby, "Nokogiri::XML::Document"), new IRubyObject[0]);
            document = doc.getDocument();
        }
        for (int i = 0; i < attributeList.length; i++) {
            if (!isNamespace(attributeList.names.get(i))) {
                Attr attr = document.createAttributeNS(attributeList.namespaces.get(i), attributeList.names.get(i));
                attr.setValue(attributeList.values.get(i));
                XmlAttr xmlAttr = (XmlAttr) NokogiriService.XML_ATTR_ALLOCATOR.allocate(ruby, getNokogiriClass(ruby, "Nokogiri::XML::Attr"));
                xmlAttr.setNode(ruby.getCurrentContext(), attr);
                array.append(xmlAttr);
            }
        }
    }
    return array;
}
Also used : RubyArray(org.jruby.RubyArray) XmlDocument(nokogiri.XmlDocument) IRubyObject(org.jruby.runtime.builtin.IRubyObject) XmlAttr(nokogiri.XmlAttr) Attr(org.w3c.dom.Attr) XmlAttr(nokogiri.XmlAttr)

Example 4 with XmlDocument

use of nokogiri.XmlDocument in project gocd by gocd.

the class NokogiriHelpers method getCachedNodeOrCreate.

/**
 * Get the XmlNode associated with the underlying
 * <code>node</code>. Creates a new XmlNode (or appropriate subclass)
 * or XmlNamespace wrapping <code>node</code> if there is no cached
 * value.
 */
public static IRubyObject getCachedNodeOrCreate(Ruby ruby, Node node) {
    if (node == null)
        return ruby.getNil();
    if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
        XmlDocument xmlDocument = (XmlDocument) node.getOwnerDocument().getUserData(CACHED_NODE);
        if (!(xmlDocument instanceof HtmlDocument)) {
            String prefix = getLocalNameForNamespace(((Attr) node).getName());
            prefix = prefix != null ? prefix : "";
            String href = ((Attr) node).getValue();
            XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
            if (xmlNamespace != null)
                return xmlNamespace;
            else
                return XmlNamespace.createFromAttr(ruby, (Attr) node);
        }
    }
    XmlNode xmlNode = getCachedNode(node);
    if (xmlNode == null) {
        xmlNode = (XmlNode) constructNode(ruby, node);
        node.setUserData(CACHED_NODE, xmlNode, null);
    }
    return xmlNode;
}
Also used : XmlNode(nokogiri.XmlNode) HtmlDocument(nokogiri.HtmlDocument) XmlNamespace(nokogiri.XmlNamespace) XmlDocument(nokogiri.XmlDocument) RubyString(org.jruby.RubyString) Attr(org.w3c.dom.Attr) XmlAttr(nokogiri.XmlAttr)

Example 5 with XmlDocument

use of nokogiri.XmlDocument in project gocd by gocd.

the class XmlDomParserContext method wrapDocument.

/**
 * This method is broken out so that HtmlDomParserContext can
 * override it.
 */
protected XmlDocument wrapDocument(ThreadContext context, RubyClass klazz, Document doc) {
    XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), klazz);
    xmlDocument.setDocumentNode(context, doc);
    xmlDocument.setEncoding(ruby_encoding);
    if (options.dtdLoad) {
        IRubyObject xmlDtdOrNil = XmlDtd.newFromExternalSubset(context.getRuntime(), doc);
        if (!xmlDtdOrNil.isNil()) {
            XmlDtd xmlDtd = (XmlDtd) xmlDtdOrNil;
            doc.setUserData(XmlDocument.DTD_EXTERNAL_SUBSET, xmlDtd, null);
        }
    }
    return xmlDocument;
}
Also used : XmlDtd(nokogiri.XmlDtd) XmlDocument(nokogiri.XmlDocument) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Aggregations

XmlDocument (nokogiri.XmlDocument)15 XmlAttr (nokogiri.XmlAttr)5 Document (org.w3c.dom.Document)5 XmlNode (nokogiri.XmlNode)4 IOException (java.io.IOException)3 XmlDtd (nokogiri.XmlDtd)3 XmlSyntaxError (nokogiri.XmlSyntaxError)3 IRubyObject (org.jruby.runtime.builtin.IRubyObject)3 Attr (org.w3c.dom.Attr)3 SAXException (org.xml.sax.SAXException)3 XmlCdata (nokogiri.XmlCdata)2 XmlComment (nokogiri.XmlComment)2 XmlElement (nokogiri.XmlElement)2 XmlEntityReference (nokogiri.XmlEntityReference)2 XmlNamespace (nokogiri.XmlNamespace)2 XmlProcessingInstruction (nokogiri.XmlProcessingInstruction)2 XmlText (nokogiri.XmlText)2 RubyArray (org.jruby.RubyArray)2 RubyClass (org.jruby.RubyClass)2 RubyString (org.jruby.RubyString)2