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