1 | #ifndef _ABSTRACT_XMLLIGHT_H |
---|
2 | #define _ABSTRACT_XMLLIGHT_H |
---|
3 | |
---|
4 | // WORK IN PROGRESS THERE MUST BE SYNTAX ERROR AND OTHER BOGUS THING |
---|
5 | |
---|
6 | #include "Behavioural/include/IXMLLight.h" |
---|
7 | #include "Behavioural/include/XMLLightVector.h" |
---|
8 | #include "Behavioural/include/XMLLightFunctors.h" |
---|
9 | #include <sstream> |
---|
10 | #include <iterator> |
---|
11 | #include <string> |
---|
12 | #include <iostream> |
---|
13 | |
---|
14 | using std::basic_stringstream ; |
---|
15 | using std::basic_string; |
---|
16 | using std::basic_ostream; |
---|
17 | using std::basic_istream; |
---|
18 | |
---|
19 | namespace morpheo { |
---|
20 | namespace XMLUtils { |
---|
21 | |
---|
22 | |
---|
23 | //******************************************Class Declaration***************************************// |
---|
24 | |
---|
25 | template< typename char_type, typename ReferenceCounter = ClassicReferenceCounter > |
---|
26 | class AbstractXMLLight : public IXMLLight<char_type,ReferenceCounter> { |
---|
27 | protected : |
---|
28 | basic_string<char_type> m_internalString; |
---|
29 | public : |
---|
30 | inline basic_string<char_type> getInternalString(void) const; |
---|
31 | protected : |
---|
32 | inline explicit AbstractXMLLight ( const basic_string<char_type> & str); |
---|
33 | inline explicit AbstractXMLLight( const basic_istream<char_type> & s ,IXMLLight<char_type,ReferenceCounter> *parent = NULL); |
---|
34 | inline ~AbstractXMLLight(void); |
---|
35 | |
---|
36 | XMLLightVector<IXMLLight<char_type, ReferenceCounter> > getNodes ( const basic_string<char_type> & pattern ); |
---|
37 | |
---|
38 | //vector<XMLLight *> getNodes ( const string & pattern ); |
---|
39 | //XMLLight & getFirstNode ( const string & pattern = ""); |
---|
40 | |
---|
41 | |
---|
42 | //IXMLLightImpl & getRoot ( void ); |
---|
43 | basic_string<char_type> getName ( void ) ; |
---|
44 | //string & getValue ( void ) ; |
---|
45 | //string & getValue ( const string & firstNodeName ) ; |
---|
46 | bool containsAttribute ( const basic_string<char_type> & attributeName ); |
---|
47 | map<basic_string<char_type> , basic_string<char_type> > getAttributes ( void ); |
---|
48 | basic_string<char_type> getAttributeValue ( const basic_string<char_type> & attributeName ); |
---|
49 | |
---|
50 | friend basic_ostream<char_type> & operator<< |
---|
51 | ( basic_ostream<char_type> & os, const AbstractXMLLight<char_type, ReferenceCounter> * xl) { |
---|
52 | return os << endl << "\t\t" << xl->m_internalString << endl; |
---|
53 | } |
---|
54 | |
---|
55 | private: |
---|
56 | inline basic_string<char_type> extractEndRootTag(void); |
---|
57 | inline void trim(void) ; |
---|
58 | |
---|
59 | }; |
---|
60 | |
---|
61 | //******************************************Inline implementation***********************************// |
---|
62 | |
---|
63 | // implementation of constructors and destructors |
---|
64 | template < typename char_type, typename ReferenceCounter> |
---|
65 | AbstractXMLLight<char_type,ReferenceCounter>::AbstractXMLLight ( const basic_string<char_type> & str) |
---|
66 | : m_internalString(str) { |
---|
67 | trim(); |
---|
68 | } |
---|
69 | // CHANGE HERE |
---|
70 | |
---|
71 | template< typename char_type, typename ReferenceCounter> |
---|
72 | AbstractXMLLight<char_type,ReferenceCounter>::AbstractXMLLight |
---|
73 | ( const basic_istream<char_type> & str, IXMLLight<char_type, ReferenceCounter> * parent) { |
---|
74 | basic_stringstream<char_type> st ; |
---|
75 | st << str.rdbuf() ; |
---|
76 | m_internalString = st.str(); |
---|
77 | trim(); |
---|
78 | if ( parent == NULL ) { |
---|
79 | typename basic_string<char_type>::iterator iterator = m_internalString.begin(); |
---|
80 | while ( *(++iterator) != BEGIN_TAG ); |
---|
81 | m_internalString = basic_string<char_type>(iterator, m_internalString.end() ); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | template< typename char_type, typename ReferenceCounter> |
---|
86 | AbstractXMLLight<char_type,ReferenceCounter>::~AbstractXMLLight( void ) {} |
---|
87 | // |
---|
88 | // implementation of getter. |
---|
89 | template< typename char_type, typename ReferenceCounter> |
---|
90 | basic_string<char_type> AbstractXMLLight<char_type,ReferenceCounter>::getInternalString(void) const { |
---|
91 | return m_internalString; |
---|
92 | } |
---|
93 | |
---|
94 | // implementation of helper method. |
---|
95 | template < typename char_type, typename ReferenceCounter> |
---|
96 | void AbstractXMLLight<char_type,ReferenceCounter>::trim(void) { |
---|
97 | |
---|
98 | int index = m_internalString.find_first_of(BEGIN_TAG); |
---|
99 | int lastIndex = m_internalString.find_last_of(END_TAG); |
---|
100 | basic_string<char_type> reducedString = m_internalString.substr(index,lastIndex - index + 1 ); |
---|
101 | m_internalString = reducedString; |
---|
102 | } |
---|
103 | |
---|
104 | template < typename char_type, typename ReferenceCounter> |
---|
105 | basic_string<char_type> AbstractXMLLight<char_type, ReferenceCounter>::extractEndRootTag(void) { |
---|
106 | basic_string<char_type> internal = m_internalString.substr(0,m_internalString.find_last_of(BEGIN_TAG)); |
---|
107 | return internal.substr(0, m_internalString.find_last_of(END_TAG) + 1 ); |
---|
108 | } |
---|
109 | |
---|
110 | template < typename char_type, typename ReferenceCounter> |
---|
111 | XMLLightVector<IXMLLight<char_type,ReferenceCounter> > AbstractXMLLight<char_type, ReferenceCounter>::getNodes ( const basic_string<char_type> & pattern ) |
---|
112 | { |
---|
113 | XMLLightVector<IXMLLight<char_type,ReferenceCounter> > returnedNodesVector; |
---|
114 | basic_string<char_type> reduceString = extractEndRootTag(); |
---|
115 | if ( reduceString.empty() ) |
---|
116 | { |
---|
117 | return returnedNodesVector; |
---|
118 | } |
---|
119 | typename basic_string<char_type>::iterator iterator = reduceString.begin(); |
---|
120 | typename basic_string<char_type>::iterator endStringIterator = reduceString.end(); |
---|
121 | typename basic_string<char_type>::iterator tempIterator = iterator; |
---|
122 | bool first = true; |
---|
123 | |
---|
124 | while ( iterator != endStringIterator ) |
---|
125 | { |
---|
126 | if ( first ) |
---|
127 | { |
---|
128 | while ( *(++iterator) != END_TAG ) ; |
---|
129 | if ( ++iterator == endStringIterator ) |
---|
130 | { |
---|
131 | return returnedNodesVector; |
---|
132 | } |
---|
133 | first = false; |
---|
134 | } |
---|
135 | tempIterator = iterator; |
---|
136 | iterator = adjacent_find(iterator, endStringIterator, XMLLightComparator()); |
---|
137 | if ( iterator != endStringIterator ) |
---|
138 | { |
---|
139 | iterator+=2; |
---|
140 | } |
---|
141 | |
---|
142 | basic_string<char_type> s ( tempIterator, iterator ); |
---|
143 | if ( s.empty() || s.find_first_of(BEGIN_TAG) == basic_string<char_type>::npos ) |
---|
144 | { |
---|
145 | return returnedNodesVector; |
---|
146 | } |
---|
147 | AbstractXMLLight<char_type, ReferenceCounter> * abstractXMLLight = new AbstractXMLLight<char_type, ReferenceCounter>(s); |
---|
148 | if ( pattern.empty() || abstractXMLLight->getName() == pattern ) |
---|
149 | { |
---|
150 | returnedNodesVector.push_back(abstractXMLLight); |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | delete abstractXMLLight; |
---|
155 | } |
---|
156 | if ( iterator != endStringIterator ) |
---|
157 | { |
---|
158 | iterator++; |
---|
159 | } |
---|
160 | } |
---|
161 | return returnedNodesVector; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | template < typename char_type, typename ReferenceCounter> |
---|
166 | basic_string<char_type> AbstractXMLLight<char_type,ReferenceCounter>::getName( void ) |
---|
167 | { |
---|
168 | if ( m_internalString.empty() ) { |
---|
169 | std::cout << " oups chaine non copie " << std::endl ; |
---|
170 | } |
---|
171 | typename basic_string<char_type>::iterator iterator = m_internalString.begin(); |
---|
172 | basic_string<char_type> resultString ; |
---|
173 | while ( *( ++ iterator ) != END_TAG && *iterator != ' ' && *iterator != '\n' && *iterator != '\t' && *iterator != SECOND_TAG ) |
---|
174 | |
---|
175 | { |
---|
176 | resultString.append(1,*( iterator) ); |
---|
177 | } |
---|
178 | return resultString; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | template < typename char_type, typename ReferenceCounter> |
---|
183 | map< basic_string<char_type>, basic_string<char_type> > AbstractXMLLight<char_type,ReferenceCounter>::getAttributes(void) |
---|
184 | { |
---|
185 | char equalSeparator = '='; |
---|
186 | basic_string<char_type> separator(" \a\b\n\t\f\r\v"); |
---|
187 | basic_string<char_type> key; |
---|
188 | basic_string<char_type> value; |
---|
189 | |
---|
190 | map<basic_string<char_type>, basic_string<char_type> > returnedMap; |
---|
191 | size_t last_position = 0; |
---|
192 | size_t previous_position = 0; |
---|
193 | size_t begin_position = 0; |
---|
194 | size_t end_position = m_internalString.find_first_of(END_TAG); |
---|
195 | basic_string<char_type> nodeString = m_internalString.substr(0,end_position); |
---|
196 | |
---|
197 | while ( last_position = nodeString.find_first_of(equalSeparator,last_position) , last_position != basic_string<char_type>::npos ) |
---|
198 | { |
---|
199 | begin_position = last_position; |
---|
200 | last_position = nodeString.find_last_not_of(separator,begin_position-1); |
---|
201 | previous_position = nodeString.find_last_of(separator,last_position); |
---|
202 | key = nodeString.substr(previous_position+1, last_position-previous_position ); |
---|
203 | last_position = nodeString.find_first_not_of(separator, begin_position +1) ; |
---|
204 | previous_position = last_position; |
---|
205 | last_position = nodeString.find_first_of(nodeString[previous_position] , previous_position +1 ); |
---|
206 | value = nodeString.substr(previous_position +1 ,last_position - previous_position - 1); |
---|
207 | returnedMap.insert(make_pair(key,value)); |
---|
208 | // test d'erreur pour l'insertion a ajouter |
---|
209 | |
---|
210 | } |
---|
211 | |
---|
212 | return returnedMap; |
---|
213 | } |
---|
214 | |
---|
215 | |
---|
216 | template < typename char_type, typename ReferenceCounter> |
---|
217 | bool AbstractXMLLight<char_type,ReferenceCounter>::containsAttribute ( const basic_string<char_type> & attributeName ) |
---|
218 | { |
---|
219 | map<basic_string<char_type>,basic_string<char_type> > attributeMap = getAttributes(); |
---|
220 | if ( ! attributeMap.empty() && attributeMap.count(attributeName) == 1 ) |
---|
221 | { |
---|
222 | return true; |
---|
223 | } |
---|
224 | |
---|
225 | return false; |
---|
226 | } |
---|
227 | |
---|
228 | template < typename char_type, typename ReferenceCounter> |
---|
229 | basic_string<char_type> AbstractXMLLight<char_type,ReferenceCounter>::getAttributeValue ( const basic_string<char_type> & attributeName ) |
---|
230 | { |
---|
231 | map<basic_string<char_type>, basic_string<char_type> > attributeMap = getAttributes() ; |
---|
232 | typename map<basic_string<char_type>,basic_string<char_type> >::iterator it = attributeMap.find(attributeName); |
---|
233 | if ( it == attributeMap.end() ) |
---|
234 | { |
---|
235 | return basic_string<char_type>(""); |
---|
236 | } |
---|
237 | return (*it).second; |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|
241 | #endif |
---|
242 | |
---|