Search in sources :

Example 11 with XmlDocument

use of nokogiri.XmlDocument in project gocd by gocd.

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 12 with XmlDocument

use of nokogiri.XmlDocument in project nokogiri by sparklemotion.

the class NokogiriHelpers method constructNode.

/**
 * Construct a new XmlNode wrapping <code>node</code>.  The proper
 * subclass of XmlNode is chosen based on the type of
 * <code>node</code>.
 */
public static IRubyObject constructNode(Ruby runtime, Node node) {
    if (node == null) {
        return runtime.getNil();
    }
    // this is slow; need a way to cache nokogiri classes/modules somewhere
    switch(node.getNodeType()) {
        case Node.ELEMENT_NODE:
            XmlElement xmlElement = (XmlElement) NokogiriService.XML_ELEMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Element"));
            xmlElement.setNode(runtime, node);
            return xmlElement;
        case Node.ATTRIBUTE_NODE:
            XmlAttr xmlAttr = (XmlAttr) NokogiriService.XML_ATTR_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Attr"));
            xmlAttr.setNode(runtime, node);
            return xmlAttr;
        case Node.TEXT_NODE:
            XmlText xmlText = (XmlText) NokogiriService.XML_TEXT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Text"));
            xmlText.setNode(runtime, node);
            return xmlText;
        case Node.COMMENT_NODE:
            XmlComment xmlComment = (XmlComment) NokogiriService.XML_COMMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Comment"));
            xmlComment.setNode(runtime, node);
            return xmlComment;
        case Node.ENTITY_NODE:
            return new XmlNode(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityDecl"), node);
        case Node.ENTITY_REFERENCE_NODE:
            XmlEntityReference xmlEntityRef = (XmlEntityReference) NokogiriService.XML_ENTITY_REFERENCE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::EntityReference"));
            xmlEntityRef.setNode(runtime, node);
            return xmlEntityRef;
        case Node.PROCESSING_INSTRUCTION_NODE:
            XmlProcessingInstruction xmlProcessingInstruction = (XmlProcessingInstruction) NokogiriService.XML_PROCESSING_INSTRUCTION_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::ProcessingInstruction"));
            xmlProcessingInstruction.setNode(runtime, node);
            return xmlProcessingInstruction;
        case Node.CDATA_SECTION_NODE:
            XmlCdata xmlCdata = (XmlCdata) NokogiriService.XML_CDATA_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::CDATA"));
            xmlCdata.setNode(runtime, node);
            return xmlCdata;
        case Node.DOCUMENT_NODE:
            XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Document"));
            xmlDocument.setDocumentNode(runtime, (Document) node);
            return xmlDocument;
        case Node.DOCUMENT_TYPE_NODE:
            XmlDtd xmlDtd = (XmlDtd) NokogiriService.XML_DTD_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::DTD"));
            xmlDtd.setNode(runtime, node);
            return xmlDtd;
        default:
            XmlNode xmlNode = (XmlNode) NokogiriService.XML_NODE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Node"));
            xmlNode.setNode(runtime, node);
            return xmlNode;
    }
}
Also used : XmlDtd(nokogiri.XmlDtd) XmlNode(nokogiri.XmlNode) XmlEntityReference(nokogiri.XmlEntityReference) XmlProcessingInstruction(nokogiri.XmlProcessingInstruction) XmlComment(nokogiri.XmlComment) XmlElement(nokogiri.XmlElement) XmlText(nokogiri.XmlText) XmlDocument(nokogiri.XmlDocument) XmlAttr(nokogiri.XmlAttr) XmlCdata(nokogiri.XmlCdata)

Example 13 with XmlDocument

use of nokogiri.XmlDocument in project nokogiri by sparklemotion.

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 runtime, Node node) {
    if (node == null) {
        return runtime.getNil();
    }
    if (node.getNodeType() == Node.ATTRIBUTE_NODE && isNamespace(node.getNodeName())) {
        XmlDocument xmlDocument = (XmlDocument) node.getOwnerDocument().getUserData(CACHED_NODE);
        if (!(xmlDocument instanceof Html4Document)) {
            String prefix = getLocalNameForNamespace(((Attr) node).getName(), null);
            String href = ((Attr) node).getValue();
            XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefix, href);
            if (xmlNamespace != null) {
                return xmlNamespace;
            }
            return XmlNamespace.createFromAttr(runtime, (Attr) node);
        }
    }
    XmlNode xmlNode = getCachedNode(node);
    if (xmlNode == null) {
        xmlNode = (XmlNode) constructNode(runtime, node);
        node.setUserData(CACHED_NODE, xmlNode, null);
    }
    return xmlNode;
}
Also used : XmlNode(nokogiri.XmlNode) Html4Document(nokogiri.Html4Document) XmlNamespace(nokogiri.XmlNamespace) XmlDocument(nokogiri.XmlDocument) RubyString(org.jruby.RubyString) Attr(org.w3c.dom.Attr) XmlAttr(nokogiri.XmlAttr)

Example 14 with XmlDocument

use of nokogiri.XmlDocument in project nokogiri by sparklemotion.

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.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();
    }
}
Also used : XmlSyntaxError(nokogiri.XmlSyntaxError) XmlDocument(nokogiri.XmlDocument)

Example 15 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, 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);
    }
}
Also used : XmlDocument(nokogiri.XmlDocument) IOException(java.io.IOException) Document(org.w3c.dom.Document) XmlDocument(nokogiri.XmlDocument) SAXException(org.xml.sax.SAXException)

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