1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
//
// System.Xml.XmlElement
//
// Author:
// Jason Diamond (jason@injektilo.org)
// Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
//
// (C) 2002 Jason Diamond http://injektilo.org/
// (C) 2002 Atsushi Enomoto
//
//
// 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.Xml.XPath;
using System.IO;
using System.Text;
using Mono.Xml;
using System.Xml.Schema;
namespace System.Xml
{
public class XmlElement : XmlLinkedNode, IHasXmlChildNode
{
#region Fields
private XmlAttributeCollection attributes;
private XmlNameEntry name;
XmlLinkedNode lastLinkedChild;
private bool isNotEmpty;
IXmlSchemaInfo schemaInfo;
#endregion
#region Constructor
protected internal XmlElement (
string prefix,
string localName,
string namespaceURI,
XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false)
{
}
internal XmlElement (
string prefix,
string localName,
string namespaceURI,
XmlDocument doc,
bool atomizedNames) : base (doc)
{
if (!atomizedNames) {
if (prefix == null)
prefix = String.Empty;
if (namespaceURI == null)
namespaceURI = String.Empty;
XmlConvert.VerifyName (localName);
prefix = doc.NameTable.Add (prefix);
localName = doc.NameTable.Add (localName);
namespaceURI = doc.NameTable.Add (namespaceURI);
}
name = doc.NameCache.Add (prefix, localName, namespaceURI, true);
if(doc.DocumentType != null)
{
DTDAttListDeclaration attlist = doc.DocumentType.DTD.AttListDecls [localName];
if (attlist != null) {
for (int i = 0; i < attlist.Definitions.Count; i++) {
DTDAttributeDefinition def = attlist [i];
if (def.DefaultValue != null) {
SetAttribute (def.Name, def.DefaultValue);
Attributes [def.Name].SetDefault ();
}
}
}
}
}
#endregion
#region Properties
XmlLinkedNode IHasXmlChildNode.LastLinkedChild {
get { return lastLinkedChild; }
set { lastLinkedChild = value; }
}
public override XmlAttributeCollection Attributes {
get {
if (attributes == null)
attributes = new XmlAttributeCollection (this);
return attributes;
}
}
public virtual bool HasAttributes {
get { return attributes != null && attributes.Count > 0; }
}
public override string InnerText {
get {
return base.InnerText;
}
set {
// Why its behavior (of MS FCL) is different from InnerXml...?
if (FirstChild != null && FirstChild.NextSibling == null && FirstChild.NodeType == XmlNodeType.Text)
FirstChild.Value = value;
else {
while (FirstChild != null)
this.RemoveChild (FirstChild);
// creates new Text node
AppendChild (OwnerDocument.CreateTextNode (value), false);
}
}
}
public override string InnerXml {
get {
return base.InnerXml;
}
set {
while (FirstChild != null)
this.RemoveChild (FirstChild);
XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
BaseURI, XmlLang, XmlSpace, null);
XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
xmlReader.XmlResolver = OwnerDocument.Resolver;
do {
XmlNode n = OwnerDocument.ReadNode (xmlReader);
if(n == null) break;
AppendChild (n);
} while (true);
}
}
public bool IsEmpty {
get {
return !isNotEmpty && (FirstChild == null);
}
set {
isNotEmpty = !value;
if(value) {
while (FirstChild != null)
RemoveChild (FirstChild);
}
}
}
public override string LocalName {
get { return name.LocalName; }
}
public override string Name {
get { return name.GetPrefixedName (OwnerDocument.NameCache); }
}
public override string NamespaceURI {
get { return name.NS; }
}
public override XmlNode NextSibling {
get { return ParentNode == null || ((IHasXmlChildNode) ParentNode).LastLinkedChild == this ? null : NextLinkedSibling; }
}
public override XmlNodeType NodeType {
get {
return XmlNodeType.Element;
}
}
internal override XPathNodeType XPathNodeType {
get {
return XPathNodeType.Element;
}
}
public override XmlDocument OwnerDocument {
get {
return base.OwnerDocument;
}
}
public override string Prefix {
get { return name.Prefix; }
set {
if (IsReadOnly)
throw new ArgumentException ("This node is readonly.");
if (value == null) {
value = string.Empty;
}
if ((!String.Empty.Equals(value))&&(!XmlChar.IsNCName (value)))
throw new ArgumentException ("Specified name is not a valid NCName: " + value);
value = OwnerDocument.NameTable.Add (value);
name = OwnerDocument.NameCache.Add (value,
name.LocalName, name.NS, true);
}
}
public override XmlNode ParentNode {
get { return base.ParentNode; }
}
public override IXmlSchemaInfo SchemaInfo {
get { return schemaInfo; }
internal set { schemaInfo = value; }
}
#endregion
#region Methods
public override XmlNode CloneNode (bool deep)
{
XmlElement node = OwnerDocument.CreateElement (
name.Prefix, name.LocalName, name.NS, true);
for (int i = 0; i < Attributes.Count; i++)
node.SetAttributeNode ((XmlAttribute)
Attributes [i].CloneNode (true));
if (deep) {
for (int i = 0; i < ChildNodes.Count; i++)
node.AppendChild (ChildNodes [i].CloneNode (true), false);
}
return node;
}
public virtual string GetAttribute (string name)
{
XmlNode attributeNode = Attributes.GetNamedItem (name);
return attributeNode != null ? attributeNode.Value : String.Empty;
}
public virtual string GetAttribute (string localName, string namespaceURI)
{
XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
return attributeNode != null ? attributeNode.Value : String.Empty;
}
public virtual XmlAttribute GetAttributeNode (string name)
{
XmlNode attributeNode = Attributes.GetNamedItem (name);
return attributeNode != null ? attributeNode as XmlAttribute : null;
}
public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
{
XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
return attributeNode != null ? attributeNode as XmlAttribute : null;
}
public virtual XmlNodeList GetElementsByTagName (string name)
{
ArrayList nodeArrayList = new ArrayList ();
this.SearchDescendantElements (name, name == "*", nodeArrayList);
return new XmlNodeArrayList (nodeArrayList);
}
public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
{
ArrayList nodeArrayList = new ArrayList ();
this.SearchDescendantElements (localName, localName == "*", namespaceURI, namespaceURI == "*", nodeArrayList);
return new XmlNodeArrayList (nodeArrayList);
}
public virtual bool HasAttribute (string name)
{
XmlNode attributeNode = Attributes.GetNamedItem (name);
return attributeNode != null;
}
public virtual bool HasAttribute (string localName, string namespaceURI)
{
XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
return attributeNode != null;
}
public override void RemoveAll ()
{
// Remove all attributes and child nodes.
base.RemoveAll ();
}
public virtual void RemoveAllAttributes ()
{
if (attributes != null)
attributes.RemoveAll ();
}
public virtual void RemoveAttribute (string name)
{
if (attributes == null)
return;
XmlAttribute attr = Attributes.GetNamedItem (name) as XmlAttribute;
if (attr != null)
Attributes.Remove(attr);
}
public virtual void RemoveAttribute (string localName, string namespaceURI)
{
if (attributes == null)
return;
XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
if (attr != null)
Attributes.Remove(attr);
}
public virtual XmlNode RemoveAttributeAt (int i)
{
if (attributes == null || attributes.Count <= i)
return null;
return Attributes.RemoveAt (i);
}
public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
{
if (attributes == null)
return null;
return Attributes.Remove (oldAttr);
}
public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
{
if (attributes == null)
return null;
return Attributes.Remove (attributes [localName, namespaceURI]);
}
public virtual void SetAttribute (string name, string value)
{
XmlAttribute attr = Attributes [name];
if (attr == null) {
attr = OwnerDocument.CreateAttribute (name);
attr.Value = value;
Attributes.SetNamedItem (attr);
}
else
attr.Value = value;
}
public virtual string SetAttribute (string localName, string namespaceURI, string value)
{
XmlAttribute attr = Attributes [localName, namespaceURI];
if (attr == null) {
attr = OwnerDocument.CreateAttribute (localName, namespaceURI);
attr.Value = value;
Attributes.SetNamedItem (attr);
}
else
attr.Value = value;
return attr.Value;
}
public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
{
if (newAttr.OwnerElement != null)
throw new InvalidOperationException (
"Specified attribute is already an attribute of another element.");
XmlAttribute ret = Attributes.SetNamedItem (newAttr) as XmlAttribute;
return ret == newAttr ? null : ret;
}
public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
{
// Note that this constraint is only for this method.
// SetAttribute() allows prefixed name.
XmlConvert.VerifyNCName (localName);
return Attributes.Append (OwnerDocument.CreateAttribute (String.Empty, localName, namespaceURI, false, true));
}
public override void WriteContentTo (XmlWriter w)
{
for (XmlNode n = FirstChild; n != null; n = n.NextSibling)
n.WriteTo (w);
}
public override void WriteTo (XmlWriter w)
{
w.WriteStartElement (
name.NS == null || name.NS.Length == 0 ? String.Empty : name.Prefix,
name.LocalName,
name.NS);
if (HasAttributes)
for (int i = 0; i < Attributes.Count; i++)
Attributes [i].WriteTo (w);
WriteContentTo (w);
if (IsEmpty)
w.WriteEndElement ();
else
w.WriteFullEndElement ();
}
#endregion
}
}
|