libzypp  15.28.6
RepomdFileReader.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
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/Date.h"
19 #include "zypp/CheckSum.h"
20 #include "zypp/parser/xml/Reader.h"
21 
23 
24 #undef ZYPP_BASE_LOGGER_LOGGROUP
25 #define ZYPP_BASE_LOGGER_LOGGROUP "parser::yum"
26 
27 using namespace std;
28 using namespace zypp::xml;
30 
31 namespace zypp
32 {
33  namespace parser
34  {
35  namespace yum
36  {
37 
38 
40  //
41  // CLASS NAME : RepomdFileReader::Impl
42  //
44  {
45  public:
46 
51  enum Tag
52  {
60  };
61 
62  public:
64  Impl(const Pathname &repomd_file, const ProcessResource2 & callback )
65  : _tag( tag_NONE )
66  , _type( ResourceType::NONE_e )
67  , _callback( callback )
68  {
69  Reader reader( repomd_file );
70  MIL << "Reading " << repomd_file << endl;
71  reader.foreachNode( bind( &RepomdFileReader::Impl::consumeNode, this, _1 ) );
72  }
74  Impl(const Pathname &repomd_file, const ProcessResource & callback)
75  : Impl( repomd_file, ProcessResource2( bind( callback, _1, _2 ) ) )
76  {}
77 
81  bool consumeNode( Reader & reader_r );
82 
83 
84  private:
87 
90 
92  std::string _typeStr;
93 
96 
99  };
101 
102  /*
103  * xpath and multiplicity of processed nodes are included in the code
104  * for convenience:
105  *
106  * // xpath: <xpath> (?|*|+)
107  *
108  * if multiplicity is ommited, then the node has multiplicity 'one'.
109  */
110 
111  // --------------------------------------------------------------------------
112 
113  bool RepomdFileReader::Impl::consumeNode( Reader & reader_r )
114  {
115  if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
116  {
117  // xpath: /repomd
118  if ( reader_r->name() == "repomd" )
119  {
120  _tag = tag_Repomd;
121  return true;
122  }
123 
124  // xpath: /repomd/data (+)
125  if ( reader_r->name() == "data" )
126  {
127  _tag = tag_Data;
128  _typeStr = reader_r->getAttribute("type").asString();
129  _type = ResourceType(_typeStr);
130  return true;
131  }
132 
133  // xpath: /repomd/location
134  if ( reader_r->name() == "location" )
135  {
136  _tag = tag_Location;
137  _location.setLocation( reader_r->getAttribute("href").asString(), 1 );
138  // ignoring attribute xml:base
139  return true;
140  }
141 
142  // xpath: /repomd/checksum
143  if ( reader_r->name() == "checksum" )
144  {
145  _tag = tag_CheckSum;
146  string checksum_type = reader_r->getAttribute("type").asString() ;
147  string checksum_vaue = reader_r.nodeText().asString();
148  _location.setChecksum( CheckSum( checksum_type, checksum_vaue ) );
149  return true;
150  }
151 
152  // xpath: /repomd/timestamp
153  if ( reader_r->name() == "timestamp" )
154  {
155  // ignore it
156  return true;
157  }
158 
159  // xpath: /repomd/size
160  if ( reader_r->name() == "size" )
161  {
162  string size_value = reader_r.nodeText().asString();
163  zypp::ByteCount size = zypp::ByteCount( str::strtonum<ByteCount::SizeType>( size_value ) );
164  _location.setDownloadSize( size );
165  return true;
166  }
167 
169  }
170 
171  else if ( reader_r->nodeType() == XML_READER_TYPE_END_ELEMENT )
172  {
173  // xpath: /repomd/data
174  if ( reader_r->name() == "data" )
175  {
176  if (_callback)
177  _callback( _location, _type, _typeStr );
178 
179  return true;
180  }
181  }
182 
183  return true;
184  }
185 
186 
188  //
189  // CLASS NAME : RepomdFileReader
190  //
192 
193  RepomdFileReader::RepomdFileReader( const Pathname & repomd_file, const ProcessResource & callback )
194  : _pimpl( new Impl(repomd_file, callback) )
195  {}
196 
197  RepomdFileReader::RepomdFileReader( const Pathname & repomd_file, const ProcessResource2 & callback )
198  : _pimpl( new Impl(repomd_file, callback) )
199  {}
200 
202  {}
203 
204 
205  } // ns yum
206  } // ns parser
207 } // ns zypp
208 
209 // vim: set ts=2 sts=2 sw=2 et ai:
#define MIL
Definition: Logger.h:64
Impl(const Pathname &repomd_file, const ProcessResource2 &callback)
Ctro taking a ProcessResource2 callback.
RepomdFileReader(const Pathname &repomd_file, const ProcessResource &callback)
CTOR.
std::string _typeStr
Type of metadata file (string)
Describes a path on a certain media amongs as the information required to download it...
function< bool(const OnMediaLocation &, const repo::yum::ResourceType &, const std::string &)> ProcessResource2
Alternate callback also receiving the ResourceType as string.
Tag
Enumeration of repomd.xml tags.
Store and operate with byte count.
Definition: ByteCount.h:30
repo::yum::ResourceType _type
Type of metadata file as enum of well known repoinded.xml entries.
const ProcessCredentials & _callback
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
OnMediaLocation _location
Location of metadata file.
ProcessResource2 _callback
Function for processing collected data.
Tag _tag
Used to remember currently processed tag.
Impl(const Pathname &repomd_file, const ProcessResource &callback)
Interface of repomd.xml file reader.
function< bool(const OnMediaLocation &, const repo::yum::ResourceType &)> ProcessResource
Callbacl taking OnMediaLocation and repo::yum::ResourceType.