extend Time class with string formatters
[libi2ncommon] / xmllib / xmlcommon.cpp
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
ce6792dd
TJ
20/***************************************************************************
21 xmlcommon.cpp - Common XML functions
22 -------------------
23 begin : Tue Apr 27 2004
24 copyright : (C) 2004 by Intra2net AG
ce6792dd
TJ
25 ***************************************************************************/
26
27#include <xmlcommon.hpp>
28#include <exception.hxx>
29#include <sstream>
30#include <vector>
31
32using namespace xmlpp;
33using namespace std;
34
35namespace I2n {
36
37void 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
56std::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
73std::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}