SAXParser.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00009 
00010 #include <iostream>
00011 #include <fstream>
00012 #include <sstream>
00013 #include <streambuf>
00014 
00015 #include <zypp/parser/SAXParser.h>
00016 #include <zypp/base/Logger.h>
00017 
00018 namespace zypp
00019 {
00020 namespace parser
00021 {
00022 
00023 static xmlSAXHandler emptySAXHandlerStruct = {
00024     NULL, /* internalSubset */
00025     NULL, /* isStandalone */
00026     NULL, /* hasInternalSubset */
00027     NULL, /* hasExternalSubset */
00028     NULL, /* resolveEntity */
00029     NULL, /* getEntity */
00030     NULL, /* entityDecl */
00031     NULL, /* notationDecl */
00032     NULL, /* attributeDecl */
00033     NULL, /* elementDecl */
00034     NULL, /* unparsedEntityDecl */
00035     NULL, /* setDocumentLocator */
00036     NULL, /* startDocument */
00037     NULL, /* endDocument */
00038     NULL, /* startElement */
00039     NULL, /* endElement */
00040     NULL, /* reference */
00041     NULL, /* characters */
00042     NULL, /* ignorableWhitespace */
00043     NULL, /* processingInstruction */
00044     NULL, /* comment */
00045     NULL, /* xmlParserWarning */
00046     NULL, /* xmlParserError */
00047     NULL, /* xmlParserError */
00048     NULL, /* getParameterEntity */
00049     NULL, /* cdataBlock; */
00050     NULL,  /* externalSubset; */
00051     1
00052 };
00053 
00054 static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
00055 extern xmlSAXHandlerPtr debugSAXHandler;
00056 
00064 static void
00065 startElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
00066 {
00067     int i;
00068 
00069     fprintf(stdout, "SAX.startElement(%s", (char *) name);
00070     if (atts != NULL) {
00071         for (i = 0;(atts[i] != NULL);i++) {
00072       fprintf(stdout, ", %s='", atts[i++]);
00073       if (atts[i] != NULL)
00074           fprintf(stdout, "%s'", atts[i]);
00075   }
00076     }
00077     fprintf(stdout, ")\n");
00078 }
00079 
00080 static xmlEntityPtr
00081 my_getEntity(void *user_data, const xmlChar *name)
00082 {
00083   return xmlGetPredefinedEntity(name);
00084 }
00085 
00086 static void
00087 endElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
00088 {
00089     fprintf(stdout, "SAX.endElement(%s)\n", (char *) name);
00090 }
00091 
00092 void
00093 SAXParser::startElement_receiver(void *ctx, const xmlChar *name, const xmlChar **atts)
00094 {
00095   SAXParser *rcv = (SAXParser *)(ctx);
00096   if ( rcv )
00097     rcv->startElement(std::string( (const char*) name), atts);
00098 }
00099 
00100 void
00101 SAXParser::characters_receiver (void *ctx, const xmlChar *ch, int len)
00102 {
00103   SAXParser *rcv = (SAXParser *)(ctx);
00104   if ( rcv )
00105     rcv->characters( ch, len);
00106 }
00107 
00108 void
00109 SAXParser::endElement_receiver(void *ctx, const xmlChar *name)
00110 {
00111   SAXParser *rcv = (SAXParser *)(ctx);
00112   if ( rcv )
00113     rcv->endElement(std::string( (const char*) name));
00114 }
00115 
00116 static xmlEntityPtr getEntity_receiver(void *user_data, const xmlChar *name)
00117 {
00118     return xmlGetPredefinedEntity(name);
00119 }
00120 
00121 
00122 void SAXParser::startElement(const std::string name, const xmlChar **atts)
00123 {
00124   MIL << "start-element:" <<  name << std::endl;
00125 }
00126 
00127 void SAXParser::endElement(const std::string name)
00128 {
00129   MIL << "end-element:" << name << std::endl;
00130 }
00131 
00132 void SAXParser::characters(const xmlChar *ch, int len)
00133 {
00134   MIL << "characters:" << std::string( (const char *)ch, len) << std::endl;
00135 }
00136 
00137 void SAXParser::parseFile( const Pathname &p)
00138 {
00139   FILE *f = fopen(p.asString().c_str(), "r");
00140   if (f != NULL)
00141   {
00142     int res = xmlSAXUserParseFile(&_saxHandler, (void *) this, p.asString().c_str());
00143     if (res != 0)
00144     {
00145       fprintf(stdout, "xmlSAXUserParseFile returned error %d\n", res);
00146     }
00147     fclose(f);
00148   }
00149   else
00150   {
00151     fprintf(stdout, "UPS\n");
00152   }
00153 }
00154 
00155 SAXParser::SAXParser()
00156 {
00157   _saxHandler.internalSubset = NULL;
00158   _saxHandler.isStandalone = NULL;   
00159   _saxHandler.hasInternalSubset = NULL;
00160   _saxHandler.hasExternalSubset = NULL;
00161   _saxHandler.resolveEntity = NULL;
00162   _saxHandler.getEntity = NULL;
00163   _saxHandler.entityDecl = NULL;
00164   _saxHandler.notationDecl = NULL;
00165   _saxHandler.attributeDecl = NULL;
00166   _saxHandler.elementDecl = NULL;
00167   _saxHandler.unparsedEntityDecl = NULL;
00168   _saxHandler.setDocumentLocator = NULL;
00169   _saxHandler.startDocument = NULL;
00170   _saxHandler.endDocument = NULL;
00171   _saxHandler.startElement = NULL;
00172   _saxHandler.endElement = NULL;
00173   _saxHandler.reference = NULL;
00174   _saxHandler.characters = NULL;
00175   _saxHandler.ignorableWhitespace = NULL;
00176   _saxHandler.processingInstruction = NULL;
00177   _saxHandler.comment = NULL;
00178    
00179   //_saxHandler.xmlParserWarning = NULL;
00180   //_saxHandler.xmlParserError = NULL;
00181   //_saxHandler.xmlParserError = NULL;
00182   
00183   _saxHandler.getParameterEntity = NULL;
00184   _saxHandler.cdataBlock = NULL;
00185   _saxHandler.externalSubset = NULL; 
00186 
00187   _saxHandler = emptySAXHandlerStruct;
00188   //_saxHandler.startDocument = startElement_receiver;
00189  _saxHandler.startElement = startElement_receiver;
00190  _saxHandler.endElement = endElement_receiver;
00191  _saxHandler.getEntity = getEntity_receiver;
00192   _saxHandler.characters = characters_receiver;
00193 }
00194 
00195 SAXParser::~SAXParser()
00196 {
00197 }
00198 
00199 } // ns parser
00200 } // ns zypp

Generated on Tue Nov 28 16:49:30 2006 for zypp by  doxygen 1.5.0