| 1 | #include <xercesc/util/PlatformUtils.hpp> |
|---|
| 2 | #include <xercesc/util/TransService.hpp> |
|---|
| 3 | #include <xercesc/sax2/SAX2XMLReader.hpp> |
|---|
| 4 | #include <xercesc/sax2/XMLReaderFactory.hpp> |
|---|
| 5 | #include "xmlParser.hxx" |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | static void usage() { |
|---|
| 9 | cout << "\nUsage: xml2newick <xml_file> <out_put_file_name>"<< endl; |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| 12 | int main(int argC, char* argV[]) { |
|---|
| 13 | const char* encodingName = "LATIN1"; |
|---|
| 14 | const char* fileName = "example" ; |
|---|
| 15 | const char* xmlFile = 0; |
|---|
| 16 | |
|---|
| 17 | XMLFormatter::UnRepFlags unRepFlags = XMLFormatter::UnRep_CharRef; |
|---|
| 18 | |
|---|
| 19 | // Initialize the XML4C2 system |
|---|
| 20 | try { |
|---|
| 21 | XMLPlatformUtils::Initialize(); |
|---|
| 22 | } |
|---|
| 23 | catch (const XMLException& toCatch) { |
|---|
| 24 | cerr << "Error during initialization! :\n" |
|---|
| 25 | << StrX(toCatch.getMessage()) << endl; |
|---|
| 26 | return 1; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | // Check command line and extract arguments. |
|---|
| 30 | if (argC < 3) { |
|---|
| 31 | usage(); |
|---|
| 32 | XMLPlatformUtils::Terminate(); |
|---|
| 33 | return 1; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | int parmInd = 1; |
|---|
| 37 | if (parmInd < argC) { |
|---|
| 38 | xmlFile = argV[parmInd++]; // xml source file name |
|---|
| 39 | fileName = argV[parmInd]; // destination file name |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | // Create a SAX parser object and set the validations |
|---|
| 43 | |
|---|
| 44 | SAX2XMLReader* parser = XMLReaderFactory::createXMLReader(); |
|---|
| 45 | |
|---|
| 46 | // Create the handler object and install it as the document and error |
|---|
| 47 | // handler for the parser. Then parse the file and catch any exceptions |
|---|
| 48 | // that propagate out |
|---|
| 49 | |
|---|
| 50 | ofstream outFile; |
|---|
| 51 | outFile.open(fileName, ios::out); //open destination file to write |
|---|
| 52 | |
|---|
| 53 | int errorCount = 0; |
|---|
| 54 | try { |
|---|
| 55 | Sax2Handler handler(encodingName, unRepFlags, outFile); |
|---|
| 56 | parser->setContentHandler(&handler); |
|---|
| 57 | parser->setErrorHandler(&handler); |
|---|
| 58 | parser->parse(xmlFile); |
|---|
| 59 | errorCount = parser->getErrorCount(); |
|---|
| 60 | outFile.close(); // close the file |
|---|
| 61 | } |
|---|
| 62 | catch (const XMLException& toCatch) { |
|---|
| 63 | cerr << "\nAn error occurred\n Error: " |
|---|
| 64 | << StrX(toCatch.getMessage())<< endl; |
|---|
| 65 | XMLPlatformUtils::Terminate(); |
|---|
| 66 | return 4; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | // Delete the parser itself. Must be done prior to calling Terminate, below. |
|---|
| 70 | |
|---|
| 71 | delete parser; |
|---|
| 72 | |
|---|
| 73 | // And call the termination method |
|---|
| 74 | XMLPlatformUtils::Terminate(); |
|---|
| 75 | |
|---|
| 76 | if (errorCount > 0) |
|---|
| 77 | return 4; |
|---|
| 78 | else |
|---|
| 79 | return 0; |
|---|
| 80 | } |
|---|
| 81 | |
|---|