~thopiekar/zypper/libzypp-manual-import

« back to all changes in this revision

Viewing changes to zypp/parser/RepoindexFileReader.cc

  • Committer: Thomas-Karl Pietrowski
  • Date: 2014-01-29 22:44:28 UTC
  • Revision ID: thopiekar@googlemail.com-20140129224428-gpcqnsdakby362n8
firstĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*---------------------------------------------------------------------\
 
2
|                          ____ _   __ __ ___                          |
 
3
|                         |__  / \ / / . \ . \                         |
 
4
|                           / / \ V /|  _/  _/                         |
 
5
|                          / /__ | | | | | |                           |
 
6
|                         /_____||_| |_| |_|                           |
 
7
|                                                                      |
 
8
\---------------------------------------------------------------------*/
 
9
/** \file zypp/parser/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
#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"
 
22
#include "zypp/parser/ParseException.h"
 
23
 
 
24
#include "zypp/RepoInfo.h"
 
25
 
 
26
#include "zypp/parser/RepoindexFileReader.h"
 
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
 
 
41
  ///////////////////////////////////////////////////////////////////////
 
42
  //
 
43
  //  CLASS NAME : RepoindexFileReader::Impl
 
44
  //
 
45
  class RepoindexFileReader::Impl : private base::NonCopyable
 
46
  {
 
47
  public:
 
48
    /**
 
49
     * CTOR
 
50
     *
 
51
     * \see RepoindexFileReader::RepoindexFileReader(Pathname,ProcessResource)
 
52
     */
 
53
    Impl(const InputStream &is, const ProcessResource & callback);
 
54
 
 
55
    /**
 
56
     * Callback provided to the XML parser.
 
57
     */
 
58
    bool consumeNode( Reader & reader_r );
 
59
 
 
60
 
 
61
  private:
 
62
    /** Function for processing collected data. Passed-in through constructor. */
 
63
    ProcessResource _callback;
 
64
    string _target_distro;
 
65
  };
 
66
  ///////////////////////////////////////////////////////////////////////
 
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
 
 
90
  bool RepoindexFileReader::Impl::consumeNode( Reader & reader_r )
 
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"));
 
127
        //! \todo FIXME this hardcodes the "/repo/" fragment - should not be if we want it to be usable by others!
 
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())
 
154
          info.setTargetDistribution(s.asString());
 
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()) {
 
165
          info.setEnabled(str::strToTrue(s.asString()));
 
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
 
 
180
  ///////////////////////////////////////////////////////////////////
 
181
  //
 
182
  //  CLASS NAME : RepoindexFileReader
 
183
  //
 
184
  ///////////////////////////////////////////////////////////////////
 
185
 
 
186
  RepoindexFileReader::RepoindexFileReader(
 
187
      const Pathname & repoindex_file, const ProcessResource & callback)
 
188
    :
 
189
      _pimpl(new Impl(InputStream(repoindex_file), callback))
 
190
  {}
 
191
 
 
192
  RepoindexFileReader::RepoindexFileReader(
 
193
       const InputStream &is, const ProcessResource & callback )
 
194
    : _pimpl(new Impl(is, callback))
 
195
  {}
 
196
 
 
197
  RepoindexFileReader::~RepoindexFileReader()
 
198
  {}
 
199
 
 
200
 
 
201
  } // ns parser
 
202
} // ns zypp
 
203
 
 
204
// vim: set ts=2 sts=2 sw=2 et ai: