Search in sources :

Example 11 with JRubyMethod

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

the class XmlDocument method create_entity.

@JRubyMethod(required = 1, optional = 4)
public IRubyObject create_entity(ThreadContext context, IRubyObject[] argv) {
    // would cause validation failure.
    if (argv.length == 0)
        throw context.getRuntime().newRuntimeError("Could not create entity");
    String tagName = rubyStringToString(argv[0]);
    Node n = this.getOwnerDocument().createElement(tagName);
    return XmlEntityDecl.create(context, n, argv);
}
Also used : Node(org.w3c.dom.Node) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 12 with JRubyMethod

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

the class XmlDocument method rbNew.

/*
     * call-seq:
     *  new(version = default)
     *
     * Create a new document with +version+ (defaults to "1.0")
     */
@JRubyMethod(name = "new", meta = true, rest = true, required = 0)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    XmlDocument xmlDocument = null;
    try {
        Document docNode = createNewDocument();
        if ("Nokogiri::HTML::Document".equals(((RubyClass) klazz).getName())) {
            xmlDocument = (XmlDocument) NokogiriService.HTML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
            xmlDocument.setDocumentNode(context, docNode);
        } else {
            // XML::Document and sublass
            xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
            xmlDocument.setDocumentNode(context, docNode);
        }
    } catch (Exception ex) {
        throw context.getRuntime().newRuntimeError("couldn't create document: " + ex.toString());
    }
    RuntimeHelpers.invoke(context, xmlDocument, "initialize", args);
    return xmlDocument;
}
Also used : Document(org.w3c.dom.Document) CanonicalizationException(nokogiri.internals.c14n.CanonicalizationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 13 with JRubyMethod

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

the class XmlSaxParserContext method parse_with.

@JRubyMethod
public IRubyObject parse_with(ThreadContext context, IRubyObject handlerRuby) {
    Ruby ruby = context.getRuntime();
    if (!invoke(context, handlerRuby, "respond_to?", ruby.newSymbol("document")).isTrue()) {
        String msg = "argument must respond_to document";
        throw ruby.newArgumentError(msg);
    }
    handler = new NokogiriHandler(ruby, handlerRuby);
    preParse(context, handlerRuby, handler);
    setContentHandler(handler);
    setErrorHandler(handler);
    try {
        setProperty("http://xml.org/sax/properties/lexical-handler", handler);
    } catch (Exception ex) {
        throw ruby.newRuntimeError("Problem while creating XML SAX Parser: " + ex.toString());
    }
    try {
        try {
            do_parse();
        } catch (SAXParseException spe) {
            // A bad document (<foo><bar></foo>) should call the
            // error handler instead of raising a SAX exception.
            // However, an EMPTY document should raise a
            // RuntimeError.  This is a bit kludgy, but AFAIK SAX
            // doesn't distinguish between empty and bad whereas
            // Nokogiri does.
            String message = spe.getMessage();
            if ("Premature end of file.".matches(message) && stringDataSize < 1) {
                throw ruby.newRuntimeError("couldn't parse document: " + message);
            } else {
                handler.error(spe);
            }
        }
    } catch (SAXException se) {
        throw RaiseException.createNativeRaiseException(ruby, se);
    } catch (IOException ioe) {
        throw ruby.newIOErrorFromException(ioe);
    }
    postParse(context, handlerRuby, handler);
    return ruby.getNil();
}
Also used : SAXParseException(org.xml.sax.SAXParseException) NokogiriHandler(nokogiri.internals.NokogiriHandler) IOException(java.io.IOException) Ruby(org.jruby.Ruby) RaiseException(org.jruby.exceptions.RaiseException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 14 with JRubyMethod

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

the class XmlNode method element_children.

@JRubyMethod(name = { "element_children", "elements" })
public IRubyObject element_children(ThreadContext context) {
    List<Node> elementNodes = new ArrayList<Node>();
    addElements(node, elementNodes, false);
    if (elementNodes.size() == 0)
        return XmlNodeSet.newEmptyNodeSet(context);
    RubyArray array = NokogiriHelpers.nodeArrayToRubyArray(context.getRuntime(), elementNodes.toArray(new Node[0]));
    XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, array);
    return xmlNodeSet;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) ArrayList(java.util.ArrayList) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 15 with JRubyMethod

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

the class XmlNode method line.

@JRubyMethod
public IRubyObject line(ThreadContext context) {
    Node root = getOwnerDocument();
    int[] counter = new int[1];
    count(root, counter);
    return RubyFixnum.newFixnum(context.getRuntime(), counter[0] + 1);
}
Also used : Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)304 Ruby (org.jruby.Ruby)157 RubyString (org.jruby.RubyString)83 IRubyObject (org.jruby.runtime.builtin.IRubyObject)77 IOException (java.io.IOException)49 RubyArray (org.jruby.RubyArray)34 Node (org.w3c.dom.Node)30 RaiseException (org.jruby.exceptions.RaiseException)27 BigInteger (java.math.BigInteger)24 GeneralSecurityException (java.security.GeneralSecurityException)24 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)24 Document (org.w3c.dom.Document)20 ByteList (org.jruby.util.ByteList)19 RubyClass (org.jruby.RubyClass)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 RubyFloat (org.jruby.RubyFloat)13 JRubyClass (org.jruby.anno.JRubyClass)13 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)12 RubyFixnum (org.jruby.RubyFixnum)12 Element (org.w3c.dom.Element)12