00001 #include <xercesc/sax2/SAX2XMLReader.hpp>
00002 #include <xercesc/sax2/XMLReaderFactory.hpp>
00003 #include <xercesc/sax2/DefaultHandler.hpp>
00004 #include <xercesc/util/XMLString.hpp>
00005 #include <xercesc/parsers/SAX2XMLReaderImpl.hpp>
00006
00007 #if defined(XERCES_NEW_IOSTREAMS)
00008 #include <iostream>
00009 #else
00010 #include <iostream.h>
00011 #endif
00012
00013 #include <AML/SAX2Wrapper.hpp>
00014
00015 XERCES_CPP_NAMESPACE_USE
00016
00017 using namespace std;
00018 namespace BU = BasicUtilities;
00019
00020
00021
00022
00023 bool SAX2Wrapper::XMLFileToUAPRep (const string& xml_file, UAPNode* root, bool convert) {
00024
00025 parse_OK = true;
00026 this_root = root;
00027 current = root;
00028 finished = false;
00029 convert_entities = convert;
00030
00031 try {
00032 XMLPlatformUtils::Initialize();
00033 }
00034 catch (const XMLException& toCatch) {
00035 char* message = XMLString::transcode(toCatch.getMessage());
00036 cout << "Error during initialization! :\n";
00037 cout << "Exception message is: \n"
00038 << message << "\n";
00039 XMLString::release(&message);
00040 return false;
00041 }
00042
00043 SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
00044
00045 parser->setFeature(XMLUni::fgSAX2CoreValidation, false);
00046 parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00047 parser->setFeature(XMLUni::fgXercesContinueAfterFatalError, true);
00048 parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, true);
00049
00050
00051
00052
00053
00054
00055
00056
00057 parser->setContentHandler(this);
00058 parser->setErrorHandler(this);
00059
00060 try{
00061 parser->parse(xml_file.c_str());
00062 } catch (RuntimeException& except) {
00063 cout << "RuntimeException!" << endl;
00064 parse_OK = false;
00065 } catch (...) {
00066 cout << "Parse Exception!" << endl;
00067 }
00068
00069 delete parser;
00070
00071 root = this_root;
00072 return parse_OK;
00073
00074 }
00075
00076
00077
00078
00079
00080 void SAX2Wrapper::error(const SAXParseException& e) {
00081 error_out ("Error", e);
00082 return;
00083 }
00084
00085 void SAX2Wrapper::fatalError(const SAXParseException& e) {
00086 error_out ("Fatal error", e);
00087 return;
00088 }
00089
00090 void SAX2Wrapper::warning(const SAXParseException& e) {
00091 error_out ("Warning", e);
00092 return;
00093 }
00094
00095 void SAX2Wrapper::error_out (const string& err_type, const SAXParseException& e) {
00096 if (finished)
00097 std::cout << err_type << ": " << "Extra stuff after end of root node" << std::endl;
00098 else
00099 std::cout << err_type << ": " << StrX(e.getMessage()) << std::endl;
00100
00101 std::cout << " In file: " << StrX(e.getSystemId()) << std::endl
00102 << " Line: " << e.getLineNumber() << " Column: " << e.getColumnNumber() << std::endl;
00103 parse_OK = false;
00104 }
00105
00106
00107
00108
00109
00110 void SAX2Wrapper::characters(const XMLCh* const chars,
00111 const unsigned int length) {
00112
00113
00114
00115 string text = BU::trim(StrX(chars));
00116 if (text == "") return;
00117 current->addChild(text, TEXT_NODE);
00118 }
00119
00120
00121 void SAX2Wrapper::endDocument() {
00122
00123 }
00124
00125
00126 void SAX2Wrapper::endElement(const XMLCh* const uri,
00127 const XMLCh* const localname,
00128 const XMLCh* const qname) {
00129
00130 current = current->getParent();
00131 if (current == this_root) finished = true;
00132
00133 }
00134
00135
00136 void SAX2Wrapper::ignorableWhitespace( const XMLCh* const chars,
00137 const unsigned int length) {
00138 std::cout << "ignorableWhite: " << StrX(chars) << std::endl;
00139 }
00140
00141 void SAX2Wrapper::processingInstruction(const XMLCh* const target,
00142 const XMLCh* const data) {
00143 std::cout << "processingInstruction: " << StrX(data) << std::endl;
00144 }
00145
00146
00147 void SAX2Wrapper::startDocument() {
00148
00149 }
00150
00151
00152 void SAX2Wrapper::startElement(const XMLCh* const uri,
00153 const XMLCh* const localname,
00154 const XMLCh* const qname,
00155 const Attributes& attributes) {
00156
00157
00158
00159
00160 string loc_name = StrX(localname);
00161
00162 if (!this_root) {
00163 this_root = new UAPNode(loc_name);
00164 current = this_root;
00165 } else {
00166 current = current->addChild(loc_name);
00167 }
00168
00169
00170
00171 current->setXMLURI(StrX(uri));
00172
00173
00174
00175 string q_name = StrX(qname);
00176 int ix = q_name.find_last_of(":");
00177 if (ix != string::npos) {
00178 current->setXMLPrefix(q_name.substr(0, ix));
00179 q_name.erase(0, ix+1);
00180 current->setName(q_name);
00181 }
00182
00183
00184
00185 unsigned int len = attributes.getLength();
00186 for (unsigned int index = 0; index < len; index++) {
00187 string value = StrX(attributes.getValue(index));
00188 if (convert_entities) BU::from_xml_string (value);
00189 current->addAttribute(StrX(attributes.getQName(index)), value);
00190 }
00191
00192 }
00193
00194
00195
00196
00197 string SAX2Wrapper::StrX(const XMLCh* const toTranscode) {
00198 char* chars = XMLString::transcode(toTranscode);
00199 string line = chars;
00200 XMLString::release(&chars);
00201 return line;
00202 }