2 #include "gearxml/MergeXML.h"
13 return _file1->
LoadFile( fileName ) ;
21 return _file2->
LoadFile( fileName ) ;
25 void MergeXML::correctDominantFile() {
27 if ( _domFile == 1 ) {
29 *_fileSwap = *_file1 ;
31 *_file2 = *_fileSwap ;
42 const std::string gear =
"gear" ;
43 const std::string global =
"global" ;
44 const std::string detectors =
"detectors" ;
45 const std::string materials =
"materials" ;
48 correctDominantFile() ;
52 *_fileMerged = *_file2 ;
56 && (_file1->
FirstChild( gear ))->FirstChild( detectors )
57 && (_fileMerged->
FirstChild( gear ))->FirstChild( detectors )
61 std::cout <<
"Files are not of type gear.\nAborted." <<std::endl ;
77 mergeNode( nodeGlobal1, nodeGlobalMerged ) ;
78 mergeNode( nodeDetectors1, nodeDetectorsMerged ) ;
79 mergeNode( nodeMaterials1, nodeMaterialsMerged ) ;
82 return _fileMerged->
SaveFile( fileName ) ;
91 std::string lastName ;
108 TiXmlNode* loopNode = 0 ;
115 int countRec = countNodes( loopNode, node ) ;
116 int countDom = countNodes( loopNode, domNode ) ;
122 if ( ((countRec > 1) || (countDom >1)) && ( countDom != countRec ) ) {
125 std::stringstream s ;
126 s <<
"ERROR. Counter for " << loopNode->Value() <<
" returns " << countRec
127 <<
" while counter for domNode " << domNode->
Value() <<
" returns "
128 << countDom <<
"." << std::endl ;
130 throw Exception( s.str() ) ;
137 if ( lastName == getNodeName( loopNode ) ) {
143 lastName = getNodeName( loopNode ) ;
146 TiXmlNode* domChildNode = getChildNode( loopNode, domNode, occurence ) ;
148 if (domChildNode == NULL ) {
158 if (loopNode->ToElement() && domChildNode->ToElement() ) {
161 mergeAttribute( (loopNode->ToElement()), (domChildNode->ToElement()) ) ;
165 if ( hasAttribute(loopNode) && !((loopNode->FirstChild())->ToText()) ) {
166 mergeNode( loopNode, domChildNode ) ;
181 std::string MergeXML::getNodeName( TiXmlNode* node ) {
184 std::string strName ;
186 if ( hasAttribute(node) ) {
187 strName = (node->ToElement())->Attribute( _name ) ;
190 strName = node->Value() ;
198 bool MergeXML::hasAttribute( TiXmlNode* node ) {
200 return ( (node->ToElement()) && ((node->ToElement())->Attribute(_name)) ) ;
205 int MergeXML::countNodes( TiXmlNode* childNode, TiXmlNode* parentNode ) {
211 std::string strName = getNodeName( childNode ) ;
214 TiXmlNode* loopNode = parentNode->FirstChild() ;
221 std::string loopName = getNodeName( loopNode ) ;
224 if ( ( loopName == strName ) && ( hasAttribute(childNode) == hasAttribute(loopNode) ) ) {
229 loopNode = loopNode->NextSibling() ;
237 TiXmlNode* MergeXML::getChildNode( TiXmlNode* node, TiXmlNode* domNode,
int getOccurence ) {
243 std::string searchName = getNodeName( node ) ;
252 TiXmlNode* loopNode = domNode->FirstChild() ;
260 std::string loopName = getNodeName( loopNode ) ;
266 if( (loopName == searchName) && ( hasAttribute(node) == hasAttribute(loopNode) ) ) {
270 if ( occurence == getOccurence ) {
278 loopNode = loopNode->NextSibling() ;
288 void MergeXML::mergeAttribute( TiXmlElement* element, TiXmlElement* domElement ) {
291 TiXmlAttribute* loopAttribute = element->FirstAttribute() ;
292 while ( loopAttribute )
295 std::string attName = loopAttribute->Name() ;
301 const char* domAttribute = domElement->Attribute( attName ) ;
303 if( domAttribute == NULL ) {
306 domElement->SetAttribute( attName, loopAttribute->Value() ) ;
318 loopAttribute = loopAttribute->Next() ;
bool SaveFile() const
Save a file using the current document value. Returns true if successful.
bool mergeFiles(const std::string &fileName)
merges files
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the current document value.
const char * Value() const
The meaning of 'value' changes for the specific type of TiXmlNode.
const TiXmlUnknown * ToUnknown() const
Cast to a more defined type. Will return null not of the requested type.
Always the top level node.
const TiXmlComment * ToComment() const
Cast to a more defined type. Will return null not of the requested type.
The parent class for everything in the Document Object Model.
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
Add a new node related to this.
bool setFile1(const std::string &fileName)
Sets first filename returns true if file can be read.
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
const TiXmlNode * IterateChildren(const TiXmlNode *previous) const
An alternate way to walk the children of a node.
bool setFile2(const std::string &fileName)
Sets second filename returns true if file can be read.
const TiXmlText * ToText() const
Cast to a more defined type. Will return null not of the requested type.