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

xml_parser.cpp

00001 // xml_parser.cpp
00002 // utility to parse XML stream from a large file
00003 
00004 //#define NDEBUG    // define before assert.h to stop assertions from being compiled 
00005 //#define CTTL_TRACE_EVERYTHING //define to turn tracing on
00006 //#define CTTL_TRACE_RULES  //define to turn light tracing on
00007 //#define CTTL_LAMBDA_SINGULAR   // turns off multidimensional depositories
00008 #define DISPOSE_CONSUMED_DATA   // when defined, free up memory as we read data from the stream
00009 
00010 #include <iostream>
00011 #include <stack>
00012 #include "utils/win32_specific.h"
00013 #include "cttl/cttl.h"
00014 #include "utils/fileio.h"
00015 #include "utils/timeutils.h"
00016 #include "lambda/lambda.h"
00017 #include "utils/offset_stack_guard.h"
00018 #include "utils/inode.h"
00019 #include <algorithm>
00020 
00021 #include "example/lambda/xml/xml_tree_struct.h"
00022 #include "example/lambda/xml/xml_tree_show.h"
00023 #include "example/lambda/xml/xml_stream_policy.h"
00024 
00025 using namespace cttl;
00026 
00027 namespace {
00028     // common xml alphabet
00029     const std::string LITERAL_XML_NAME          = CTTL_QWERTY_123_ ":-";
00030     const std::string LITERAL_XML_COMMENT_OPEN  = "<!--";
00031     const std::string LITERAL_XML_COMMENT_CLOSE = "-->";
00032     const std::string LITERAL_XML_PI_OPEN       = "<?";
00033     const std::string LITERAL_XML_PI_CLOSE      = "?>";
00034     const std::string LITERAL_XML_CLOSE_ELEM    = "</";
00035     const std::string LITERAL_XML_ELEM_CLOSED   = "/>";
00036 }
00037 
00038 #include "example/lambda/xml/xml_parser.h"
00039 #include "example/lambda/xml/xml_lexer.h"
00040 
00041 int main(int argc, char* argv[])
00042 {
00043     if ( argc < 2 ) {
00044         std::cout
00045             << "Usage: "
00046             << std::endl
00047             << '\t'
00048             << argv[ 0 ]
00049             << " path/file.xml [-o]"
00050             << std::endl
00051             << "If -o switch is specified, results will be sent to the standard output."
00052             << std::endl
00053             ;
00054         return 1;
00055 
00056     } else if ( ( argc > 2 ) && strcmp( argv[ 2 ], "-o" ) ) {
00057         std::cout
00058             << "ERROR: unknown switch "
00059             << argv[ 2 ]
00060             << std::endl
00061             ;
00062         return 1;
00063     }
00064 
00065 
00066     input<> inp;
00067     edge<> consumed_data( new_edge( inp ) ); 
00068 
00069     policy_relaxed_stream file_stream( consumed_data );
00070     if ( !file_stream.init( argv[ 1 ] ) ) {
00071         std::cout
00072             << "ERROR: failed to open input file "
00073             << std::endl
00074             << '\t'
00075             << argv[ 1 ]
00076             << std::endl
00077             ;
00078         return 1;
00079     }
00080 
00081     typedef edge< policy_relaxed_stream > universe_T;
00082     universe_T universe( new_edge( inp ), file_stream );
00083     lexer< parser< universe_T > > xml_parser( consumed_data );
00084 
00085     std::cout
00086         << time2string( current_time() )
00087         << " parsing "
00088         << argv[ 1 ]
00089         << std::endl
00090         ;
00091 
00092     if ( xml_parser.xml_grammar( universe ) == universe_T::string_T::npos ) {
00093         std::cout
00094             << time2string( current_time() )
00095             << " parser failed"
00096             << std::endl
00097             ;
00098 
00099         return 1;
00100     }
00101 
00102     std::cout
00103         << time2string( current_time() )
00104         << " done"
00105         << std::endl
00106         ;
00107 
00108     if ( ( argc > 2 ) && !strcmp( argv[ 2 ], "-o" ) ) {
00109         inode_reader<> root( xml_parser.vect_xml_tree, 1 );
00110         std::for_each(
00111             root,
00112             root.end(),
00113             xml_tree_show(
00114                 1,
00115                 xml_parser.vect_xml_names,
00116                 xml_parser.vect_xml_text
00117                 )
00118             );
00119     }
00120     return 0;
00121 }

Generated on Thu Nov 2 17:43:26 2006 for CTTL XML stream parser sample by  doxygen 1.3.9.1