Search in sources :

Example 76 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

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 77 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class ReaderNode method getNamespaces.

public IRubyObject getNamespaces(ThreadContext context) {
    final Ruby runtime = context.runtime;
    if (namespaces == null)
        return runtime.getNil();
    RubyHash hash = RubyHash.newHash(runtime);
    for (Map.Entry<String, String> entry : namespaces.entrySet()) {
        IRubyObject k = stringOrBlank(runtime, entry.getKey());
        IRubyObject v = stringOrBlank(runtime, entry.getValue());
        // hash.op_aset(context, k, v)
        hash.fastASetCheckString(runtime, k, v);
    }
    return hash;
}
Also used : RubyHash(org.jruby.RubyHash) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) HashMap(java.util.HashMap) Map(java.util.Map)

Example 78 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlSchema method from_document.

/*
     * call-seq:
     *  from_document(doc)
     *
     * Create a new Schema from the Nokogiri::XML::Document +doc+
     */
@JRubyMethod(meta = true)
public static IRubyObject from_document(ThreadContext context, IRubyObject klazz, IRubyObject document) {
    XmlDocument doc = ((XmlDocument) ((XmlNode) document).document(context));
    RubyArray errors = (RubyArray) doc.getInstanceVariable("@errors");
    if (!errors.isEmpty()) {
        throw new RaiseException((XmlSyntaxError) errors.first());
    }
    DOMSource source = new DOMSource(doc.getDocument());
    IRubyObject uri = doc.url(context);
    if (!uri.isNil()) {
        source.setSystemId(uri.convertToString().asJavaString());
    }
    return getSchema(context, (RubyClass) klazz, source);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) RubyArray(org.jruby.RubyArray) RaiseException(org.jruby.exceptions.RaiseException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 79 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlText method accept.

@Override
public void accept(ThreadContext context, SaveContextVisitor visitor) {
    visitor.enter((Text) node);
    Node child = node.getFirstChild();
    while (child != null) {
        IRubyObject nokoNode = getCachedNodeOrCreate(context.getRuntime(), child);
        if (nokoNode instanceof XmlNode) {
            XmlNode cur = (XmlNode) nokoNode;
            cur.accept(context, visitor);
        } else if (nokoNode instanceof XmlNamespace) {
            XmlNamespace cur = (XmlNamespace) nokoNode;
            cur.accept(context, visitor);
        }
        child = child.getNextSibling();
    }
    visitor.leave(node);
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 80 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XsltStylesheet method createDocumentFromString.

private IRubyObject createDocumentFromString(ThreadContext context, Ruby runtime, String stringResult) {
    IRubyObject[] args = new IRubyObject[4];
    args[0] = stringOrBlank(runtime, stringResult);
    // url
    args[1] = runtime.getNil();
    // encoding
    args[2] = runtime.getNil();
    RubyClass parse_options = (RubyClass) runtime.getClassFromPath("Nokogiri::XML::ParseOptions");
    if (htmlish) {
        args[3] = parse_options.getConstant("DEFAULT_HTML");
        RubyClass htmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
        return RuntimeHelpers.invoke(context, htmlDocumentClass, "parse", args);
    } else {
        args[3] = parse_options.getConstant("DEFAULT_XML");
        RubyClass xmlDocumentClass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
        XmlDocument xmlDocument = (XmlDocument) RuntimeHelpers.invoke(context, xmlDocumentClass, "parse", args);
        if (((Document) xmlDocument.getNode()).getDocumentElement() == null) {
            RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
            RuntimeHelpers.invoke(context, errors, "<<", args[0]);
        }
        return xmlDocument;
    }
}
Also used : RubyArray(org.jruby.RubyArray) JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Aggregations

IRubyObject (org.jruby.runtime.builtin.IRubyObject)105 JRubyMethod (org.jruby.anno.JRubyMethod)32 Document (org.w3c.dom.Document)27 Ruby (org.jruby.Ruby)25 Node (org.w3c.dom.Node)24 RubyString (org.jruby.RubyString)16 RubyArray (org.jruby.RubyArray)13 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)11 RaiseException (org.jruby.exceptions.RaiseException)10 InputStream (java.io.InputStream)6 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)6 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)6 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)6 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)6 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)6 RubyProc (org.jruby.RubyProc)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)4 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)4