Search in sources :

Example 6 with IRubyObject

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

the class XmlReader method setAndRaiseErrorsIfAny.

private IRubyObject setAndRaiseErrorsIfAny(final Ruby runtime, final RaiseException ex) throws RaiseException {
    final ReaderNode currentNode = currentNode();
    if (currentNode == null)
        return runtime.getNil();
    if (currentNode.isError()) {
        RubyArray errors = (RubyArray) getInstanceVariable("@errors");
        IRubyObject error = currentNode.toSyntaxError();
        errors.append(error);
        setInstanceVariable("@errors", errors);
        throw ex != null ? ex : new RaiseException((XmlSyntaxError) error);
    }
    if (ex != null)
        throw ex;
    return this;
}
Also used : RubyArray(org.jruby.RubyArray) RaiseException(org.jruby.exceptions.RaiseException) ReaderNode(nokogiri.internals.ReaderNode) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 7 with IRubyObject

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

the class XmlSaxParserContext method maybeTrimLeadingAndTrailingWhitespace.

/**
     * If the handler's document is a FragmentHandler, attempt to trim
     * leading and trailing whitespace.
     *
     * This is a bit hackish and depends heavily on the internals of
     * FragmentHandler.
     */
protected void maybeTrimLeadingAndTrailingWhitespace(ThreadContext context, IRubyObject parser) {
    final String path = "Nokogiri::XML::FragmentHandler";
    RubyObjectAdapter adapter = JavaEmbedUtils.newObjectAdapter();
    RubyModule mod = context.getRuntime().getClassFromPath(path);
    IRubyObject handler = adapter.getInstanceVariable(parser, "@document");
    if (handler == null || handler.isNil() || !adapter.isKindOf(handler, mod))
        return;
    IRubyObject stack = adapter.getInstanceVariable(handler, "@stack");
    if (stack == null || stack.isNil())
        return;
    // doc is finally a DocumentFragment whose nodes we can check
    IRubyObject doc = adapter.callMethod(stack, "first");
    if (doc == null || doc.isNil())
        return;
    IRubyObject children;
    for (; ; ) {
        children = adapter.callMethod(doc, "children");
        IRubyObject first = adapter.callMethod(children, "first");
        if (isWhitespaceText(context, first))
            adapter.callMethod(first, "unlink");
        else
            break;
    }
    for (; ; ) {
        children = adapter.callMethod(doc, "children");
        IRubyObject last = adapter.callMethod(children, "last");
        if (isWhitespaceText(context, last))
            adapter.callMethod(last, "unlink");
        else
            break;
    }
    // While we have a document, normalize it.
    ((XmlNode) doc).normalize();
}
Also used : RubyObjectAdapter(org.jruby.RubyObjectAdapter) RubyModule(org.jruby.RubyModule) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 8 with IRubyObject

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

the class XmlNode method internal_subset.

@JRubyMethod
public IRubyObject internal_subset(ThreadContext context) {
    Document document = getOwnerDocument();
    if (document == null) {
        return context.getRuntime().getNil();
    }
    XmlDocument xdoc = (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
    IRubyObject xdtd = xdoc.getInternalSubset(context);
    return xdtd;
}
Also used : Document(org.w3c.dom.Document) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 9 with IRubyObject

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

the class XmlNodeSet method initialize.

final void initialize(Ruby runtime, IRubyObject refNode) {
    if (refNode instanceof XmlNode) {
        IRubyObject doc = ((XmlNode) refNode).document(runtime);
        setDocumentAndDecorate(runtime.getCurrentContext(), this, doc);
    }
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 10 with IRubyObject

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

the class XmlDomParserContext method wrapDocument.

/**
     * This method is broken out so that HtmlDomParserContext can
     * override it.
     */
protected XmlDocument wrapDocument(ThreadContext context, RubyClass klazz, Document doc) {
    XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), klazz);
    xmlDocument.setDocumentNode(context, doc);
    xmlDocument.setEncoding(ruby_encoding);
    if (options.dtdLoad) {
        IRubyObject xmlDtdOrNil = XmlDtd.newFromExternalSubset(context.getRuntime(), doc);
        if (!xmlDtdOrNil.isNil()) {
            XmlDtd xmlDtd = (XmlDtd) xmlDtdOrNil;
            doc.setUserData(XmlDocument.DTD_EXTERNAL_SUBSET, xmlDtd, null);
        }
    }
    return xmlDocument;
}
Also used : XmlDtd(nokogiri.XmlDtd) XmlDocument(nokogiri.XmlDocument) 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