SANTUARIO-521 - Remove reset property of Canonicalizers


git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1873006 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java b/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java
index 45c6542..77be9e8 100644
--- a/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java
+++ b/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java
@@ -105,7 +105,6 @@
                 canonicalizerHash.get(algorithmURI);
 
             canonicalizerSpi = implementingClass.newInstance();
-            canonicalizerSpi.reset = true;
         } catch (Exception e) {
             Object[] exArgs = { algorithmURI };
             throw new InvalidCanonicalizerException(
@@ -212,24 +211,6 @@
     }
 
     /**
-     * Method getURI
-     *
-     * @return the URI defined for this c14n instance.
-     */
-    public final String getURI() {
-        return canonicalizerSpi.engineGetURI();
-    }
-
-    /**
-     * Method getIncludeComments
-     *
-     * @return true if the c14n respect the comments.
-     */
-    public boolean getIncludeComments() {
-        return canonicalizerSpi.engineGetIncludeComments();
-    }
-
-    /**
      * This method tries to canonicalize the given bytes. It's possible to even
      * canonicalize non-wellformed sequences if they are well-formed after being
      * wrapped with a <CODE>&gt;a&lt;...&gt;/a&lt;</CODE>.
@@ -320,22 +301,6 @@
         canonicalizerSpi.setWriter(os);
     }
 
-    /**
-     * Returns the name of the implementing {@link CanonicalizerSpi} class
-     *
-     * @return the name of the implementing {@link CanonicalizerSpi} class
-     */
-    public String getImplementingCanonicalizerClass() {
-        return canonicalizerSpi.getClass().getName();
-    }
-
-    /**
-     * Set the canonicalizer behaviour to not reset.
-     */
-    public void notReset() {
-        canonicalizerSpi.reset = false;
-    }
-
     public boolean isSecureValidation() {
         return secureValidation;
     }
diff --git a/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java b/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java
index d2d784c..40fb072 100644
--- a/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java
+++ b/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java
@@ -32,8 +32,6 @@
  */
 public abstract class CanonicalizerSpi {
 
-    /** Reset the writer after a c14n */
-    protected boolean reset = false;
     protected boolean secureValidation;
 
     /**
diff --git a/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java b/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
index 11e0cac..cb0bf9b 100644
--- a/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
+++ b/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
@@ -61,7 +61,10 @@
     public static final String XMLNS_URI = Constants.NamespaceSpecNS;
     public static final String XML_LANG_URI = Constants.XML_LANG_SPACE_SpecNS;
 
-    protected static final AttrCompare COMPARE = new AttrCompare();
+    protected static final AttrCompare COMPARE = new AttrCompare();     // thread-safe
+    protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
+    protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
+    protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
 
     // Make sure you clone the following mutable arrays before passing to
     // potentially untrusted objects such as OutputStreams.
@@ -79,20 +82,11 @@
     private static final byte[] AMP = {'&','a','m','p',';'};
     private static final byte[] EQUALS_STR = {'=','\"'};
 
-    protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
-    protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
-    protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
-
     private List<NodeFilter> nodeFilter;
 
     private boolean includeComments;
     private Set<Node> xpathNodeSet;
 
-    /**
-     * The node to be skipped/excluded from the DOM tree
-     * in subtree canonicalizations.
-     */
-    private Node excludeNode;
     private OutputStream writer = new ByteArrayOutputStream();
 
    /**
@@ -192,7 +186,6 @@
      */
     protected byte[] engineCanonicalizeSubTree(Node rootNode, Node excludeNode)
         throws CanonicalizationException {
-        this.excludeNode = excludeNode;
         try {
             NameSpaceSymbTable ns = new NameSpaceSymbTable();
             int nodeLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
@@ -201,23 +194,15 @@
                 getParentNameSpaces((Element)rootNode, ns);
                 nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
             }
-            this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel);
+            this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel, excludeNode);
             this.writer.flush();
             if (this.writer instanceof ByteArrayOutputStream) {
                 byte[] result = ((ByteArrayOutputStream)this.writer).toByteArray();
-                if (reset) {
-                    ((ByteArrayOutputStream)this.writer).reset();
-                } else {
-                    this.writer.close();
-                }
+                this.writer.close();
                 return result;
             } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
                 byte[] result = ((UnsyncByteArrayOutputStream)this.writer).toByteArray();
-                if (reset) {
-                    ((UnsyncByteArrayOutputStream)this.writer).reset();
-                } else {
-                    this.writer.close();
-                }
+                this.writer.close();
                 return result;
             } else {
                 this.writer.close();
@@ -238,11 +223,14 @@
      * @param currentNode
      * @param ns
      * @param endnode
+     * @param documentLevel
+     * @param excludeNode
      * @throws CanonicalizationException
      * @throws IOException
      */
-    protected final void canonicalizeSubTree(
-        Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel
+    private void canonicalizeSubTree(
+        Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel,
+        Node excludeNode
     ) throws CanonicalizationException, IOException {
         if (currentNode == null || isVisibleInt(currentNode) == -1) {
             return;
@@ -250,8 +238,6 @@
         Node sibling = null;
         Node parentNode = null;
         final OutputStream writer = this.writer;
-        final Node excludeNode = this.excludeNode;
-        final boolean includeComments = this.includeComments;
         Map<String, byte[]> cache = new HashMap<>();
         do {
             switch (currentNode.getNodeType()) {
@@ -350,19 +336,11 @@
             this.writer.flush();
             if (this.writer instanceof ByteArrayOutputStream) {
                 byte[] sol = ((ByteArrayOutputStream)this.writer).toByteArray();
-                if (reset) {
-                    ((ByteArrayOutputStream)this.writer).reset();
-                } else {
-                    this.writer.close();
-                }
+                this.writer.close();
                 return sol;
             } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
                 byte[] result = ((UnsyncByteArrayOutputStream)this.writer).toByteArray();
-                if (reset) {
-                    ((UnsyncByteArrayOutputStream)this.writer).reset();
-                } else {
-                    this.writer.close();
-                }
+                this.writer.close();
                 return result;
             } else {
                 this.writer.close();
@@ -384,7 +362,7 @@
      * @throws CanonicalizationException
      * @throws IOException
      */
-    protected final void canonicalizeXPathNodeSet(Node currentNode, Node endnode)
+    private void canonicalizeXPathNodeSet(Node currentNode, Node endnode)
         throws CanonicalizationException, IOException {
         if (isVisibleInt(currentNode) == -1) {
             return;
@@ -418,7 +396,7 @@
                 break;
 
             case Node.COMMENT_NODE :
-                if (this.includeComments && isVisibleDO(currentNode, ns.getLevel()) == 1) {
+                if (includeComments && isVisibleDO(currentNode, ns.getLevel()) == 1) {
                     outputCommentToWriter((Comment) currentNode, writer, documentLevel);
                 }
                 break;
@@ -603,7 +581,7 @@
      * @param el
      * @param ns
      */
-    protected final void getParentNameSpaces(Element el, NameSpaceSymbTable ns)  {
+    private void getParentNameSpaces(Element el, NameSpaceSymbTable ns)  {
         Node n1 = el.getParentNode();
         if (n1 == null || Node.ELEMENT_NODE != n1.getNodeType()) {
             return;
@@ -828,7 +806,7 @@
      * @param writer writer where to write the things
      * @throws IOException
      */
-    protected static final void outputTextToWriter(
+    private static final void outputTextToWriter(
         final String text, final OutputStream writer
     ) throws IOException {
         final int length = text.length();
diff --git a/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java b/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
index 99fb947..5f2b36b 100644
--- a/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
+++ b/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
@@ -100,7 +100,6 @@
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
             canon.setWriter(baos);
-            canon.notReset();
             for (int i = 0; i < content.getLength(); i++) {
                 canon.canonicalizeSubtree(content.item(i));
             }
@@ -123,7 +122,6 @@
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
             canon.setWriter(baos);
-            canon.notReset();
             for (int i = 0; i < content.getLength(); i++) {
                 canon.canonicalizeSubtree(content.item(i));
             }
@@ -141,7 +139,6 @@
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
             canon.setWriter(baos);
-            canon.notReset();
             canon.canonicalizeSubtree(node);
             String ret = baos.toString(StandardCharsets.UTF_8.name());
             baos.reset();
@@ -159,7 +156,6 @@
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
             canon.setWriter(baos);
-            canon.notReset();
             canon.canonicalizeSubtree(node);
             return baos.toByteArray();
         }
diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
index 28a1eab..4a1b0dd 100644
--- a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
+++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
@@ -831,7 +831,6 @@
             Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         canon.setWriter(baos);
-        canon.notReset();
         canon.canonicalizeSubtree(e);
         baos.close();
         String before = baos.toString(StandardCharsets.UTF_8.name());