initial Service implementation. Also little refactor of RepoManager.
[platform/upstream/libzypp.git] / zypp / parser / RepoindexFileReader.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/parser/yum/RepoindexFileReader.cc
10  * Implementation of repoindex.xml file reader.
11  */
12 #include <iostream>
13
14 #include "zypp/base/String.h"
15 #include "zypp/base/Logger.h"
16
17 #include "zypp/Pathname.h"
18 #include "zypp/parser/xml/Reader.h"
19
20 #include "zypp/parser/RepoindexFileReader.h"
21
22 #undef ZYPP_BASE_LOGGER_LOGGROUP
23 #define ZYPP_BASE_LOGGER_LOGGROUP "parser"
24
25 using namespace std;
26 using namespace zypp::xml;
27
28 namespace zypp
29 {
30   namespace parser
31   {
32
33
34   ///////////////////////////////////////////////////////////////////////
35   //
36   //  CLASS NAME : RepoindexFileReader::Impl
37   //
38   class RepoindexFileReader::Impl : private base::NonCopyable
39   {
40   public:
41     /**
42      * CTOR
43      *
44      * \see RepoindexFileReader::RepoindexFileReader(Pathname,ProcessResource)
45      */
46     Impl(const Pathname &repoindex_file, const ProcessResource & callback);
47
48     /**
49      * Callback provided to the XML parser.
50      */
51     bool consumeNode( Reader & reader_r );
52
53
54   private:
55     /** Function for processing collected data. Passed-in through constructor. */
56     ProcessResource _callback;
57   };
58   ///////////////////////////////////////////////////////////////////////
59
60   RepoindexFileReader::Impl::Impl(
61       const Pathname &repoindex_file, const ProcessResource & callback)
62     : _callback(callback)
63   {
64     Reader reader( repoindex_file );
65     MIL << "Reading " << repoindex_file << endl;
66     reader.foreachNode( bind( &RepoindexFileReader::Impl::consumeNode, this, _1 ) );
67   }
68
69   // --------------------------------------------------------------------------
70
71   /*
72    * xpath and multiplicity of processed nodes are included in the code
73    * for convenience:
74    *
75    * // xpath: <xpath> (?|*|+)
76    *
77    * if multiplicity is ommited, then the node has multiplicity 'one'.
78    */
79
80   // --------------------------------------------------------------------------
81
82   bool RepoindexFileReader::Impl::consumeNode( Reader & reader_r )
83   {
84     if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
85     {
86       // xpath: /repoindex
87       if ( reader_r->name() == "repoindex" )
88       {
89         return true;
90       }
91
92       // xpath: /repoindex/data (+)
93       if ( reader_r->name() == "repo" )
94       {
95         RepoInfo info;
96         info.setPath(Pathname(string("/repo/")+reader_r->getAttribute("path").asString()));
97         info.setAlias(reader_r->getAttribute("alias").asString());
98         info.setType(repo::RepoType::RPMMD_e); //TODO hardwired rpmmd???
99         XmlString s = reader_r->getAttribute("name");
100         if (s.get()) //name setted so also set it
101           info.setName(s.asString());
102         //ignore rest
103         _callback(info);
104         return true;
105       }
106     }
107
108     return true;
109   }
110
111
112   ///////////////////////////////////////////////////////////////////
113   //
114   //  CLASS NAME : RepoindexFileReader
115   //
116   ///////////////////////////////////////////////////////////////////
117
118   RepoindexFileReader::RepoindexFileReader(
119       const Pathname & repoindex_file, const ProcessResource & callback)
120     :
121       _pimpl(new Impl(repoindex_file, callback))
122   {}
123
124   RepoindexFileReader::~RepoindexFileReader()
125   {}
126
127
128   } // ns parser
129 } // ns zypp
130
131 // vim: set ts=2 sts=2 sw=2 et ai: