Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XsltOld / BuilderInfo.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="BuilderInfo.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">[....]</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.Text;
13     using System.Collections.Generic;
14     using System.Xml;
15     using System.Xml.XPath;
16
17     internal class BuilderInfo {
18         private     string              name;
19         private     string              localName;
20         private     string              namespaceURI;
21         private     string              prefix;
22         
23         private     XmlNodeType         nodeType;
24         private     int                 depth;
25         private     bool                isEmptyTag;
26                 
27         internal    string[]            TextInfo = new string[4];
28         internal    int                 TextInfoCount = 0;        
29
30         internal    bool                search;
31         internal    HtmlElementProps    htmlProps;
32         internal    HtmlAttributeProps  htmlAttrProps;
33
34         internal BuilderInfo() {
35             Initialize(string.Empty, string.Empty, string.Empty);
36         }
37
38         internal void Initialize(string prefix, string name, string nspace) {
39             this.prefix         = prefix;
40             this.localName      = name;
41             this.namespaceURI   = nspace;
42             this.name           = null;
43             this.htmlProps      = null;
44             this.htmlAttrProps  = null;
45             this.TextInfoCount  = 0;
46         }
47
48         internal void Initialize(BuilderInfo src) {
49             this.prefix        = src.Prefix;
50             this.localName     = src.LocalName;
51             this.namespaceURI  = src.NamespaceURI;
52             this.name          = null;
53             this.depth         = src.Depth;
54             this.nodeType      = src.NodeType;
55             this.htmlProps     = src.htmlProps;
56             this.htmlAttrProps = src.htmlAttrProps;
57
58             this.TextInfoCount = 0;
59             EnsureTextInfoSize(src.TextInfoCount);
60             src.TextInfo.CopyTo(this.TextInfo, 0);
61             this.TextInfoCount = src.TextInfoCount;
62         }
63         
64         void EnsureTextInfoSize(int newSize) {
65             if (this.TextInfo.Length < newSize) {
66                 string[] newArr = new string[newSize * 2];
67                 Array.Copy(this.TextInfo, newArr, this.TextInfoCount);
68                 this.TextInfo = newArr;
69             }
70         }
71         
72         internal BuilderInfo Clone() {
73             BuilderInfo info = new BuilderInfo();
74             info.Initialize(this);
75             Debug.Assert(info.NodeType != XmlNodeType.Text || XmlCharType.Instance.IsOnlyWhitespace(info.Value));
76             return info;
77         }
78        
79         internal string Name {
80             get {
81                 if (this.name == null) {
82                     string prefix    = Prefix;
83                     string localName = LocalName;
84
85                     if (prefix != null && 0 < prefix.Length) {
86                         if (localName.Length > 0) {
87                             this.name = prefix + ":" + localName;
88                         }
89                         else {
90                             this.name = prefix;
91                         }
92                     }
93                     else {
94                         this.name = localName;
95                     }
96                 }
97                 return this.name;
98             }
99         }
100
101         internal string LocalName {
102             get { return this.localName; }
103             set { this.localName = value; }
104         }
105         internal string NamespaceURI {
106             get { return this.namespaceURI; }
107             set { this.namespaceURI = value; }
108         }
109         internal string Prefix {
110             get { return this.prefix; }
111             set { this.prefix = value; }
112         }
113
114         // The true value of this object is a list of TextInfo
115         // Value.get merges them together but discards each node's escape info
116         // Value.set clears this object, and appends the new, single string
117         internal string Value {
118             get {
119                 switch  (this.TextInfoCount) {
120                 case 0: return string.Empty;
121                 case 1: return this.TextInfo[0];
122                 default :
123                     int size = 0;
124                     for (int i = 0; i < this.TextInfoCount; i++) {
125                         string ti = this.TextInfo[i];
126                         if (ti == null) continue; // ignore disableEscaping
127                         size += ti.Length;
128                     }
129                     StringBuilder sb = new StringBuilder(size);
130                     for (int i = 0; i < this.TextInfoCount; i++) {
131                         string ti = this.TextInfo[i];
132                         if (ti == null) continue; // ignore disableEscaping
133                         sb.Append(ti);
134                     }
135                     return sb.ToString();
136                 }
137             }
138             set {
139                 this.TextInfoCount = 0;
140                 ValueAppend(value, /*disableEscaping:*/false);
141             }
142         }
143
144         internal void ValueAppend(string s, bool disableEscaping) {
145             if (s == null || s.Length == 0) {
146                 return;
147             }
148             EnsureTextInfoSize(this.TextInfoCount + (disableEscaping ? 2 : 1));
149             if (disableEscaping) {
150                 this.TextInfo[this.TextInfoCount ++] = null;
151             }
152             this.TextInfo[this.TextInfoCount++] = s;
153         }
154
155         internal XmlNodeType NodeType {
156             get { return this.nodeType; }
157             set { this.nodeType = value; }
158         }
159         internal int Depth {
160             get { return this.depth; }
161             set { this.depth = value; }
162         }
163         internal bool IsEmptyTag {
164             get { return this.isEmptyTag; }
165             set { this.isEmptyTag = value; }
166         }
167     }
168 }