/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ /*************************************************************************** xmlcommon.cpp - Common XML functions ------------------- begin : Tue Apr 27 2004 copyright : (C) 2004 by Intra2net AG ***************************************************************************/ #include #include #include #include using namespace xmlpp; using namespace std; namespace I2n { void xml_validate_dtd(Node *root, const std::string &subsetname, const std::string &dtdname) { // Create empty document, set DTD Document doc; doc.set_internal_subset(subsetname, "", dtdname); Element *newRoot = doc.create_root_node(root->get_name(), "", ""); // Import nodes Node::NodeList list = root->get_children(); Node::NodeList::iterator list_end = list.end(); for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list_end; ++iter) newRoot->import_node(*iter); // Parse XML again to validate it DomParser parser; parser.set_validate(); parser.parse_memory(doc.write_to_string_formatted()); } std::string xml_get_child_content(xmlpp::Node *base, const std::string &elementname) { vector nodes; // Read in value nodes = base->find(elementname); if (nodes.size() != 1) { ostringstream os; os << "xml parse error at line " << base->get_line() << ": element ambiguous or not found: " << elementname; throw EXCEPTION(runtime_error_src,os.str()); } return xml_get_text_content(nodes[0], elementname); } std::string xml_get_text_content(xmlpp::Node *node, const std::string &elementname) { if (node->get_children().size() <= 0) { ostringstream os; os << "xml parse error at line " << node->get_line() << ": element content not found in " << elementname; throw EXCEPTION(runtime_error_src,os.str()); } xmlpp::Node::NodeList childs = node->get_children(); xmlpp::Node::NodeList::iterator text = childs.begin(); const xmlpp::ContentNode *nodeText=dynamic_cast(*text); if (nodeText) return nodeText->get_content(); else { ostringstream os; os << "xml parse error at line " << (*text)->get_line() << ": element " << elementname << " does not contain content"; throw EXCEPTION(runtime_error_src,os.str()); } // not reached return ""; } }