libzypp  10.5.0
RepoindexFileReader.cc
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 
00014 #include "zypp/base/String.h"
00015 #include "zypp/base/Logger.h"
00016 #include "zypp/base/Gettext.h"
00017 #include "zypp/base/InputStream.h"
00018 
00019 #include "zypp/Pathname.h"
00020 
00021 #include "zypp/parser/xml/Reader.h"
00022 #include "zypp/parser/ParseException.h"
00023 
00024 #include "zypp/RepoInfo.h"
00025 
00026 #include "zypp/parser/RepoindexFileReader.h"
00027 
00028 
00029 #undef ZYPP_BASE_LOGGER_LOGGROUP
00030 #define ZYPP_BASE_LOGGER_LOGGROUP "parser"
00031 
00032 using namespace std;
00033 using namespace zypp::xml;
00034 
00035 namespace zypp
00036 {
00037   namespace parser
00038   {
00039 
00040 
00042   //
00043   //  CLASS NAME : RepoindexFileReader::Impl
00044   //
00045   class RepoindexFileReader::Impl : private base::NonCopyable
00046   {
00047   public:
00053     Impl(const InputStream &is, const ProcessResource & callback);
00054 
00058     bool consumeNode( Reader & reader_r );
00059 
00060 
00061   private:
00063     ProcessResource _callback;
00064     string _target_distro;
00065   };
00067 
00068   RepoindexFileReader::Impl::Impl(const InputStream &is,
00069                                   const ProcessResource & callback)
00070     : _callback(callback)
00071   {
00072     Reader reader( is );
00073     MIL << "Reading " << is.path() << endl;
00074     reader.foreachNode( bind( &RepoindexFileReader::Impl::consumeNode, this, _1 ) );
00075   }
00076 
00077   // --------------------------------------------------------------------------
00078 
00079   /*
00080    * xpath and multiplicity of processed nodes are included in the code
00081    * for convenience:
00082    *
00083    * // xpath: <xpath> (?|*|+)
00084    *
00085    * if multiplicity is ommited, then the node has multiplicity 'one'.
00086    */
00087 
00088   // --------------------------------------------------------------------------
00089 
00090   bool RepoindexFileReader::Impl::consumeNode( Reader & reader_r )
00091   {
00092     if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
00093     {
00094       // xpath: /repoindex
00095       if ( reader_r->name() == "repoindex" )
00096       {
00097         return true;
00098       }
00099 
00100       // xpath: /repoindex/data (+)
00101       if ( reader_r->name() == "repo" )
00102       {
00103         XmlString s;
00104 
00105         RepoInfo info;
00106 
00107         // enabled or disabled is controlled by the
00108         // reposToEnable/Disable list, unless the
00109         // enabled attribute is set
00110         info.setEnabled(false);
00111 
00112         // Set some defaults that are not contained in the repo information
00113         info.setAutorefresh( true );
00114 
00115         // url and/or path
00116         string url_s;
00117         s = reader_r->getAttribute("url");
00118         if (s.get())
00119           url_s = s.asString();
00120         string path_s;
00121         s = reader_r->getAttribute("path");
00122         if (s.get())
00123           path_s = s.asString();
00124 
00125         if (url_s.empty() && path_s.empty())
00126           throw ParseException(str::form(_("One or both of '%s' or '%s' attributes is required."), "url", "path"));
00128         else if (url_s.empty())
00129           info.setPath(Pathname(string("/repo/") + path_s));
00130         else if (path_s.empty())
00131           info.setBaseUrl(Url(url_s));
00132         else
00133           info.setBaseUrl(Url(url_s + "/repo/" + path_s));
00134 
00135         // required alias
00136         s = reader_r->getAttribute("alias");
00137         if (!s.get())
00138           throw ParseException(str::form(_("Required attribute '%s' is missing."), "alias"));
00139         info.setAlias(s.asString());
00140 
00141         // optional type
00142         s = reader_r->getAttribute("type");
00143         if (s.get())
00144           info.setType(repo::RepoType(s.asString()));
00145 
00146         // optional name
00147         s = reader_r->getAttribute("name");
00148         if (s.get())
00149           info.setName(s.asString());
00150 
00151         // optional targetDistro
00152         s = reader_r->getAttribute("distro_target");
00153         if (s.get())
00154           info.setTargetDistribution(s.asString());
00155 
00156         // optional priority
00157         s = reader_r->getAttribute("priority");
00158         if (s.get()) {
00159           info.setPriority(str::strtonum<unsigned>(s.asString()));
00160         }
00161 
00162         // optional enabled
00163         s = reader_r->getAttribute("enabled");
00164         if (s.get()) {
00165           info.setEnabled(str::strToTrue(s.asString()));
00166         }
00167 
00168         DBG << info << endl;
00169 
00170         // ignore the rest
00171         _callback(info);
00172         return true;
00173       }
00174     }
00175 
00176     return true;
00177   }
00178 
00179 
00181   //
00182   //  CLASS NAME : RepoindexFileReader
00183   //
00185 
00186   RepoindexFileReader::RepoindexFileReader(
00187       const Pathname & repoindex_file, const ProcessResource & callback)
00188     :
00189       _pimpl(new Impl(InputStream(repoindex_file), callback))
00190   {}
00191 
00192   RepoindexFileReader::RepoindexFileReader(
00193        const InputStream &is, const ProcessResource & callback )
00194     : _pimpl(new Impl(is, callback))
00195   {}
00196 
00197   RepoindexFileReader::~RepoindexFileReader()
00198   {}
00199 
00200 
00201   } // ns parser
00202 } // ns zypp
00203 
00204 // vim: set ts=2 sts=2 sw=2 et ai: