1 using System;
2 using System.Collections;
3
4
5
6
7
8 namespace System.XPath
9 {
10 using NodeType = System.Dom.NodeType;
11 public enum XPathObjectType
12 {
13 nodeSet, boolean, number, string
14 }
15 public abstract class XPathObject
16 {
17 public XPathObject(XPathObjectType type_) :
18 type(type_)
19 {
20 }
21 public virtual ~XPathObject()
22 {
23 }
24 public XPathObjectType Type() const
25 {
26 return type;
27 }
28 public abstract UniquePtr<System.Dom.Node> ToDom() const;
29 private XPathObjectType type;
30 }
31 public class XPathNodeSet : XPathObject
32 {
33 public XPathNodeSet() :
34 base(XPathObjectType.nodeSet)
35 {
36 }
37 public System.Dom.Node* operator[](int index) const
38 {
39 return nodes[index];
40 }
41 public int Length() const
42 {
43 return nodes.Length();
44 }
45 public void Add(System.Dom.Node* node)
46 {
47 nodes.InternalAddNode(node);
48 }
49 public override UniquePtr<System.Dom.Node> ToDom() const
50 {
51 UniquePtr<System.Dom.Element> result(new System.Dom.Element(u"nodeset"));
52 result->SetAttribute(u"length", ToUtf32(ToString(nodes.Length())));
53 int n = nodes.Length();
54 for (int i = 0; i < n; ++i;)
55 {
56 System.Dom.Node* node = nodes[i];
57 if (node->GetNodeType() == NodeType.attributeNode)
58 {
59 UniquePtr<System.Dom.Element> element(new System.Dom.Element(u"attribute"));
60 UniquePtr<System.Dom.Node> clonedAttrNode = node->CloneNode(false);
61 UniquePtr<System.Dom.Attr> clonedAttr(cast<System.Dom.Attr*>(clonedAttrNode.Get()));
62 element->AddAttribute(Rvalue(clonedAttr));
63 result->AppendChild(UniquePtr<System.Dom.Node>(element.Release()));
64 }
65 else
66 {
67 ustring nodeName;
68 switch (node->GetNodeType())
69 {
70 case NodeType.documentNode:
71 {
72 nodeName = u"document";
73 break;
74 }
75 case NodeType.elementNode:
76 {
77 nodeName = u"element";
78 break;
79 }
80 case NodeType.textNode:
81 {
82 nodeName = u"text";
83 break;
84 }
85 case NodeType.cdataSectionNode:
86 {
87 nodeName = u"cdataSection";
88 break;
89 }
90 case NodeType.commentNode:
91 {
92 nodeName = u"comment";
93 break;
94 }
95 case NodeType.processingInstructionNode:
96 {
97 nodeName = u"processingInstruction";
98 break;
99 }
100 }
101 UniquePtr<System.Dom.Element> element(new System.Dom.Element(nodeName));
102 element->AppendChild(node->CloneNode(true));
103 result->AppendChild(UniquePtr<System.Dom.Node>(element.Release()));
104 }
105 }
106 return UniquePtr<System.Dom.Node>(result.Release());
107 }
108 private System.Dom.NodeList nodes;
109 }
110 public class XPathBoolean : XPathObject
111 {
112 public XPathBoolean(bool value_) :
113 base(XPathObjectType.boolean), value(value_)
114 {
115 }
116 public bool Value() const
117 {
118 return value;
119 }
120 public override UniquePtr<System.Dom.Node> ToDom() const
121 {
122 UniquePtr<System.Dom.Element> result(new System.Dom.Element(u"boolean"));
123 ustring val = u"true";
124 if (!value)
125 {
126 val = u"false";
127 }
128 result->SetAttribute(u"value", val);
129 return UniquePtr<System.Dom.Node>(result.Release());
130 }
131 private bool value;
132 }
133 public class XPathNumber : XPathObject
134 {
135 public XPathNumber(double value_) :
136 base(XPathObjectType.number), value(value_)
137 {
138 }
139 public double Value() const
140 {
141 return value;
142 }
143 public override UniquePtr<System.Dom.Node> ToDom() const
144 {
145 UniquePtr<System.Dom.Element> result(new System.Dom.Element(u"number"));
146 result->SetAttribute(u"value", ToUtf32(ToString(value)));
147 return UniquePtr<System.Dom.Node>(result.Release());
148 }
149 private double value;
150 }
151 public class XPathString : XPathObject
152 {
153 public XPathString(const ustring& value_) :
154 base(XPathObjectType.string), value(value_)
155 {
156 }
157 public const ustring& Value() const
158 {
159 return value;
160 }
161 public override UniquePtr<System.Dom.Node> ToDom() const
162 {
163 UniquePtr<System.Dom.Element> result(new System.Dom.Element(u"string"));
164 result->SetAttribute(u"value", value);
165 return UniquePtr<System.Dom.Node>(result.Release());
166 }
167 private ustring value;
168 }
169 }