ProductFileReader.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00009 
00010 #include "zypp/base/Logger.h"
00011 #include "zypp/parser/xml/Reader.h"
00012 #include "zypp/data/ResolvableData.h"
00013 #include "zypp/ZConfig.h"
00014 
00015 #include "zypp/parser/yum/FileReaderBaseImpl.h"
00016 #include "zypp/parser/yum/ProductFileReader.h"
00017 
00018 #undef ZYPP_BASE_LOGGER_LOGGROUP
00019 #define ZYPP_BASE_LOGGER_LOGGROUP "parser::yum"
00020 
00021 using namespace std;
00022 using namespace zypp::xml;
00023 
00024 namespace zypp
00025 {
00026   namespace parser
00027   {
00028     namespace yum
00029     {
00030 
00031 
00033   //
00034   //  CLASS NAME : ProductFileReader::Impl
00035   //
00036   class ProductFileReader::Impl : public BaseImpl
00037   {
00038   public:
00039     Impl(const Pathname & products_file,
00040          const ProcessProduct & callback);
00041 
00050     bool consumeNode(xml::Reader & reader_r);
00051 
00057     data::Product_Ptr handoutProduct();
00058 
00059   private:
00063     ProcessProduct _callback;
00064 
00069     data::Product_Ptr _product;
00070   };
00072 
00073   ProductFileReader::Impl::Impl(
00074       const Pathname & products_file,
00075       const ProcessProduct & callback)
00076     :
00077       _callback(callback)
00078   {
00079     Reader reader(products_file);
00080     MIL << "Reading " << products_file << endl;
00081     reader.foreachNode(bind(&ProductFileReader::Impl::consumeNode, this, _1));
00082   }
00083 
00084   // --------------------------------------------------------------------------
00085 
00086   /*
00087    * xpath and multiplicity of processed nodes are included in the code
00088    * for convenience:
00089    *
00090    * // xpath: <xpath> (?|*|+)
00091    *
00092    * if multiplicity is ommited, then the node has multiplicity 'one'.
00093    */
00094 
00095   // --------------------------------------------------------------------------
00096 
00097   bool ProductFileReader::Impl::consumeNode(Reader & reader_r)
00098   {
00099     // dependency block nodes
00100     if (_product && consumeDependency(reader_r, _product->deps))
00101       return true;
00102 
00103     if (reader_r->nodeType() == XML_READER_TYPE_ELEMENT)
00104     {
00105       // xpath: /products/product
00106       if (reader_r->name() == "product")
00107       {
00108         tag(tag_product); // just for the case of reuse somewhere/sometimes
00109         _product = new data::Product;
00110         return true;
00111       }
00112 
00113       // xpath: /products/product/name
00114       if (reader_r->name() == "name")
00115       {
00116         _product->name = reader_r.nodeText().asString();
00117         // product type (base, add-on)
00118         _product->type = reader_r->getAttribute("type").asString();
00119         return true;
00120       }
00121 
00122       // xpath: /products/product/vendor
00123       if (reader_r->name() == "vendor")
00124       {
00125         _product->vendor = reader_r.nodeText().asString();
00126       }
00127 
00128       // xpath: /products/product/version
00129       if (reader_r->name() == "version")
00130       {
00131         _product->edition = Edition(reader_r->getAttribute("ver").asString(),
00132                                     reader_r->getAttribute("rel").asString(),
00133                                     reader_r->getAttribute("epoch").asString());
00134       }
00135 
00136       // xpath: /products/product/displayname (+)
00137       if (reader_r->name() == "displayname")
00138       {
00139         Locale locale(reader_r->getAttribute("lang").asString());
00140         _product->longName.setText(reader_r.nodeText().asString(), locale);
00141         return true;
00142       }
00143 
00144       // xpath: /products/product/shortname (*)
00145       if (reader_r->name() == "shortname")
00146       {
00147         Locale locale(reader_r->getAttribute("lang").asString());
00148         _product->shortName.setText(reader_r.nodeText().asString(), locale);
00149         return true;
00150       }
00151 
00152       // xpath: /products/product/description (+)
00153       if (reader_r->name() == "description")
00154       {
00155         Locale locale(reader_r->getAttribute("lang").asString());
00156         _product->description.setText(reader_r.nodeText().asString(), locale);
00157         return true;
00158       }
00159 
00160       // xpath: /products/product/distribution-name (+)
00161       if (reader_r->name() == "distribution-name")
00162       {
00163         _product->distributionName = reader_r.nodeText().asString();
00164         return true;
00165       }
00166 
00167       // xpath: /products/product/distribution-edition (+)
00168       if (reader_r->name() == "distribution-edition")
00169       {
00170         _product->distributionEdition = reader_r.nodeText().asString();
00171         return true;
00172       }
00173 
00174       // xpath: /products/product/release-notes-url (+)
00175       if (reader_r->name() == "release-notes-url")
00176       {
00177         string value = reader_r.nodeText().asString();
00178 
00179         for( std::string::size_type pos = value.find("%a");
00180             pos != std::string::npos;
00181             pos = value.find("%a") )
00182         {
00183           value.replace( pos, 2, ZConfig::instance().systemArchitecture().asString() );
00184         }
00185         try
00186         {
00187           _product->releasenotesUrl = value;
00188         }
00189         catch( const Exception & excpt_r )
00190         {
00191           WAR << "Malformed url ignored: '" << value << "' " << excpt_r.asString() << endl;
00192         }
00193         return true;
00194       }
00195 
00196       // xpath: /products/product/update-url (*)
00197       if (reader_r->name() == "update-url")
00198       {
00199         string value = reader_r.nodeText().asString();
00200 
00201         try
00202         {
00203           _product->updateUrls.push_back(Url(value));
00204         }
00205         catch( const Exception & excpt_r )
00206         {
00207           WAR << "Malformed url ignored: '" << value << "' " << excpt_r.asString() << endl;
00208         }
00209         return true;
00210       }
00211 
00212       // xpath: /products/product/extra-url (*)
00213       if (reader_r->name() == "extra-url")
00214       {
00215         string value = reader_r.nodeText().asString();
00216 
00217         try
00218         {
00219           _product->extraUrls.push_back(Url(value));
00220         }
00221         catch( const Exception & excpt_r )
00222         {
00223           WAR << "Malformed url ignored: '" << value << "' " << excpt_r.asString() << endl;
00224         }
00225         return true;
00226       }
00227 
00228       // xpath: /products/product/optional-url (*)
00229       if (reader_r->name() == "optional-url")
00230       {
00231         string value = reader_r.nodeText().asString();
00232 
00233         try
00234         {
00235           _product->optionalUrls.push_back(Url(value));
00236         }
00237         catch( const Exception & excpt_r )
00238         {
00239           WAR << "Malformed url ignored: '" << value << "' " << excpt_r.asString() << endl;
00240         }
00241         return true;
00242       }
00243     }
00244 
00245     else if (reader_r->nodeType() == XML_READER_TYPE_END_ELEMENT)
00246     {
00247       // xpath: /products/product
00248       if (reader_r->name() == "product")
00249       {
00250         if (_callback)
00251           _callback(handoutProduct());
00252 
00253         toParentTag(); // just for the case of reuse somewhere/sometimes
00254 
00255         return true;
00256       }
00257     }
00258 
00259     return true;
00260   }
00261 
00262   // --------------------------------------------------------------------------
00263 
00264   data::Product_Ptr ProductFileReader::Impl::handoutProduct()
00265   {
00266     data::Product_Ptr ret;
00267     ret.swap(_product);
00268     return ret;
00269   }
00270 
00272   //
00273   //  CLASS NAME : ProductFileReader
00274   //
00276 
00277   ProductFileReader::ProductFileReader(const Pathname & product_file, ProcessProduct callback)
00278       : _pimpl(new ProductFileReader::Impl(product_file, callback))
00279   {}
00280 
00281 
00282   ProductFileReader::~ProductFileReader()
00283   {}
00284 
00285 
00286     } // ns yum
00287   } // ns parser
00288 } // ns zypp
00289 
00290 // vim: set ts=2 sts=2 sw=2 et ai:

Generated on Tue Sep 25 19:23:04 2007 for libzypp by  doxygen 1.5.3