link to homepage

Institute for Neuroscience and Medicine

Navigation and service


API documentation

XMLIO.cpp

Go to the documentation of this file.
00001 
00005 /*
00006  *  JEMRIS Copyright (C) 2007-2010  Tony Stöcker, Kaveh Vahedipour
00007  *                                  Forschungszentrum Jülich, Germany
00008  *
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  */
00023 
00024 #include "config.h"
00025 #include "XMLIO.h"
00026 #include <xercesc/framework/StdOutFormatTarget.hpp>
00027 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00028 
00029 /**********************************************************/
00030 XMLIO::~XMLIO() {
00031         if (m_parser)
00032                 delete m_parser;
00033         if (m_err_reporter)
00034                 delete m_err_reporter;
00035 }
00036 
00037 /**********************************************************/
00038 XMLIO::XMLIO () {
00039 
00040         try {
00041         XMLPlatformUtils::Initialize();
00042     } catch (const  XMLException& e) {
00043         XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
00044              << "  Exception message:"
00045              << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
00046     }
00047 
00048         m_parser       = new XercesDOMParser;
00049     m_err_reporter = new DOMTreeErrorReporter;
00050 
00051         XercesDOMParser::ValSchemes valScheme    = XercesDOMParser::Val_Auto;
00052         doNamespaces                             = false;
00053         doSchema                                 = false;
00054         schemaFullChecking                       = false;
00055         doCreate                                 = false;
00056 
00057         m_parser->setValidationScheme             (valScheme);
00058         m_parser->setDoNamespaces                 (doNamespaces);
00059         m_parser->setDoSchema                     (doSchema);
00060         m_parser->setValidationSchemaFullChecking (schemaFullChecking);
00061         m_parser->setCreateEntityReferenceNodes   (doCreate);
00062         m_parser->setErrorHandler                 (m_err_reporter);
00063 
00064 }
00065 
00066 /**********************************************************/
00067 DOMDocument* XMLIO::Parse (string uri) {
00068 
00069         DOMDocument* doc = NULL;
00070 
00071         try {
00072             LocalFileInputSource LocFile( StrX(uri).XMLchar() );
00073             m_parser->parse( LocFile );
00074         
00075         } catch (const OutOfMemoryException&) {
00076 
00077             XERCES_STD_QUALIFIER cerr
00078                 << "OutOfMemoryException"
00079                 << XERCES_STD_QUALIFIER endl;
00080             return NULL;
00081 
00082         } catch (const XMLException& e) {
00083 
00084             XERCES_STD_QUALIFIER cerr
00085                 << "An error occurred during parsing\n   Message: "
00086                 << StrX(e.getMessage())
00087                 << XERCES_STD_QUALIFIER endl;
00088             return NULL;
00089 
00090         } catch (const DOMException& e) {
00091 
00092             const unsigned int maxChars = 2047;
00093 
00094             XMLCh errText[maxChars + 1];
00095 
00096             XERCES_STD_QUALIFIER cerr << "\nDOM Error during parsing: '"
00097                 << uri << "'\n"
00098                 << "DOMException code is:  "
00099                 << e.code << XERCES_STD_QUALIFIER endl;
00100 
00101             if (DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars))
00102                  XERCES_STD_QUALIFIER cerr
00103                  << "Message is: " << StrX(errText) << XERCES_STD_QUALIFIER endl;
00104 
00105             return NULL;
00106 
00107         } catch (...) {
00108 
00109             XERCES_STD_QUALIFIER cerr
00110                 << "An error occurred during parsing\n "
00111                 << XERCES_STD_QUALIFIER endl;
00112             return NULL;
00113 
00114         }
00115 
00116         if (m_parser) {
00117 
00118             doc  = m_parser->getDocument();
00119             if (doc) {
00120 
00121                 try {
00122 
00123                     doc->normalizeDocument();
00124 
00125                 } catch (const DOMException& e) {
00126                     const unsigned int maxChars = 2047;
00127 
00128                     XMLCh errText[maxChars + 1];
00129 
00130                     XERCES_STD_QUALIFIER cerr << "\nDOM Error during normalizing: '"
00131                         << uri << "'\n"
00132                         << "DOMException code is:  "
00133                         << e.code << XERCES_STD_QUALIFIER endl;
00134 
00135                     if (DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars))
00136                          XERCES_STD_QUALIFIER cerr
00137                          << "Message is: " << StrX(errText)
00138                          << XERCES_STD_QUALIFIER endl;
00139 
00140                      return NULL;
00141                 }
00142 
00143               } else
00144                 return NULL;
00145 
00146         }
00147 
00148 
00149         return doc;
00150 }
00151 
00152 /**********************************************************/
00153 DOMNode* XMLIO::RunTree (DOMNode* node, void* ptr, unsigned int (*fun) (void*, DOMNode*) ) {
00154 
00155     DOMNode* child;
00156     DOMNode* rnode=NULL;
00157 
00158     if (node) {
00159         if (node->getNodeType() == DOMNode::ELEMENT_NODE) {
00160 
00161             unsigned int code = fun(ptr,node);
00162             if (code>0) return node;
00163 
00164             for (child = node->getFirstChild(); child != 0; child=child->getNextSibling()) {
00165                 rnode = RunTree(child,ptr,fun);
00166                 if (rnode!=NULL) break;
00167             }
00168         }
00169     }
00170     return rnode;
00171 }
00172 
00173 /**********************************************************/
00174 bool XMLIO::Write (DOMImplementation* impl, DOMNode* node, string filename) {
00175 
00176         XMLFormatTarget* mft;
00177 
00178         if (filename.empty())
00179                 mft = new StdOutFormatTarget();
00180         else
00181                 mft = new LocalFileFormatTarget (StrX(filename).XMLchar());
00182 
00183         // Xerces 2
00184     #ifdef XSEC_XERCES_HAS_SETIDATTRIBUTE
00185         DOMWriter* serializer = ((DOMImplementationLS*)impl)->createDOMWriter();
00186 
00187     if (serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
00188                 serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
00189         
00190         serializer->writeNode(mft, *node);
00191 
00192         serializer->release();
00193     #endif 
00194 
00195     #ifdef XSEC_XERCES_HAS_BOOLSETIDATTRIBUTE
00196         DOMLSSerializer*   serializer    = ((DOMImplementationLS*) impl)->createLSSerializer();
00197         DOMLSOutput*       output        = ((DOMImplementationLS*)impl)->createLSOutput(); 
00198     DOMConfiguration*  configuration = serializer->getDomConfig(); 
00199 
00200         if (configuration->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
00201                 configuration->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true); 
00202 
00203         output->setByteStream (mft);
00204         serializer->write(node, output);
00205 
00206         output->release();
00207         serializer->release(); 
00208     #endif
00209 
00210         return true;
00211 
00212 }

Servicemeu

institutes

Scientific Technical Facilities