~ubuntu-branches/ubuntu/trusty/ticcutils/trusty-proposed

« back to all changes in this revision

Viewing changes to include/ticcutils/XMLtools.h

  • Committer: Package Import Robot
  • Author(s): Ko van der Sloot
  • Date: 2013-04-18 15:14:58 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130418151458-o3zeb8f0676003y3
Tags: 0.4-3
debian/control: also added Replaces: libticcutils1-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  $Id: XMLtools.h 15892 2013-04-03 08:49:18Z sloot $
 
3
  $URL: https://ilk.uvt.nl/svn/sources/libticcutils/trunk/include/ticcutils/XMLtools.h $
 
4
 
 
5
  Copyright (c) 1998 - 2013
 
6
  ILK   - Tilburg University
 
7
  CLiPS - University of Antwerp
 
8
 
 
9
  This file is part of ticcutils
 
10
 
 
11
  timbl is free software; you can redistribute it and/or modify
 
12
  it under the terms of the GNU General Public License as published by
 
13
  the Free Software Foundation; either version 3 of the License, or
 
14
  (at your option) any later version.
 
15
 
 
16
  timbl is distributed in the hope that it will be useful,
 
17
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
  GNU General Public License for more details.
 
20
 
 
21
  You should have received a copy of the GNU General Public License
 
22
  along with this program; if not, see <http://www.gnu.org/licenses/>.
 
23
 
 
24
  For questions and suggestions, see:
 
25
      http://ilk.uvt.nl/software.html
 
26
  or send mail to:
 
27
      timbl@uvt.nl
 
28
*/
 
29
 
 
30
#ifndef TICC_XML_TOOLS_H
 
31
#define TICC_XML_TOOLS_H
 
32
 
 
33
#include <string>
 
34
#include <map>
 
35
#include <list>
 
36
#include "libxml/tree.h"
 
37
 
 
38
namespace TiCC {
 
39
 
 
40
  inline xmlNode *XmlNewNode( const std::string& elem ){
 
41
    return xmlNewNode( 0, (const xmlChar*)elem.c_str() );
 
42
  }
 
43
  
 
44
  inline xmlNode *XmlNewNode( xmlNs *ns, const std::string& elem ){
 
45
    return xmlNewNode( ns, (const xmlChar*)elem.c_str() );
 
46
  }
 
47
  
 
48
  inline xmlNode *XmlNewComment( const std::string& elem ){
 
49
    return xmlNewComment( (const xmlChar*)elem.c_str() );
 
50
  }
 
51
  
 
52
  inline xmlNode *XmlNewChild( xmlNode *node, 
 
53
                               const std::string& elem ){
 
54
    xmlNode *chld = xmlNewNode( 0, (const xmlChar*)elem.c_str() );
 
55
    return xmlAddChild( node, chld );
 
56
  }
 
57
 
 
58
  inline xmlNode *XmlNewChild( xmlNode *node, 
 
59
                               xmlNs *ns,
 
60
                               const std::string& elem ){
 
61
    xmlNode *chld = xmlNewNode( ns, (const xmlChar*)elem.c_str() );
 
62
    return xmlAddChild( node, chld );
 
63
  }
 
64
 
 
65
  inline xmlNode *XmlNewTextChild( xmlNode *node, 
 
66
                                   const std::string& elem, 
 
67
                                   const std::string& val ){
 
68
    if ( val.empty() )
 
69
      return xmlNewTextChild( node, 0, (xmlChar*)elem.c_str(), 0 );
 
70
    else 
 
71
      return xmlNewTextChild( node, 0, 
 
72
                              (const xmlChar*)elem.c_str(),
 
73
                              (const xmlChar*)val.c_str() );
 
74
  }
 
75
 
 
76
  inline xmlNode *XmlNewTextChild( xmlNode *node, 
 
77
                                   xmlNs *ns,
 
78
                                   const std::string& elem, 
 
79
                                   const std::string& val ){
 
80
    if ( val.empty() )
 
81
      return xmlNewTextChild( node, ns,
 
82
                              (xmlChar*)elem.c_str(), 0 );
 
83
    else 
 
84
      return xmlNewTextChild( node, ns,
 
85
                              (const xmlChar*)elem.c_str(),
 
86
                              (const xmlChar*)val.c_str() );
 
87
  }
 
88
 
 
89
  inline void XmlAddContent( xmlNode *node, const std::string& cont ){
 
90
    xmlNodeAddContent( node, (const xmlChar*)cont.c_str() );
 
91
  }
 
92
  
 
93
  inline xmlAttr *XmlSetAttribute( xmlNode *node, 
 
94
                                   const std::string& att,
 
95
                                   const std::string& val ){
 
96
    return xmlSetProp( node, 
 
97
                       (const xmlChar*)att.c_str(), 
 
98
                       (const xmlChar*)val.c_str() );
 
99
  }
 
100
  
 
101
  inline std::string getAttribute( const xmlNode *node, 
 
102
                                   const std::string& att ){
 
103
    if ( node ){
 
104
      xmlAttr *a = node->properties;
 
105
      while ( a ){
 
106
        if ( att == (char*)a->name )
 
107
          return (char *)a->children->content;
 
108
        a = a->next;
 
109
      }
 
110
    }
 
111
    return "";
 
112
  }
 
113
 
 
114
  std::string getNS( const xmlNode *, std::string& );
 
115
  inline std::string getNS( const xmlNode *n ) {
 
116
    std::string s;
 
117
    return getNS( n, s);
 
118
  }
 
119
 
 
120
  std::map<std::string,std::string> getNSvalues( const xmlNode * );
 
121
 
 
122
  std::string serialize( const xmlNode& node );
 
123
 
 
124
  inline std::string Name( const xmlNode *node ){
 
125
    std::string result;
 
126
    if ( node ){
 
127
      result = (char *)node->name;
 
128
    }
 
129
    return result;
 
130
  }
 
131
 
 
132
  inline std::string XmlContent( const xmlNode *node ){
 
133
    std::string result;
 
134
    if ( node ){
 
135
      xmlChar *tmp = xmlNodeListGetString( node->doc, node->children, 1 );
 
136
      if ( tmp ){
 
137
        result = std::string( (char *)tmp );
 
138
        xmlFree( tmp );
 
139
      }
 
140
    }
 
141
    return result;
 
142
  }
 
143
 
 
144
  class XmlDoc {
 
145
    friend std::ostream& operator << ( std::ostream& , const XmlDoc& );
 
146
  public:
 
147
    XmlDoc( const std::string& );
 
148
    ~XmlDoc(){
 
149
      xmlFreeDoc( the_doc );
 
150
    }
 
151
    void setRoot( xmlNode* );
 
152
    xmlNode *getRoot() const;
 
153
    xmlNode *MakeRoot( const std::string& );
 
154
    const std::string toString() const;
 
155
  private:
 
156
    xmlDoc *the_doc;
 
157
  };
 
158
 
 
159
  inline std::ostream& operator << ( std::ostream& os, const XmlDoc& doc ){
 
160
    os << doc.toString();
 
161
    return os;
 
162
  }
 
163
 
 
164
  inline std::ostream& operator << ( std::ostream& os, const xmlNode& node ){
 
165
    os << serialize( node );
 
166
    return os;
 
167
  }
 
168
 
 
169
  inline std::ostream& operator << ( std::ostream& os, const xmlNode *node ){
 
170
    os << serialize( *node );
 
171
    return os;
 
172
  }
 
173
 
 
174
  std::list<xmlNode*> FindNodes( xmlNode *, const std::string& );
 
175
  xmlNode *xPath( xmlNode *, const std::string& );
 
176
 
 
177
} // namespace TiCC
 
178
 
 
179
#endif