(no commit message)
[platform/upstream/libzypp.git] / devel / devel.ma / Xml.cc
1 #include "Tools.h"
2
3 extern "C"
4 {
5 #include <libxml/xmlreader.h>
6 #include <libxml/xmlerror.h>
7 }
8 #include <iostream>
9 #include <fstream>
10 #include <list>
11 #include <map>
12 #include <set>
13
14 #include <boost/call_traits.hpp>
15
16 #include <zypp/base/LogControl.h>
17 #include <zypp/base/LogTools.h>
18
19 #include "zypp/base/Exception.h"
20 #include "zypp/base/InputStream.h"
21 #include "zypp/base/DefaultIntegral.h"
22 #include <zypp/base/Function.h>
23 #include <zypp/base/Iterator.h>
24 #include <zypp/Pathname.h>
25 #include <zypp/Edition.h>
26 #include <zypp/CheckSum.h>
27 #include <zypp/Date.h>
28 #include <zypp/Rel.h>
29
30 #include "zypp/parser/xml/Reader.h"
31
32 using namespace std;
33 using namespace zypp;
34
35 ///////////////////////////////////////////////////////////////////
36
37 static const Pathname sysRoot( "/Local/ROOT" );
38
39 ///////////////////////////////////////////////////////////////////
40
41 template<class _Cl>
42     void ti( const _Cl & c )
43 {
44   SEC << __PRETTY_FUNCTION__ << endl;
45 }
46
47 ///////////////////////////////////////////////////////////////////
48
49 bool dump( xml::Reader & reader_r )
50 {
51   MIL << *reader_r << endl;
52 }
53
54 ///////////////////////////////////////////////////////////////////
55
56 bool dumpNode( xml::Reader & reader_r )
57 {
58   switch ( reader_r->nodeType() )
59   {
60     case XML_READER_TYPE_ELEMENT:
61       MIL << *reader_r << endl;
62       for ( int i = 0; i < reader_r->attributeCount(); ++i )
63       {
64         MIL << " attr no " << i << " '" << reader_r->getAttributeNo( i ) << "'" << endl;
65       }
66       break;
67
68     case XML_READER_TYPE_ATTRIBUTE:
69       WAR << *reader_r << endl;
70       break;
71
72     case XML_READER_TYPE_TEXT:
73     case XML_READER_TYPE_CDATA:
74       DBG << *reader_r << endl;
75       break;
76
77     default:
78       //ERR << *reader_r << endl;
79       break;
80   }
81   return true;
82 }
83
84 ///////////////////////////////////////////////////////////////////
85
86 bool dumpEd( xml::Reader & reader_r )
87 {
88   static int num = 5;
89   if ( reader_r->nodeType() == XML_READER_TYPE_ELEMENT
90        && reader_r->name() == "version" )
91     {
92       MIL << *reader_r << endl;
93       DBG << reader_r->getAttribute( "rel" ) << endl;
94       ERR << *reader_r << endl;
95       DBG << reader_r->getAttribute( "ver" ) << endl;
96       ERR << *reader_r << endl;
97       DBG << reader_r->getAttribute( "epoch" ) << endl;
98       ERR << *reader_r << endl;
99       WAR << Edition( reader_r->getAttribute( "ver" ).asString(),
100                       reader_r->getAttribute( "rel" ).asString(),
101                       reader_r->getAttribute( "epoch" ).asString() ) << endl;
102       --num;
103     }
104   return num;
105 }
106
107 ///////////////////////////////////////////////////////////////////
108
109 template<class _OutputIterator>
110   struct DumpDeps
111   {
112     DumpDeps( _OutputIterator result_r )
113     : _result( result_r )
114     {}
115
116     bool operator()( xml::Reader & reader_r )
117     {
118       if ( reader_r->nodeType()     == XML_READER_TYPE_ELEMENT
119            && reader_r->prefix()    == "rpm"
120            && reader_r->localName() == "entry" )
121         {
122           string n( reader_r->getAttribute( "name" ).asString() );
123           Rel op( reader_r->getAttribute( "flags" ).asString() );
124           if ( op != Rel::ANY )
125             {
126               n += " ";
127               n += op.asString();
128               n += " ";
129               n += reader_r->getAttribute( "ver" ).asString();
130               n += "-";
131               n += reader_r->getAttribute( "rel" ).asString();
132             }
133           *_result = n;
134           ++_result;
135         }
136       return true;
137     }
138
139     _OutputIterator _result;
140   };
141
142 template<class _OutputIterator>
143   DumpDeps<_OutputIterator> dumpDeps( _OutputIterator result_r )
144   { return DumpDeps<_OutputIterator>( result_r ); }
145
146 ///////////////////////////////////////////////////////////////////
147
148 /******************************************************************
149 **
150 **      FUNCTION NAME : main
151 **      FUNCTION TYPE : int
152 */
153 int main( int argc, char * argv[] )
154 {
155   INT << "===[START]==========================================" << endl;
156
157   --argc;
158   ++argv;
159
160   Pathname repodata;
161   if ( argc )
162     {
163       repodata = *argv;
164     }
165   else
166     {
167       repodata = "/Local/FACTORY/repodata";
168       repodata /= "primary.xml";
169     }
170
171   if ( 1 )
172     {
173       Measure m( "Parse" );
174       xml::Reader reader( repodata );
175       if ( 0 )
176         reader.foreachNode( dumpNode );
177       else
178         reader.foreachNodeOrAttribute( dumpNode );
179     }
180
181   INT << "===[END]============================================" << endl << endl;
182   return 0;
183 }
184
185 /*
186 <?xml version="1.0" encoding="UTF-8"?>
187 <metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="23230">
188 <package type="rpm">
189 <name>fam-devel</name>
190 <arch>ppc</arch>
191 <version epoch="0" ver="2.6.10" rel="141"/>
192 <checksum type="sha" pkgid="YES">59d6a65cdadd911fe8ceee87740a54305b2ab053</checksum>
193 <summary>Include Files and Libraries Mandatory for Development</summary>
194 <description>Fam is a file alteration monitoring service. This means that you can
195
196 foreachNode( dumpNode )
197 =======================
198 START MEASURE(Parse)
199 0:ELEMENT <metadata>  [attr 3]
200  attr no 0 'http://linux.duke.edu/metadata/common'
201  attr no 1 'http://linux.duke.edu/metadata/rpm'
202  attr no 2 '23230'
203 1: ELEMENT <package>  [attr 1]
204  attr no 0 'rpm'
205 2:  ELEMENT <name>  [noattr]
206 3:   TEXT <#text>  [noattr] {fam-devel}
207 2:  ELEMENT <arch>  [noattr]
208 3:   TEXT <#text>  [noattr] {ppc}
209 2:  ELEMENT <version>  [attr 3|empty]
210  attr no 0 '0'
211  attr no 1 '2.6.10'
212  attr no 2 '141'
213 2:  ELEMENT <checksum>  [attr 2]
214  attr no 0 'sha'
215  attr no 1 'YES'
216 3:   TEXT <#text>  [noattr] {59d6a65cdadd911fe8ceee87740a54305b2ab053}
217 2:  ELEMENT <summary>  [noattr]
218 3:   TEXT <#text>  [noattr] {Include Files and Libraries Mandatory for Development}
219 2:  ELEMENT <description>  [noattr]
220 3:   TEXT <#text>  [noattr] {Fam is a file alteration monitoring service. This means that you can
221
222 foreachNodeOrAttribute( dumpNode )
223 ==================================
224 START MEASURE(Parse)
225 0:ELEMENT <metadata>  [attr 3]
226  attr no 0 'http://linux.duke.edu/metadata/common'
227  attr no 1 'http://linux.duke.edu/metadata/rpm'
228  attr no 2 '23230'
229 1: ATTRIBUTE <xmlns>  [noattr] {http://linux.duke.edu/metadata/common}
230 1: ATTRIBUTE <xmlns:rpm>  [noattr] {http://linux.duke.edu/metadata/rpm}
231 1: ATTRIBUTE <packages>  [noattr] {23230}
232 1: ELEMENT <package>  [attr 1]
233  attr no 0 'rpm'
234 2:  ATTRIBUTE <type>  [noattr] {rpm}
235 2:  ELEMENT <name>  [noattr]
236 3:   TEXT <#text>  [noattr] {fam-devel}
237 2:  ELEMENT <arch>  [noattr]
238 3:   TEXT <#text>  [noattr] {ppc}
239 2:  ELEMENT <version>  [attr 3|empty]
240  attr no 0 '0'
241  attr no 1 '2.6.10'
242  attr no 2 '141'
243 3:   ATTRIBUTE <epoch>  [noattr] {0}
244 3:   ATTRIBUTE <ver>  [noattr] {2.6.10}
245 3:   ATTRIBUTE <rel>  [noattr] {141}
246 2:  ELEMENT <checksum>  [attr 2]
247  attr no 0 'sha'
248  attr no 1 'YES'
249 3:   ATTRIBUTE <type>  [noattr] {sha}
250 3:   ATTRIBUTE <pkgid>  [noattr] {YES}
251 3:   TEXT <#text>  [noattr] {59d6a65cdadd911fe8ceee87740a54305b2ab053}
252 3:   ATTRIBUTE <type>  [noattr] {sha}
253 3:   ATTRIBUTE <pkgid>  [noattr] {YES}
254 2:  ELEMENT <summary>  [noattr]
255 3:   TEXT <#text>  [noattr] {Include Files and Libraries Mandatory for Development}
256 2:  ELEMENT <description>  [noattr]
257 3:   TEXT <#text>  [noattr] {Fam is a file alteration monitoring service. This means that you can
258
259 */