Search in sources :

Example 21 with IRubyObject

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

the class XmlDocument method getInternalSubset.

public IRubyObject getInternalSubset(ThreadContext context) {
    IRubyObject dtd = (IRubyObject) node.getUserData(DTD_INTERNAL_SUBSET);
    if (dtd == null) {
        Document document = getDocument();
        if (document.getUserData(XmlDocument.DTD_RAW_DOCUMENT) != null) {
            dtd = XmlDtd.newFromInternalSubset(context.getRuntime(), document);
        } else if (document.getDoctype() != null) {
            DocumentType docType = document.getDoctype();
            IRubyObject name, publicId, systemId;
            name = publicId = systemId = context.getRuntime().getNil();
            if (docType.getName() != null) {
                name = context.getRuntime().newString(docType.getName());
            }
            if (docType.getPublicId() != null) {
                publicId = context.getRuntime().newString(docType.getPublicId());
            }
            if (docType.getSystemId() != null) {
                systemId = context.getRuntime().newString(docType.getSystemId());
            }
            dtd = XmlDtd.newEmpty(context.getRuntime(), document, name, publicId, systemId);
        } else {
            dtd = context.getRuntime().getNil();
        }
        setInternalSubset(dtd);
    }
    return dtd;
}
Also used : DocumentType(org.w3c.dom.DocumentType) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Example 22 with IRubyObject

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

the class XmlDtd method extractDecls.

/**
     * Recursively extract various DTD declarations and store them in
     * the various collections.
     */
protected void extractDecls(ThreadContext context) {
    Ruby runtime = context.getRuntime();
    // initialize data structures
    allDecls = RubyArray.newArray(runtime);
    attributes = RubyHash.newHash(runtime);
    elements = RubyHash.newHash(runtime);
    entities = RubyHash.newHash(runtime);
    notations = RubyHash.newHash(runtime);
    contentModels = RubyHash.newHash(runtime);
    children = runtime.getNil();
    // leave all the decl hash's empty
    if (node == null)
        return;
    extractDecls(context, node.getFirstChild());
    // convert allDecls to a NodeSet
    children = XmlNodeSet.newXmlNodeSet(context, allDecls);
    // add attribute decls as attributes to the matching element decl
    RubyArray keys = attributes.keys();
    for (int i = 0; i < keys.getLength(); ++i) {
        IRubyObject akey = keys.entry(i);
        IRubyObject val;
        val = attributes.op_aref(context, akey);
        if (val.isNil())
            continue;
        XmlAttributeDecl attrDecl = (XmlAttributeDecl) val;
        IRubyObject ekey = attrDecl.element_name(context);
        val = elements.op_aref(context, ekey);
        if (val.isNil())
            continue;
        XmlElementDecl elemDecl = (XmlElementDecl) val;
        elemDecl.appendAttrDecl(attrDecl);
    }
    // add content models to the matching element decl
    keys = contentModels.keys();
    for (int i = 0; i < keys.getLength(); ++i) {
        IRubyObject key = keys.entry(i);
        IRubyObject cm = contentModels.op_aref(context, key);
        IRubyObject elem = elements.op_aref(context, key);
        if (elem.isNil())
            continue;
        if (((XmlElementDecl) elem).isEmpty())
            continue;
        ((XmlElementDecl) elem).setContentModel(cm);
    }
}
Also used : RubyArray(org.jruby.RubyArray) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby)

Example 23 with IRubyObject

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

the class XmlDtd method extractDecls.

/**
     * The <code>node</code> is either the first child of the root dtd
     * node (as returned by getInternalSubset()) or the first child of
     * the external subset node (as returned by getExternalSubset()).
     *
     * This recursive function will not descend into an
     * 'externalSubset' node, thus for an internal subset it only
     * extracts nodes in the internal subset, and for an external
     * subset it extracts everything and assumess <code>node</code>
     * and all children are part of the external subset.
     */
protected void extractDecls(ThreadContext context, Node node) {
    while (node != null) {
        if (isExternalSubset(node)) {
            return;
        } else if (isAttributeDecl(node)) {
            XmlAttributeDecl decl = (XmlAttributeDecl) XmlAttributeDecl.create(context, node);
            attributes.op_aset(context, decl.attribute_name(context), decl);
            allDecls.append(decl);
        } else if (isElementDecl(node)) {
            XmlElementDecl decl = (XmlElementDecl) XmlElementDecl.create(context, node);
            elements.op_aset(context, decl.element_name(context), decl);
            allDecls.append(decl);
        } else if (isEntityDecl(node)) {
            XmlEntityDecl decl = (XmlEntityDecl) XmlEntityDecl.create(context, node);
            entities.op_aset(context, decl.node_name(context), decl);
            allDecls.append(decl);
        } else if (isNotationDecl(node)) {
            XmlNode tmp = (XmlNode) NokogiriHelpers.constructNode(context.getRuntime(), node);
            IRubyObject decl = invoke(context, notationClass, "new", tmp.getAttribute(context, "name"), tmp.getAttribute(context, "pubid"), tmp.getAttribute(context, "sysid"));
            notations.op_aset(context, tmp.getAttribute(context, "name"), decl);
            allDecls.append(decl);
        } else if (isContentModel(node)) {
            XmlElementContent cm = new XmlElementContent(context.getRuntime(), (XmlDocument) document(context), node);
            contentModels.op_aset(context, cm.element_name(context), cm);
        } else {
            // recurse
            extractDecls(context, node.getFirstChild());
        }
        node = node.getNextSibling();
    }
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 24 with IRubyObject

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

the class HtmlElementDescription method sub_elements.

@JRubyMethod()
public IRubyObject sub_elements(ThreadContext context) {
    Ruby ruby = context.getRuntime();
    List<String> subs = findSubElements(element);
    IRubyObject[] ary = new IRubyObject[subs.size()];
    for (int i = 0; i < subs.size(); ++i) {
        ary[i] = ruby.newString(subs.get(i));
    }
    return ruby.newArray(ary);
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 25 with IRubyObject

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

the class HtmlSaxPushParser method native_write.

@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
    try {
        initialize_task(context);
    } catch (IOException e) {
        throw context.getRuntime().newRuntimeError(e.getMessage());
    }
    final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
    if (data == null) {
        terminateTask(context);
        // Nokogiri::HTML::SyntaxError
        throw new RaiseException(XmlSyntaxError.createHTMLSyntaxError(context.runtime));
    }
    int errorCount0 = parserTask.getErrorCount();
    if (isLast.isTrue()) {
        IRubyObject document = invoke(context, this, "document");
        invoke(context, document, "end_document");
        terminateTask(context);
    } else {
        try {
            Future<Void> task = stream.addChunk(data);
            task.get();
        } catch (ClosedStreamException ex) {
        // this means the stream is closed, ignore this exception
        } catch (Exception e) {
            throw context.getRuntime().newRuntimeError(e.getMessage());
        }
    }
    if (!options.recover && parserTask.getErrorCount() > errorCount0) {
        terminateTask(context);
        throw new RaiseException(parserTask.getLastError(), true);
    }
    return this;
}
Also used : ClosedStreamException(nokogiri.internals.ClosedStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) RaiseException(org.jruby.exceptions.RaiseException) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RaiseException(org.jruby.exceptions.RaiseException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) RubyException(org.jruby.RubyException) IOException(java.io.IOException) ClosedStreamException(nokogiri.internals.ClosedStreamException) JRubyMethod(org.jruby.anno.JRubyMethod)

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