1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/parser/xml/Reader.h
12 #ifndef ZYPP_PARSER_XML_READER_H
13 #define ZYPP_PARSER_XML_READER_H
17 #include "zypp/base/NonCopyable.h"
18 #include "zypp/base/InputStream.h"
19 #include "zypp/base/Function.h"
21 #include "zypp/parser/xml/Node.h"
23 ///////////////////////////////////////////////////////////////////
25 { /////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
28 { /////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////
32 // CLASS NAME : Validate
34 /** xmlTextReader document validation.
35 * \todo Implement RelaxNG and W3C XSD
39 static Validate none()
40 { return Validate(); }
42 ///////////////////////////////////////////////////////////////////
44 ///////////////////////////////////////////////////////////////////
46 // CLASS NAME : Reader
48 /** xmlTextReader based interface to iterate xml streams.
52 * bool consumeNode( XML::Reader & reader_r )
54 * DBG << *reader_r << endl;
58 * // Consume all nodes (omitting attributes)
63 * XML::Reader reader( "/Local/repodata/repomd.xml" );
64 * reader.foreachNode( consumeNode );
66 * catch ( const Exception & )
67 * { ; } // parse error
73 * bool consumeNodeAndAttribute( XML::Reader & reader_r )
75 * consumeNode( reader_r );
76 * return reader_r.foreachNodeAttribute( consumeNode );
79 * // Consume all nodes and thair attributes.
82 * Pathname repodata( "/Local/repodata/repomd.xml" );
85 * XML::Reader reader( "/Local/repodata/repomd.xml" );
86 * reader.foreachNode( consumeNodeAndAttribute );
88 * // reader.foreachNodeOrAttribute( consumeNode )
90 * catch ( const Exception & )
91 * { ; } // parse error
95 class Reader : private zypp::base::NonCopyable
98 /** Ctor. Setup xmlTextReader and advance to the 1st Node. */
99 Reader( const InputStream & stream_r,
100 const Validate & validate_r = Validate::none() );
108 * If the curent node is not empty, advances the reader to the next
109 * node, and returns the value
111 * \note if the node has a xml subtree you will probably jump to that node
112 * and get a empty text value back. Use it only if you are sure the node
113 * has no XML subtree.
115 XmlString nodeText();
121 bool nextNodeAttribute();
124 bool nextNodeOrAttribute()
125 { return( nextNodeAttribute() || nextNode() ); }
129 { return( _node.readState() == XML_TEXTREADER_MODE_CLOSED ); }
132 const Node & operator*() const
136 const Node * operator->() const
141 typedef function<bool( Reader & )> ProcessNode;
144 bool foreachNode( ProcessNode fnc_r )
146 if ( _node.isAttribute() )
148 for ( ; ! atEnd(); nextNode() )
150 if ( ! fnc_r( *this ) )
157 bool foreachNodeAttribute( ProcessNode fnc_r )
159 if ( _node.isAttribute() && ! fnc_r( *this ) )
161 while( nextNodeAttribute() )
163 if ( ! fnc_r( *this ) )
170 bool foreachNodeOrAttribute( ProcessNode fnc_r )
172 for ( ; ! atEnd(); nextNodeOrAttribute() )
174 if ( ! fnc_r( *this ) )
182 bool seekToNode( int depth_r, const std::string & name_r );
185 bool seekToEndNode( int depth_r, const std::string & name_r );
192 xmlTextReaderPtr _reader;
195 ///////////////////////////////////////////////////////////////////
197 /////////////////////////////////////////////////////////////////
199 ///////////////////////////////////////////////////////////////////
200 /////////////////////////////////////////////////////////////////
202 ///////////////////////////////////////////////////////////////////
203 #endif // ZYPP_PARSER_XML_READER_H