Search in sources :

Example 1 with Canonicalizer

use of nokogiri.internals.c14n.Canonicalizer in project nokogiri by sparklemotion.

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) {
    int 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 = ((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 : 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 2 with Canonicalizer

use of nokogiri.internals.c14n.Canonicalizer 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

UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 List (java.util.List)2 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)2 CanonicalFilter (nokogiri.internals.c14n.CanonicalFilter)2 CanonicalizationException (nokogiri.internals.c14n.CanonicalizationException)2 Canonicalizer (nokogiri.internals.c14n.Canonicalizer)2 JRubyMethod (org.jruby.anno.JRubyMethod)2 NodeList (org.w3c.dom.NodeList)2 RubyArray (org.jruby.RubyArray)1