libzypp 17.25.7
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>
21
23
24#undef ZYPP_BASE_LOGGER_LOGGROUP
25#define ZYPP_BASE_LOGGER_LOGGROUP "parser::yum"
26
27using std::endl;
28using namespace zypp::xml;
29
30namespace zypp
31{
32 namespace parser
33 {
34 namespace yum
35 {
36
37
39 //
40 // CLASS NAME : RepomdFileReader::Impl
41 //
43 {
44 public:
46 Impl(const Pathname &repomd_file, const ProcessResource & callback )
47 : _callback( callback )
48 {
49 Reader reader( repomd_file );
50 MIL << "Reading " << repomd_file << endl;
51 reader.foreachNode( bind( &RepomdFileReader::Impl::consumeNode, this, _1 ) );
52 }
53
57 bool consumeNode( Reader & reader_r );
58
59 private:
62 { return CheckSum( reader_r->getAttribute("type").asString(), reader_r.nodeText().asString() ); }
63
65 ByteCount getSize( Reader & reader_r )
66 { return ByteCount( str::strtonum<ByteCount::SizeType>( reader_r.nodeText().asString() ) ); }
67
68
69 private:
72
74 std::string _typeStr;
75
78 };
80
81 /*
82 * xpath and multiplicity of processed nodes are included in the code
83 * for convenience:
84 *
85 * // xpath: <xpath> (?|*|+)
86 *
87 * if multiplicity is ommited, then the node has multiplicity 'one'.
88 */
89
90 // --------------------------------------------------------------------------
91
93 {
94 if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
95 {
96 // xpath: /repomd
97 if ( reader_r->name() == "repomd" )
98 {
99 return true;
100 }
101
102 // xpath: /repomd/data (+)
103 if ( reader_r->name() == "data" )
104 {
105 _typeStr = reader_r->getAttribute("type").asString();
106 return true;
107 }
108
109 // xpath: /repomd/location
110 if ( reader_r->name() == "location" )
111 {
112 _location.setLocation( reader_r->getAttribute("href").asString(), 1 );
113 // ignoring attribute xml:base
114 return true;
115 }
116
117 // xpath: /repomd/checksum
118 if ( reader_r->name() == "checksum" )
119 {
120 _location.setChecksum( getChecksum( reader_r ) );
121 return true;
122 }
123
124 // xpath: /repomd/header-checksum
125 if ( reader_r->name() == "header-checksum" )
126 {
128 return true;
129 }
130
131 // xpath: /repomd/timestamp
132 if ( reader_r->name() == "timestamp" )
133 {
134 // ignore it
135 return true;
136 }
137
138 // xpath: /repomd/size
139 if ( reader_r->name() == "size" )
140 {
141 _location.setDownloadSize( getSize( reader_r ) );
142 return true;
143 }
144
145 // xpath: /repomd/header-size
146 if ( reader_r->name() == "header-size" )
147 {
148 _location.setHeaderSize( getSize( reader_r ) );
149 return true;
150 }
151
153 }
154
155 else if ( reader_r->nodeType() == XML_READER_TYPE_END_ELEMENT )
156 {
157 // xpath: /repomd/data
158 if ( reader_r->name() == "data" )
159 {
160 if (_callback) {
161 _callback( std::move(_location), _typeStr );
163 _typeStr.clear();
164 }
165 return true;
166 }
167 }
168
169 return true;
170 }
171
172
174 //
175 // CLASS NAME : RepomdFileReader
176 //
178
179 RepomdFileReader::RepomdFileReader( const Pathname & repomd_file, const ProcessResource & callback )
180 : _pimpl( new Impl(repomd_file, callback) )
181 {}
182
184 {}
185
186
187 } // ns yum
188 } // ns parser
189} // ns zypp
190
191// vim: set ts=2 sts=2 sw=2 et ai:
#define MIL
Definition: Logger.h:79
Interface of repomd.xml file reader.
Store and operate with byte count.
Definition: ByteCount.h:31
Describes a resource file located on a medium.
OnMediaLocation & setDownloadSize(ByteCount val_r)
Set the downloadSize.
OnMediaLocation & setChecksum(CheckSum val_r)
Set the checksum.
OnMediaLocation & setHeaderSize(ByteCount val_r)
Set the headerSize.
OnMediaLocation & setLocation(Pathname filename_r, unsigned medianr_r=1)
Set filename_r and medianr_r (defaults to 1).
OnMediaLocation & setHeaderChecksum(CheckSum val_r)
Set the headerChecksum.
CheckSum getChecksum(Reader &reader_r)
Retrieve a checksum node.
bool consumeNode(Reader &reader_r)
Callback provided to the XML parser.
Impl(const Pathname &repomd_file, const ProcessResource &callback)
Ctro taking a ProcessResource callback.
ByteCount getSize(Reader &reader_r)
Retrieve a size node.
std::string _typeStr
The resource type string.
OnMediaLocation _location
Location of metadata file.
ProcessResource _callback
Function for processing collected data.
function< bool(OnMediaLocation &&, const std::string &)> ProcessResource
Callback taking OnMediaLocation and the resource type string.
RW_pointer< Impl, rw_pointer::Scoped< Impl > > _pimpl
RepomdFileReader(const Pathname &repomd_file, const ProcessResource &callback)
CTOR.
NodeType nodeType() const
Get the node type of the current node.
Definition: Node.h:126
XmlString getAttribute(const char *name_r) const
Provides a copy of the attribute value with the specified qualified name.
Definition: Node.h:71
XmlString name() const
The qualified name of the node, equal to Prefix :LocalName.
Definition: Node.h:118
xmlTextReader based interface to iterate xml streams.
Definition: Reader.h:96
XmlString nodeText()
If the current node is not empty, advances the reader to the next node, and returns the value.
Definition: Reader.cc:140
bool foreachNode(ProcessNode fnc_r)
Definition: Reader.h:144
std::string asString() const
Explicit conversion to std::string.
Definition: XmlString.h:77
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2