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

xs-xerces.cpp

Go to the documentation of this file.
00001 
00002  //
00003  // XML storage C++ classes version 1.5
00004  //
00005  // Copyright (c) 2006, 2008, 2010 Martin Fuchs <martin-fuchs@gmx.net>
00006  //
00007 
00008  /// \file xs-xerces.cpp
00009  /// XMLStorage code using the Xerces XML parser
00010 
00011 
00012 /*
00013 
00014   All rights reserved.
00015 
00016   Redistribution and use in source and binary forms, with or without
00017   modification, are permitted provided that the following conditions are met:
00018 
00019   * Redistributions of source code must retain the above copyright
00020     notice, this list of conditions and the following disclaimer.
00021   * Redistributions in binary form must reproduce the above copyright
00022     notice, this list of conditions and the following disclaimer in
00023     the documentation and/or other materials provided with the
00024     distribution.
00025 
00026   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036   POSSIBILITY OF SUCH DAMAGE.
00037 
00038 */
00039 
00040 #ifdef XS_USE_XERCES
00041 
00042 #ifndef XS_NO_COMMENT
00043 #define XS_NO_COMMENT   // no #pragma comment(lib, ...) statements in .lib files to enable static linking
00044 #endif
00045 
00046 #include "xmlstorage.h"
00047 
00048 
00049 #include <xercesc/sax/Locator.hpp>
00050 #include <xercesc/util/XMemory.hpp>
00051 #include <xercesc/framework/LocalFileInputSource.hpp>
00052 #include <xercesc/framework/MemBufInputSource.hpp>
00053 
00054 
00055 namespace XMLStorage {
00056 
00057 
00058 #pragma init_seg(lib)
00059 static struct XercesInit
00060 {
00061     XercesInit()
00062     {
00063         XERCES_CPP_NAMESPACE_QUALIFIER XMLPlatformUtils::Initialize();
00064     }
00065 } s_XercesInit;
00066 
00067 
00068 XMLReaderBase::XMLReaderBase(XMLNode* node, InputSource* source, bool adoptSource)
00069  :  _pos(node),
00070 #ifdef XMLNODE_LOCATION
00071     _locator(NULL),
00072 #endif
00073     _source(source),
00074     _deleteSource(adoptSource)
00075 {
00076      // Create a SAX parser object.
00077     _parser = new SAXParser;
00078 
00079     _parser->setValidationScheme(SAXParser::Val_Always);
00080     _parser->setDoNamespaces(true);
00081     _parser->setDoSchema(true);
00082     _parser->setValidationSchemaFullChecking(true);
00083 
00084      // Install the reader as the document and error handler on the parser.
00085     _parser->setDocumentHandler(this);
00086     _parser->setErrorHandler(this);
00087 
00088     _last_tag = TAG_NONE;
00089 }
00090 
00091 XMLReaderBase::~XMLReaderBase()
00092 {
00093     if (_deleteSource)
00094         delete _source;
00095 
00096     delete _parser;
00097 }
00098 
00099  /// read XML stream into XML tree below _pos
00100 void XMLReaderBase::read()
00101 {
00102     try {
00103         _parser->parse(*_source);
00104     } catch(const XERCES_CPP_NAMESPACE_QUALIFIER SAXParseException& e) {
00105         XMLError err;
00106 
00107         err._message = e.getMessage();
00108         err._systemId = e.getSystemId();
00109         err._line = e.getLineNumber();
00110         err._column = e.getColumnNumber();
00111 
00112         _errors.push_back(err);
00113     } catch(const XERCES_CPP_NAMESPACE_QUALIFIER SAXException& e) {
00114         XMLError err;
00115 
00116         err._message = e.getMessage();
00117 
00118         _errors.push_back(err);
00119     } catch(const XERCES_CPP_NAMESPACE_QUALIFIER XMLException& e) {
00120         XMLError err;
00121 
00122         err._message = e.getMessage();
00123 
00124         _errors.push_back(err);
00125     }
00126 }
00127 
00128 
00129 XercesXMLReader::XercesXMLReader(XMLNode* node, LPCTSTR path)
00130  :  XMLReaderBase(node, new XERCES_CPP_NAMESPACE_QUALIFIER LocalFileInputSource(
00131 #ifdef UNICODE
00132         path
00133 #else
00134         std::wstring(XS_String(path)).c_str()
00135 #endif
00136     ), true)
00137 {
00138 }
00139 
00140 XercesXMLReader::XercesXMLReader(XMLNode* node, const XMLByte* buffer, size_t bytes, const std::string& system_id)
00141 #ifdef UNICODE
00142  :  XMLReaderBase(node, new XERCES_CPP_NAMESPACE_QUALIFIER MemBufInputSource(buffer, bytes, system_id, true)
00143 #else
00144  :  XMLReaderBase(node, new XERCES_CPP_NAMESPACE_QUALIFIER MemBufInputSource(buffer, bytes, std::wstring(XS_String(system_id)).c_str()), true)
00145 #endif
00146 {
00147 }
00148 
00149 
00150 #ifdef XMLNODE_LOCATION
00151 
00152 XMLLocation XMLReaderBase::get_location() const
00153 {
00154     if (_locator)
00155         // only temporary: XS_String(_locator->getSystemId())
00156         return XMLLocation(_display_path, _locator->getLineNumber(), _locator->getColumnNumber());
00157 
00158     return XMLLocation(_display_path, 0, 0);
00159 }
00160 
00161 #endif
00162 
00163 
00164  /// store XML version and encoding into XML reader
00165 void XMLReaderBase::XMLDecl(const XMLCh* const versionStr, const XMLCh* const encodingStr,
00166                             const XMLCh* const standaloneStr, const XMLCh* const actualEncodingStr)
00167 {
00168     int standalone;
00169 
00170     if (standaloneStr)
00171         standalone = !_stricmp(String_from_XML_Char(standaloneStr).c_str(), "yes");
00172     else
00173         standalone = -1;
00174 
00175 #ifdef UNICODE
00176     XmlDeclHandler(std::string(String_from_XML_Char(versionStr)).c_str(),
00177                     std::string(String_from_XML_Char(encodingStr)).c_str(), standalone);
00178 #else
00179     XmlDeclHandler(String_from_XML_Char(versionStr),
00180                     String_from_XML_Char(encodingStr), standalone);
00181 #endif
00182 }
00183 
00184 void XMLReaderBase::setDocumentLocator(const Locator* const locator)
00185 {
00186 #ifdef XMLNODE_LOCATION
00187     _locator = locator;
00188 #endif
00189 }
00190 
00191  /// notifications about XML start tag
00192 void XMLReaderBase::startElement(const XMLCh* const name, AttributeList& attributes)
00193 {
00194     XMLNode::AttributeMap attribs;
00195 
00196     for(int i=attributes.getLength(); --i>=0; ) {
00197         const XMLCh* attr_name = attributes.getName(i);
00198         const XMLCh* attr_value = attributes.getValue(i);
00199 
00200         attribs[String_from_XML_Char(attr_name)] = String_from_XML_Char(attr_value);
00201     }
00202 
00203     StartElementHandler(String_from_XML_Char(name), attribs);
00204 }
00205 
00206  /// notifications about XML end tag
00207 void XMLReaderBase::endElement(const XMLCh* const name)
00208 {
00209     EndElementHandler();
00210 }
00211 
00212  /// store content
00213 void XMLReaderBase::characters(const XMLCh* const chars, const unsigned int length)
00214 {
00215     DefaultHandler(chars, length);
00216 }
00217 
00218  /// store white space and comments
00219 void XMLReaderBase::ignorableWhitespace(const XMLCh* const chars, const unsigned int length)
00220 {
00221     DefaultHandler(chars, length);
00222 }
00223 
00224 
00225 void XMLReaderBase::error(const SAXParseException& e)
00226 {
00227     XMLError err;
00228 
00229     err._message = e.getMessage();
00230     err._systemId = e.getSystemId();
00231     err._line = e.getLineNumber();
00232     err._column = e.getColumnNumber();
00233 
00234     _errors.push_back(err);
00235 }
00236 
00237 void XMLReaderBase::fatalError(const SAXParseException& e)
00238 {
00239     XMLError err;
00240 
00241     err._message = e.getMessage();
00242     err._systemId = e.getSystemId();
00243     err._line = e.getLineNumber();
00244     err._column = e.getColumnNumber();
00245 
00246     _errors.push_back(err);
00247 }
00248 
00249 void XMLReaderBase::warning(const SAXParseException& e)
00250 {
00251     XMLError err;
00252 
00253     err._message = e.getMessage();
00254     err._systemId = e.getSystemId();
00255     err._line = e.getLineNumber();
00256     err._column = e.getColumnNumber();
00257 
00258     _warnings.push_back(err);
00259 }
00260 
00261 void XMLReaderBase::resetErrors()
00262 {
00263     _errors.clear();
00264 }
00265 
00266 
00267 }   // namespace XMLStorage
00268 
00269 #endif // XS_USE_XERCES

Generated on Fri Nov 14 2014 00:05:04 for XMLStorage/C++ by  doxygen 1.7.2