Search in sources :

Example 6 with RubyArray

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

the class XsltStylesheet method ensureDocumentHasNoError.

private static void ensureDocumentHasNoError(ThreadContext context, XmlDocument xmlDoc) {
    Ruby runtime = context.getRuntime();
    RubyArray errors_of_xmlDoc = (RubyArray) xmlDoc.getInstanceVariable("@errors");
    if (!errors_of_xmlDoc.isEmpty()) {
        throw runtime.newRuntimeError(errors_of_xmlDoc.first().asString().asJavaString());
    }
}
Also used : RubyArray(org.jruby.RubyArray) Ruby(org.jruby.Ruby)

Example 7 with RubyArray

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

the class XmlDocument method canonicalize.

/* call-seq:
     *  doc.canonicalize(mode=XML_C14N_1_0,inclusive_namespaces=nil,with_comments=false)
     *  doc.canonicalize { |obj, parent| ... }
     *
     * Canonicalize a document and return the results.  Takes an optional block
     * that takes two parameters: the +obj+ and that node's +parent+.
     * The  +obj+ will be either a Nokogiri::XML::Node, or a Nokogiri::XML::Namespace
     * The block must return a non-nil, non-false value if the +obj+ passed in
     * should be included in the canonicalized document.
     */
@JRubyMethod(optional = 3)
public IRubyObject canonicalize(ThreadContext context, IRubyObject[] args, Block block) {
    Integer mode = 0;
    String inclusive_namespace = null;
    Boolean with_comments = false;
    if (args.length > 0 && !(args[0].isNil())) {
        mode = RubyFixnum.fix2int(args[0]);
    }
    if (args.length > 1) {
        if (!args[1].isNil() && !(args[1] instanceof List)) {
            throw context.getRuntime().newTypeError("Expected array");
        }
        if (!args[1].isNil()) {
            inclusive_namespace = (String) ((RubyArray) args[1]).join(context, context.getRuntime().newString(" ")).asString().asJavaString();
        }
    }
    if (args.length > 2) {
        with_comments = args[2].isTrue();
    }
    String algorithmURI = null;
    switch(mode) {
        case // XML_C14N_1_0
        0:
            if (with_comments)
                algorithmURI = Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
            else
                algorithmURI = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
            break;
        case // XML_C14N_EXCLUSIVE_1_0
        1:
            if (with_comments)
                algorithmURI = Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS;
            else
                algorithmURI = Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;
            break;
        case // XML_C14N_1_1 = 2
        2:
            if (with_comments)
                algorithmURI = Canonicalizer.ALGO_ID_C14N11_WITH_COMMENTS;
            else
                algorithmURI = Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS;
    }
    try {
        Canonicalizer canonicalizer = Canonicalizer.getInstance(algorithmURI);
        XmlNode startingNode = getStartingNode(block);
        byte[] result;
        CanonicalFilter filter = new CanonicalFilter(context, block);
        if (inclusive_namespace == null) {
            result = canonicalizer.canonicalizeSubtree(startingNode.getNode(), filter);
        } else {
            result = canonicalizer.canonicalizeSubtree(startingNode.getNode(), inclusive_namespace, filter);
        }
        String resultString = new String(result, "UTF-8");
        return stringOrNil(context.getRuntime(), resultString);
    } catch (CanonicalizationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return context.getRuntime().getNil();
}
Also used : RubyArray(org.jruby.RubyArray) CanonicalizationException(nokogiri.internals.c14n.CanonicalizationException) CanonicalFilter(nokogiri.internals.c14n.CanonicalFilter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NodeList(org.w3c.dom.NodeList) List(java.util.List) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) Canonicalizer(nokogiri.internals.c14n.Canonicalizer) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 8 with RubyArray

use of org.jruby.RubyArray 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 9 with RubyArray

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

the class XmlElement method accept.

@Override
public void accept(ThreadContext context, SaveContextVisitor visitor) {
    visitor.enter((Element) node);
    XmlNodeSet xmlNodeSet = (XmlNodeSet) children(context);
    if (xmlNodeSet.length() > 0) {
        RubyArray array = (RubyArray) xmlNodeSet.to_a(context);
        for (int i = 0; i < array.getLength(); i++) {
            Object item = array.get(i);
            if (item instanceof XmlNode) {
                XmlNode cur = (XmlNode) item;
                cur.accept(context, visitor);
            } else if (item instanceof XmlNamespace) {
                XmlNamespace cur = (XmlNamespace) item;
                cur.accept(context, visitor);
            }
        }
    }
    visitor.leave((Element) node);
}
Also used : RubyArray(org.jruby.RubyArray)

Example 10 with RubyArray

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

the class XmlNode method accept.

// Users might extend XmlNode. This method works for such a case.
public void accept(ThreadContext context, SaveContextVisitor visitor) {
    visitor.enter(node);
    XmlNodeSet xmlNodeSet = (XmlNodeSet) children(context);
    if (xmlNodeSet.length() > 0) {
        RubyArray array = (RubyArray) xmlNodeSet.to_a(context);
        for (int i = 0; i < array.getLength(); i++) {
            Object item = array.get(i);
            if (item instanceof XmlNode) {
                XmlNode cur = (XmlNode) item;
                cur.accept(context, visitor);
            } else if (item instanceof XmlNamespace) {
                XmlNamespace cur = (XmlNamespace) item;
                cur.accept(context, visitor);
            }
        }
    }
    visitor.leave(node);
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) RubyObject(org.jruby.RubyObject) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Aggregations

RubyArray (org.jruby.RubyArray)65 JRubyMethod (org.jruby.anno.JRubyMethod)34 Ruby (org.jruby.Ruby)26 IRubyObject (org.jruby.runtime.builtin.IRubyObject)26 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)13 RubyString (org.jruby.RubyString)13 IOException (java.io.IOException)11 RaiseException (org.jruby.exceptions.RaiseException)10 ArrayList (java.util.ArrayList)8 X509AuxCertificate (org.jruby.ext.openssl.x509store.X509AuxCertificate)8 RubyClass (org.jruby.RubyClass)6 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)5 RubyFixnum (org.jruby.RubyFixnum)5 ThreadContext (org.jruby.runtime.ThreadContext)5 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)4 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)4 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)4 ASN1String (org.bouncycastle.asn1.ASN1String)4 OperatorCreationException (org.bouncycastle.operator.OperatorCreationException)4 RubyModule (org.jruby.RubyModule)4