5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
9 // (C) 2003 Atsushi Enomoto
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
36 using System.Xml.XPath;
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)
45 c.AssertAttribute ("select");
46 select = c.CompileExpression (c.GetAttribute ("select"));
49 void CopyNode (XslTransformProcessor p, XPathNavigator nav)
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);
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);
66 if (nav.MoveToFirstNamespace (XPathNamespaceScope.ExcludeXml))
69 if (prefix == nav.Name)
71 if (nav.Name.Length == 0 && ns.Length == 0)
73 outputter.WriteNamespaceDecl (nav.Name, nav.Value);
74 } while (nav.MoveToNextNamespace (XPathNamespaceScope.ExcludeXml));
78 if (nav.MoveToFirstAttribute())
81 outputter.WriteAttributeString (nav.Prefix, nav.LocalName, nav.NamespaceURI, nav.Value);
82 } while (nav.MoveToNextAttribute ());
86 if (nav.MoveToFirstChild ()) {
89 } while (nav.MoveToNext ());
93 if (nav.IsEmptyElement)
94 outputter.WriteEndElement ();
96 outputter.WriteFullEndElement ();
98 p.PopCDataState (isCData);
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);
106 case XPathNodeType.Attribute:
107 outputter.WriteAttributeString (nav.Prefix, nav.LocalName, nav.NamespaceURI, nav.Value);
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;
116 case XPathNodeType.Text:
117 outputter.WriteString (nav.Value);
119 case XPathNodeType.ProcessingInstruction:
120 outputter.WriteProcessingInstruction (nav.Name, nav.Value);
122 case XPathNodeType.Comment:
123 outputter.WriteComment (nav.Value);
128 public override void Evaluate (XslTransformProcessor p)
130 object o = p.Evaluate (select);
131 XPathNodeIterator itr = o as XPathNodeIterator;
133 XPathNavigator nav = o as XPathNavigator; // RTF
135 itr = nav.SelectChildren (XPathNodeType.All);
138 while (itr.MoveNext ())
139 CopyNode (p, itr.Current);
141 p.Out.WriteString (XPathFunctions.ToString (o));