Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

msdSax2Parser.cc

00001 //-----------------------------------------------------
00002 //msdSax2Parser.cc
00003 // 
00004 // Project: PDBe API Framework 
00005 // Module: Implementation of validation method for validating 
00006 // a given xml / URL according to a given XML schema
00007 // 
00008 // Data Layer
00009 // Last updated: 25 February 2004 10:17
00010 // (C) Siamak Sobhany
00011 //------------------------------------------------------
00012 
00013 #include <iostream>
00014 #include <xercesc/sax2/SAX2XMLReader.hpp>
00015 #include <xercesc/sax2/XMLReaderFactory.hpp>
00016 #include <xercesc/util/XMLString.hpp>
00017 #include <xercesc/util/PlatformUtils.hpp>
00018 #include <xercesc/util/Janitor.hpp>
00019 #include <xercesc/framework/URLInputSource.hpp>
00020 #ifndef __MSD_SAX_2_PARSER__
00021  #include "msdSax2Parser.h"
00022 #endif
00023 // Classes/data/variables must be namespace qualified since
00024 // C++ Namespace support is ENABLED in Xerces.
00025 
00026 
00027 using namespace std;
00028 using namespace xercesc;
00029 
00030 
00031 //  msdSax2Handler: Overrides of the SAX ErrorHandler interface
00032 // ---------------------------------------------------------------------------
00033 void msdSax2Handler::error(const SAXParseException& e)
00034 {
00035   fSawErrors = true;
00036   errors << "[Error] " << XMLString::transcode(e.getSystemId())
00037          << ": line " << e.getLineNumber()
00038          << ", col " << e.getColumnNumber()
00039          << ": " << XMLString::transcode(e.getMessage()) << std::endl;
00040 }
00041  
00042 void msdSax2Handler::fatalError(const SAXParseException& e)
00043 {
00044   fSawErrors = true;
00045   errors << "[Fatal Error] " << XMLString::transcode(e.getSystemId())
00046          << ": line " << e.getLineNumber()
00047          << ", col " << e.getColumnNumber()
00048          << ": " << XMLString::transcode(e.getMessage()) << std::endl;
00049 }
00050  
00051 void msdSax2Handler::warning(const SAXParseException& e)
00052 {
00053   fSawErrors = true;
00054   errors << "[Warning] " << XMLString::transcode(e.getSystemId())
00055          << ": line " << e.getLineNumber()
00056          << ", col " << e.getColumnNumber()
00057          << ": " << XMLString::transcode(e.getMessage()) << std::endl;
00058 }
00059 
00060 const char* msdSax2Handler::getErrors()
00061 {
00062   return errors.str().c_str();
00063 }
00064 
00065 
00066 
00067 
00068 const char* msdSax2Parser::validate(char* xmlFile) {
00069 
00070   std::ostringstream result;
00071 
00072   // Before any calls to Xerces-C++ APIs, we must initialize
00073   // the Xerces system and set its internal variables.
00074   try {
00075     XMLPlatformUtils::Initialize();
00076   }
00077   catch (const XMLException& toCatch) {
00078     // The message must be transcoded from XMLCh to the native code page.
00079     char* message = XMLString::transcode(toCatch.getMessage());
00080     result << "Error during initialization! :\n";
00081     result << "XMLException message is: \n"
00082          << message << "\n";
00083     // Release the parameter char string that was allocated.
00084     XMLString::release(&message);
00085     return result.str().c_str();
00086   }
00087 
00088   SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
00089 
00090   // The externalSchemaLocation string must be
00091   // transcoded to the native code page.
00092   XMLCh* externalSchemaLocation = XMLString::transcode("http://www.geodesy.org/river# River.xsd");
00093 
00094   // The ArrayJanitor template class provides auto_ptr-like functionality.
00095   ArrayJanitor<XMLCh> janValue(externalSchemaLocation);
00096 
00097   // Associates the schema grammar with the instance document using namespaces.
00098   // A secure validating parser MUST NOT rely on the schema location in the
00099   // instance document !
00100   parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation, externalSchemaLocation);
00101 
00102   // Reports all validation errors (not only well-formedness).
00103   parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
00104 
00105   // Performs namespace processing.
00106   parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00107 
00108   // Performs schema processing.
00109   parser->setFeature(XMLUni::fgXercesSchema, true);
00110 
00111   // Checks the schema grammar itself for additional errors.
00112   parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
00113 
00114   // Adds the errorHandler with customized error messages.
00115   msdSax2Handler* errorHandler = new msdSax2Handler();
00116   parser->setErrorHandler(errorHandler);
00117 
00118   try {
00119     //XMLURL xmlUrl(XMLString::transcode(xmlFile));
00120     //URLInputSource in(xmlUrl);
00121     //parser->parse(in);
00122         parser->parse(xmlFile);
00123     result << errorHandler->getErrors();
00124   }
00125   catch (const XMLException& toCatch) {
00126     char* message = XMLString::transcode(toCatch.getMessage());
00127     result << "XMLException message is: \n"
00128          << message << "\n";
00129     XMLString::release(&message);
00130     return result.str().c_str();
00131   }
00132   catch (const SAXParseException& toCatch) {
00133     char* message = XMLString::transcode(toCatch.getMessage());
00134     result << "SAXParseException message is: \n"
00135          << message << "\n";
00136     XMLString::release(&message);
00137     return result.str().c_str();
00138   }
00139 
00140   delete parser;
00141   delete errorHandler;
00142   return result.str().c_str();
00143 }
00144 

Generated on Fri Apr 16 13:47:45 2004 for MSDAPI by doxygen 1.3.4-20031005