New test.
[mono.git] / mcs / class / System.Security / Mono.Xml / XmlCanonicalizer.cs
old mode 100755 (executable)
new mode 100644 (file)
index 3aee882..111bd04
@@ -8,6 +8,27 @@
 // (C) 2003 Aleksey Sanin (aleksey@aleksey.com)
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using System;
 using System.Collections;
 using System.IO;
 using System.Text;
@@ -62,7 +83,7 @@ namespace Mono.Xml {
                {
                        xnl = nodes;
                        if (nodes == null || nodes.Count < 1)
-                           return null;
+                               return new MemoryStream ();
                        return Canonicalize (nodes[0].OwnerDocument);
                }               
 
@@ -96,7 +117,6 @@ namespace Mono.Xml {
                                WriteProcessingInstructionNode (node, visible);
                                break;
                        case XmlNodeType.EntityReference:
-//                             throw new XmlException ("Entity references should be resolved by parser", null);
                                for (int i = 0; i < node.ChildNodes.Count; i++)
                                        WriteNode (node.ChildNodes [i]);
                                break;
@@ -297,10 +317,12 @@ namespace Mono.Xml {
                        // nodes of E's attribute axis that are in the node-set. The 
                        // result of visiting the attribute axis is computed by 
                        // processing the attribute nodes in this merged attribute list.
-                       if (!exclusive && node.ParentNode != null && !IsNodeVisible (node.ParentNode.ParentNode)) {
+                       if (!exclusive && node.ParentNode != null && node.ParentNode.ParentNode != null && !IsNodeVisible (node.ParentNode.ParentNode)) {
                                // if we have whole document then the node.ParentNode.ParentNode
                                // is always visible
                                for (XmlNode cur = node.ParentNode; cur != null; cur = cur.ParentNode) {
+                                       if (cur.Attributes == null)
+                                               continue;
                                        foreach (XmlNode attribute in cur.Attributes) {
                                                // we are looking for "xml:*" attributes
                                                if (attribute.Prefix != "xml")
@@ -352,7 +374,8 @@ namespace Mono.Xml {
                {
                        // Console.WriteLine ("Debug: text node");
                        if (visible)
-                               res.Append (NormalizeString (node.Value, XmlNodeType.Text));
+                               res.Append (NormalizeString (node.Value, node.NodeType));
+//                             res.Append (NormalizeString (node.Value, XmlNodeType.Text));
                }               
 
                // Comment Nodes
@@ -447,7 +470,7 @@ namespace Mono.Xml {
                                        string p = string.Empty;
                                        if (node.Prefix == "xmlns") 
                                                p = node.LocalName;
-                                       if (p == prefix) 
+                                       if (p == prefix)
                                                return node.Value == uri;
                                }
                        }
@@ -462,16 +485,28 @@ namespace Mono.Xml {
                        return node.NamespaceURI == "http://www.w3.org/2000/xmlns/";
                }
     
+               private bool IsTextNode (XmlNodeType type)
+               {
+                       switch (type) {
+                       case XmlNodeType.Text:
+                       case XmlNodeType.CDATA:
+                       case XmlNodeType.SignificantWhitespace:
+                       case XmlNodeType.Whitespace:
+                               return true;
+                       }
+                       return false;
+               }
+
                private string NormalizeString (string input, XmlNodeType type)
                {
                        StringBuilder sb = new StringBuilder ();
                        for (int i = 0; i < input.Length; i++) {
                                char ch = input[i];
-                               if (ch == '<' && (type == XmlNodeType.Attribute || type == XmlNodeType.Text))
+                               if (ch == '<' && (type == XmlNodeType.Attribute || IsTextNode (type)))
                                        sb.Append ("&lt;");
-                               else if (ch == '>' && type == XmlNodeType.Text)
+                               else if (ch == '>' && IsTextNode (type))
                                        sb.Append ("&gt;");
-                               else if (ch == '&' && (type == XmlNodeType.Attribute || type == XmlNodeType.Text))
+                               else if (ch == '&' && (type == XmlNodeType.Attribute || IsTextNode (type)))
                                        sb.Append ("&amp;");
                                else if (ch == '\"' && type == XmlNodeType.Attribute)
                                        sb.Append ("&quot;");
@@ -479,10 +514,7 @@ namespace Mono.Xml {
                                        sb.Append ("&#x9;");
                                else if (ch == '\x0A' && type == XmlNodeType.Attribute)
                                        sb.Append ("&#xA;");
-                               else if (ch == '\x0D' && (type == XmlNodeType.Attribute ||
-                                                         type == XmlNodeType.Text ||
-                                                         type == XmlNodeType.Comment ||
-                                                         type == XmlNodeType.ProcessingInstruction))
+                               else if (ch == '\x0D')
                                        sb.Append ("&#xD;");
                                else
                                        sb.Append (ch);