Search in sources :

Example 71 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlReader method empty_element_p.

@JRubyMethod(name = { "empty_element?", "self_closing?" })
public IRubyObject empty_element_p(ThreadContext context) {
    ReaderNode readerNode = currentNode();
    ensureNodeClosed(context);
    if (readerNode == null)
        return context.getRuntime().getNil();
    if (!(readerNode instanceof ElementNode))
        context.getRuntime().getFalse();
    return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
}
Also used : ReaderNode(nokogiri.internals.ReaderNode) ElementNode(nokogiri.internals.ReaderNode.ElementNode) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 72 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlReader method from_memory.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
    // args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
    Ruby runtime = context.getRuntime();
    // Not nil allowed!
    if (args[0].isNil())
        throw runtime.newArgumentError("string cannot be nil");
    XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
    reader.init(runtime);
    reader.setInstanceVariable("@source", args[0]);
    reader.setInstanceVariable("@errors", runtime.newArray());
    IRubyObject url = context.nil;
    if (args.length > 1)
        url = args[1];
    if (args.length > 2)
        reader.setInstanceVariable("@encoding", args[2]);
    Options options;
    if (args.length > 3) {
        options = new ParserContext.Options((Long) args[3].toJava(Long.class));
    } else {
        // use the default options RECOVER | NONET
        options = new ParserContext.Options(2048 | 1);
    }
    IRubyObject stringIO = runtime.getClass("StringIO").newInstance(context, args[0], Block.NULL_BLOCK);
    InputStream in = new UncloseableInputStream(new IOInputStream(stringIO));
    reader.setInput(context, in, url, options);
    return reader;
}
Also used : Options(nokogiri.internals.ParserContext.Options) Options(nokogiri.internals.ParserContext.Options) UncloseableInputStream(nokogiri.internals.UncloseableInputStream) IOInputStream(org.jruby.util.IOInputStream) InputStream(java.io.InputStream) UncloseableInputStream(nokogiri.internals.UncloseableInputStream) IRubyObject(org.jruby.runtime.builtin.IRubyObject) ParserContext(nokogiri.internals.ParserContext) Ruby(org.jruby.Ruby) IOInputStream(org.jruby.util.IOInputStream) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 73 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlAttributeDecl method enumeration.

/**
     * FIXME: will enumerations all be of the simple (val1|val2|val3)
     * type string?
     */
@JRubyMethod
public IRubyObject enumeration(ThreadContext context) {
    RubyArray enumVals = RubyArray.newArray(context.getRuntime());
    String atype = ((Element) node).getAttribute("atype");
    if (atype != null && atype.length() != 0 && atype.charAt(0) == '(') {
        // removed enclosing parens
        String valueStr = atype.substring(1, atype.length() - 1);
        String[] values = valueStr.split("\\|");
        for (int i = 0; i < values.length; i++) {
            enumVals.append(context.getRuntime().newString(values[i]));
        }
    }
    return enumVals;
}
Also used : RubyArray(org.jruby.RubyArray) Element(org.w3c.dom.Element) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 74 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlDocument method rbNew.

/*
     * call-seq:
     *  new(version = default)
     *
     * Create a new document with +version+ (defaults to "1.0")
     */
@JRubyMethod(name = "new", meta = true, rest = true, required = 0)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    XmlDocument xmlDocument;
    try {
        Document docNode = createNewDocument();
        if ("Nokogiri::HTML::Document".equals(((RubyClass) klazz).getName())) {
            xmlDocument = (XmlDocument) NokogiriService.HTML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
            xmlDocument.setDocumentNode(context, docNode);
        } else {
            // XML::Document and sublass
            xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
            xmlDocument.setDocumentNode(context, docNode);
        }
    } catch (Exception ex) {
        throw context.getRuntime().newRuntimeError("couldn't create document: " + ex.toString());
    }
    RuntimeHelpers.invoke(context, xmlDocument, "initialize", args);
    return xmlDocument;
}
Also used : Document(org.w3c.dom.Document) CanonicalizationException(nokogiri.internals.c14n.CanonicalizationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 75 with JRubyMethod

use of org.jruby.anno.JRubyMethod 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

JRubyMethod (org.jruby.anno.JRubyMethod)103 IRubyObject (org.jruby.runtime.builtin.IRubyObject)32 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)31 Ruby (org.jruby.Ruby)31 Node (org.w3c.dom.Node)29 RubyString (org.jruby.RubyString)28 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)23 Document (org.w3c.dom.Document)19 RubyArray (org.jruby.RubyArray)17 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)12 Element (org.w3c.dom.Element)12 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)10 JRubyClass (org.jruby.anno.JRubyClass)10 RaiseException (org.jruby.exceptions.RaiseException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 RubyClass (org.jruby.RubyClass)7 InputStream (java.io.InputStream)6 NokogiriNamespaceCache (nokogiri.internals.NokogiriNamespaceCache)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4