Merge branch 'daemon-ext'
[libi2ncommon] / xmllib / xmlcommon.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /***************************************************************************
21                    xmlcommon.cpp - Common XML functions
22                              -------------------
23     begin                : Tue Apr 27 2004
24     copyright            : (C) 2004 by Intra2net AG
25  ***************************************************************************/
26
27 #include <xmlcommon.hpp>
28 #include <exception.hxx>
29 #include <sstream>
30 #include <vector>
31
32 using namespace xmlpp;
33 using namespace std;
34
35 namespace I2n {
36
37 void xml_validate_dtd(Node *root, const std::string &subsetname, const std::string &dtdname)
38 {
39     // Create empty document, set DTD
40     Document doc;
41     doc.set_internal_subset(subsetname, "", dtdname);
42     Element *newRoot = doc.create_root_node(root->get_name(), "", "");
43     
44     // Import nodes
45     Node::NodeList list = root->get_children();
46     Node::NodeList::iterator list_end = list.end();
47     for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list_end; ++iter)
48         newRoot->import_node(*iter);
49
50     // Parse XML again to validate it
51     DomParser parser;
52     parser.set_validate();
53     parser.parse_memory(doc.write_to_string_formatted());
54 }
55
56 std::string xml_get_child_content(xmlpp::Node *base, const std::string &elementname)
57 {
58     vector<xmlpp::Node*> nodes;
59
60     // Read in value
61     nodes = base->find(elementname);
62
63     if (nodes.size() != 1)
64     {
65         ostringstream os;
66         os << "xml parse error at line " << base->get_line() << ": element ambiguous or not found: " << elementname;
67         throw EXCEPTION(runtime_error_src,os.str());
68     }
69
70     return xml_get_text_content(nodes[0], elementname);
71 }
72
73 std::string xml_get_text_content(xmlpp::Node *node, const std::string &elementname)
74 {
75     if (node->get_children().size() <= 0)
76     {
77         ostringstream os;
78         os << "xml parse error at line " << node->get_line() << ": element content not found in " << elementname;
79         throw EXCEPTION(runtime_error_src,os.str());
80     }
81
82     xmlpp::Node::NodeList childs = node->get_children();
83     xmlpp::Node::NodeList::iterator text = childs.begin();
84     const xmlpp::ContentNode *nodeText=dynamic_cast<const xmlpp::ContentNode*>(*text);
85
86     if (nodeText)
87         return nodeText->get_content();
88     else
89     {
90         ostringstream os;
91         os << "xml parse error at line " << (*text)->get_line() << ": element " << elementname << " does not contain content";
92         throw EXCEPTION(runtime_error_src,os.str());
93     }
94
95     // not reached
96     return "";
97 }
98
99 }