Add MIT license to System.XML.DLL
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslCopy.cs
1 //
2 // XslCopy.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //      
8 // (C) 2003 Ben Maurer
9 // (C) 2003 Atsushi Enomoto
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Xml;
36 using System.Xml.XPath;
37 using System.Xml.Xsl;
38
39 namespace Mono.Xml.Xsl.Operations {
40         internal class XslCopy : XslCompiledElement {
41                 XslOperation children;
42                 XmlQualifiedName [] useAttributeSets;
43                 
44                 public XslCopy (Compiler c) : base (c) {}
45                 
46                 protected override void Compile (Compiler c)
47                 {
48                         if (c.Input.MoveToFirstAttribute ()) {
49                                 do {
50                                         if (c.Input.NamespaceURI == String.Empty && c.Input.LocalName != "use-attribute-sets")
51                                                 throw new XsltCompileException ("Unrecognized attribute \"" + c.Input.Name + "\" in XSLT copy element.", null, c.Input);
52                                 } while (c.Input.MoveToNextAttribute ());
53                                 c.Input.MoveToParent ();
54                         }
55
56                         useAttributeSets = c.ParseQNameListAttribute ("use-attribute-sets");
57                         
58                         if (!c.Input.MoveToFirstChild ()) return;
59                         children = c.CompileTemplateContent();
60                         c.Input.MoveToParent ();
61                 }
62
63                 public override void Evaluate (XslTransformProcessor p)
64                 {
65                         switch (p.CurrentNode.NodeType)
66                         {
67                         case XPathNodeType.Root:
68                                 if (p.Out.CanProcessAttributes && useAttributeSets != null)
69                                         foreach (XmlQualifiedName s in useAttributeSets) {
70                                                 XslAttributeSet attset = p.ResolveAttributeSet (s);
71                                                 if (attset == null)
72                                                         throw new XsltException ("Attribute set was not found.", null, p.CurrentNode);
73                                                 attset.Evaluate (p);
74                                         }
75
76                                 if (children != null) children.Evaluate (p);
77                                 break;
78                         case XPathNodeType.Element:
79                                 bool isCData = p.InsideCDataElement;
80                                 p.PushElementState (p.CurrentNode.LocalName, p.CurrentNode.NamespaceURI, true);
81                                 p.Out.WriteStartElement (p.CurrentNode.Prefix, p.CurrentNode.LocalName, p.CurrentNode.NamespaceURI);
82                                 
83                                 p.TryStylesheetNamespaceOutput (null);
84                                 if (useAttributeSets != null)
85                                         foreach (XmlQualifiedName s in useAttributeSets)
86                                                 p.ResolveAttributeSet (s).Evaluate (p);
87
88                                 if (p.CurrentNode.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
89                                         do {
90                                                 p.Out.WriteNamespaceDecl (p.CurrentNode.LocalName, p.CurrentNode.Value);
91                                         } while (p.CurrentNode.MoveToNextNamespace (XPathNamespaceScope.Local));
92                                         p.CurrentNode.MoveToParent ();
93                                 }
94                         
95                                 if (children != null) children.Evaluate (p);
96
97                                 p.Out.WriteFullEndElement ();
98                                 p.PopCDataState (isCData);
99                                 break;
100                         case XPathNodeType.Attribute:
101                                 p.Out.WriteAttributeString (p.CurrentNode.Prefix, p.CurrentNode.LocalName, p.CurrentNode.NamespaceURI, p.CurrentNode.Value);
102                                 break;
103                         
104                         case XPathNodeType.SignificantWhitespace:
105                         case XPathNodeType.Whitespace:
106                                 bool cdata = p.Out.InsideCDataSection;
107                                 p.Out.InsideCDataSection = false;
108                                 p.Out.WriteString (p.CurrentNode.Value);
109                                 p.Out.InsideCDataSection = cdata;
110                                 break;
111                         case XPathNodeType.Text:
112                                 p.Out.WriteString (p.CurrentNode.Value);
113                                 break;
114                         
115                         case XPathNodeType.Comment:
116                                 p.Out.WriteComment (p.CurrentNode.Value);
117                                 break;
118                         
119                         case XPathNodeType.ProcessingInstruction:
120                                 p.Out.WriteProcessingInstruction (p.CurrentNode.Name, p.CurrentNode.Value);
121                                 break;
122
123                         case XPathNodeType.Namespace:
124                                 p.Out.WriteNamespaceDecl (p.CurrentNode.Name, p.CurrentNode.Value);
125                                 break;
126
127                         default:
128 //                              Console.WriteLine ("unhandled node type {0}", p.CurrentNode.NodeType);
129                                 break;
130                         }
131                 }
132         }
133 }