Search in sources :

Example 1 with HtmlDomParserContext

use of nokogiri.internals.HtmlDomParserContext in project nokogiri by sparklemotion.

the class HtmlDocument method do_parse.

public static IRubyObject do_parse(ThreadContext context, IRubyObject klass, IRubyObject[] args) {
    Ruby ruby = context.getRuntime();
    Arity.checkArgumentCount(ruby, args, 4, 4);
    HtmlDomParserContext ctx = new HtmlDomParserContext(ruby, args[2], args[3]);
    ctx.setInputSource(context, args[0], args[1]);
    return ctx.parse(context, klass, args[1]);
}
Also used : HtmlDomParserContext(nokogiri.internals.HtmlDomParserContext) Ruby(org.jruby.Ruby)

Example 2 with HtmlDomParserContext

use of nokogiri.internals.HtmlDomParserContext in project gocd by gocd.

the class HtmlDocument method do_parse.

public static IRubyObject do_parse(ThreadContext context, IRubyObject klass, IRubyObject[] args) {
    Ruby ruby = context.getRuntime();
    Arity.checkArgumentCount(ruby, args, 4, 4);
    HtmlDomParserContext ctx = new HtmlDomParserContext(ruby, args[2], args[3]);
    ctx.setInputSource(context, args[0], args[1]);
    return ctx.parse(context, klass, args[1]);
}
Also used : HtmlDomParserContext(nokogiri.internals.HtmlDomParserContext) Ruby(org.jruby.Ruby)

Example 3 with HtmlDomParserContext

use of nokogiri.internals.HtmlDomParserContext in project gocd by gocd.

the class XmlNode method in_context.

/**
     * TODO: this is a stub implementation.  It's not clear what
     * 'in_context' is supposed to do.  Also should take
     * <code>options</code> into account.
     */
@JRubyMethod(required = 2, visibility = Visibility.PRIVATE)
public IRubyObject in_context(ThreadContext context, IRubyObject str, IRubyObject options) {
    RubyModule klass;
    XmlDomParserContext ctx;
    InputStream istream;
    XmlDocument document;
    IRubyObject d = document(context);
    Ruby runtime = context.getRuntime();
    if (d != null && d instanceof XmlDocument) {
        document = (XmlDocument) d;
    } else {
        return runtime.getNil();
    }
    if (document instanceof HtmlDocument) {
        klass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
        ctx = new HtmlDomParserContext(runtime, options);
        ((HtmlDomParserContext) ctx).enableDocumentFragment();
        istream = new ByteArrayInputStream((rubyStringToString(str)).getBytes());
    } else if (document instanceof XmlDocument) {
        klass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
        ctx = new XmlDomParserContext(runtime, options);
        String input = rubyStringToString(str);
        istream = new ByteArrayInputStream(input.getBytes());
    } else {
        return runtime.getNil();
    }
    ctx.setInputSource(istream);
    XmlDocument doc = ctx.parse(context, klass, runtime.getNil());
    RubyArray documentErrors = getErrorArray(document);
    RubyArray docErrors = getErrorArray(doc);
    if (isErrorIncreased(documentErrors, docErrors)) {
        for (int i = 0; i < docErrors.getLength(); i++) {
            documentErrors.add(docErrors.get(i));
        }
        document.setInstanceVariable("@errors", documentErrors);
        XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, RubyArray.newArray(runtime));
        return xmlNodeSet;
    }
    // The first child might be document type node (dtd declaration).
    // XmlNodeSet to be return should not have dtd decl in its list.
    Node first;
    if (doc.node.getFirstChild().getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        first = doc.node.getFirstChild().getNextSibling();
    } else {
        first = doc.node.getFirstChild();
    }
    RubyArray nodeArray = RubyArray.newArray(runtime);
    nodeArray.add(NokogiriHelpers.getCachedNodeOrCreate(runtime, first));
    XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, nodeArray);
    return xmlNodeSet;
}
Also used : RubyModule(org.jruby.RubyModule) HtmlDomParserContext(nokogiri.internals.HtmlDomParserContext) RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) XmlDomParserContext(nokogiri.internals.XmlDomParserContext) ByteArrayInputStream(java.io.ByteArrayInputStream) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 4 with HtmlDomParserContext

use of nokogiri.internals.HtmlDomParserContext in project nokogiri by sparklemotion.

the class XmlNode method in_context.

/**
     * TODO: this is a stub implementation.  It's not clear what
     * 'in_context' is supposed to do.  Also should take
     * <code>options</code> into account.
     */
@JRubyMethod(required = 2, visibility = Visibility.PRIVATE)
public IRubyObject in_context(ThreadContext context, IRubyObject str, IRubyObject options) {
    RubyModule klass;
    XmlDomParserContext ctx;
    InputStream istream;
    XmlDocument document;
    IRubyObject d = document(context);
    Ruby runtime = context.getRuntime();
    if (d != null && d instanceof XmlDocument) {
        document = (XmlDocument) d;
    } else {
        return runtime.getNil();
    }
    if (document instanceof HtmlDocument) {
        klass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
        ctx = new HtmlDomParserContext(runtime, options);
        ((HtmlDomParserContext) ctx).enableDocumentFragment();
        istream = new ByteArrayInputStream((rubyStringToString(str)).getBytes());
    } else {
        klass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
        ctx = new XmlDomParserContext(runtime, options);
        String input = rubyStringToString(str);
        istream = new ByteArrayInputStream(input.getBytes());
    }
    ctx.setInputSource(istream);
    // run `test_parse_with_unparented_html_text_context_node' few times to see this happen
    if (document instanceof HtmlDocument && !(document.getEncoding() == null || document.getEncoding().isNil())) {
        HtmlDomParserContext htmlCtx = (HtmlDomParserContext) ctx;
        htmlCtx.setEncoding(document.getEncoding().asJavaString());
    }
    XmlDocument doc = ctx.parse(context, klass, runtime.getNil());
    RubyArray documentErrors = getErrorArray(document);
    RubyArray docErrors = getErrorArray(doc);
    if (isErrorIncreased(documentErrors, docErrors)) {
        for (int i = 0; i < docErrors.getLength(); i++) {
            documentErrors.add(docErrors.get(i));
        }
        document.setInstanceVariable("@errors", documentErrors);
        XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, RubyArray.newArray(runtime));
        return xmlNodeSet;
    }
    // The first child might be document type node (dtd declaration).
    // XmlNodeSet to be return should not have dtd decl in its list.
    Node first;
    if (doc.node.getFirstChild().getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        first = doc.node.getFirstChild().getNextSibling();
    } else {
        first = doc.node.getFirstChild();
    }
    RubyArray nodeArray = RubyArray.newArray(runtime);
    nodeArray.add(NokogiriHelpers.getCachedNodeOrCreate(runtime, first));
    XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, nodeArray);
    return xmlNodeSet;
}
Also used : RubyModule(org.jruby.RubyModule) HtmlDomParserContext(nokogiri.internals.HtmlDomParserContext) RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) XmlDomParserContext(nokogiri.internals.XmlDomParserContext) ByteArrayInputStream(java.io.ByteArrayInputStream) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

HtmlDomParserContext (nokogiri.internals.HtmlDomParserContext)4 Ruby (org.jruby.Ruby)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)2 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)2 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)2 XmlDomParserContext (nokogiri.internals.XmlDomParserContext)2 RubyArray (org.jruby.RubyArray)2 RubyModule (org.jruby.RubyModule)2 RubyString (org.jruby.RubyString)2 JRubyMethod (org.jruby.anno.JRubyMethod)2 IRubyObject (org.jruby.runtime.builtin.IRubyObject)2 Node (org.w3c.dom.Node)2 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)1