inline void trim(SequenceT& Input, const std::locale& Loc=std::locale()) { trim_if( Input, is_space( Loc ) ); }
int Node::parseTag(string filename, string xmlData, unsigned int position) { if (xmlData.at(position) != '<') { throw new Exception("Did not find < as expected. Character " + convert::int2string(position)); } position++; int endPosition = xmlData.find('>', position); if (xmlData.at(position) == '?' || xmlData.at(position) == '!' || xmlData.at(position) == '/') { return endPosition; } this->myIsEmpty = false; string tagString; try { tagString = xmlData.substr(position, endPosition - position); } catch (std::out_of_range & e) { LOG.debug("parseTag exception: " + string(e.what())); LOG.debug("A xmlData=" + xmlData + " :: position=" + convert::int2string(position) + " :: endPosition=" + convert::int2string(endPosition)); throw e; } trim(tagString); trim_if(tagString, is_any_of("\n")); trim(tagString); tagString += " "; bool foundEnd = false; bool foundTag = false; string buffer; unsigned int pos = 0; char c; int s, e; while (pos < tagString.length()) { c = tagString.at(pos); switch (c) { case ' ': if (!foundTag) { this->myTagName = buffer; buffer = ""; //cout << "Found tag: " << myTagName << endl; foundTag = true; } break; case '/': foundEnd = true; break; case '=': s = tagString.find('"', pos) + 1; e = tagString.find('"', s); try { this->myAttributes[buffer] = tagString.substr(s, e - s); //LOG.debug("Added attribute \"" + buffer + "\" = \"" + this->myAttributes[buffer] + "\""); } catch (std::out_of_range & ex) { LOG.debug("parseTag exception: " + string(ex.what())); LOG.debug("A xmlData=" + xmlData + " :: position=" + convert::int2string(position) + " :: endPosition=" + convert::int2string(endPosition)); throw ex; } //cout << "Found attribute: " << buffer << " = " << myAttributes[buffer] << endl; pos = e; buffer = ""; break; default: buffer += c; break; } pos++; } position = endPosition; if (!foundEnd) { while (position < xmlData.length()) { c = xmlData.at(position); switch (c) { case '<': if (xmlData.at(position + 1) == '/') { position += 2; endPosition = xmlData.find('>', position); try { if (myTagName.compare(xmlData.substr(position, endPosition - position)) == 0) { //cout << "Found end tag: " << myTagName << endl; return endPosition; } else { throw new Exception("Invalid end tag found. " + xmlData.substr(position, endPosition - position) + "::" + myTagName); } } catch (std::out_of_range & ex) { LOG.debug("parseTag exception: " + string(ex.what())); LOG.debug("A xmlData=" + xmlData + " :: position=" + convert::int2string(position) + " :: endPosition=" + convert::int2string(endPosition)); throw ex; } } Node subNode; position = subNode.parseTag(filename, xmlData, position); if (!subNode.myIsEmpty) { this->myChildren.push_back(subNode); } break; } position++; } } return position; }