New test.
[mono.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Xml / QualifiedName.cs
1 //
2 // Microsoft.Web.Services.Xml.QualifiedName.cs
3 //
4 // Author: Todd Berman <tberman@gentoo.org>
5 //
6 // (C) 2003 Todd Berman
7
8 using System;
9 using System.Xml;
10
11 namespace Microsoft.Web.Services.Xml
12 {
13         public class QualifiedName : XmlQualifiedName
14         {
15
16                 private string _prefix;
17
18                 public QualifiedName (string prefix, string name, string namespaceURI) : base (name, namespaceURI)
19                 {
20                         if(prefix == null) {
21                                 throw new ArgumentNullException ("prefix");
22                         }
23                         _prefix = prefix;
24                 }
25
26                 public static QualifiedName FromString (string value, XmlNode node)
27                 {
28                         if(node == null) {
29                                 throw new ArgumentNullException ("node");
30                         }
31
32                         if(value.IndexOf(':') > 0) {
33                                 string[] strings = value.Split(':');
34
35                                 if(strings.Length != 2 || strings[0].Length == 0 || strings[1].Length == 0) {
36                                         throw new FormatException ("xml_ImproperQName");
37                                 }
38                                 
39                                 string nspace = node.GetNamespaceOfPrefix (strings[0]);
40
41                                 if(nspace == null) {
42                                         throw new FormatException ("xml_CouldNotResolveNSPrefix");
43                                 }
44                                 
45                                 if(nspace.Length == 0) {
46                                         throw new FormatException ("xml_CouldNotResolveNSPrefix");
47                                 }
48
49                                 return new QualifiedName (strings[0], strings[1], nspace);
50                         }
51                         
52                         throw new FormatException ("xml_ImproperQName");
53                 }
54
55                 public XmlAttribute GetNamespaceDecl (XmlDocument document)
56                 {
57                         if(document == null) {
58                                 throw new ArgumentNullException ("document");
59                         }
60                         XmlAttribute attrib = document.CreateAttribute("xmlns",
61                                                                        _prefix,
62                                                                        "http://www.w3.org/2000/xmlns");
63
64                         attrib.Value = Namespace;
65                         return attrib;
66                 }
67
68                 public void GetQualifiedName (XmlDocument document, XmlElement element)
69                 {
70                         if(document == null) {
71                                 throw new ArgumentNullException ("document");
72                         }
73                         if(element == null) {
74                                 throw new ArgumentNullException ("element");
75                         }
76                         element.InnerText = Value;
77                         if(Namespace != element.NamespaceURI) {
78                                 element.Attributes.Append(GetNamespaceDecl(document));
79                         }
80                 }
81
82                 public string Prefix {
83                         get { return _prefix; }
84                 }
85
86                 public string Value {
87                         get { return _prefix + ":" + Name; }
88                 }
89         }
90 }