libzypp  13.10.6
RepoindexFileReader.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 #include "zypp/base/Gettext.h"
17 #include "zypp/base/InputStream.h"
18 
19 #include "zypp/Pathname.h"
20 
21 #include "zypp/parser/xml/Reader.h"
23 
24 #include "zypp/RepoInfo.h"
25 
27 
28 
29 #undef ZYPP_BASE_LOGGER_LOGGROUP
30 #define ZYPP_BASE_LOGGER_LOGGROUP "parser"
31 
32 using namespace std;
33 using namespace zypp::xml;
34 
35 namespace zypp
36 {
37  namespace parser
38  {
39 
40 
42  //
43  // CLASS NAME : RepoindexFileReader::Impl
44  //
46  {
47  public:
53  Impl(const InputStream &is, const ProcessResource & callback);
54 
58  bool consumeNode( Reader & reader_r );
59 
60 
61  private:
65  };
67 
68  RepoindexFileReader::Impl::Impl(const InputStream &is,
69  const ProcessResource & callback)
70  : _callback(callback)
71  {
72  Reader reader( is );
73  MIL << "Reading " << is.path() << endl;
74  reader.foreachNode( bind( &RepoindexFileReader::Impl::consumeNode, this, _1 ) );
75  }
76 
77  // --------------------------------------------------------------------------
78 
79  /*
80  * xpath and multiplicity of processed nodes are included in the code
81  * for convenience:
82  *
83  * // xpath: <xpath> (?|*|+)
84  *
85  * if multiplicity is ommited, then the node has multiplicity 'one'.
86  */
87 
88  // --------------------------------------------------------------------------
89 
91  {
92  if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
93  {
94  // xpath: /repoindex
95  if ( reader_r->name() == "repoindex" )
96  {
97  return true;
98  }
99 
100  // xpath: /repoindex/data (+)
101  if ( reader_r->name() == "repo" )
102  {
103  XmlString s;
104 
105  RepoInfo info;
106 
107  // enabled or disabled is controlled by the
108  // reposToEnable/Disable list, unless the
109  // enabled attribute is set
110  info.setEnabled(false);
111 
112  // Set some defaults that are not contained in the repo information
113  info.setAutorefresh( true );
114 
115  // url and/or path
116  string url_s;
117  s = reader_r->getAttribute("url");
118  if (s.get())
119  url_s = s.asString();
120  string path_s;
121  s = reader_r->getAttribute("path");
122  if (s.get())
123  path_s = s.asString();
124 
125  if (url_s.empty() && path_s.empty())
126  throw ParseException(str::form(_("One or both of '%s' or '%s' attributes is required."), "url", "path"));
128  else if (url_s.empty())
129  info.setPath(Pathname(string("/repo/") + path_s));
130  else if (path_s.empty())
131  info.setBaseUrl(Url(url_s));
132  else
133  info.setBaseUrl(Url(url_s + "/repo/" + path_s));
134 
135  // required alias
136  s = reader_r->getAttribute("alias");
137  if (!s.get())
138  throw ParseException(str::form(_("Required attribute '%s' is missing."), "alias"));
139  info.setAlias(s.asString());
140 
141  // optional type
142  s = reader_r->getAttribute("type");
143  if (s.get())
144  info.setType(repo::RepoType(s.asString()));
145 
146  // optional name
147  s = reader_r->getAttribute("name");
148  if (s.get())
149  info.setName(s.asString());
150 
151  // optional targetDistro
152  s = reader_r->getAttribute("distro_target");
153  if (s.get())
155 
156  // optional priority
157  s = reader_r->getAttribute("priority");
158  if (s.get()) {
159  info.setPriority(str::strtonum<unsigned>(s.asString()));
160  }
161 
162  // optional enabled
163  s = reader_r->getAttribute("enabled");
164  if (s.get()) {
166  }
167 
168  DBG << info << endl;
169 
170  // ignore the rest
171  _callback(info);
172  return true;
173  }
174  }
175 
176  return true;
177  }
178 
179 
181  //
182  // CLASS NAME : RepoindexFileReader
183  //
185 
187  const Pathname & repoindex_file, const ProcessResource & callback)
188  :
189  _pimpl(new Impl(InputStream(repoindex_file), callback))
190  {}
191 
193  const InputStream &is, const ProcessResource & callback )
194  : _pimpl(new Impl(is, callback))
195  {}
196 
198  {}
199 
200 
201  } // ns parser
202 } // ns zypp
203 
204 // vim: set ts=2 sts=2 sw=2 et ai:
Interface to gettext.
#define MIL
Definition: Logger.h:47
function< bool(const RepoInfo &)> ProcessResource
Callback definition.
ProcessResource _callback
Function for processing collected data.
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition: RepoInfoBase.cc:94
void setPriority(unsigned newval_r)
Set repository priority for solver.
Definition: RepoInfo.cc:164
const xmlChar * get() const
Access the xmlChar *.
Definition: XmlString.h:61
void setEnabled(bool enabled)
enable or disable the repository
Definition: RepoInfoBase.cc:89
void setAlias(const std::string &alias)
set the repository alias
Definition: RepoInfoBase.cc:99
xmlChar * wrapper.
Definition: XmlString.h:40
What is known about a repository.
Definition: RepoInfo.h:66
Helper to create and pass std::istream.
Definition: InputStream.h:56
void setBaseUrl(const Url &url)
Clears current base URL list and adds url.
Definition: RepoInfo.cc:179
Interface of repoindex.xml file reader.
RepoindexFileReader(const zypp::Pathname &repoindexFile, const ProcessResource &callback)
CTOR.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
void setPath(const Pathname &path)
set the product path.
Definition: RepoInfo.cc:185
const Pathname & path() const
Path to the input file or empty if no file.
Definition: InputStream.h:111
void setType(const repo::RepoType &t)
set the repository type
Definition: RepoInfo.cc:188
XmlString name() const
The qualified name of the node, equal to Prefix :LocalName.
Definition: Node.h:115
#define _(MSG)
Return translated text.
Definition: Gettext.h:21
std::string asString() const
Explicit conversion to std::string.
Definition: XmlString.h:77
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition: String.cc:61
std::string form(const char *format,...)
Printf style construction of std::string.
Definition: String.cc:34
XmlString getAttribute(const char *name_r) const
Provides a copy of the attribute value with the specified qualified name.
Definition: Node.h:71
void setTargetDistribution(const std::string &targetDistribution)
Sets the distribution for which is this repository meant.
Definition: RepoInfo.cc:207
bool foreachNode(ProcessNode fnc_r)
Definition: Reader.h:144
void setName(const std::string &name)
set the repository name
bool consumeNode(Reader &reader_r)
Callback provided to the XML parser.
RW_pointer< Impl, rw_pointer::Scoped< Impl > > _pimpl
NodeType nodeType() const
Get the node type of the current node.
Definition: Node.h:123
Url manipulation class.
Definition: Url.h:87
#define DBG
Definition: Logger.h:46
Repository type enumeration.
Definition: RepoType.h:27
xmlTextReader based interface to iterate xml streams.
Definition: Reader.h:95