Spinning Topp Logo BlackTopp Studios
inc
xmlstring.cpp
Go to the documentation of this file.
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 /*
41  *
42  * Software, Files, Libraries and all other items referenced in this clause refers only
43  * to the contents of this file and associated documentation.
44  *
45  * Pugixml parser - version 1.0
46  * --------------------------------------------------------
47  * Copyright © 2006-2012, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
48  * Report bugs and download new versions at http://pugixml.org/
49  *
50  * This library is distributed under the MIT License. See notice at the end
51  * of this file.
52  *
53  * This work is based on the pugxml parser, which is:
54  * Copyright © 2003, by Kristen Wegner (kristen@tima.net)
55  */
56 #ifndef _xmlutfconversion_cpp
57 #define _xmlutfconversion_cpp
58 
59 /// @file
60 /// @brief Describe file here
61 
62 #include "XML/document.h"
63 #include "XML/xmlstring.h"
64 
65 #include "exception.h"
66 
67 namespace Mezzanine
68 {
69  namespace XML
70  {
71 
72  String GetOneTag(std::istream& stream ) // this function might be optimizes by counting characters, then copying several characters, or something like that.
73  {
74  Char8 ReadOne = 0;
75  Char8 PrevOne = 0;
76  Integer TagCount = 0;
77  bool HaveSlash = false;
78  bool InText = false;
79 
80  String OneTag;
81  while (!stream.get(ReadOne).fail() && !stream.eof()) //Read one character and if you didn't fail continue the loop
82  {
83  OneTag.push_back(ReadOne);
84 
85  if ('<' == ReadOne && !InText) //If a new tag starts
86  {
87  ++TagCount;
88  }
89 
90  if ( '"' == ReadOne && '\\' != PrevOne ) //If we find an unescaped " the we are either leaving or entering a quote
91  { InText = !InText; }
92 
93  if ( !InText && '/' == ReadOne ) //If we are not In a quote then a / means the next > will end a tag
94  { HaveSlash=true; }
95 
96  if ( '>' == ReadOne ) // if are we at a > then the amount of nested tags we are in changes
97  {
98  if (HaveSlash) // if have passed an unescaped slash then we are ending a tag
99  {
100  --TagCount;
101  if ( 0>=TagCount ) // if we are ending the last tag then we are done
102  { break; }
103  }else{
104  //++TagCount; // if the tag does not have a slash then we are going one deeper
105  }
106  HaveSlash=false;
107  }
108 
109  PrevOne=ReadOne; //Store the Previous Character to check for escapes
110  }
111 
112  return OneTag;
113 
114  }
115 
116  Document* PreParseClassFromSingleTag(const String& NameSpace, const String& ClassName, const String& OneTag)
117  {
118  Document* Doc = new Document();
119  if(!Doc->Load(OneTag.c_str()))
120  {
121  delete Doc;
122  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Could not Deserialize XML Stream which should contain:" + ClassName + "\n XML looked Like: " + OneTag + ".");
123  }
124 
125  Node InputNode = Doc->GetFirstChild();
126  if (InputNode)
127  {
128  if( String(ClassName) == String(InputNode.Name()))
129  {
130  return Doc;
131  }else{
132  delete Doc;
133  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,NameSpace + ClassName + " not next item in stream, failed to deserialize.");
134  }
135  }else{
136  delete Doc;
137  MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"No valid XML element in stream, when attempting to deserialize " + NameSpace + ClassName + ".");
138  }
139  }
140 
141  Document* PreParseClassFromSingleTag(const String& ClassName, const String& OneTag)
142  { return PreParseClassFromSingleTag("",ClassName,OneTag); }
143 
144  String EscapeXML(const String& XMLText)
145  {
146  String Results;
147  for(Whole Counter=0; Counter<XMLText.size(); ++Counter)
148  {
149  switch(XMLText.at(Counter))
150  {
151  case '"':
152  Results.append("&quot;");
153  break;
154  case '&':
155  Results.append("&amp;");
156  break;
157  case '<':
158  Results.append("&lt;");
159  break;
160  case '>':
161  Results.append("&gt;");
162  break;
163  default:
164  Results.append(1,XMLText.at(Counter));
165  }
166  }
167  return Results;
168  }
169 
170  }
171 } // /namespace Mezzanine
172 
173 #endif // Include guard
174 
175 
176 /*
177  *
178  * Software, Files, Libraries and all other items referenced in this clause refers only
179  * to the contents of this file and associated documentation.
180  *
181  * Copyright © 2006-2012 Arseny Kapoulkine
182  *
183  * Permission is hereby granted, free of charge, to any person
184  * obtaining a copy of this software and associated documentation
185  * files (the "Software"), to deal in the Software without
186  * restriction, including without limitation the rights to use,
187  * copy, modify, merge, publish, distribute, sublicense, and/or sell
188  * copies of the Software, and to permit persons to whom the
189  * Software is furnished to do so, subject to the following
190  * conditions:
191  *
192  * The above copyright notice and this permission notice shall be
193  * included in all copies or substantial portions of the Software.
194  *
195  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
196  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
197  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
198  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
199  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
200  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
202  * OTHER DEALINGS IN THE SOFTWARE.
203  */
String GetOneTag(std::istream &stream)
Gets the first tag out of the Stream and returns it as a String.
Definition: xmlstring.cpp:72
Document * PreParseClassFromSingleTag(const String &NameSpace, const String &ClassName, const String &OneTag)
Perform a basic series of checks for extracting meaning from a single xml tag.
Definition: xmlstring.cpp:116
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
A few functions to help the XML system perform conversions between UTF8 and the native wide character...
ParseResult Load(std::basic_istream< char, std::char_traits< char > > &stream, unsigned int options=ParseDefault, Encoding DocumentEncoding=EncodingAuto)
Load XML from a stream.
This implements the exception hiearchy for Mezzanine.
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
The definition of the XML::Document Class.
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
The root node of any xml hierarchy is a Document.
Definition: document.h:83
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
String EscapeXML(const String &XMLText)
Convert < > & and " in text to <, >, & and "e so text can safely be stored in XML.
Definition: xmlstring.cpp:144
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159