Page Menu
Home
HEPForge
Search
Configure Global Search
Log In
Files
F7877860
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
14 KB
Subscribers
None
View Options
diff --git a/Utilities/XML/Element.cc b/Utilities/XML/Element.cc
--- a/Utilities/XML/Element.cc
+++ b/Utilities/XML/Element.cc
@@ -1,204 +1,217 @@
// -*- C++ -*-
//
// Element.cpp is a part of myXML
// Copyright (C) 2012-2013 Simon Platzer
//
// myXML is licenced under version 2 of the GPL, see COPYING for details.
//
#include "Element.h"
#include <exception>
#include <stdexcept>
using namespace XML;
using namespace std;
Element::Element(const Element& other)
: theType(other.theType),
theNameOrContent(other.theNameOrContent),
theAttributes(other.theAttributes),
theChildren(other.theChildren) {
index();
}
Element& Element::operator=(const Element& other) {
theType = other.theType;
theNameOrContent = other.theNameOrContent;
theAttributes = other.theAttributes;
theChildren = other.theChildren;
index();
return *this;
}
namespace XML {
bool operator==(const XML::Element &one, const XML::Element &two) {
return(one.theType == two.theType &&
one.theNameOrContent == two.theNameOrContent &&
one.theAttributes == two.theAttributes &&
one.theChildren == two.theChildren
);
}
bool operator!=(const XML::Element &one, const XML::Element &two) {
return !(one == two);
}
XML::Element operator+(const XML::Element& one, const XML::Element& two) {
if ( one.type() != two.type())
- throw logic_error("[XML::Element] Trying to combine elements with different types");
+ throw logic_error("[XML::Element] Trying to combine elements with different types.");
+ if ( one.name() != two.name())
+ throw logic_error("[XML::Element] Trying to combine elements with different names.");
XML::Element returnElement = XML::Element(one);
+ if(two.hasAttributes()) {
+ std::map<std::string,std::string> attributesOfSecond = two.attributes();
+ for(std::map<std::string,std::string>::const_iterator attributesofSecondIter=attributesOfSecond.begin(); attributesofSecondIter!=attributesOfSecond.end(); ++attributesofSecondIter) {
+ returnElement.appendAttribute(attributesofSecondIter->first, attributesofSecondIter->second);
+ }
+ }
+ if(two.hasChildren()) {
+ std::list<Element> childrenOfSecond = two.children();
+ for(std::list<Element>::const_iterator childrenOfSecondIter = childrenOfSecond.begin(); childrenOfSecondIter != childrenOfSecond.end(); ++childrenOfSecondIter)
+ returnElement.append(*childrenOfSecondIter);
+ }
return returnElement;
}
}
const string& Element::name() const {
assertNamed();
return theNameOrContent;
}
const string& Element::content() const {
assertContent();
return theNameOrContent;
}
string& Element::content() {
assertContent();
return theNameOrContent;
}
bool Element::hasAttribute(const string& id) const {
assertAttributes();
return theAttributes.find(id) != theAttributes.end();
}
const map<string,string>& Element::attributes() const {
assertAttributes();
return theAttributes;
}
const string& Element::attribute(const string& id) const {
assertAttributes();
map<string,string>::const_iterator ait = theAttributes.find(id);
if ( ait == theAttributes.end() )
throw runtime_error("[XML::Element] no such attribute found.");
return ait->second;
}
string& Element::attribute(const string& id) {
assertAttributes();
string& ret = theAttributes[id];
return ret;
}
Element& Element::operator<<(const Element::Attribute& a) {
attribute(a.name) = a.value;
return *this;
}
const list<Element>& Element::children() const {
assertChildren();
return theChildren;
}
list<Element>& Element::children() {
assertChildren();
return theChildren;
}
Element& Element::append(const Element& e) {
assertChildren();
theChildren.push_back(e);
index();
return theChildren.back();
}
Element& Element::prepend(const Element& e) {
assertChildren();
theChildren.push_front(e);
index();
return theChildren.front();
}
void Element::insert(list<Element>::iterator pos,
const Element& e) {
assertChildren();
theChildren.insert(pos,e);
index();
}
void Element::erase(list<Element>::iterator pos) {
assertChildren();
theChildren.erase(pos);
index();
}
Element& Element::operator<<(const Element& e) {
append(e);
return *this;
}
void Element::index() {
theIndex.clear();
for ( list<Element>::iterator i = theChildren.begin();
i != theChildren.end(); ++i ) {
if ( i->type() == ElementTypes::Element ||
i->type() == ElementTypes::EmptyElement )
theIndex.insert(make_pair(pair<int,string>(i->type(),i->name()),i));
else
theIndex.insert(make_pair(pair<int,string>(i->type(),string("")),i));
}
}
list<Element>::const_iterator
Element::findFirst(int type, const string& name) const {
assertChildren();
typedef
multimap<pair<int,string>,list<Element>::iterator>::const_iterator
IndexIterator;
IndexIterator i = theIndex.find(make_pair(type,name));
if ( i == theIndex.end() )
return theChildren.end();
return i->second;
}
pair<multimap<pair<int,string>,list<Element>::iterator>::const_iterator,
multimap<pair<int,string>,list<Element>::iterator>::const_iterator>
Element::findAll(int type, const string& name) const {
assertChildren();
return theIndex.equal_range(make_pair(type,name));
}
void Element::assertContent() const {
if ( type() == ElementTypes::ProcessingInstruction ||
type() == ElementTypes::CharacterData ||
type() == ElementTypes::ParsedCharacterData ||
type() == ElementTypes::Comment )
return;
throw logic_error("[XML::Element] element has no plain character content.");
}
void Element::assertNamed() const {
if ( type() == ElementTypes::EmptyElement ||
type() == ElementTypes::Element )
return;
throw logic_error("[XML::Element] element has no name.");
}
void Element::assertAttributes() const {
if ( type() == ElementTypes::EmptyElement ||
type() == ElementTypes::Element )
return;
throw logic_error("[XML::Element] element has no attributes.");
}
void Element::assertChildren() const {
if ( type() == ElementTypes::Element ||
type() == ElementTypes::Root )
return;
throw logic_error("[XML::Element] element has no children elements.");
}
diff --git a/Utilities/XML/tests/xmlTests.cc b/Utilities/XML/tests/xmlTests.cc
--- a/Utilities/XML/tests/xmlTests.cc
+++ b/Utilities/XML/tests/xmlTests.cc
@@ -1,205 +1,197 @@
// -*- C++ -*-
//
// xmlTests.cc is a part of myXML
// Copyright (C) 2015 Simon Plaetzer, Marco A. Harrendorf
//
// myXML is licenced under version 2 of the GPL, see COPYING for details.
//
#include "Herwig++/Utilities/XML/Element.h"
#include "Herwig++/Utilities/XML/ElementIO.h"
#define BOOST_TEST_MODULE xmlTest
#include <boost/test/included/unit_test.hpp>
/*
* Fixture which defines the common variables for testing Element class
*/
struct Fix1 {
Fix1() : elementUnknown(XML::ElementTypes::Unknown,""),
elementRoot(XML::ElementTypes::Root,"Root"),
elementEmpty(XML::ElementTypes::EmptyElement,""),
elementElement(XML::ElementTypes::Element, "Element"),
elementProcessingInstruction(XML::ElementTypes::ProcessingInstruction, "ProcessingInstruction"),
elementCharacterData(XML::ElementTypes::CharacterData, "CharacterData"),
elementParsedCharacterData(XML::ElementTypes::ParsedCharacterData, "ParsedCharacterData"),
elementComment(XML::ElementTypes::Comment, "Comment"),
elementGrids(XML::ElementTypes::Element,"Grids")
{BOOST_TEST_MESSAGE( "setup fixture for xmlTest" ); }
~Fix1() { BOOST_TEST_MESSAGE( "teardown fixture for xmlTest" ); }
XML::Element elementUnknown;
XML::Element elementRoot;
XML::Element elementEmpty;
XML::Element elementElement;
XML::Element elementProcessingInstruction;
XML::Element elementCharacterData;
XML::Element elementParsedCharacterData;
XML::Element elementComment;
XML::Element elementGrids;
};
//____________________________________________________________________________//
/*
* Start of boost unit tests
* @todo Implement tests for ElementIO
*/
BOOST_FIXTURE_TEST_SUITE(xmlTestElement, Fix1 )
/*
* Boost unit tests for Element.h
* @todo Implement further tests for child operations
*/
BOOST_AUTO_TEST_CASE(elementTypes)
{
BOOST_CHECK_EQUAL(elementUnknown.type(), -1);
BOOST_CHECK_EQUAL(elementRoot.type(), 0);
BOOST_CHECK_EQUAL(elementEmpty.type(), 1);
BOOST_CHECK_EQUAL(elementElement.type(), 2);
BOOST_CHECK_EQUAL(elementGrids.type(), 2);
BOOST_CHECK_EQUAL(elementProcessingInstruction.type(), 3);
BOOST_CHECK_EQUAL(elementCharacterData.type(), 4);
BOOST_CHECK_EQUAL(elementParsedCharacterData.type(), 5);
BOOST_CHECK_EQUAL(elementComment.type(), 6);
}
BOOST_AUTO_TEST_CASE(constructors)
{
BOOST_CHECK(elementUnknown == XML::Element());
BOOST_CHECK(elementComment == XML::Element(XML::ElementTypes::Comment, "Comment"));
BOOST_CHECK(elementUnknown != elementComment);
XML::Element elementCopy = elementComment;
BOOST_CHECK(elementCopy == elementComment);
XML::Element elementAssignment = XML::Element();
elementAssignment = elementGrids;
BOOST_CHECK(elementAssignment == elementGrids);
}
BOOST_AUTO_TEST_CASE(combineOperator)
{
-
BOOST_CHECK(elementElement + elementElement == elementElement);
BOOST_CHECK_THROW((elementElement + elementComment), std::logic_error);
XML::Element elementName1 = XML::Element(XML::ElementTypes::Element, "Name1") ;
XML::Element elementName2 = XML::Element(XML::ElementTypes::Element, "Name2") ;
BOOST_CHECK_THROW(elementElement + elementGrids, std::logic_error);
XML::Element elementWithBothChildren = XML::Element(elementElement);
elementWithBothChildren.append(elementComment);
elementWithBothChildren.append(elementParsedCharacterData);
XML::Element elementWithChild1 = XML::Element(elementElement);
elementWithChild1.append(elementComment);
XML::Element elementWithChild2 = XML::Element(elementElement);
elementWithChild2.append(elementParsedCharacterData);
BOOST_CHECK(elementWithChild1 + elementWithChild2 == elementWithBothChildren);
+
+ XML::Element elementWithBothAttributes = XML::Element(elementElement);
+ elementWithBothAttributes.appendAttribute("Entry1", "Value1");
+ elementWithBothAttributes.appendAttribute("Entry2", "Value2");
+ XML::Element elementWithAttribute1 = XML::Element(elementElement);
+ elementWithAttribute1.appendAttribute("Entry1", "Value1");
+ XML::Element elementWithAttribute2 = XML::Element(elementElement);
+ elementWithAttribute2.appendAttribute("Entry2", "Value2");
+ BOOST_CHECK(elementWithAttribute1 + elementWithAttribute2 == elementWithBothAttributes);
}
BOOST_AUTO_TEST_CASE(type)
{
BOOST_CHECK(elementUnknown.type() != elementGrids.type());
BOOST_CHECK_EQUAL(elementGrids.type(), XML::ElementTypes::Element);
}
BOOST_AUTO_TEST_CASE(name)
{
BOOST_CHECK(elementElement.name() != elementEmpty.name());
BOOST_CHECK_EQUAL(elementGrids.name(), "Grids");
}
BOOST_AUTO_TEST_CASE(content)
{
BOOST_CHECK_EQUAL(elementProcessingInstruction.content(), "ProcessingInstruction");
BOOST_CHECK_EQUAL(elementCharacterData.content(), "CharacterData");
BOOST_CHECK_EQUAL(elementParsedCharacterData.content(), "ParsedCharacterData");
BOOST_CHECK_EQUAL(elementComment.content(), "Comment");
XML::Element elementAppendContent = XML::Element(elementComment);
elementAppendContent << std::string("Test");
BOOST_CHECK_EQUAL(elementAppendContent.content(), "CommentTest");
}
BOOST_AUTO_TEST_CASE(attributes)
{
BOOST_CHECK(elementEmpty.hasAttributes());
BOOST_CHECK(elementElement.hasAttributes());
BOOST_CHECK(elementGrids.hasAttributes());
XML::Element elementEmptyWithAttributes = XML::Element(elementEmpty);
elementEmptyWithAttributes.appendAttribute("Entry1", "Value1");
BOOST_CHECK(elementEmptyWithAttributes.hasAttribute("Entry1"));
XML::Element elementElementWithAttributes = XML::Element(elementElement);
XML::Element::Attribute elementAttribute = XML::Element::Attribute("Entry2","Value2");
elementElementWithAttributes << elementAttribute;
elementElementWithAttributes.appendAttribute("Entry3","Value3");
BOOST_CHECK(elementElementWithAttributes.hasAttribute("Entry2"));
BOOST_CHECK(elementElementWithAttributes.hasAttribute("Entry3"));
std::map<std::string,std::string> comparisonAttributes;
comparisonAttributes.insert(std::pair<std::string,std::string>("Entry2", "Value2"));
comparisonAttributes.insert(std::pair<std::string,std::string>("Entry3", "Value3"));
std::map<std::string,std::string> elementElementWithAttributesMap = elementElementWithAttributes.attributes();
BOOST_CHECK(comparisonAttributes == elementElementWithAttributesMap);
BOOST_CHECK_EQUAL(elementElementWithAttributes.attribute("Entry3"), "Value3");
std::string elementAttributeOutput;
elementElementWithAttributes.getFromAttribute("Entry3", elementAttributeOutput);
BOOST_CHECK_EQUAL(elementAttributeOutput, "Value3");
XML::Element::Attribute elementAttribute4 = XML::Element::Attribute("Entry4","Value4");
XML::Element::Attribute elementAttribute5 = XML::Element::Attribute("Entry5","Value5");
BOOST_CHECK(elementAttribute4 == XML::Element::Attribute("Entry4","Value4"));
BOOST_CHECK(elementAttribute4 != XML::Element::Attribute("Entry4",""));
BOOST_CHECK(elementAttribute4 != XML::Element::Attribute("","Value4"));
BOOST_CHECK(elementAttribute4 != elementAttribute5);
}
BOOST_AUTO_TEST_CASE(children)
{
BOOST_CHECK(elementRoot.hasChildren());
BOOST_CHECK(elementElement.hasChildren());
BOOST_CHECK(elementGrids.hasChildren());
std::list<XML::Element> listWithoutElement;
BOOST_CHECK(elementRoot.children() == listWithoutElement);
BOOST_CHECK(elementElement.children() == listWithoutElement);
std::list<XML::Element> listWithElement;
listWithElement.push_back(elementComment);
listWithElement.push_front(elementParsedCharacterData);
XML::Element elementWithChildren = XML::Element(elementElement);
elementWithChildren.append(elementComment);
elementWithChildren.prepend(elementParsedCharacterData);
BOOST_CHECK(elementWithChildren.children() == listWithElement);
elementWithChildren << elementGrids;
listWithElement.push_back(elementGrids);
BOOST_CHECK(elementWithChildren.children() == listWithElement);
}
-
-
-
-
-
-
-
-
-
-
-
-BOOST_AUTO_TEST_CASE(generalTest)
-{
- BOOST_FAIL( "Test finished" );
-}
-
BOOST_AUTO_TEST_SUITE_END()
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Nov 19, 4:30 PM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3805182
Default Alt Text
(14 KB)
Attached To
rHERWIGHG herwighg
Event Timeline
Log In to Comment