libzypp  11.13.5
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: