Search in sources :

Example 6 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlNode method children.

@JRubyMethod
public IRubyObject children(ThreadContext context) {
    XmlNodeSet xmlNodeSet = XmlNodeSet.create(context.runtime);
    NodeList nodeList = node.getChildNodes();
    if (nodeList.getLength() > 0) {
        // initializes @document from first node
        xmlNodeSet.setNodeList(nodeList);
    } else {
        // TODO this is very ripe for refactoring
        setDocumentAndDecorate(context, xmlNodeSet, doc);
    }
    return xmlNodeSet;
}
Also used : NodeList(org.w3c.dom.NodeList) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 7 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlNode method namespace_definitions.

/**
     * Return an array of XmlNamespace nodes based on the attributes
     * of this node.
     */
@JRubyMethod
public IRubyObject namespace_definitions(ThreadContext context) {
    // don't use namespace_definitions cache anymore since
    // namespaces might be deleted. Reflecting the result of 
    // namesapce removals is complicated, so the cache might not be
    // updated.
    Ruby ruby = context.getRuntime();
    RubyArray namespace_definitions = ruby.newArray();
    if (doc == null)
        return namespace_definitions;
    if (doc instanceof HtmlDocument)
        return namespace_definitions;
    List<XmlNamespace> namespaces = ((XmlDocument) doc).getNamespaceCache().get(node);
    for (XmlNamespace namespace : namespaces) {
        namespace_definitions.append(namespace);
    }
    return namespace_definitions;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 8 with JRubyMethod

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

the class XsltStylesheet method parse_stylesheet_doc.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject parse_stylesheet_doc(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    Ruby runtime = context.getRuntime();
    ensureFirstArgIsDocument(runtime, args[0]);
    XmlDocument xmlDoc = (XmlDocument) args[0];
    ensureDocumentHasNoError(context, xmlDoc);
    Document doc = ((XmlDocument) xmlDoc.dup_implementation(context, true)).getDocument();
    XsltStylesheet xslt = (XsltStylesheet) NokogiriService.XSLT_STYLESHEET_ALLOCATOR.allocate(runtime, (RubyClass) klazz);
    try {
        xslt.init(args[1], doc);
    } catch (TransformerConfigurationException ex) {
        throw runtime.newRuntimeError("could not parse xslt stylesheet");
    }
    return xslt;
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) Document(org.w3c.dom.Document) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 9 with JRubyMethod

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

the class XsltStylesheet method transform.

@JRubyMethod(rest = true, required = 1, optional = 2)
public IRubyObject transform(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.getRuntime();
    argumentTypeCheck(runtime, args[0]);
    NokogiriXsltErrorListener elistener = new NokogiriXsltErrorListener();
    DOMSource domSource = new DOMSource(((XmlDocument) args[0]).getDocument());
    DOMResult result = null;
    String stringResult = null;
    try {
        // DOMResult
        result = tryXsltTransformation(context, args, domSource, elistener);
        if (result.getNode().getFirstChild() == null) {
            // StreamResult
            stringResult = retryXsltTransformation(context, args, domSource, elistener);
        }
    } catch (TransformerConfigurationException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    } catch (TransformerException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    } catch (IOException ex) {
        throw runtime.newRuntimeError(ex.getMessage());
    }
    switch(elistener.getErrorType()) {
        case ERROR:
        case FATAL:
            throw runtime.newRuntimeError(elistener.getErrorMessage());
        case WARNING:
        default:
    }
    if (stringResult == null) {
        return createDocumentFromDomResult(context, runtime, result);
    } else {
        return createDocumentFromString(context, runtime, stringResult);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) RubyString(org.jruby.RubyString) IOException(java.io.IOException) Ruby(org.jruby.Ruby) TransformerException(javax.xml.transform.TransformerException) NokogiriXsltErrorListener(nokogiri.internals.NokogiriXsltErrorListener) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 10 with JRubyMethod

use of org.jruby.anno.JRubyMethod 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)

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