Permalink
Browse files

Adds impl for canonicalize with comment option. Pure Java

  • Loading branch information...
1 parent 1ca0a1e commit d05f37fd0212f2179fb89708f477142aa225caa3 @yokolet yokolet committed Nov 18, 2011
@@ -523,10 +523,27 @@ public IRubyObject toJavaDocument(ThreadContext context) {
return JavaUtil.convertJavaToUsableRubyObject(context.getRuntime(), (org.w3c.dom.Document)node);
}
+ /* 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 canonicalOpts = 1;
+ int mode = 0;
+ // todo: args[1] inclusive_namespace thing
+ if (args.length == 3) {
+ mode = (Integer)(args[0].isNil() ? 0 : args[0].toJava(Integer.class));
+ canonicalOpts = args[2].isTrue() ? (canonicalOpts | 4) : canonicalOpts;
+ }
// 38 = NO_DECL | NO_EMPTY | AS_XML
- SaveContextVisitor visitor = new SaveContextVisitor(38, null, "UTF-8", false, false, true);
+ SaveContextVisitor visitor = new SaveContextVisitor(38, null, "UTF-8", false, false, canonicalOpts);
accept(context, visitor);
Ruby runtime = context.getRuntime();
IRubyObject result = runtime.getTrue();
@@ -1026,7 +1026,7 @@ public IRubyObject native_write_to(ThreadContext context,
SaveContextVisitor visitor =
new SaveContextVisitor((Integer)options.toJava(Integer.class), rubyStringToString(indentString), encString,
- isHtmlDoc(context), isFragment(), false);
+ isHtmlDoc(context), isFragment(), 0);
accept(context, visitor);
IRubyObject rubyString = stringOrNil(context.getRuntime(), visitor.toString());
RuntimeHelpers.invoke(context, io, "write", rubyString);
@@ -72,7 +72,7 @@
private Stack<String> indentation;
private String encoding, indentString;
private boolean format, noDecl, noEmpty, noXhtml, asXhtml, asXml, asHtml, asBuilder, htmlDoc, fragment;
- private boolean canonical;
+ private boolean canonical, incl_ns, with_comments;
private List<Node> c14nNodeList;
/*
* U can't touch this.
@@ -89,14 +89,17 @@
public static final int AS_XML = 32;
public static final int AS_HTML = 64;
public static final int AS_BUILDER = 128;
+
+ public static final int CANONICAL = 1;
+ public static final int INCL_NS = 2;
+ public static final int WITH_COMMENTS = 4;
- public SaveContextVisitor(int options, String indent, String encoding, boolean htmlDoc, boolean fragment, boolean canonical) {
+ public SaveContextVisitor(int options, String indent, String encoding, boolean htmlDoc, boolean fragment, int canonicalOpts) {
buffer = new StringBuffer();
this.encoding = encoding;
indentation = new Stack<String>(); indentation.push("");
this.htmlDoc = htmlDoc;
this.fragment = fragment;
- this.canonical = canonical;
c14nNodeList = new ArrayList<Node>();
format = (options & FORMAT) == FORMAT;
@@ -107,6 +110,11 @@ public SaveContextVisitor(int options, String indent, String encoding, boolean h
asXml = (options & AS_XML) == AS_XML;
asHtml = (options & AS_HTML) == AS_HTML;
asBuilder = (options & AS_BUILDER) == AS_BUILDER;
+
+ canonical = (canonicalOpts & CANONICAL) == CANONICAL;
+ incl_ns = (canonicalOpts & INCL_NS) == INCL_NS;
+ with_comments = (canonicalOpts & WITH_COMMENTS) == WITH_COMMENTS;
+
if ((format && indent == null) || (format && indent.length() == 0)) indent = " "; // default, two spaces
if ((!format && indent != null) && indent.length() > 0) format = true;
if ((asBuilder && indent == null) || (asBuilder && indent.length() == 0)) indent = " "; // default, two spaces
@@ -291,7 +299,7 @@ public void leave(CDATASection cdata) {
public boolean enter(Comment comment) {
if (canonical) {
c14nNodeList.add(comment);
- return true;
+ if (!with_comments) return true;
}
buffer.append("<!--");
buffer.append(comment.getData());

0 comments on commit d05f37f

Please sign in to comment.