std::string get_content(xml::element const& element) { try { std::string s; typedef xml::node::const_iterator NodeConstIterator; for(NodeConstIterator i = element.begin(); i != element.end(); ++i) { if(i->is_text()) { char const* content = i->get_content(); if(content) { s += i->get_content(); } } } return s; } catch(std::exception const& e) { fatal_error() << e.what() << LMI_FLUSH; throw "Unreachable--silences a compiler diagnostic."; } }
void snappable_detail::save(xml::element& Element, const ipersistent::save_context& Context) { xml::element* xml_snap_sources = 0; for(snap_sources_t::const_iterator snap_source = m_snap_sources.begin(); snap_source != m_snap_sources.end(); ++snap_source) { if(explicit_snap_source* const source = dynamic_cast<explicit_snap_source*>(*snap_source)) { if(!xml_snap_sources) xml_snap_sources = &Element.append(xml::element("snap_sources")); xml::element& xml_snap_source = xml_snap_sources->append(xml::element("snap_source")); xml_snap_source.append(xml::attribute("label", source->m_label)); xml_snap_source.append(xml::attribute("position", source->m_position)); if(source->m_look.get()) xml_snap_source.append(xml::attribute("look", *source->m_look)); if(source->m_up.get()) xml_snap_source.append(xml::attribute("up", *source->m_up)); if(source->m_groups.size()) { xml::element& xml_groups = xml_snap_source.append(xml::element("groups")); for(isnap_source::groups_t::iterator group = source->m_groups.begin(); group != source->m_groups.end(); ++group) xml_groups.append(xml::element("group", *group)); } } } xml::element* xml_snap_targets = 0; for(snap_targets_t::const_iterator snap_target = m_snap_targets.begin(); snap_target != m_snap_targets.end(); ++snap_target) { if(explicit_snap_target* const target = dynamic_cast<explicit_snap_target*>(*snap_target)) { if(!xml_snap_targets) xml_snap_targets = &Element.append(xml::element("snap_targets")); xml::element& xml_snap_target = xml_snap_targets->append(xml::element("snap_target")); xml_snap_target.append(xml::attribute("label", target->m_label)); xml_snap_target.append(xml::attribute("position", target->m_position)); if(target->m_look.get()) xml_snap_target.append(xml::attribute("look", *target->m_look)); if(target->m_up.get()) xml_snap_target.append(xml::attribute("up", *target->m_up)); if(target->m_groups.size()) { xml::element& xml_groups = xml_snap_target.append(xml::element("groups")); for(isnap_source::groups_t::iterator group = target->m_groups.begin(); group != target->m_groups.end(); ++group) xml_groups.append(xml::element("group", *group)); } } } }
bool get_attr (xml::element const& element ,std::string const& name ,std::string& value ) { try { xml::attributes const& attrs = element.get_attributes(); xml::attributes::const_iterator i = attrs.find(name.c_str()); if(i != attrs.end()) { value = i->get_value(); return true; } else { return false; } } catch(std::exception const& e) { fatal_error() << e.what() << LMI_FLUSH; throw "Unreachable--silences a compiler diagnostic."; } }
xml::node::const_iterator retrieve_element (xml::element const& parent ,std::string const& name ) { xml::node::const_iterator i = parent.find(name.c_str()); if(parent.end() == i) { fatal_error() << "Required element '" << name << "' not found." << LMI_FLUSH ; } return i; }
void database_entity::read(xml::element const& e) { key_ = db_key_from_name(e.get_name()); xml_serialize::get_element(e, "axis_lengths", axis_lengths_); xml_serialize::get_element(e, "data_values" , data_values_ ); xml_serialize::get_element(e, "gloss" , gloss_ ); assert_invariants(); }
std::string get_name(xml::element const& element) { try { char const* name = element.get_name(); return name ? name : ""; } catch(std::exception const& e) { fatal_error() << e.what() << LMI_FLUSH; throw "Unreachable--silences a compiler diagnostic."; } }
void set_attr (xml::element& element ,std::string const& name ,std::string const& value ) { try { element.get_attributes().insert(name.c_str(), value.c_str()); } catch(std::exception const& e) { fatal_error() << e.what() << LMI_FLUSH; throw "Unreachable--silences a compiler diagnostic."; } }
bool run_test(const xml::element& test, fs::path base_dir) { bool result = true; fs::path input(base_dir / test.get_attribute("URI")); fs::path output(base_dir / test.get_attribute("OUTPUT")); if (VERBOSE) { cout << "-----------------------------------------------" << endl << "ID: " << test.get_attribute("ID") << endl << "TYPE: " << test.get_attribute("TYPE") << endl << "FILE: " << fs::system_complete(input) << endl << test.content() << endl << endl; } ++total_tests; if (not fs::exists(input)) { cout << "test file " << input << " does not exist" << endl; return false; } if (test.get_attribute("SECTIONS") == "B.") { if (VERBOSE) cout << "skipping unicode character validation tests" << endl; ++skipped_tests; return true; } fs::current_path(input.branch_path()); fs::ifstream is(input); if (not is.is_open()) throw zeep::exception("test file not open"); try { fs::current_path(input.branch_path()); if (test.get_attribute("TYPE") == "valid") result = run_valid_test(is, output); else if (test.get_attribute("TYPE") == "not-wf" or test.get_attribute("TYPE") == "invalid") { bool failed = false; try { DOC doc; doc.set_validating(test.get_attribute("TYPE") == "invalid"); is >> doc; ++should_have_failed; result = false; } catch (zeep::xml::not_wf_exception& e) { if (test.get_attribute("TYPE") != "not-wf") { ++wrong_exception; throw zeep::exception(string("Wrong exception (should have been invalid):\n\t") + e.what()); } failed = true; if (VERBOSE) cout << e.what() << endl; } catch (zeep::xml::invalid_exception& e) { if (test.get_attribute("TYPE") != "invalid") { ++wrong_exception; throw zeep::exception(string("Wrong exception (should have been not-wf):\n\t") + e.what()); } failed = true; if (VERBOSE) cout << e.what() << endl; } catch (std::exception& e) { throw zeep::exception(string("Wrong exception:\n\t") + e.what()); } if (VERBOSE and not failed) throw zeep::exception("invalid document, should have failed"); } else // if (test.get_attribute("TYPE") == "not-wf" or test.get_attribute("TYPE") == "error" ) { bool failed = false; try { DOC doc; is >> doc; ++should_have_failed; result = false; } catch (std::exception& e) { if (VERBOSE) cout << e.what() << endl; failed = true; } if (VERBOSE and not failed) throw zeep::exception("invalid document, should have failed"); } }
bool run_test(const xml::element& test) { cout << "----------------------------------------------------------" << endl << "ID: " << test.get_attribute("ID") << endl << "xpath: " << test.get_attribute("xpath") << endl // << "data: " << test.content() << endl // << "expected-size: " << test.get_attribute("expected-size") << endl << endl; fs::path data_file = fs::current_path() / test.get_attribute("data"); if (not fs::exists(data_file)) throw zeep::exception("file does not exist"); xml::document doc; doc.set_validating(false); fs::ifstream file(data_file); file >> doc; if (VERBOSE) cout << "test doc:" << endl << doc << endl; xml::xpath xp(test.get_attribute("xpath")); xml::context context; foreach (const xml::element* e, test.find("var")) context.set(e->get_attribute("name"), e->get_attribute("value")); xml::node_set ns = xp.evaluate<xml::node>(*doc.root(), context); if (VERBOSE) { int nr = 1; foreach (const xml::node* n, ns) cout << nr++ << ">> " << *n << endl; } bool result = true; if (ns.size() != boost::lexical_cast<unsigned int>(test.get_attribute("expected-size"))) { cout << "incorrect number of nodes in returned node-set" << endl << "expected: " << test.get_attribute("expected-size") << endl; result = false; } string test_attr_name = test.get_attribute("test-name"); string attr_test = test.get_attribute("test-attr"); if (not attr_test.empty()) { if (VERBOSE) cout << "testing attribute " << test_attr_name << " for " << attr_test << endl; foreach (const xml::node* n, ns) { const xml::element* e = dynamic_cast<const xml::element*>(n); if (e == NULL) continue; if (e->get_attribute(test_attr_name) != attr_test) { cout << "expected attribute content is not found for node " << e->qname() << endl; result = false; } } } if (result) cout << "Test passed" << endl; else { cout << "Test failed" << endl; int nr = 1; foreach (const xml::node* n, ns) cout << nr++ << ") " << *n << endl; } return result; }