From 4235f42672569c390609bc5154fac7f1e3f08c96 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 4 Jan 2015 14:40:25 +0000 Subject: Switch to using metadata rather than the @ hack for using XML attributes --- slicer/test/initial/xmlattr.xml | 7 +++++++ slicer/test/serializers.cpp | 7 ++++++- slicer/test/types.ice | 1 + slicer/xml/Jamfile.jam | 1 + slicer/xml/serializer.cpp | 24 +++++++++++++++--------- slicer/xml/serializer.h | 10 +++++----- 6 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 slicer/test/initial/xmlattr.xml diff --git a/slicer/test/initial/xmlattr.xml b/slicer/test/initial/xmlattr.xml new file mode 100644 index 0000000..475f0f3 --- /dev/null +++ b/slicer/test/initial/xmlattr.xml @@ -0,0 +1,7 @@ + + + + 0 + 3 + + diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp index 0319473..045146c 100644 --- a/slicer/test/serializers.cpp +++ b/slicer/test/serializers.cpp @@ -36,7 +36,7 @@ class FileBased : public FileStructure { BOOST_TEST_CHECKPOINT("Deserialize: " << input); IceInternal::Handle p = Slicer::Deserialize(input); - + if (check) { BOOST_TEST_CHECKPOINT("Check1: " << input); check(*p); @@ -293,5 +293,10 @@ BOOST_AUTO_TEST_CASE( optionals_areset_xml ) verifyByHelper("optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); } +BOOST_AUTO_TEST_CASE( xml_attribute_xml ) +{ + verifyByFile("xmlattr.xml"); +} + BOOST_AUTO_TEST_SUITE_END(); diff --git a/slicer/test/types.ice b/slicer/test/types.ice index 761e5e1..62037a1 100644 --- a/slicer/test/types.ice +++ b/slicer/test/types.ice @@ -46,6 +46,7 @@ module TestModule { class ClassClass { ClassType cls; StructType str; + ["slicer:xml:attribute"] int simp; }; struct StructStruct { diff --git a/slicer/xml/Jamfile.jam b/slicer/xml/Jamfile.jam index 6c5322b..e959b64 100644 --- a/slicer/xml/Jamfile.jam +++ b/slicer/xml/Jamfile.jam @@ -13,6 +13,7 @@ lib slicer-xml : boost_filesystem IceUtil libxmlpp + ../slicer//slicer : : libxmlpp ; diff --git a/slicer/xml/serializer.cpp b/slicer/xml/serializer.cpp index ad3e87b..85fe99e 100644 --- a/slicer/xml/serializer.cpp +++ b/slicer/xml/serializer.cpp @@ -1,12 +1,16 @@ #include "serializer.h" +#include #include #include #include #include +#include #include #include namespace Slicer { + const std::string md_attribute = "xml:attribute"; + class BadBooleanValue : public std::invalid_argument { public: BadBooleanValue(const Glib::ustring &) : @@ -151,7 +155,7 @@ namespace Slicer { { } }; - + class XmlContentValueTarget : public XmlValueTarget { public: XmlContentValueTarget(xmlpp::Element * p) : @@ -159,13 +163,14 @@ namespace Slicer { { } }; - + void XmlDeserializer::DocumentTreeIterate(const xmlpp::Node * node, ModelPartPtr mp) { while (node) { if (auto element = dynamic_cast(node)) { - auto smp = mp->GetChild(element->get_name()); + auto smp = mp->GetChild(element->get_name(), + boost::bind(metaDataFlagNotSet, boost::bind(&Slicer::HookCommon::GetMetadata, _1), md_attribute)); if (smp) { if (auto typeIdPropName = smp->GetTypeIdProperty()) { if (auto typeAttr = element->get_attribute(*typeIdPropName)) { @@ -182,7 +187,8 @@ namespace Slicer { } } else if (auto attribute = dynamic_cast(node)) { - auto smp = mp->GetChild("@" + attribute->get_name()); + auto smp = mp->GetChild(attribute->get_name(), + boost::bind(metaDataFlagSet, boost::bind(&Slicer::HookCommon::GetMetadata, _1), md_attribute)); if (smp) { smp->Create(); smp->SetValue(new XmlAttributeValueSource(attribute)); @@ -203,13 +209,13 @@ namespace Slicer { } void - XmlSerializer::ModelTreeIterate(xmlpp::Element * n, const std::string & name, ModelPartPtr mp) + XmlSerializer::ModelTreeIterate(xmlpp::Element * n, const std::string & name, ModelPartPtr mp, HookCommonPtr hp) { if (name.empty()) { return; } - if (name[0] == '@') { - mp->GetValue(new XmlAttributeValueTarget(n, name.substr(1))); + if (hp && metaDataFlagSet(hp->GetMetadata(), md_attribute)) { + mp->GetValue(new XmlAttributeValueTarget(n, name)); } else if (mp) { auto element = n->add_child(name); @@ -220,7 +226,7 @@ namespace Slicer { mp = mp->GetSubclassModelPart(*typeId); } mp->GetValue(new XmlContentValueTarget(element)); - mp->OnEachChild(boost::bind(&XmlSerializer::ModelTreeIterate, element, _1, _2)); + mp->OnEachChild(boost::bind(&XmlSerializer::ModelTreeIterate, element, _1, _2, _3)); } } @@ -228,7 +234,7 @@ namespace Slicer { XmlSerializer::ModelTreeIterateRoot(xmlpp::Document * doc, const std::string & name, ModelPartPtr mp) { auto root = doc->create_root_node(name); - mp->OnEachChild(boost::bind(&XmlSerializer::ModelTreeIterate, root, _1, _2)); + mp->OnEachChild(boost::bind(&XmlSerializer::ModelTreeIterate, root, _1, _2, _3)); } XmlFileSerializer::XmlFileSerializer(const boost::filesystem::path & p) : diff --git a/slicer/xml/serializer.h b/slicer/xml/serializer.h index fc5794d..4e37eed 100644 --- a/slicer/xml/serializer.h +++ b/slicer/xml/serializer.h @@ -7,10 +7,10 @@ namespace Slicer { class XmlSerializer : public Serializer { protected: - static void ModelTreeIterate(xmlpp::Element *, const std::string &, ModelPartPtr mp); + static void ModelTreeIterate(xmlpp::Element *, const std::string &, ModelPartPtr mp, HookCommonPtr hp); static void ModelTreeIterateRoot(xmlpp::Document *, const std::string &, ModelPartPtr mp); }; - + class XmlFileSerializer : public XmlSerializer { public: XmlFileSerializer(const boost::filesystem::path &); @@ -20,7 +20,7 @@ namespace Slicer { private: const boost::filesystem::path path; }; - + class XmlDocumentSerializer : public XmlSerializer { public: XmlDocumentSerializer(xmlpp::Document * &); @@ -36,7 +36,7 @@ namespace Slicer { static void DocumentTreeIterate(const xmlpp::Node * node, ModelPartPtr mp); static void DocumentTreeIterate(const xmlpp::Document * doc, ModelPartPtr mp); }; - + class XmlFileDeserializer : public XmlDeserializer { public: XmlFileDeserializer(const boost::filesystem::path &); @@ -46,7 +46,7 @@ namespace Slicer { private: const boost::filesystem::path path; }; - + class XmlDocumentDeserializer : public XmlDeserializer { public: XmlDocumentDeserializer(const xmlpp::Document *); -- cgit v1.2.3