New test.
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl.Operations / XslCopyOf.cs
1 //
2 // XslCopyOf.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 XslCopyOf : XslCompiledElement {
41                 XPathExpression select;
42                 public XslCopyOf (Compiler c) : base (c) {}
43                 protected override void Compile (Compiler c)
44                 {
45                         c.AssertAttribute ("select");
46                         select = c.CompileExpression (c.GetAttribute ("select"));
47                 }
48                         
49                 void CopyNode (XslTransformProcessor p, XPathNavigator nav)
50                 {
51                         Outputter outputter = p.Out;
52                         switch (nav.NodeType) {
53                         case XPathNodeType.Root:
54                                 XPathNodeIterator itr = nav.SelectChildren (XPathNodeType.All);
55                                 while (itr.MoveNext ())
56                                         CopyNode (p, itr.Current);
57                                 break;
58                                 
59                         case XPathNodeType.Element:
60                                 bool isCData = p.InsideCDataElement;
61                                 string prefix = nav.Prefix;
62                                 string ns = nav.NamespaceURI;
63                                 p.PushElementState (prefix, nav.LocalName, ns, false);
64                                 outputter.WriteStartElement (prefix, nav.LocalName, ns);
65                                 
66                                 if (nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml))
67                                 {
68                                         do {
69                                                 if (prefix == nav.Name)
70                                                         continue;
71                                                 if (nav.Name.Length == 0 && ns.Length == 0)
72                                                         continue;
73                                                 outputter.WriteNamespaceDecl (nav.Name, nav.Value);
74                                         } while (nav.MoveToNextNamespace (XPathNamespaceScope.ExcludeXml));
75                                         nav.MoveToParent ();
76                                 }
77                                 
78                                 if (nav.MoveToFirstAttribute())
79                                 {
80                                         do {
81                                                 outputter.WriteAttributeString (nav.Prefix, nav.LocalName, nav.NamespaceURI, nav.Value);
82                                         } while (nav.MoveToNextAttribute ());
83                                         nav.MoveToParent();
84                                 }
85                                 
86                                 if (nav.MoveToFirstChild ()) {
87                                         do {
88                                                 CopyNode (p, nav);
89                                         } while (nav.MoveToNext ());
90                                         nav.MoveToParent ();
91                                 }
92
93                                 if (nav.IsEmptyElement)
94                                         outputter.WriteEndElement ();
95                                 else
96                                         outputter.WriteFullEndElement ();
97
98                                 p.PopCDataState (isCData);
99                                 break;
100                                 
101                         case XPathNodeType.Namespace:
102                                 if (nav.Name != p.XPathContext.ElementPrefix &&
103                                         (p.XPathContext.ElementNamespace.Length > 0 || nav.Name.Length > 0))
104                                         outputter.WriteNamespaceDecl (nav.Name, nav.Value);
105                                 break;
106                         case XPathNodeType.Attribute:
107                                 outputter.WriteAttributeString (nav.Prefix, nav.LocalName, nav.NamespaceURI, nav.Value);
108                                 break;
109                         case XPathNodeType.Whitespace:
110                         case XPathNodeType.SignificantWhitespace:
111                                 bool cdata = outputter.InsideCDataSection;
112                                 outputter.InsideCDataSection = false;
113                                 outputter.WriteString (nav.Value);
114                                 outputter.InsideCDataSection = cdata;
115                                 break;
116                         case XPathNodeType.Text:
117                                 outputter.WriteString (nav.Value);
118                                 break;
119                         case XPathNodeType.ProcessingInstruction:
120                                 outputter.WriteProcessingInstruction (nav.Name, nav.Value);
121                                 break;
122                         case XPathNodeType.Comment:
123                                 outputter.WriteComment (nav.Value);
124                                 break;
125                         }                       
126                 }
127         
128                 public override void Evaluate (XslTransformProcessor p)
129                 {
130                         object o = p.Evaluate (select);
131                         XPathNodeIterator itr = o as XPathNodeIterator;
132                         if (itr == null) {
133                                 XPathNavigator nav = o as XPathNavigator; // RTF
134                                 if (nav != null)
135                                         itr = nav.SelectChildren (XPathNodeType.All);
136                         }
137                         if (itr != null) {
138                                 while (itr.MoveNext ())
139                                         CopyNode (p, itr.Current);
140                         } else {
141                                 p.Out.WriteString (XPathFunctions.ToString (o));
142                         }
143
144                 }
145         }
146 }