ed1beb49ea4ecb98f8361980b434f9d69743dc6c
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XsltOld / PrefixQname.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="PrefixQName.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7
8 namespace System.Xml.Xsl.XsltOld {
9     using Res = System.Xml.Utils.Res;
10     using System;
11     using System.Diagnostics; 
12     using System.Xml;
13
14     internal sealed class PrefixQName {
15         public string Prefix;
16         public string Name;
17         public string Namespace;
18
19         internal void ClearPrefix() {
20             Prefix = string.Empty;
21         }
22
23         internal void SetQName(string qname) {
24             PrefixQName.ParseQualifiedName(qname, out Prefix, out Name);
25         }
26
27         //
28         // Parsing qualified names
29         //
30
31         public static void ParseQualifiedName(string qname, out string prefix, out string local) {
32             Debug.Assert(qname != null);
33             prefix = string.Empty;
34             local = string.Empty;
35
36             // parse first NCName (prefix or local name)
37             int position = ValidateNames.ParseNCName(qname);
38             if (position == 0) {
39                 throw XsltException.Create(Res.Xslt_InvalidQName, qname);
40             }
41             local = qname.Substring(0, position);
42             
43             // not at the end -> parse ':' and the second NCName (local name)
44             if (position < qname.Length) {
45                 if (qname[position] == ':') {
46                     int startLocalNamePos = ++position;
47                     prefix = local;
48                     int len = ValidateNames.ParseNCName(qname, position);
49                     position += len;
50                     if (len == 0) {
51                         throw XsltException.Create(Res.Xslt_InvalidQName, qname);
52                     }
53                     local = qname.Substring(startLocalNamePos, len);
54                 }
55
56                 // still not at the end -> error
57                 if (position < qname.Length) {
58                     throw XsltException.Create(Res.Xslt_InvalidQName, qname);
59                 }
60             }
61         }
62
63         public static bool ValidatePrefix(string prefix) {
64             if (prefix.Length == 0) {
65                 return false;
66             }
67             int endPos = ValidateNames.ParseNCName(prefix, 0);
68             return endPos == prefix.Length;
69         }
70     }
71 }