Search in sources :

Example 1 with RubyObjectAdapter

use of org.jruby.RubyObjectAdapter 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 RubyObjectAdapter

use of org.jruby.RubyObjectAdapter 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)2 RubyObjectAdapter (org.jruby.RubyObjectAdapter)2 IRubyObject (org.jruby.runtime.builtin.IRubyObject)2