b5ad479e11141fd6e9235f771e8c80ae6a6daa58
[platform/upstream/libzypp.git] / zypp / parser / yum / PatchesFileReader.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/parser/yum/PatchesFileReader.cc
10  * Implementation of patches.xml file reader.
11  */
12 #include <iostream>
13
14 #include "zypp/base/String.h"
15 #include "zypp/base/Logger.h"
16
17 #include "zypp/Date.h"
18 #include "zypp/CheckSum.h"
19 #include "zypp/OnMediaLocation.h"
20
21 #include "zypp/parser/xml/Reader.h"
22 #include "zypp/parser/yum/PatchesFileReader.h"
23
24
25 using namespace std;
26 using namespace zypp::xml;
27
28 namespace zypp
29 {
30   namespace parser
31   {
32     namespace yum
33     {
34
35
36   enum Tag
37   {
38     tag_NONE,
39     tag_Patches,
40     tag_Patch,
41     tag_Location,
42     tag_CheckSum,
43     tag_Timestamp,
44     tag_OpenCheckSum
45   };
46
47
48   ///////////////////////////////////////////////////////////////////////
49   //
50   //  CLASS NAME : PatchesFileReader::Impl
51   //
52   class PatchesFileReader::Impl : private base::NonCopyable
53   {
54   public:
55    /**
56     * CTOR
57     */
58     Impl(const Pathname &patches_file, const ProcessResource & callback);
59
60     /**
61     * Callback provided to the XML parser. Don't use it.
62     */
63     bool consumeNode( Reader & reader_r );
64
65   private:
66     OnMediaLocation _location;
67     Tag _tag;
68     std::string _id;
69     ProcessResource _callback;
70     CheckSum _checksum;
71     std::string _checksum_type;
72     Date _timestamp;
73   };
74   ///////////////////////////////////////////////////////////////////////
75
76
77   PatchesFileReader::Impl::Impl(const Pathname & patches_file,
78                                 const ProcessResource & callback)
79     : _tag(tag_NONE), _callback(callback)
80   {
81     Reader reader( patches_file );
82     MIL << "Reading " << patches_file << endl;
83     reader.foreachNode(bind( &PatchesFileReader::Impl::consumeNode, this, _1 ));
84   }
85
86   // --------------------------------------------------------------------------
87
88   bool PatchesFileReader::Impl::consumeNode( Reader & reader_r )
89   {
90     //MIL << reader_r->name() << endl;
91     std::string data_type;
92     if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT )
93     {
94       if ( reader_r->name() == "patches" )
95       {
96         _tag = tag_Patches;
97         return true;
98       }
99       if ( reader_r->name() == "patch" )
100       {
101         _tag = tag_Patch;
102         _id = reader_r->getAttribute("id").asString();
103         return true;
104       }
105       if ( reader_r->name() == "location" )
106       {
107         _tag = tag_Location;
108         _location.setLocation( reader_r->getAttribute("href").asString(), 1 );
109         return true;
110       }
111       if ( reader_r->name() == "checksum" )
112       {
113         _tag = tag_CheckSum;
114         string checksum_type = reader_r->getAttribute("type").asString() ;
115         string checksum_vaue = reader_r.nodeText().asString();
116         _location.setChecksum( CheckSum( checksum_type, checksum_vaue ) );
117         return true;
118       }
119       if ( reader_r->name() == "timestamp" )
120       {
121         // ignore it
122         return true;
123       }
124     }
125     else if ( reader_r->nodeType() == XML_READER_TYPE_END_ELEMENT )
126     {
127       //MIL << "end element" << endl;
128       if ( reader_r->name() == "patch" )
129         _callback( _location, _id );
130       return true;
131     }
132     return true;
133   }
134
135
136   ///////////////////////////////////////////////////////////////////
137   //
138   //  CLASS NAME : PatchesFileReader
139   //
140   ///////////////////////////////////////////////////////////////////
141
142   PatchesFileReader::PatchesFileReader(const Pathname & patches_file,
143                                        const ProcessResource & callback)
144     : _pimpl(new Impl(patches_file, callback))
145   {}
146
147   PatchesFileReader::~PatchesFileReader()
148   {}
149
150
151     } // ns yum
152   } // ns parser
153 } // ns zypp
154
155 // vim: set ts=2 sts=2 sw=2 et ai: