#ifndef _ABSTRACT_XMLLIGHT_H #define _ABSTRACT_XMLLIGHT_H // WORK IN PROGRESS THERE MUST BE SYNTAX ERROR AND OTHER BOGUS THING #include "Behavioural/include/IXMLLight.h" #include "Behavioural/include/XMLLightVector.h" #include "Behavioural/include/XMLLightFunctors.h" #include #include #include #include using std::basic_stringstream ; using std::basic_string; using std::basic_ostream; using std::basic_istream; namespace morpheo { namespace XMLUtils { //******************************************Class Declaration***************************************// template< typename char_type, typename ReferenceCounter = ClassicReferenceCounter > class AbstractXMLLight : public IXMLLight { protected : basic_string m_internalString; public : inline basic_string getInternalString(void) const; protected : inline explicit AbstractXMLLight ( const basic_string & str); inline explicit AbstractXMLLight( const basic_istream & s ,IXMLLight *parent = NULL); inline ~AbstractXMLLight(void); XMLLightVector > getNodes ( const basic_string & pattern ); //vector getNodes ( const string & pattern ); //XMLLight & getFirstNode ( const string & pattern = ""); //IXMLLightImpl & getRoot ( void ); basic_string getName ( void ) ; //string & getValue ( void ) ; //string & getValue ( const string & firstNodeName ) ; bool containsAttribute ( const basic_string & attributeName ); map , basic_string > getAttributes ( void ); basic_string getAttributeValue ( const basic_string & attributeName ); friend basic_ostream & operator<< ( basic_ostream & os, const AbstractXMLLight * xl) { return os << endl << "\t\t" << xl->m_internalString << endl; } private: inline basic_string extractEndRootTag(void); inline void trim(void) ; }; //******************************************Inline implementation***********************************// // implementation of constructors and destructors template < typename char_type, typename ReferenceCounter> AbstractXMLLight::AbstractXMLLight ( const basic_string & str) : m_internalString(str) { trim(); } // CHANGE HERE template< typename char_type, typename ReferenceCounter> AbstractXMLLight::AbstractXMLLight ( const basic_istream & str, IXMLLight * parent) { basic_stringstream st ; st << str.rdbuf() ; m_internalString = st.str(); trim(); if ( parent == NULL ) { typename basic_string::iterator iterator = m_internalString.begin(); while ( *(++iterator) != BEGIN_TAG ); m_internalString = basic_string(iterator, m_internalString.end() ); } } template< typename char_type, typename ReferenceCounter> AbstractXMLLight::~AbstractXMLLight( void ) {} // // implementation of getter. template< typename char_type, typename ReferenceCounter> basic_string AbstractXMLLight::getInternalString(void) const { return m_internalString; } // implementation of helper method. template < typename char_type, typename ReferenceCounter> void AbstractXMLLight::trim(void) { int index = m_internalString.find_first_of(BEGIN_TAG); int lastIndex = m_internalString.find_last_of(END_TAG); basic_string reducedString = m_internalString.substr(index,lastIndex - index + 1 ); m_internalString = reducedString; } template < typename char_type, typename ReferenceCounter> basic_string AbstractXMLLight::extractEndRootTag(void) { basic_string internal = m_internalString.substr(0,m_internalString.find_last_of(BEGIN_TAG)); return internal.substr(0, m_internalString.find_last_of(END_TAG) + 1 ); } template < typename char_type, typename ReferenceCounter> XMLLightVector > AbstractXMLLight::getNodes ( const basic_string & pattern ) { XMLLightVector > returnedNodesVector; basic_string reduceString = extractEndRootTag(); if ( reduceString.empty() ) { return returnedNodesVector; } typename basic_string::iterator iterator = reduceString.begin(); typename basic_string::iterator endStringIterator = reduceString.end(); typename basic_string::iterator tempIterator = iterator; bool first = true; while ( iterator != endStringIterator ) { if ( first ) { while ( *(++iterator) != END_TAG ) ; if ( ++iterator == endStringIterator ) { return returnedNodesVector; } first = false; } tempIterator = iterator; iterator = adjacent_find(iterator, endStringIterator, XMLLightComparator()); if ( iterator != endStringIterator ) { iterator+=2; } basic_string s ( tempIterator, iterator ); if ( s.empty() || s.find_first_of(BEGIN_TAG) == basic_string::npos ) { return returnedNodesVector; } AbstractXMLLight * abstractXMLLight = new AbstractXMLLight(s); if ( pattern.empty() || abstractXMLLight->getName() == pattern ) { returnedNodesVector.push_back(abstractXMLLight); } else { delete abstractXMLLight; } if ( iterator != endStringIterator ) { iterator++; } } return returnedNodesVector; } template < typename char_type, typename ReferenceCounter> basic_string AbstractXMLLight::getName( void ) { if ( m_internalString.empty() ) { std::cout << " oups chaine non copie " << std::endl ; } typename basic_string::iterator iterator = m_internalString.begin(); basic_string resultString ; while ( *( ++ iterator ) != END_TAG && *iterator != ' ' && *iterator != '\n' && *iterator != '\t' && *iterator != SECOND_TAG ) { resultString.append(1,*( iterator) ); } return resultString; } template < typename char_type, typename ReferenceCounter> map< basic_string, basic_string > AbstractXMLLight::getAttributes(void) { char equalSeparator = '='; basic_string separator(" \a\b\n\t\f\r\v"); basic_string key; basic_string value; map, basic_string > returnedMap; size_t last_position = 0; size_t previous_position = 0; size_t begin_position = 0; size_t end_position = m_internalString.find_first_of(END_TAG); basic_string nodeString = m_internalString.substr(0,end_position); while ( last_position = nodeString.find_first_of(equalSeparator,last_position) , last_position != basic_string::npos ) { begin_position = last_position; last_position = nodeString.find_last_not_of(separator,begin_position-1); previous_position = nodeString.find_last_of(separator,last_position); key = nodeString.substr(previous_position+1, last_position-previous_position ); last_position = nodeString.find_first_not_of(separator, begin_position +1) ; previous_position = last_position; last_position = nodeString.find_first_of(nodeString[previous_position] , previous_position +1 ); value = nodeString.substr(previous_position +1 ,last_position - previous_position - 1); returnedMap.insert(make_pair(key,value)); // test d'erreur pour l'insertion a ajouter } return returnedMap; } template < typename char_type, typename ReferenceCounter> bool AbstractXMLLight::containsAttribute ( const basic_string & attributeName ) { map,basic_string > attributeMap = getAttributes(); if ( ! attributeMap.empty() && attributeMap.count(attributeName) == 1 ) { return true; } return false; } template < typename char_type, typename ReferenceCounter> basic_string AbstractXMLLight::getAttributeValue ( const basic_string & attributeName ) { map, basic_string > attributeMap = getAttributes() ; typename map,basic_string >::iterator it = attributeMap.find(attributeName); if ( it == attributeMap.end() ) { return basic_string(""); } return (*it).second; } } } #endif