From 3bbd61689bb9faa8d628adce55403c6430e079a5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 7 Jan 2015 22:00:23 +0000 Subject: Support for XML text in otherwise complex objects, test data and test case --- slicer/test/initial/entityref.xml | 2 ++ slicer/test/preprocessor.cpp | 2 +- slicer/test/serializers.cpp | 12 ++++++++++++ slicer/test/types.ice | 6 ++++++ slicer/xml/serializer.cpp | 15 ++++++++++++++- 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 slicer/test/initial/entityref.xml diff --git a/slicer/test/initial/entityref.xml b/slicer/test/initial/entityref.xml new file mode 100644 index 0000000..05f3f62 --- /dev/null +++ b/slicer/test/initial/entityref.xml @@ -0,0 +1,2 @@ + +Hull City diff --git a/slicer/test/preprocessor.cpp b/slicer/test/preprocessor.cpp index 54099df..10fedef 100644 --- a/slicer/test/preprocessor.cpp +++ b/slicer/test/preprocessor.cpp @@ -13,7 +13,7 @@ namespace fs = boost::filesystem; -const unsigned int COMPONENTS_IN_TEST_ICE = 26; +const unsigned int COMPONENTS_IN_TEST_ICE = 27; BOOST_FIXTURE_TEST_SUITE ( preprocessor, FileStructure ); diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp index 12dc37b..6395ce0 100644 --- a/slicer/test/serializers.cpp +++ b/slicer/test/serializers.cpp @@ -204,6 +204,13 @@ checkAssertEq(const T & expected, const T & actual) BOOST_REQUIRE_EQUAL(expected, actual); } +void +checkEntityRef(const TestModule2::EntityRef & er) +{ + BOOST_REQUIRE_EQUAL(er.Id, 26); + BOOST_REQUIRE_EQUAL(er.Name, "Hull City"); +} + xmlpp::Document * readXml(const fs::path & path) { @@ -282,6 +289,11 @@ BOOST_AUTO_TEST_CASE( simpleint_json ) verifyByFile("int2.json", boost::bind(checkAssertEq, 27, _1)); } +BOOST_AUTO_TEST_CASE( complexClass_xmlattrAndText ) +{ + verifyByFile("entityref.xml", checkEntityRef); +} + BOOST_AUTO_TEST_CASE( sequenceOfClass_xml ) { verifyByFile("seqOfClass.xml", checkSeqOfClass); diff --git a/slicer/test/types.ice b/slicer/test/types.ice index 6339cd7..7a66387 100644 --- a/slicer/test/types.ice +++ b/slicer/test/types.ice @@ -110,5 +110,11 @@ module TestModule2 { TestModule::DateTime dt; TestModule::Base base; }; + struct EntityRef { + [ "slicer:xml:attribute" ] + int Id; + [ "slicer:xml:text" ] + string Name; + }; }; diff --git a/slicer/xml/serializer.cpp b/slicer/xml/serializer.cpp index 4670012..99c4e1c 100644 --- a/slicer/xml/serializer.cpp +++ b/slicer/xml/serializer.cpp @@ -10,6 +10,7 @@ namespace Slicer { const std::string md_attribute = "xml:attribute"; + const std::string md_text = "xml:text"; class BadBooleanValue : public std::invalid_argument { public: @@ -196,7 +197,16 @@ namespace Slicer { } } else if (auto content = dynamic_cast(node)) { - mp->SetValue(new XmlContentValueSource(content)); + ModelPartPtr smp; + if (!content->is_white_space()) { + smp = mp->GetChild(boost::bind(metaDataFlagSet, boost::bind(&Slicer::HookCommon::GetMetadata, _1), md_text)); + } + if (smp) { + smp->SetValue(new XmlContentValueSource(content)); + } + else { + mp->SetValue(new XmlContentValueSource(content)); + } } node = node->get_next_sibling(); } @@ -217,6 +227,9 @@ namespace Slicer { if (hp && metaDataFlagSet(hp->GetMetadata(), md_attribute)) { mp->GetValue(new XmlAttributeValueTarget(n, name)); } + else if (hp && metaDataFlagSet(hp->GetMetadata(), md_text)) { + mp->GetValue(new XmlContentValueTarget(n)); + } else { ModelTreeProcessElement(n->add_child(name), mp); } -- cgit v1.2.3