// // Copyright 2011 by Craig Stuart Sapp, All Rights Reserved. // Programmer: Craig Stuart Sapp // Creation Date: Tue Jun 7 13:04:03 PDT 2011 // Last Modified: Fri Jun 24 14:54:42 PDT 2011 // Filename: ...sig/src/sigInfo/XmlFile.cpp // Web Address: http://sig.sapp.org/src/sigInfo/XmlFile.cpp // Syntax: C++ // // Description: A class that stores a list of XML text fragments and // parses the element structure. // #ifndef OLDCPP #include #include #define SSTREAM stringstream #define CSTRING str().c_str() using namespace std; #else #ifdef VISUAL #include /* for Windows 95 */ #else #include #endif #include #define SSTREAM strstream #define CSTRING str() #endif #include "PerlRegularExpression.h" #include "XmlFile.h" #include #include ////////////////////////////// // // XmlFile::XmlFile -- // XmlFile::XmlFile(void) { elements.setSize(0); parsedQ = 0; } XmlFile::XmlFile(int allocationSize) { elements.setSize(0); parsedQ = 0; } ////////////////////////////// // // XmlFile::~XmlFile -- // XmlFile::~XmlFile() { clear(); } ////////////////////////////// // // XmlFile::clearElements -- remove all contents of the data and the // parsed element data. // void XmlFile::clearElements(void) { int i; for (i=0; i& item) { parsedQ = 0; return xmlitems.appendItem(item); } ////////////////////////////// // // XmlFile::insertItem -- place an element at the given // index into the list of items. 0 = instart at start of list. // Use appendItem to place at end of list. Return value is the // serial number of the inserted item. // int XmlFile::insertItem(Array& item, int index) { parsedQ = 0; return xmlitems.insertItem(item, index); } ////////////////////////////// // // XmlFile::deleteItem -- returns true if an item was deleted. // int XmlFile::deleteItem(int index) { parsedQ = 0; return xmlitems.deleteItem(index); } ////////////////////////////// // // XmlFile::printElementList -- used for debugging the parsing of elements. // ostream& XmlFile::printElementList(ostream& out) { int i; XmlFile& xmlfile = *this; for (i=0; i]+).*/\\s*>")) { // element starts/ends at the same index point. // No need to do any further processing so just store and continue. XmlElement *ptr = new XmlElement; ptr->setName(pre.getSubmatch(1)); ptr->setStartSerial(xmlitems.getSerial(startindex)); ptr->setEndSerial(xmlitems.getSerial(startindex)); elements.append(ptr); return endindex; // index will be incremented in calling function } // element-start tag is the input, so search for its ending // in the data. Apply a recursive processing of children // elements at the same time. if (!pre.search(xmlitems[index].cstr(), "^\\s*<\\s*([^\\s>]+)")) { // malformed (empty) element tag, skip it. return index; } pre.getSubmatch(1); // fill in the internal match with element name // search for the ending tag like "" SigString endname("<\\s*/\\s*"); endname += pre.getSubmatch(); endname += "\\s*>"; // start name is the regular expression for a starting element name SigString startname("<\\s*"); startname += pre.getSubmatch(); startname += "[\\s>"; // create and store the current element (so that intervening elements // can be place in the list in the correct order. XmlElement *ptr = new XmlElement; ptr->setName(pre.getSubmatch()); ptr->setStartSerial(xmlitems.getSerial(startindex)); elements.append(ptr); // set the end index later with ptr->setEndSerial(); int i = index+1; PerlRegularExpression pre2; while (i]+).*/\\s*>")) { // found a single-item element process it and continue; i = processElement(i) + 1; continue; } // check for an ending tag which matches current element if (pre2.search(xmlitems[i].cstr(), endname.getBase())) { ptr->setEndSerial(xmlitems.getSerial(i)); i++; continue; } // check for element starting, and process if so: if (pre2.search(xmlitems[i].cstr(), "^\\s*<([^\\s>]+).*\\s*>")) { i = processElement(i) + 1; continue; } i++; } return i; } ////////////////////////////// // // XmlFile::isParsed -- // int XmlFile::isParsed(void) { return parsedQ; } /////////////////////////////////////////////////////////////////////////// // // friendly functions // ////////////////////////////// // // operator<< -- // ostream& operator<<(ostream& out, XmlFile& xmlfile) { int i; int asize = xmlfile.getItemSize(); for (i=0; i