• Main Page
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List

XMLDocReader.cs

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // XMLDocReader.cs
00006 //
00007 // Copyright (c) 2010, 2014 Martin Fuchs <martin-fuchs@gmx.net>
00008 //
00009 
00010 
00011 /*
00012 
00013  All rights reserved.
00014 
00015  Redistribution and use in source and binary forms, with or without
00016  modification, are permitted provided that the following conditions are met:
00017 
00018  * Redistributions of source code must retain the above copyright
00019     notice, this list of conditions and the following disclaimer.
00020  * Redistributions in binary form must reproduce the above copyright
00021     notice, this list of conditions and the following disclaimer in
00022     the documentation and/or other materials provided with the
00023     distribution.
00024 
00025  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  POSSIBILITY OF SUCH DAMAGE.
00036 
00037 */
00038 
00039 using System.IO;
00040 using System.Xml;
00041 using System.Text;
00042 
00043 namespace XMLStorage
00044 {
00045 
00046 /**
00047  * XMLDocReader is the XML file reader of XMLStorage.
00048  */
00049 class XMLDocReader
00050 {
00051     public XMLDocReader(XMLNode node)
00052     {
00053         _pos = new XMLPos(node);
00054         _lastTag = TAG_NONE;
00055     }
00056 
00057      /// read XML stream into XML tree below _pos
00058     public void read(TextReader reader, XmlReaderSettings xmlSettings=null)
00059     {
00060          // read XML data
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          // process last data pieces
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;   // enum
00127 
00128 
00129     public void startElement(string qName, XmlReader reader)
00130     {
00131          // search for content end leaving only whitespaces for _end_leading
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) {  // no children in last node?
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                 // else TAG_NONE -> don't store white space in root node
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         //XMLNode node = new XMLNode(XMLHelper.String_from_XML_Char(qName), leading);
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              // search for content end leaving only whitespaces for leading
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) // no children in current node?
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          // encode _content compatible to XMLNode.setContent()
00204         _content += XMLHelper.encodeXMLString(text);
00205 
00206         if (_inCDATASection)
00207             _isContentCDATA = true;
00208     }
00209 
00210 
00211      // CDATA Handling
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 } // namespace XMLStorage

Generated on Tue Oct 28 2014 00:05:07 for XMLStorage/C# by  doxygen 1.7.2