Search in sources :

Example 11 with IRubyObject

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

the class ReaderNode method getAttributes.

public IRubyObject getAttributes(ThreadContext context) {
    final Ruby runtime = context.runtime;
    if (attributeList == null)
        return runtime.getNil();
    RubyHash hash = RubyHash.newHash(runtime);
    for (int i = 0; i < attributeList.length; i++) {
        IRubyObject k = stringOrBlank(runtime, attributeList.names.get(i));
        IRubyObject v = stringOrBlank(runtime, attributeList.values.get(i));
        // hash.op_aset(context, k, v)
        hash.fastASetCheckString(runtime, k, v);
    }
    return hash;
}
Also used : RubyHash(org.jruby.RubyHash) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby)

Example 12 with IRubyObject

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

the class NokogiriHandler method startElement.

/*
     * This calls "start_element_namespace".
     *
     * Attributes that define namespaces are passed in a separate
     * array of <code>[:prefix, :uri]</code> arrays and are not
     * passed with the other attributes.
     */
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
    // for attributes other than namespace attrs
    RubyArray rubyAttr = RubyArray.newArray(ruby);
    // for namespace defining attributes
    RubyArray rubyNSAttr = RubyArray.newArray(ruby);
    ThreadContext context = ruby.getCurrentContext();
    // isFromFragmentHandler();
    boolean fromFragmentHandler = false;
    for (int i = 0; i < attrs.getLength(); i++) {
        String u = attrs.getURI(i);
        String qn = attrs.getQName(i);
        String ln = attrs.getLocalName(i);
        String val = attrs.getValue(i);
        String pre;
        pre = getPrefix(qn);
        if (ln == null || ln.equals(""))
            ln = getLocalPart(qn);
        if (isNamespace(qn) && !fromFragmentHandler) {
            // I haven't figured the reason out yet, but, in somewhere,
            // namespace is converted to array in array and cause
            // TypeError at line 45 in fragment_handler.rb
            RubyArray ns = RubyArray.newArray(ruby, 2);
            if (ln.equals("xmlns"))
                ln = null;
            ns.add(stringOrNil(ruby, ln));
            ns.add(ruby.newString(val));
            rubyNSAttr.add(ns);
        } else {
            IRubyObject[] args = null;
            if (needEmptyAttrCheck) {
                if (isEmptyAttr(ln)) {
                    args = new IRubyObject[3];
                    args[0] = stringOrNil(ruby, ln);
                    args[1] = stringOrNil(ruby, pre);
                    args[2] = stringOrNil(ruby, u);
                }
            }
            if (args == null) {
                args = new IRubyObject[4];
                args[0] = stringOrNil(ruby, ln);
                args[1] = stringOrNil(ruby, pre);
                args[2] = stringOrNil(ruby, u);
                args[3] = stringOrNil(ruby, val);
            }
            IRubyObject attr = RuntimeHelpers.invoke(context, attrClass, "new", args);
            rubyAttr.add(attr);
        }
    }
    if (localName == null || localName.equals(""))
        localName = getLocalPart(qName);
    call("start_element_namespace", stringOrNil(ruby, localName), rubyAttr, stringOrNil(ruby, getPrefix(qName)), stringOrNil(ruby, uri), rubyNSAttr);
    characterStack.push(new StringBuffer());
}
Also used : RubyArray(org.jruby.RubyArray) ThreadContext(org.jruby.runtime.ThreadContext) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 13 with IRubyObject

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

the class NokogiriHandler method isFromFragmentHandler.

private boolean isFromFragmentHandler() {
    if (object != null && object instanceof RubyObject) {
        RubyObject rubyObj = (RubyObject) object;
        IRubyObject document = rubyObj.getInstanceVariable("@document");
        if (document != null) {
            String name = document.getMetaClass().getName();
            if ("Nokogiri::XML::FragmentHandler".equals(name)) {
                return true;
            }
        }
    }
    return false;
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject) RubyObject(org.jruby.RubyObject) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 14 with IRubyObject

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

the class XmlText method init.

@Override
protected void init(ThreadContext context, IRubyObject[] args) {
    if (args.length < 2) {
        throw getRuntime().newArgumentError(args.length, 2);
    }
    content = args[0];
    IRubyObject xNode = args[1];
    XmlNode xmlNode = asXmlNode(context, xNode);
    XmlDocument xmlDoc = (XmlDocument) xmlNode.document(context);
    doc = xmlDoc;
    Document document = xmlDoc.getDocument();
    // text node content should not be encoded when it is created by Text node.
    // while content should be encoded when it is created by Element node.
    Node node = document.createTextNode(rubyStringToString(content));
    setNode(context, node);
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Example 15 with IRubyObject

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

the class NokogiriEncodingReaderWrapper method read.

@Override
public int read(byte[] b, int off, int len) {
    if (b == null) {
        throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }
    int copyLength = Math.min(firstChunkLength - firstChunkOff, len);
    if (copyLength > 0) {
        System.arraycopy(firstChunk, firstChunkOff, b, off, copyLength);
        len -= copyLength;
        firstChunkOff += copyLength;
    }
    if (len <= 0)
        return copyLength;
    IRubyObject returnValue = encodingReader.callMethod(context, "read", ruby.newFixnum(len));
    if (returnValue.isNil())
        return -1;
    ByteList bytes = returnValue.asString().getByteList();
    int length = bytes.length();
    System.arraycopy(bytes.unsafeBytes(), bytes.getBegin(), b, off + copyLength, length);
    return length + copyLength;
}
Also used : ByteList(org.jruby.util.ByteList) 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