USGS

Isis 3.0 Object Programmers' Reference

Home

IsisXMLHandler.cpp

Go to the documentation of this file.
00001 
00024 using namespace std;
00025 
00026 #include <string>
00027 
00028 #include <xercesc/sax2/SAX2XMLReader.hpp>
00029 #include <xercesc/util/XMLException.hpp>
00030 
00031 #include <sstream>
00032 
00033 #include "IException.h"
00034 
00035 #include "IString.h"
00036 
00037 #include "IsisXMLHandler.h"
00038 #include "IsisXMLChTrans.h"
00039 
00040 namespace XERCES = XERCES_CPP_NAMESPACE;
00041 
00042 
00043 // Constructors
00044 
00045 IsisXMLHandler::IsisXMLHandler() {
00046   value = NULL;
00047   outputEndTag = 0;
00048 }
00049 
00050 
00051 IsisXMLHandler::IsisXMLHandler(char *PencodingName,
00052                                bool &PexpandNamespaces,
00053                                XERCES::SAX2XMLReader* &Pparser,
00054                                QString *Pvalue) {
00055 
00056   encodingName = PencodingName;
00057   expandNamespaces = PexpandNamespaces;
00058   parser = Pparser;
00059   value = Pvalue;
00060 
00061   outputEndTag = 0;
00062 
00063   prevDocHandler = parser->getContentHandler();
00064   prevErrorHandler = parser->getErrorHandler();
00065 
00066   parser->setContentHandler(this);
00067   parser->setErrorHandler(this);
00068 }
00069 
00070 
00071 IsisXMLHandler::IsisXMLHandler(char *PencodingName,
00072                                bool &PexpandNamespaces,
00073                                XERCES::SAX2XMLReader* &Pparser) {
00074 
00075   value = NULL;
00076   outputEndTag = 0;
00077 
00078   encodingName = PencodingName;
00079   expandNamespaces = PexpandNamespaces;
00080   parser = Pparser;
00081 
00082   prevDocHandler = parser->getContentHandler();
00083   prevErrorHandler = parser->getErrorHandler();
00084 
00085   parser->setContentHandler(this);
00086   parser->setErrorHandler(this);
00087 }
00088 
00089 
00090 IsisXMLHandler::~IsisXMLHandler() {}
00091 
00092 
00093 //  IsisXMLHandler: Overrides the SAX ErrorHandler
00094 void IsisXMLHandler::error(const XERCES::SAXParseException &e) {
00095   ostringstream os;
00096   os << "Error in application XML file line: " << e.getLineNumber()
00097      << " char: " << e.getColumnNumber() << ". "
00098      << XERCES::XMLString::transcode(e.getMessage());
00099   throw Isis::IException(Isis::IException::Programmer, os.str(), _FILEINFO_);
00100 }
00101 
00102 void IsisXMLHandler::fatalError(const XERCES::SAXParseException &e) {
00103   ostringstream os;
00104   os << "Error in application XML file line: " << e.getLineNumber()
00105      << " char: " << e.getColumnNumber() << ". "
00106      << XERCES::XMLString::transcode(e.getMessage());
00107   throw Isis::IException(Isis::IException::Programmer, os.str(), _FILEINFO_);
00108 }
00109 
00110 void IsisXMLHandler::warning(const XERCES::SAXParseException &e) {
00111   ostringstream os;
00112   os << "Error in application XML file line: " << e.getLineNumber()
00113      << " char: " << e.getColumnNumber() << ". "
00114      << XERCES::XMLString::transcode(e.getMessage());
00115   throw Isis::IException(Isis::IException::Programmer, os.str(), _FILEINFO_);
00116 }
00117 
00118 
00119 //  IsisXMLHandler: Overrides of the SAX DocumentHandler interface
00120 void IsisXMLHandler::characters(const XMLCh *const chars,
00121                                 const XMLSize_t length) {
00122 
00123   if(value != NULL) {
00124     QString str;
00125     str = XERCES::XMLString::transcode(chars);
00126     str = str.trimmed();
00127     *value += str;
00128   }
00129 }
00130 
00131 
00132 void IsisXMLHandler::endDocument() {
00133 }
00134 
00135 
00136 void IsisXMLHandler::endElement(const XMLCh *const uri,
00137                                 const XMLCh *const localname,
00138                                 const XMLCh *const qname) {
00139 
00140   if(outputEndTag > 0) {
00141     QString str;
00142     str = XERCES::XMLString::transcode(localname);
00143     *value += "</" + str + ">";
00144     outputEndTag--;
00145   }
00146   else {
00147     parser->setContentHandler(prevDocHandler);
00148     parser->setErrorHandler(prevErrorHandler);
00149   }
00150 }
00151 
00152 
00153 void IsisXMLHandler::ignorableWhitespace(const XMLCh *const chars,
00154     const unsigned int length) {
00155 }
00156 
00157 
00158 
00159 void IsisXMLHandler::processingInstruction(const XMLCh *const target,
00160     const XMLCh *const data) {
00161 }
00162 
00163 
00164 void IsisXMLHandler::startDocument() {
00165 }
00166 
00167 
00168 void IsisXMLHandler::startElement(const XMLCh *const uri,
00169                                   const XMLCh *const localname,
00170                                   const XMLCh *const qname,
00171                                   const XERCES::Attributes &attributes) {
00172 
00173   if(value != NULL) {
00174     QString str;
00175     str = XERCES::XMLString::transcode(localname);
00176     // Note: need to build the attributes into the string too
00177     *value += "<" + str + ">";
00178     outputEndTag++;
00179   }
00180 }
00181 
00182