Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 using System.IO;
00040 using System.Xml;
00041 using System.Text;
00042
00043 namespace XMLStorage
00044 {
00045
00046
00047
00048
00049 class XMLDocReader
00050 {
00051 public XMLDocReader(XMLNode node)
00052 {
00053 _pos = new XMLPos(node);
00054 _lastTag = TAG_NONE;
00055 }
00056
00057
00058 public void read(TextReader reader, XmlReaderSettings xmlSettings=null)
00059 {
00060
00061 _parser = XmlReader.Create(reader, xmlSettings);
00062
00063 XMLNode.Children children = _pos.cur().getChildren();
00064
00065 while(_parser.Read())
00066 {
00067 switch(_parser.NodeType)
00068 {
00069 case XmlNodeType.Element:
00070 {
00071 bool emptyElement = _parser.IsEmptyElement;
00072 startElement(_parser.Name, _parser);
00073
00074 if (emptyElement)
00075 endElement(_parser.Name);
00076 }
00077 break;
00078
00079 case XmlNodeType.EndElement:
00080 endElement(_parser.Name);
00081 break;
00082
00083 case XmlNodeType.Text:
00084 characters(_parser.Value);
00085 break;
00086
00087 case XmlNodeType.CDATA:
00088 characters(_parser.Value);
00089 break;
00090
00091 case XmlNodeType.Comment:
00092 characters(_parser.Value);
00093 break;
00094
00095 case XmlNodeType.XmlDeclaration:
00096 break;
00097
00098 case XmlNodeType.ProcessingInstruction:
00099 processingInstruction(_parser.Name, _parser.Value);
00100 break;
00101
00102 case XmlNodeType.DocumentType:
00103 break;
00104 }
00105 }
00106
00107
00108 if (children.Count == 0)
00109 _pos.cur()._trailing += _content;
00110 else
00111 children.Last.Value._trailing += _content;
00112
00113 _content = "";
00114 _isContentCDATA = false;
00115 }
00116
00117 private XMLPos _pos;
00118 private XmlReader _parser;
00119
00120 private string _content = "";
00121 private StringBuilder _instructions = new StringBuilder();
00122
00123 private static int TAG_NONE = 0;
00124 private static int TAG_START = 1;
00125 private static int TAG_END = 2;
00126 private int _lastTag;
00127
00128
00129 public void startElement(string qName, XmlReader reader)
00130 {
00131
00132 int i = _content.Length;
00133 for (; i>0; i--) {
00134 char ch = _content[i-1];
00135 if (!char.IsWhiteSpace(ch))
00136 break;
00137 }
00138
00139 if (i > 0)
00140 if (_pos.cur().getChildren().Count == 0) {
00141 if (_lastTag == TAG_START)
00142 _pos.cur()._content += _content.Substring(0, i);
00143 else if (_lastTag == TAG_END)
00144 _pos.cur()._trailing += _content.Substring(0, i);
00145
00146 } else
00147 _pos.cur().getChildren().Last.Value._trailing += _content.Substring(0, i);
00148
00149 string leading = "";
00150
00151 if (i < _content.Length)
00152 leading = _content.Substring(i);
00153
00154 XMLNode node = new XMLNode(qName, leading);
00155
00156
00157 _pos.addDown(node);
00158
00159 node.setAttributes(reader);
00160
00161 _lastTag = TAG_START;
00162 _content = "";
00163 _isContentCDATA = false;
00164 }
00165
00166 public void endElement(string qName)
00167 {
00168 int i;
00169
00170 if (_isContentCDATA)
00171 i = _content.Length;
00172 else {
00173
00174 i = _content.Length;
00175 for (; i>0; i--) {
00176 char ch = _content[i-1];
00177 if (!char.IsWhiteSpace(ch))
00178 break;
00179 }
00180 }
00181
00182 if (i > 0)
00183 if (_pos.cur().getChildren().Count == 0)
00184 _pos.cur()._content += _content.Substring(0, i);
00185 else
00186 if (_lastTag == TAG_START)
00187 _pos.cur()._content += _content.Substring(0, i);
00188 else
00189 _pos.cur().getChildren().Last.Value._trailing += _content.Substring(0, i);
00190
00191 if (i < _content.Length)
00192 _pos.cur()._endLeading = _content.Substring(i);
00193
00194 _pos.back();
00195
00196 _lastTag = TAG_END;
00197 _content = "";
00198 _isContentCDATA = false;
00199 }
00200
00201 public void characters(string text)
00202 {
00203
00204 _content += XMLHelper.encodeXMLString(text);
00205
00206 if (_inCDATASection)
00207 _isContentCDATA = true;
00208 }
00209
00210
00211
00212
00213 private bool _inCDATASection = false;
00214 private bool _isContentCDATA = false;
00215
00216 public void startCDATA()
00217 {
00218 _inCDATASection = true;
00219 }
00220
00221 public void endCDATA()
00222 {
00223 _inCDATASection = false;
00224 }
00225
00226
00227 public void processingInstruction(string target, string data)
00228 {
00229 _instructions.Append("<?").Append(target).Append(" ").Append(data).Append("?>");
00230 }
00231
00232 public string getInstructions()
00233 {
00234 return _instructions.ToString();
00235 }
00236 }
00237
00238 }