Search in sources :

Example 46 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlProcessingInstruction method rbNew.

@JRubyMethod(name = "new", meta = true, rest = true, required = 3)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    IRubyObject doc = args[0];
    IRubyObject target = args[1];
    IRubyObject data = args[2];
    Document document = ((XmlNode) doc).getOwnerDocument();
    Node node = document.createProcessingInstruction(rubyStringToString(target), rubyStringToString(data));
    XmlProcessingInstruction self = new XmlProcessingInstruction(context.getRuntime(), (RubyClass) klazz, node);
    RuntimeHelpers.invoke(context, self, "initialize", args);
    return self;
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 47 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlReader method from_memory.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
    // args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
    Ruby runtime = context.getRuntime();
    // Not nil allowed!
    if (args[0].isNil())
        throw runtime.newArgumentError("string cannot be nil");
    XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
    reader.init(runtime);
    reader.setInstanceVariable("@source", args[0]);
    reader.setInstanceVariable("@errors", runtime.newArray());
    IRubyObject url = context.nil;
    if (args.length > 1)
        url = args[1];
    if (args.length > 2)
        reader.setInstanceVariable("@encoding", args[2]);
    Options options;
    if (args.length > 3) {
        options = new ParserContext.Options((Long) args[3].toJava(Long.class));
    } else {
        // use the default options RECOVER | NONET
        options = new ParserContext.Options(2048 | 1);
    }
    IRubyObject stringIO = NokogiriService.getNokogiriClassCache(context.getRuntime()).get("StringIO").newInstance(context, args[0], Block.NULL_BLOCK);
    InputStream in = new UncloseableInputStream(new IOInputStream(stringIO));
    reader.setInput(context, in, url, options);
    return reader;
}
Also used : Options(nokogiri.internals.ParserContext.Options) Options(nokogiri.internals.ParserContext.Options) UncloseableInputStream(nokogiri.internals.UncloseableInputStream) IOInputStream(org.jruby.util.IOInputStream) InputStream(java.io.InputStream) UncloseableInputStream(nokogiri.internals.UncloseableInputStream) IRubyObject(org.jruby.runtime.builtin.IRubyObject) ParserContext(nokogiri.internals.ParserContext) Ruby(org.jruby.Ruby) IOInputStream(org.jruby.util.IOInputStream) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 48 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlReader method empty_element_p.

@JRubyMethod(name = { "empty_element?", "self_closing?" })
public IRubyObject empty_element_p(ThreadContext context) {
    ReaderNode readerNode = currentNode();
    ensureNodeClosed(context);
    if (readerNode == null)
        return context.getRuntime().getNil();
    if (!(readerNode instanceof ElementNode))
        context.getRuntime().getFalse();
    return RubyBoolean.newBoolean(context.getRuntime(), !readerNode.hasChildren);
}
Also used : ReaderNode(nokogiri.internals.ReaderNode) ElementNode(nokogiri.internals.ReaderNode.ElementNode) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 49 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlSaxPushParser 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());
    }
    byte[] data = null;
    if (chunk instanceof RubyString || chunk.respondsTo("to_str")) {
        data = chunk.convertToString().getBytes();
    } else {
        terminateTask(context);
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        throw new RaiseException(xmlSyntaxError);
    }
    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(new ByteArrayInputStream(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) RubyString(org.jruby.RubyString) RaiseException(org.jruby.exceptions.RaiseException) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RaiseException(org.jruby.exceptions.RaiseException) RubyException(org.jruby.RubyException) IOException(java.io.IOException) ClosedStreamException(nokogiri.internals.ClosedStreamException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 50 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlSchema method validate_file.

@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject validate_file(ThreadContext context, IRubyObject file) {
    Ruby ruby = context.getRuntime();
    XmlDomParserContext ctx = new XmlDomParserContext(ruby, RubyFixnum.newFixnum(ruby, 1L));
    ctx.setInputSource(context, file, context.getRuntime().getNil());
    XmlDocument xmlDocument = ctx.parse(context, getNokogiriClass(ruby, "Nokogiri::XML::Document"), ruby.getNil());
    return validate_document_or_file(context, xmlDocument);
}
Also used : XmlDomParserContext(nokogiri.internals.XmlDomParserContext) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)103 IRubyObject (org.jruby.runtime.builtin.IRubyObject)32 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)31 Ruby (org.jruby.Ruby)31 Node (org.w3c.dom.Node)29 RubyString (org.jruby.RubyString)28 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)23 Document (org.w3c.dom.Document)19 RubyArray (org.jruby.RubyArray)17 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)12 Element (org.w3c.dom.Element)12 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)10 JRubyClass (org.jruby.anno.JRubyClass)10 RaiseException (org.jruby.exceptions.RaiseException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 RubyClass (org.jruby.RubyClass)7 InputStream (java.io.InputStream)6 NokogiriNamespaceCache (nokogiri.internals.NokogiriNamespaceCache)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4