Search in sources :

Example 1 with RubyModule

use of org.jruby.RubyModule 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 2 with RubyModule

use of org.jruby.RubyModule in project nokogiri by sparklemotion.

the class NokogiriService method init.

private void init(Ruby ruby) {
    RubyModule nokogiri = ruby.defineModule("Nokogiri");
    RubyModule xmlModule = nokogiri.defineModuleUnder("XML");
    RubyModule xmlSaxModule = xmlModule.defineModuleUnder("SAX");
    RubyModule htmlModule = nokogiri.defineModuleUnder("HTML");
    RubyModule htmlSaxModule = htmlModule.defineModuleUnder("SAX");
    RubyModule xsltModule = nokogiri.defineModuleUnder("XSLT");
    createJavaLibraryVersionConstants(ruby, nokogiri);
    createNokogiriModule(ruby, nokogiri);
    createSyntaxErrors(ruby, nokogiri, xmlModule);
    RubyClass xmlNode = createXmlModule(ruby, xmlModule);
    createHtmlModule(ruby, htmlModule);
    createDocuments(ruby, xmlModule, htmlModule, xmlNode);
    createSaxModule(ruby, xmlSaxModule, htmlSaxModule);
    createXsltModule(ruby, xsltModule);
    nokogiri.setInternalVariable("cache", populateNokogiriClassCahce(ruby));
}
Also used : RubyModule(org.jruby.RubyModule) RubyClass(org.jruby.RubyClass)

Example 3 with RubyModule

use of org.jruby.RubyModule in project gocd by gocd.

the class NokogiriService method init.

private void init(Ruby ruby) {
    RubyModule nokogiri = ruby.defineModule("Nokogiri");
    RubyModule xmlModule = nokogiri.defineModuleUnder("XML");
    RubyModule xmlSaxModule = xmlModule.defineModuleUnder("SAX");
    RubyModule htmlModule = nokogiri.defineModuleUnder("HTML");
    RubyModule htmlSaxModule = htmlModule.defineModuleUnder("SAX");
    RubyModule xsltModule = nokogiri.defineModuleUnder("XSLT");
    createJavaLibraryVersionConstants(ruby, nokogiri);
    createNokogiriModule(ruby, nokogiri);
    createSyntaxErrors(ruby, nokogiri, xmlModule);
    RubyClass xmlNode = createXmlModule(ruby, xmlModule);
    createHtmlModule(ruby, htmlModule);
    createDocuments(ruby, xmlModule, htmlModule, xmlNode);
    createSaxModule(ruby, xmlSaxModule, htmlSaxModule);
    createXsltModule(ruby, xsltModule);
    nokogiri.setInternalVariable("cache", populateNokogiriClassCahce(ruby));
}
Also used : RubyModule(org.jruby.RubyModule) RubyClass(org.jruby.RubyClass)

Example 4 with RubyModule

use of org.jruby.RubyModule in project gocd by gocd.

the class NokogiriService method createDocuments.

private void createDocuments(Ruby ruby, RubyModule xmlModule, RubyModule htmlModule, RubyClass node) {
    RubyClass xmlDocument = xmlModule.defineClassUnder("Document", node, XML_DOCUMENT_ALLOCATOR);
    xmlDocument.defineAnnotatedMethods(XmlDocument.class);
    //RubyModule htmlDoc = html.defineOrGetClassUnder("Document", document);
    RubyModule htmlDocument = htmlModule.defineClassUnder("Document", xmlDocument, HTML_DOCUMENT_ALLOCATOR);
    htmlDocument.defineAnnotatedMethods(HtmlDocument.class);
}
Also used : RubyModule(org.jruby.RubyModule) RubyClass(org.jruby.RubyClass)

Example 5 with RubyModule

use of org.jruby.RubyModule in project gocd by gocd.

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)

Aggregations

RubyModule (org.jruby.RubyModule)12 RubyClass (org.jruby.RubyClass)8 IRubyObject (org.jruby.runtime.builtin.IRubyObject)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 HtmlDomParserContext (nokogiri.internals.HtmlDomParserContext)2 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)2 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)2 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)2 XmlDomParserContext (nokogiri.internals.XmlDomParserContext)2 Ruby (org.jruby.Ruby)2 RubyArray (org.jruby.RubyArray)2 RubyObjectAdapter (org.jruby.RubyObjectAdapter)2 RubyString (org.jruby.RubyString)2 JRubyMethod (org.jruby.anno.JRubyMethod)2 Node (org.w3c.dom.Node)2 ExecutorService (java.util.concurrent.ExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)1 Finalizable (org.jruby.Finalizable)1