Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

xmladaptivemodel.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002     xmladaptivemodel.cpp  -  Definitions of class XmlAdaptiveModel methods
00003                              -------------------
00004     begin                : November 18 2002
00005     copyright            : (C) 2003 by Vojtìch Toman
00006     email                : vtoman@lit.cz
00007 ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00026 #ifdef __GNUG__
00027 # pragma implementation
00028 #endif
00029 
00030 
00031 #include "xmladaptivemodel.h"
00032 
00033 
00041 #define SAFE_CALL_EMITTER(_x_)          \
00042 if (saxEmitter)                         \
00043   saxEmitter->_x_;
00044 
00045 
00047 #define RESET_BUFFER                    \
00048 {                                       \
00049   buffer[0] = 0;                        \
00050   bufferLength = 0;                     \
00051 }
00052 
00053 
00061 #define CHANGE_STATE(_s_)                                               \
00062 {                                                                       \
00063   stateChanged = false;                                                 \
00064   if (state != AdaptiveStructuralSymbols::KnownElement &&               \
00065       (state == AdaptiveStructuralSymbols::None || state == _s_))       \
00066   {                                                                     \
00067     state = _s_;                                                        \
00068     stateChanged = true;                                                \
00069   }                                                                     \
00070 }
00071 
00072 
00074 #define RESET_STATE             state = AdaptiveStructuralSymbols::None
00075 
00076 
00085 #define APPEND_ALL_DATA_LENGTH(_gr_, _data_, _length_, _complete_)      \
00086 {                                                                       \
00087   CHECK_POINTER(_gr_);                                                  \
00088                                                                         \
00089   for (int _i_ = 0; _i_ < _length_; _i_++)                              \
00090     _gr_->append((TerminalValue)_data_[_i_]);                           \
00091                                                                         \
00092   if (_complete_)                                                       \
00093     _gr_->append(AdaptiveStructuralSymbols::EndOfBlock);                \
00094 }
00095 
00096 
00105 #define APPEND_ALL_DATA_LENGTH_CONVERT_AMP(_gr_, _data_, _length_, _complete_)  \
00106 {                                                                               \
00107   CHECK_POINTER(_gr_);                                                          \
00108                                                                                 \
00109   for (int _i_ = 0; _i_ < _length_; _i_++)                                      \
00110     {                                                                           \
00111       if (_data_[_i_] == '&')                                                   \
00112         _gr_->append((TerminalValue)AMP_REPLACEMENT);                           \
00113       else                                                                      \
00114         _gr_->append((TerminalValue)_data_[_i_]);                               \
00115     }                                                                           \
00116                                                                                 \
00117   if (_complete_)                                                               \
00118     _gr_->append(AdaptiveStructuralSymbols::EndOfBlock);                        \
00119 }
00120 
00121 
00129 #define APPEND_ALL_DATA(_gr_, _data_, _complete_)               \
00130 {                                                               \
00131   CHECK_POINTER(_gr_);                                          \
00132                                                                 \
00133   if (!_data_)                                                  \
00134     return false;                                               \
00135                                                                 \
00136   for (int _i_ = 0; _data_[_i_]; _i_++)                         \
00137     _gr_->append((TerminalValue)_data_[_i_]);                   \
00138                                                                 \
00139   if (_complete_)                                               \
00140     _gr_->append(AdaptiveStructuralSymbols::EndOfBlock);        \
00141 }
00142 
00143 
00151 #define APPEND_BUFFER_DATA(_gr_, _buffer_, _nrItems_)   \
00152 {                                                       \
00153   for (size_t _i_ = 0; _i_ < _nrItems_; _i_++)          \
00154     {                                                   \
00155       _gr_->append(_buffer_[_i_]);                      \
00156     }                                                   \
00157 }
00158 
00159 
00167 #define END_CHARACTER_DATA_SECTION(_gr_)                \
00168 {                                                       \
00169   _gr_->append(AdaptiveStructuralSymbols::EndOfBlock);  \
00170   inCharacters = false;                                 \
00171 }
00172 
00173 
00175 #define RESET_CLUE_EDGE                         \
00176 {                                               \
00177   clueEdge = 1;                                 \
00178   DBG("CLUE EDGE RESET!");\
00179 }
00180 
00181 
00183 #define INCREASE_CLUE_EDGE                      \
00184 {                                               \
00185   clueEdge++;                                   \
00186   DBG("value of clue edge after the incrementation " << clueEdge);\
00187 }
00188 
00189 
00191 #define REPORT_NACK                                                             \
00192 {                                                                               \
00193   DBG("NACK edge: " << clueEdge);                                               \
00194                                                                                 \
00195   grammar->append(AdaptiveStructuralSymbols::NACK);                             \
00196   fibItems = Fibonacci::encodeToBuffer(fibBuf, SIZEOF_XML_CHAR, clueEdge);      \
00197   APPEND_BUFFER_DATA(grammar, fibBuf, fibItems);                                \
00198   DBG("FIB ITEMS: " << (unsigned int)fibBuf[0]);                                \
00199 }
00200                                                         
00201 
00207 #define REPORT_DIFFERENT_EDGE(_edge_)                                           \
00208 {                                                                               \
00209   DBG("edge no. " << edge << " selected");                                      \
00210   grammar->append(AdaptiveStructuralSymbols::DifferentEdge);                    \
00211   fibItems = Fibonacci::encodeToBuffer(fibBuf, SIZEOF_XML_CHAR, _edge_);        \
00212   APPEND_BUFFER_DATA(grammar, fibBuf, fibItems);                                \
00213 }
00214 
00215 
00217 #define FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES                                 \
00218 {                                                                               \
00219   if (!attrPos && !dataQueue->isEmpty())                                        \
00220     {                                                                           \
00221       DBG("FINISHING WITH NO ATTRS!");                                          \
00222       dataQueueItem = dataQueue->dequeue();                                     \
00223       /* update the attribute stats */                                          \
00224       elementModelerStack.top()->modeler->setAttributes(false);                 \
00225       /* emit startElement event with no attributes */                          \
00226       SAFE_CALL_EMITTER(startElement(userData, dataQueueItem->data, 0));        \
00227       attrPos = 1;                                                              \
00228     }                                                                           \
00229 }
00230 
00231 
00232 
00236 XmlAdaptiveModel::XmlAdaptiveModel(void)
00237   : XmlModelBase()
00238 {
00239   grammar = 0;
00240 
00241 
00242   state = AdaptiveStructuralSymbols::None;
00243   
00244   buffer = 0;
00245   bufferLength = 0;
00246 
00247   fibBits = 0;
00248   fibCode = 0;
00249   fibLastWasOne = false;
00250   
00251   NEW(dataQueue, DataQueue);
00252   dataQueue->setAutoDelete(true);
00253 
00254   saxEmitter = 0;
00255   userData = 0;
00256 
00257   //    dataInEncoding = UTF_8;
00258   //    dataOutEncoding = UTF_8;
00259 
00260   inCDATA = false;
00261   inDoctype = false;
00262   inCharacters = false;
00263   inProlog = true;
00264   inNACK = false;
00265   inAttr= false;
00266   firstAttrRead = false;
00267   inAttrValue = false;
00268   nackEndedInCharacters = false;
00269 
00270   attrPos = 1;  //0 = in the attributes, 1 = not in the attributes
00271 
00272 
00273   //stack of element ids
00274   NEW(elementStack, Stack<unsigned long>);
00275 
00276   elementStack->setAutoDelete(true);
00277 
00278   NEW(elementIds, ElementIds);
00279   elementIds->setAutoDelete(true);
00280 
00281   NEW(elements, AdaptiveElementTable);
00282   elements->setAutoDelete(true);
00283 
00284 
00285   //list containing all element names
00286   //used just for freeing all the data
00287   NEW(elementNamesList, ElementNamesList);
00288   elementNamesList->setAutoDelete(true);
00289 
00290 
00291   NEW(attributeIds, AttributeIds);
00292   attributeIds->setAutoDelete(true);
00293 
00294   //list containing all attributes names
00295   //used just for freeing all the data
00296   NEW(attributeNamesList, AttributeNamesList);
00297   attributeNamesList->setAutoDelete(true);
00298 
00299   NEW(attributeNames, AttributeNamesTable);
00300   attributeNames->setAutoDelete(false);         //the attribute names will be deleted by attributeNamesList!
00301 
00302   elementCounter = 1;
00303   attributeCounter = 1;
00304 
00305   elementModelerList.setAutoDelete(true);
00306 
00307   
00308   clueEdge = 1;
00309   readingNackData = false;
00310   elementModelerStack.setAutoDelete(true);
00311 }
00312 
00313 
00314   
00318 XmlAdaptiveModel::~XmlAdaptiveModel(void)
00319 {
00320   DELETE(elements);
00321   DELETE(elementStack);
00322 
00323 
00324   DELETE(dataQueue);
00325 
00326   DELETE(attributeIds);
00327   DELETE(attributeNames);
00328 
00329   //pre-compute the average entropy of the element models if in verbose (or "print models") mode
00330   if (ExaltOptions::getOption(ExaltOptions::PrintModels) == ExaltOptions::Yes ||
00331       ExaltOptions::getOption(ExaltOptions::Verbose) == ExaltOptions::Yes)
00332     {
00333       for (ElementModeler *em = elementModelerList.first(); em; em = elementModelerList.next())
00334         {
00335           //compute the average entropy of the models
00336           //also, as a side effect, the ref counts of the models are adjusted
00337           em->computeAverageEntropy();
00338         }
00339     }
00340       
00341   //display the document entropy information if in verbose mode
00342   if (ExaltOptions::getOption(ExaltOptions::Verbose) == ExaltOptions::Yes)
00343     {
00344       size_t totalModels = 0;
00345       double totalAverageEntropy = 0;
00346       for (ElementModeler *em = elementModelerList.first(); em; em = elementModelerList.next())
00347         {
00348           totalModels += em->getRefCount();
00349           //DBG("Model for element " << em->getElementName() << " referenced " << em->getRefCount() << " times and has entropy: " << em->getAverageEntropy());
00350           totalAverageEntropy += em->getRefCount()*em->getAverageEntropy();
00351         }
00352       
00353       totalAverageEntropy = totalAverageEntropy / totalModels;
00354       
00355       OUTPUTENL("  Number of elements:\t\t\t" << elementNamesList->count());
00356       OUTPUTENL("  Number of attributes:\t\t\t" << attributeNamesList->count());
00357       OUTPUTENL("  Average entropy of the document:\t" << totalAverageEntropy);
00358       OUTPUTEENDLINE;
00359     }
00360 
00361 
00362   //display the element models if in "print models" mode
00363   if (ExaltOptions::getOption(ExaltOptions::PrintModels) == ExaltOptions::Yes)
00364     {
00365       OUTPUTENL("Models of the elements");
00366 
00367       for (ElementModeler *em = elementModelerList.first(); em; em = elementModelerList.next())
00368         {
00369           em->print();
00370         }
00371     }
00372 
00373 
00374   DELETE(elementNamesList);
00375   DELETE(attributeNamesList);
00376 }
00377 
00378 
00385 void XmlAdaptiveModel::setSAXEmitter(SAXEmitter *emitter, void *data = 0)
00386 {
00387   saxEmitter = emitter;
00388   userData = data;
00389 }
00390 
00391 
00397 bool XmlAdaptiveModel::manageEvent(XmlModelEvent *event)
00398 {
00399   XmlStartElementEvent *startElementEvent;
00400   XmlEndElementEvent *endElementEvent;
00401   XmlCharactersEvent *charactersEvent;
00402   XmlCommentEvent *commentEvent;
00403   XmlPIEvent *piEvent;
00404   XmlDeclEvent *xmlDeclEvent;
00405   XmlStartDoctypeEvent *xmlStartDoctypeEvent;
00406   XmlEntityDeclEvent *xmlEntityDeclEvent;
00407   XmlNotationDeclEvent *xmlNotationDeclEvent;
00408   XmlDefaultEvent *defaultEvent;
00409 
00410   XmlChar *elName;
00411   unsigned long *elId;
00412   unsigned long elementId;
00413   ElementModelerInfo *emi;
00414 
00415   XmlChar *attrName;
00416   unsigned long *attrId;
00417   unsigned long attributeId;
00418 
00419   AdaptiveElementContext *ec;
00420   size_t fibItems;
00421   XmlChar fibBuf[30];
00422   Encodings::MIB mib;
00423 
00424   ElementModel::TransitionState result;
00425   size_t edge;
00426   size_t elts = 0;
00427   bool clueEdgeReset = false;
00428 
00429   //examine the event type and perform appropriate actions
00430   switch (event->type)
00431     {
00432       /**** XML declaration ****/
00433     case XmlModelEvent::XmlDecl:
00434       xmlDeclEvent = (XmlDeclEvent *)event;
00435 
00436       if (!textCodec)
00437         //use default text codec
00438         createDefaultTextCodec();
00439 
00440       mib = textCodec->getMIB(xmlDeclEvent->encoding);
00441           
00442       grammar->setAlphabetBaseSize(textCodec->suggestAlphabetBaseSize(mib));
00443 
00444       grammar->append(AdaptiveStructuralSymbols::XmlDecl);
00445 
00446       APPEND_ALL_DATA(grammar, xmlDeclEvent->version, true);
00447 
00448       if (xmlDeclEvent->encoding)
00449         {
00450           APPEND_ALL_DATA(grammar, xmlDeclEvent->encoding, true);
00451         }
00452       else
00453         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00454 
00455       if (xmlDeclEvent->standalone != -1)
00456         {
00457           if (xmlDeclEvent->standalone)
00458             grammar->append(AdaptiveStructuralSymbols::StandaloneYes);
00459           else
00460             grammar->append(AdaptiveStructuralSymbols::StandaloneNo);
00461         }
00462       else
00463         grammar->append(AdaptiveStructuralSymbols::StandaloneNotSpecified);
00464 
00465       break;
00466 
00467 
00468 
00469       /**** Doctype ****/
00470     case XmlModelEvent::StartDoctype:
00471       xmlStartDoctypeEvent = (XmlStartDoctypeEvent *)event;
00472 
00473       grammar->append(AdaptiveStructuralSymbols::Doctype);
00474 
00475       if (xmlStartDoctypeEvent->doctypeName)
00476         {
00477           APPEND_ALL_DATA(grammar, xmlStartDoctypeEvent->doctypeName, true);
00478         }
00479       else
00480         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00481 
00482       if (xmlStartDoctypeEvent->publicId)
00483         {
00484           if (!xmlStartDoctypeEvent->publicId[0])
00485             {
00486               grammar->append(AdaptiveStructuralSymbols::EmptyString);
00487               grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00488             }
00489           else
00490             {
00491               APPEND_ALL_DATA(grammar, xmlStartDoctypeEvent->publicId, true);
00492             }
00493         }
00494       else
00495         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00496           
00497       if (xmlStartDoctypeEvent->systemId)
00498         {
00499           if (!xmlStartDoctypeEvent->systemId[0])
00500             {
00501               grammar->append(AdaptiveStructuralSymbols::EmptyString);
00502               grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00503             }
00504           else
00505             {
00506               APPEND_ALL_DATA(grammar, xmlStartDoctypeEvent->systemId, true);
00507             }
00508         }
00509       else
00510         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00511           
00512       if (xmlStartDoctypeEvent->hasInternalSubset)
00513         grammar->append(AdaptiveStructuralSymbols::HasInternalSubsetYes);
00514       else
00515         grammar->append(AdaptiveStructuralSymbols::HasInternalSubsetNo);
00516           
00517       break;
00518 
00519       /**** End Doctype ****/
00520     case XmlModelEvent::EndDoctype:
00521       grammar->append(AdaptiveStructuralSymbols::Doctype);
00522       break;
00523 
00524 
00525       /**** Entity declaration ****/
00526     case XmlModelEvent::EntityDecl:
00527       xmlEntityDeclEvent = (XmlEntityDeclEvent *)event;
00528 
00529       grammar->append(AdaptiveStructuralSymbols::EntityDecl);
00530 
00531       if (xmlEntityDeclEvent->entityName)
00532         {
00533           APPEND_ALL_DATA(grammar, xmlEntityDeclEvent->entityName, true);
00534         }
00535       else
00536         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00537 
00538      
00539       if (!xmlEntityDeclEvent->valueLength && xmlEntityDeclEvent->value)
00540         {
00541           //zero length and value not NULL
00542           grammar->append(AdaptiveStructuralSymbols::EmptyString);
00543           grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00544         }
00545       else
00546         {
00547           if (xmlEntityDeclEvent->value)
00548             {
00549               //value of nonzero length
00550               APPEND_ALL_DATA_LENGTH(grammar, xmlEntityDeclEvent->value, xmlEntityDeclEvent->valueLength, true);
00551             }
00552           else
00553             //value is NULL
00554             grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00555         }
00556 
00557       if (xmlEntityDeclEvent->systemId)
00558         {
00559           if (!xmlEntityDeclEvent->systemId[0])
00560             {
00561               grammar->append(AdaptiveStructuralSymbols::EmptyString);
00562               grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00563             }
00564           else
00565             {
00566               APPEND_ALL_DATA(grammar, xmlEntityDeclEvent->systemId, true);
00567             }
00568         }
00569       else
00570         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00571 
00572       if (xmlEntityDeclEvent->publicId)
00573         {
00574           if (!xmlEntityDeclEvent->publicId[0])
00575             {
00576               grammar->append(AdaptiveStructuralSymbols::EmptyString);
00577               grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00578             }
00579           else
00580             {
00581               APPEND_ALL_DATA(grammar, xmlEntityDeclEvent->publicId, true);
00582             }
00583         }
00584       else
00585         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00586 
00587       if (xmlEntityDeclEvent->notationName)
00588         {
00589           if (!xmlEntityDeclEvent->notationName[0])
00590             {
00591               grammar->append(AdaptiveStructuralSymbols::EmptyString);
00592               grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00593             }
00594           else
00595             {
00596               APPEND_ALL_DATA(grammar, xmlEntityDeclEvent->notationName, true);
00597             }
00598         }
00599       else
00600         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00601 
00602       if (xmlEntityDeclEvent->isParameterEntity)
00603         grammar->append(AdaptiveStructuralSymbols::IsParameterEntityYes);
00604       else
00605         grammar->append(AdaptiveStructuralSymbols::IsParameterEntityNo);
00606 
00607       break;
00608 
00609 
00610       /**** Notation declaration ****/
00611     case XmlModelEvent::NotationDecl:
00612       xmlNotationDeclEvent = (XmlNotationDeclEvent *)event;
00613 
00614       grammar->append(AdaptiveStructuralSymbols::NotationDecl);
00615 
00616       if (xmlNotationDeclEvent->notationName)
00617         {
00618           APPEND_ALL_DATA(grammar, xmlNotationDeclEvent->notationName, true);
00619         }
00620       else
00621         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00622 
00623      
00624       if (xmlNotationDeclEvent->systemId)
00625         {
00626           if (!xmlNotationDeclEvent->systemId[0])
00627             {
00628               grammar->append(AdaptiveStructuralSymbols::EmptyString);
00629               grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00630             }
00631           else
00632             {
00633               APPEND_ALL_DATA(grammar, xmlNotationDeclEvent->systemId, true);
00634             }
00635         }
00636       else
00637         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00638 
00639       if (xmlNotationDeclEvent->publicId)
00640         {
00641           if (!xmlNotationDeclEvent->publicId[0])
00642             {
00643               grammar->append(AdaptiveStructuralSymbols::EmptyString);
00644               grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00645             }
00646           else
00647             {
00648               APPEND_ALL_DATA(grammar, xmlNotationDeclEvent->publicId, true);
00649             }
00650         }
00651       else
00652         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00653 
00654 
00655       break;
00656 
00657 
00658       /**** Start of element ****/
00659     case XmlModelEvent::StartElement:
00660       startElementEvent = (XmlStartElementEvent *)event;
00661 
00662       //end the eventual character data section
00663       if (inCharacters)
00664         {
00665           END_CHARACTER_DATA_SECTION(grammar);
00666         }
00667 
00668 
00669       if (!(elId = elementIds->find((XmlChar *)startElementEvent->name)))
00670         {
00671           //unknown element started
00672           //--> we'll create new element context for it
00673 
00674 
00675 
00676           NEW(elName, XmlChar[xmlchar_strlen(startElementEvent->name) + 1]);
00677           xmlchar_strcpy(elName, startElementEvent->name);
00678           elementNamesList->append(elName);
00679       
00680 
00681           //insert new element id to elementIds
00682           elementId = elementCounter;
00683           elementIds->insert(elName, new unsigned long(elementId));
00684 
00685 
00686           NEW(ec, AdaptiveElementContext);
00687 
00688           //set unique element id
00689           ec->name = elName;
00690 
00691           NEW(ec->modeler, ElementModeler(elName));
00692 
00693           NEW(emi, ElementModelerInfo);
00694           emi->modeler = ec->modeler;
00695 
00696           //new elements always build
00697           emi->building = true;
00698 
00699           DBG("NEW ELEMENT: " << startElementEvent->name);
00700 
00701           DBG("Element has this id: " << elementId);
00702           if (elementModelerStack.isEmpty())
00703             {
00704 
00705               //first element processed
00706               elementModelerStack.push(emi);
00707               emi->modeler->resetCurrentNode();
00708               clueEdge = 1;
00709 
00710               //append "new element" symbol to the grammar
00711               grammar->append(AdaptiveStructuralSymbols::NewElement);
00712               //append the name of the element to the grammar
00713               APPEND_ALL_DATA(grammar, startElementEvent->name, true);
00714             }
00715           else
00716             {
00717               //move to the new element
00718               result = elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::ElementNode, &edge, &elts, emi->modeler);
00719 
00720               if (result == ElementModel::NewNodeCreated)
00721                 {
00722                   if (elementModelerStack.top()->building)
00723                     {
00724                       DBG("BUILDING: New node created");
00725 
00726                       //append "new element" symbol to the grammar
00727                       grammar->append(AdaptiveStructuralSymbols::NewElement);
00728                       //append the name of the element to the grammar
00729                       APPEND_ALL_DATA(grammar, startElementEvent->name, true);
00730                       RESET_CLUE_EDGE;
00731                     }
00732                   else
00733                     {
00734                       DBG("NACK: New node created");
00735                       REPORT_NACK;
00736                       RESET_CLUE_EDGE;
00737 
00738                       //nack + new node created --> we're building
00739                       elementModelerStack.top()->building = true;
00740 
00741                       //append "new element" symbol to the grammar
00742                       grammar->append(AdaptiveStructuralSymbols::NewElement);
00743                       //append the name of the element to the grammar
00744                       APPEND_ALL_DATA(grammar, startElementEvent->name, true);
00745                     }
00746                 }
00747               else
00748                 if (result == ElementModel::Indefinite)
00749                   {
00750                     //prediction failed --> use different edge
00751                     DBG("NACK NEW: indefinite - add code");
00752                     REPORT_NACK;
00753                     REPORT_DIFFERENT_EDGE(edge);
00754 
00755                     RESET_CLUE_EDGE;
00756                     clueEdge = 1;
00757                   }
00758                 else
00759                   {
00760                     //              DBG("NEW definite - add code");
00761                   }
00762               elementModelerStack.push(emi);
00763               ec->modeler->resetCurrentNode();
00764               clueEdge = 1;
00765             }
00766 
00767           elementModelerList.append(ec->modeler);
00768 
00769           //add new element context to the element hashtable
00770           elements->insert(elementId, ec);
00771 
00772 
00773           //update the element counter
00774           elementCounter++;
00775         }
00776       else
00777         {
00778           //known element started
00779           //--> update its element context
00780           ec = elements->find(*elId);
00781           CHECK_POINTER(ec);
00782 
00783           DBG("KNOWN: " << ec->name);
00784           DBG("KNOWN ID: " << *elId);
00785 
00786           NEW(emi, ElementModelerInfo);
00787           emi->modeler = ec->modeler;
00788           //known elements use prediction
00789           emi->building = false;
00790 
00791           emi->modeler->increaseRefCount();
00792 
00793           elementId = *elId;
00794 
00795 
00796           result = elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::ElementNode, &edge, &elts, emi->modeler);
00797           
00798           if (result == ElementModel::NewNodeCreated)
00799             {
00800               if (elementModelerStack.top()->building)
00801                 {
00802                   DBG("BUILDING: New node created");
00803 
00804                   grammar->append(AdaptiveStructuralSymbols::KnownElement);
00805                   fibItems = Fibonacci::encodeToBuffer(fibBuf, SIZEOF_XML_CHAR, elementId);
00806                   APPEND_BUFFER_DATA(grammar, fibBuf, fibItems);
00807                   RESET_CLUE_EDGE;
00808                 }
00809               else
00810                 {
00811                   DBG("NACK: New node created");
00812                   REPORT_NACK;
00813                   RESET_CLUE_EDGE;
00814                   //nack + new node created --> we're building
00815                   elementModelerStack.top()->building = true;
00816 
00817                   grammar->append(AdaptiveStructuralSymbols::KnownElement);
00818                   fibItems = Fibonacci::encodeToBuffer(fibBuf, SIZEOF_XML_CHAR, elementId);
00819                   APPEND_BUFFER_DATA(grammar, fibBuf, fibItems);
00820                 }
00821             }
00822           else
00823             if (result == ElementModel::Indefinite)
00824               {
00825                 //prediction failed --> use different edge
00826 
00827                 DBG("NACK KNOWN indefinite - add code");
00828                 REPORT_NACK;
00829                 REPORT_DIFFERENT_EDGE(edge);
00830 
00831                 RESET_CLUE_EDGE;
00832                 clueEdge = 1;
00833                 clueEdgeReset = true;
00834               }
00835             else
00836               {
00837                 DBG("KNOWN definite");
00838                 clueEdge++;
00839               }
00840           
00841           
00842 
00843           elementModelerStack.push(emi);
00844           ec->modeler->resetCurrentNode();
00845 
00846           DBG("clue edge = " << clueEdge);
00847         }
00848 
00849       //push element name to the stack
00850       elementStack->push(new unsigned long(elementId));
00851 
00852 
00853 
00854       if (startElementEvent->attr[0])
00855         {
00856           DBG("element has attributes");
00857           
00858           if (emi->modeler->setAttributes(true))
00859             {
00860               DBG("... as supposed");
00861               RESET_CLUE_EDGE;
00862             }
00863           else
00864             {
00865               DBG("... violated!");
00866               REPORT_NACK;
00867               RESET_CLUE_EDGE;
00868             }
00869 
00870           //here is the code that writes down the attributes
00871           for (size_t i = 0; startElementEvent->attr[i]; i += 2)
00872             {
00873               if (!(attrId = attributeIds->find((XmlChar *)startElementEvent->attr[i])))
00874                 {
00875                   //unknown attribute started
00876                   //--> we'll create new element context for it
00877 
00878 
00879                   NEW(attrName, XmlChar[xmlchar_strlen(startElementEvent->attr[i]) + 1]);
00880                   xmlchar_strcpy(attrName, startElementEvent->attr[i]);
00881                   attributeNamesList->append(attrName);
00882       
00883 
00884 
00885                   //insert new element id to elementIds
00886                   attributeId = attributeCounter;
00887                   attributeIds->insert(attrName, new unsigned long(attributeId));
00888 
00889                   DBG("INSERTING ATTRIBUTE " << attributeId << ": " << attrName);
00890                   attributeNames->insert(attributeId, attrName);
00891 
00892 
00893                   //update the element counter
00894                   attributeCounter++;
00895 
00896 
00897                   //append "new element" symbol to the grammar
00898                   grammar->append(AdaptiveStructuralSymbols::NewElement);
00899 
00900                   //append the name of the element to the grammar
00901                   //ending zero  included
00902                   APPEND_ALL_DATA(grammar, attrName, true);
00903                 }
00904               else
00905                 {
00906                   //known attribute started
00907                   //--> update its element context
00908                   attributeId = *attrId;
00909 
00910                   //append "known element" symbol to the grammar
00911                   grammar->append(AdaptiveStructuralSymbols::KnownElement);
00912                   fibItems = Fibonacci::encodeToBuffer(fibBuf, SIZEOF_XML_CHAR, attributeId);
00913                   APPEND_BUFFER_DATA(grammar, fibBuf, fibItems);
00914                 }
00915 
00916               //append the attribute value
00917               //no zero value at the end of the data block
00918               if (startElementEvent->attr[i+1][0] == 0)
00919                 grammar->append(AdaptiveStructuralSymbols::EmptyString);
00920               else
00921                 APPEND_ALL_DATA(grammar, startElementEvent->attr[i+1], false);
00922             }
00923 
00924           //append the "end of block" symbol to indicate the end of the attribute section
00925           grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
00926         }
00927       else
00928         {
00929           DBG("element hasn't attributes");
00930 
00931           if (!emi->modeler->setAttributes(false))
00932             {
00933               DBG("... as supposed");
00934               clueEdge++;
00935             }
00936           else
00937             {
00938               DBG("... violated!");
00939               REPORT_NACK;
00940               RESET_CLUE_EDGE;
00941             }
00942         }
00943 
00944       DBG("clue edge = " << clueEdge);
00945 
00946       break;
00947 
00948 
00949 
00950       /**** End of element ****/
00951     case XmlModelEvent::EndElement:
00952       endElementEvent = (XmlEndElementEvent *)event;
00953 
00954       //end the eventual character data section
00955       if (inCharacters)
00956         {
00957           END_CHARACTER_DATA_SECTION(grammar);
00958         }
00959 
00960       //AdaptiveElementContext *ec;
00961 
00962       if (!(elId = elementIds->find((XmlChar *)endElementEvent->name)))
00963         {
00964           //end of unknown element occurred
00965           //--> should never happen
00966           FATAL("End of unknown element acurred: " << (const char *)endElementEvent->name);
00967         }
00968       else
00969         {
00970           //known element ended
00971 
00972           //      DBG("  Known element " << (const char *)endElementEvent->name << " ended");
00973         }
00974 
00975       elementStack->pop();
00976 
00977       DBG("ELEMENT ENDED: " << endElementEvent->name);
00978 
00979       result = elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::EndNode, &edge, &elts);
00980 
00981       if (result == ElementModel::NewNodeCreated)
00982         {
00983           if (elementModelerStack.top()->building)
00984             {
00985               DBG("BUILDING: New node created (end node)");
00986 
00987               RESET_CLUE_EDGE;
00988 
00989               grammar->append(AdaptiveStructuralSymbols::EndElement);
00990             }
00991           else
00992             {
00993               DBG("NACK: New node created (end node)");
00994               REPORT_NACK;
00995 
00996               RESET_CLUE_EDGE;
00997 
00998               grammar->append(AdaptiveStructuralSymbols::EndElement);
00999             }
01000         }
01001       else
01002         if (result == ElementModel::Indefinite)
01003           {
01004             //prediction failed --> use different edge
01005             DBG("NACK END: indefinite - add code");
01006             REPORT_NACK;
01007             REPORT_DIFFERENT_EDGE(edge);
01008 
01009             RESET_CLUE_EDGE;
01010           }
01011         else
01012           {
01013             //              DBG("END definite - add code");
01014             INCREASE_CLUE_EDGE;
01015           }
01016 
01017 
01018 
01019       elementModelerStack.pop();
01020 
01021       if (!elementModelerStack.isEmpty())
01022         {
01023           elementModelerStack.top()->modeler->popCurrentNode();
01024         }
01025       break;
01026 
01027 
01028 
01029       /**** Character data ****/
01030     case XmlModelEvent::Characters:
01031       charactersEvent = (XmlCharactersEvent *)event;
01032 
01033       if (!inCharacters)
01034         {
01035           inCharacters = true;
01036 
01037           result = elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::CharactersNode, &edge, &elts);
01038       
01039           if (result == ElementModel::NewNodeCreated)
01040             {
01041               if (elementModelerStack.top()->building)
01042                 {
01043                   DBG("BUILDING: New character node created");
01044               
01045                   RESET_CLUE_EDGE;
01046               
01047                 }
01048               else
01049                 {
01050                   DBG("NACK: New node created");
01051                   DBG("unexpected characters");
01052                   REPORT_NACK;
01053               
01054                   RESET_CLUE_EDGE;
01055               
01056                   //nack + new node created --> we're building
01057                   elementModelerStack.top()->building = true;
01058               
01059                 }
01060             }
01061           else
01062             if (result == ElementModel::Indefinite)
01063               {
01064                 //prediction failed --> use different edge
01065             
01066                 DBG("NACK KNOWN indefinite - add code");
01067                 REPORT_NACK;
01068                 REPORT_DIFFERENT_EDGE(edge);
01069             
01070                 RESET_CLUE_EDGE;
01071               }
01072             else
01073               {
01074                 DBG("CHARACTERS definite - add code");
01075                 RESET_CLUE_EDGE;
01076               }
01077 
01078           grammar->append(AdaptiveStructuralSymbols::Characters);
01079           RESET_CLUE_EDGE;
01080 
01081         }
01082 
01083       //append the character data without the ending 0 (that will be appended at another time)
01084       APPEND_ALL_DATA_LENGTH(grammar, charactersEvent->data, charactersEvent->length, false);
01085           
01086       break;
01087 
01088       /**** Default data ****/
01089     case XmlModelEvent::Default:
01090       defaultEvent = (XmlDefaultEvent *)event;
01091 
01092       if (elementModelerStack.isEmpty())
01093         {
01094           //append "default" symbol to the grammar
01095           grammar->append(AdaptiveStructuralSymbols::Default);
01096           APPEND_ALL_DATA_LENGTH(grammar, defaultEvent->data, defaultEvent->length, true);
01097         }
01098       else
01099         {
01100           if (!inCharacters)
01101             {
01102               inCharacters = true;
01103 
01104               result = elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::CharactersNode, &edge, &elts);
01105               //          INCREASE_CLUE_EDGE;
01106       
01107               if (result == ElementModel::NewNodeCreated)
01108                 {
01109                   if (elementModelerStack.top()->building)
01110                     {
01111                       DBG("BUILDING: New character node created (default)");
01112               
01113                       RESET_CLUE_EDGE;
01114               
01115                     }
01116                   else
01117                     {
01118                       DBG("NACK: New node created (default)");
01119                       REPORT_NACK;
01120               
01121                       RESET_CLUE_EDGE;
01122               
01123                       //nack + new node created --> we're building
01124                       elementModelerStack.top()->building = true;
01125               
01126                     } 
01127                 }
01128               else
01129                 if (result == ElementModel::Indefinite)
01130                   {
01131                     //prediction failed --> use different edge
01132             
01133                     DBG("NACK KNOWN indefinite - add code (default)");
01134                     REPORT_NACK;
01135                     REPORT_DIFFERENT_EDGE(edge);
01136             
01137                     RESET_CLUE_EDGE;
01138                   }
01139                 else
01140                   {
01141                     //                  DBG("KNOWN definite - add code");
01142                     RESET_CLUE_EDGE;
01143                   }
01144 
01145               grammar->append(AdaptiveStructuralSymbols::Characters);
01146               RESET_CLUE_EDGE;
01147 
01148             }
01149         
01150 
01151           //grammar->append(AdaptiveStructuralSymbols::Default);
01152           APPEND_ALL_DATA_LENGTH_CONVERT_AMP(grammar, defaultEvent->data, defaultEvent->length, false);
01153         }
01154       break;
01155 
01156 
01157       /**** Comment ****/
01158     case XmlModelEvent::Comment:
01159       commentEvent = (XmlCommentEvent *)event;
01160 
01161       if (elementModelerStack.isEmpty())
01162         {
01163           //comments before the root element are reported via the default handler
01164           grammar->append(AdaptiveStructuralSymbols::Default);
01165         }
01166       else
01167         {
01168           if (!inCharacters)
01169             {
01170               inCharacters = true;
01171 
01172               result = elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::CharactersNode, &edge, &elts);
01173               //          INCREASE_CLUE_EDGE;
01174       
01175               if (result == ElementModel::NewNodeCreated)
01176                 {
01177                   if (elementModelerStack.top()->building)
01178                     {
01179                       DBG("BUILDING: New character node created (comment)");
01180               
01181                       RESET_CLUE_EDGE;
01182               
01183                     }
01184                   else
01185                     {
01186                       DBG("NACK: New node created (comment)");
01187                       REPORT_NACK;
01188               
01189                       RESET_CLUE_EDGE;
01190               
01191                       //nack + new node created --> we're building
01192                       elementModelerStack.top()->building = true;
01193               
01194                     }
01195                 }
01196               else
01197                 if (result == ElementModel::Indefinite)
01198                   {
01199                     //prediction failed --> use different edge
01200             
01201                     DBG("NACK KNOWN indefinite - add code (comment)");
01202                     REPORT_NACK;
01203                     REPORT_DIFFERENT_EDGE(edge);
01204             
01205                     RESET_CLUE_EDGE;
01206                   }
01207                 else
01208                   {
01209                     //                  DBG("KNOWN definite - add code");
01210                     RESET_CLUE_EDGE;
01211                   }
01212 
01213               grammar->append(AdaptiveStructuralSymbols::Characters);
01214               RESET_CLUE_EDGE;
01215 
01216             }
01217         }
01218 
01219 
01220       //append "comment" symbol to the grammar
01221       grammar->append(AdaptiveStructuralSymbols::Comment);
01222 
01223       APPEND_ALL_DATA(grammar, commentEvent->data, true);
01224       break;
01225 
01226 
01227       /**** StartCDATA ****/
01228     case XmlModelEvent::StartCDATA:
01229 
01230       if (!inCharacters)
01231         {
01232           inCharacters = true;
01233 
01234           result = elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::CharactersNode, &edge, &elts);
01235           INCREASE_CLUE_EDGE;
01236       
01237           if (result == ElementModel::NewNodeCreated)
01238             {
01239               if (elementModelerStack.top()->building)
01240                 {
01241                   DBG("BUILDING: New character node created (CDATA)");
01242               
01243                   RESET_CLUE_EDGE;
01244                 }
01245               else
01246                 {
01247                   DBG("NACK: New node created (CDATA)");
01248                   REPORT_NACK;
01249               
01250                   RESET_CLUE_EDGE;
01251               
01252                   //nack + new node created --> we're building
01253                   elementModelerStack.top()->building = true;
01254               
01255                 }
01256             }
01257           else
01258             if (result == ElementModel::Indefinite)
01259               {
01260                 //prediction failed --> use different edge
01261             
01262                 DBG("NACK KNOWN indefinite - add code (CDATA)");
01263                 REPORT_NACK;
01264                 REPORT_DIFFERENT_EDGE(edge);
01265             
01266                 RESET_CLUE_EDGE;
01267               }
01268             else
01269               {
01270                 //              DBG("KNOWN definite - add code");
01271               }
01272 
01273           grammar->append(AdaptiveStructuralSymbols::Characters);
01274           RESET_CLUE_EDGE;
01275 
01276         }
01277 
01278 
01279 
01280       //append "CDATA" symbol to the grammar
01281       grammar->append(AdaptiveStructuralSymbols::CDATA);
01282       break;
01283 
01284       /**** EndCDATA ****/
01285     case XmlModelEvent::EndCDATA:
01286       //endCDATAEvent = (XmlEndCDATAEvent *)event;
01287 
01288       //append "comment" symbol to the grammar
01289       grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
01290       break;
01291 
01292 
01293 
01294       /**** Processing instruction ****/
01295     case XmlModelEvent::PI:
01296       piEvent = (XmlPIEvent *)event;
01297 
01298 
01299       if (elementModelerStack.isEmpty())
01300         {
01301           //PIs before the root element are reported via the default handler
01302           grammar->append(AdaptiveStructuralSymbols::Default);
01303         }
01304       else
01305         {
01306 
01307 
01308           if (!inCharacters)
01309             {
01310               inCharacters = true;
01311 
01312               result = elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::CharactersNode, &edge, &elts);
01313               //          INCREASE_CLUE_EDGE;
01314       
01315               if (result == ElementModel::NewNodeCreated)
01316                 {
01317                   if (elementModelerStack.top()->building)
01318                     {
01319                       DBG("BUILDING: New character node created (PI)");
01320               
01321                       RESET_CLUE_EDGE;
01322                     }
01323                   else
01324                     {
01325                       DBG("NACK: New node created (PI)");
01326                       DBG("unexpected PI");
01327                       REPORT_NACK;
01328               
01329                       RESET_CLUE_EDGE;
01330               
01331                       //nack + new node created --> we're building
01332                       elementModelerStack.top()->building = true;
01333                     }
01334                 }
01335               else
01336                 if (result == ElementModel::Indefinite)
01337                   {
01338                     //prediction failed --> use different edge
01339             
01340                     DBG("NACK KNOWN indefinite (PI)");
01341                     REPORT_NACK;
01342                     REPORT_DIFFERENT_EDGE(edge);
01343             
01344                     RESET_CLUE_EDGE;
01345                   }
01346                 else
01347                   {
01348                     //                  DBG("KNOWN definite - add code");
01349                     RESET_CLUE_EDGE;
01350                   }
01351 
01352               grammar->append(AdaptiveStructuralSymbols::Characters);
01353               RESET_CLUE_EDGE;
01354 
01355             }
01356         }
01357 
01358       //append "pi" symbol to the grammar
01359       grammar->append(AdaptiveStructuralSymbols::PI);
01360 
01361       if (piEvent->target)
01362         {
01363           APPEND_ALL_DATA(grammar, piEvent->target, true);
01364 
01365           if (piEvent->data)
01366             {
01367               APPEND_ALL_DATA(grammar, piEvent->data, true);
01368             }
01369           else
01370             grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
01371         }
01372       else
01373         grammar->append(AdaptiveStructuralSymbols::EndOfBlock);
01374       
01375       break;
01376 
01377     default:
01378       WRN("Unknown XML event: " << event->type);
01379       DELETE(event);
01380       return false;
01381     }
01382 
01383   //delete the event structure
01384   DELETE(event);
01385   return true;
01386 }
01387 
01388 
01389 
01396 void XmlAdaptiveModel::receiveData(XmlChar *data, size_t size)
01397 {
01398   bool stateChanged = false;
01399   DataQueueItem *dataQueueItem;
01400   AdaptiveElementContext *ec;
01401   XmlChar *elName;
01402   unsigned long elementId;
01403   unsigned long *elId;
01404 
01405   //some variables for working with Fibonacci codes
01406   unsigned char fibTmpChar, fibMask;
01407   bool fibFinished = false;
01408 
01409   size_t edge;
01410   size_t differentEdge;
01411   size_t elts = 0;
01412 
01413   if (!buffer)
01414     {
01415       NEW(buffer, XmlChar[XML_MODEL_BUFFER_DEFAULT_SIZE]);
01416       RESET_BUFFER;
01417     }
01418 
01419   for (size_t i = 0; i < size; i++)
01420     {
01421       if (state != AdaptiveStructuralSymbols::KnownElement)
01422         {
01423           switch (data[i])
01424             {
01425             case AdaptiveStructuralSymbols::Characters:
01426               DBG("Changing to characters, state = " << state);
01427               if (state != AdaptiveStructuralSymbols::Characters)
01428                 {
01429                   CHANGE_STATE(AdaptiveStructuralSymbols::Characters);
01430                   DBG("Changed to characters");
01431 
01432                 }
01433               else
01434                 stateChanged = true;
01435 
01436               if (stateChanged)
01437                 {
01438                   DBG("1");
01439                   FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
01440 
01441                   //if we are not reading the attribute values, move in the models
01442                   if (!inAttr)
01443                     {
01444                       attrPos = 1;
01445                       bool willAppend = true;
01446                       //go as far as we can and then build a new character node - if needed
01447                       if (!readingNackData && !nackEndedInCharacters)
01448                         {
01449                           DBG("CHARACTERS --> moving forward");
01450                           ElementModel::NodeType res = ElementModel::ElementNode;                               
01451                           ElementModel::Node *node = 0;                                 
01452                                                                 
01453                           while (res != ElementModel::NoNode)                           
01454                             {                                                           
01455                               DBG("before moving forward:");
01456 //                            elementModelerStack.top()->modeler->print();
01457                               res = elementModelerStack.top()->modeler->moveForward(&node, saxEmitter, userData);       
01458                               DBG("after moving forward:");
01459 //                            elementModelerStack.top()->modeler->print();
01460                                                                 
01461                               //moving over the element's end?
01462                               if (res == ElementModel::EndNode)
01463                                 {
01464                                   DBG("moving over the element's end --> performing pop()");
01465                                   elementModelerStack.pop();
01466                                   elementModelerStack.top()->modeler->popCurrentNode();
01467                                 }
01468                               else
01469                                 //jumped to other element model                         
01470                                 if (res == ElementModel::ElementNode)                   
01471                                   {                                                     
01472                                     ElementModelerInfo *eminfo;                         
01473                                     NEW(eminfo, ElementModelerInfo);                    
01474                                     eminfo->modeler = node->modeler;                            
01475                                     eminfo->building = false;                           
01476                                                                 
01477                                     //DBG("%%%%%%%%%%%%%%%%% 3");
01478 
01479                                     SAFE_CALL_EMITTER(startElement(userData, eminfo->modeler->getElementName(), 0));
01480                                     eminfo->modeler->setAttributes(false);
01481 
01482                                     elementModelerStack.push(eminfo);                   
01483                                     eminfo->modeler->resetCurrentNode();
01484                                   }             
01485                                 else
01486                                   if (res == ElementModel::CharactersNode)
01487                                     {
01488                                       DBG("CHARACTERS NODE FOUND!");
01489                                       willAppend = false;
01490                                       break;
01491                                     }
01492 
01493                             }
01494                         }
01495                       else
01496                         {
01497                           readingNackData = false;
01498                           inNACK = false;
01499                         }
01500 
01501                       if (willAppend && !nackEndedInCharacters)
01502                         {
01503                           DBG("Appending characters node:");
01504                           elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::CharactersNode, &edge, &elts);
01505                           DBG("After appending:");
01506 //                        elementModelerStack.top()->modeler->print();
01507                         }
01508 
01509                       if (nackEndedInCharacters)
01510                         nackEndedInCharacters = false;
01511 
01512 
01513                       continue;
01514                     }
01515 
01516 
01517                 }
01518               break;
01519 
01520               //also end of block(ignored) and differentedge!
01521             case AdaptiveStructuralSymbols::NACK:
01522               if (!inCharacters && !inProlog)
01523                 {
01524                   DBG("changing to NACK/different edge");
01525                   DBG((int)data[i]);
01526                   DBG(inNACK);
01527                       
01528                   CHANGE_STATE(AdaptiveStructuralSymbols::NACK);
01529                   if (stateChanged)
01530                     {
01531                       DBG("changed to NACK/differentedge");
01532                       continue;
01533                     }
01534 
01535                   DBG("not changed to NACK/differentedge");
01536                   break;
01537                 }
01538 
01539 
01540               //also used as XmlDecl!
01541             case AdaptiveStructuralSymbols::NewElement:
01542 
01543               CHANGE_STATE(AdaptiveStructuralSymbols::NewElement);
01544               if (stateChanged)
01545                 {
01546                   if (!inAttr)
01547                     {
01548                       FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
01549                     }
01550                   continue;
01551                 }
01552               break;
01553 
01554             case AdaptiveStructuralSymbols::KnownElement:
01555               //DBG("KNOWN ELEMENT");
01556 
01557               CHANGE_STATE(AdaptiveStructuralSymbols::KnownElement);
01558               if (stateChanged)
01559                 {
01560                   if (!inAttr)
01561                     {
01562                       FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
01563                     }
01564                   continue;
01565                 }
01566               break;
01567 
01568             case AdaptiveStructuralSymbols::EndElement:
01569               if (inProlog)
01570                 {
01571                   if (!inDoctype)
01572                     {
01573                       DBG("doctype starting");
01574                       CHANGE_STATE(AdaptiveStructuralSymbols::EndElement);
01575                       inDoctype = true;
01576                       
01577                       if (stateChanged)
01578                         {
01579                           continue;
01580                         }
01581                     }
01582                   else
01583                     {
01584                       SAFE_CALL_EMITTER(endDoctypeDecl(userData));
01585                       inDoctype = false;
01586                       DBG("doctype ended");
01587                       RESET_STATE;
01588                       RESET_BUFFER;
01589                       continue;
01590                     }
01591                 }
01592               else
01593                 {
01594                   DBG("element ended");
01595                   CHANGE_STATE(AdaptiveStructuralSymbols::None);
01596 
01597                   if (stateChanged)
01598                     {
01599                       DBG("4");
01600                       FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
01601 
01602                       elId = elementStack->pop();
01603                       CHECK_POINTER(elId);
01604                       ec = elements->find(*elId);
01605 
01606               
01607 
01608                       /*****/
01609                       //go as far as we can and then build a new element node
01610                       if (!readingNackData)
01611                         {
01612                           DBG("END --> moving forward");
01613                           ElementModel::NodeType res = ElementModel::ElementNode;                               
01614                           ElementModel::Node *node = 0;                                 
01615                                                                 
01616                           while (res != ElementModel::NoNode)                           
01617                             {                                                           
01618                               DBG("before moving forward:");
01619 //                            elementModelerStack.top()->modeler->print();
01620                               res = elementModelerStack.top()->modeler->moveForward(&node, saxEmitter, userData);       
01621                               DBG("after moving forward:");
01622 //                            elementModelerStack.top()->modeler->print();
01623                                                                 
01624                               //moving over the element's end?
01625                               if (res == ElementModel::EndNode)
01626                                 {
01627                                   DBG("moving over the element's end --> performing pop()");
01628                                   elementModelerStack.pop();
01629                                   elementModelerStack.top()->modeler->popCurrentNode();
01630                                 }
01631                               else
01632                                 /*jumped to other element model*/                               
01633                                 if (res == ElementModel::ElementNode)                   
01634                                   {                                                     
01635                                     ElementModelerInfo *eminfo;                         
01636                                     NEW(eminfo, ElementModelerInfo);                    
01637                                     eminfo->modeler = node->modeler;                            
01638                                     eminfo->building = false;                           
01639                                                                 
01640                                     elementModelerStack.push(eminfo);                   
01641                                     eminfo->modeler->resetCurrentNode();
01642                                   }                                                     
01643                             }
01644                         }
01645                       else
01646                         {
01647                           readingNackData = false;
01648                           inNACK = false;
01649                         }
01650 
01651                       DBG("Appending end element node:");
01652                       elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::EndNode, &edge, &elts);
01653                       DBG("After appending:");
01654 //                    elementModelerStack.top()->modeler->print();
01655 
01656                       SAFE_CALL_EMITTER(endElement(userData, elementModelerStack.top()->modeler->getElementName()));
01657 
01658 
01659                       DBG("moving over the element's end --> performing pop()");
01660                       elementModelerStack.pop();
01661                       if (!elementModelerStack.isEmpty())
01662                         {
01663                           elementModelerStack.top()->modeler->popCurrentNode();
01664                         }
01665 
01666                       DBG("Top of the stack (" << elementModelerStack.count() << "):");
01667                       if (elementModelerStack.top())
01668                         {
01669 //                        elementModelerStack.top()->modeler->print();
01670                         }
01671                       else
01672                         {
01673                           DBG(" stack empty");
01674                         }
01675                       /******/
01676 
01677                       continue;
01678                     }
01679                   else
01680                     {
01681                       //DBG("!!!!!!!!! " << state);
01682                       //DBG((unsigned int)data[i]);
01683                     }
01684                 }
01685 
01686               break;
01687 
01688             case AdaptiveStructuralSymbols::Default:
01689               //DBG("COMMENT");
01690               CHANGE_STATE(AdaptiveStructuralSymbols::Default);
01691               if (stateChanged)
01692                 {
01693                   DBG("5 default...");
01694                   FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
01695                   continue;
01696                 }
01697               break;
01698 
01699 
01700             case AdaptiveStructuralSymbols::EntityDecl:
01701               //DBG("ENTITY DECL");
01702               CHANGE_STATE(AdaptiveStructuralSymbols::EntityDecl);
01703               if (stateChanged)
01704                 {
01705                   continue;
01706                 }
01707               break;
01708 
01709             case AdaptiveStructuralSymbols::NotationDecl:
01710               //DBG("NOTATION DECL");
01711               CHANGE_STATE(AdaptiveStructuralSymbols::NotationDecl);
01712               if (stateChanged)
01713                 {
01714                   continue;
01715                 }
01716               break;
01717 
01718             default:
01719               if (inAttrValue)
01720                 {
01721                   DBG("attribute value");
01722                   state = AdaptiveStructuralSymbols::Characters;
01723                 }
01724             }
01725         }
01726 
01727       /***************************************/
01728 
01729       
01730       switch (state)
01731         {
01732           //used as Doctype!
01733         case AdaptiveStructuralSymbols::EndElement:
01734           if (dataQueue->count() == 3)
01735             {
01736               XmlChar *doctype, *publicId, *systemId;
01737 
01738               doctype = dataQueue->dequeue()->data;
01739               publicId = dataQueue->dequeue()->data;
01740               systemId = dataQueue->dequeue()->data;
01741 
01742               SAFE_CALL_EMITTER(startDoctypeDecl(userData, doctype, systemId, publicId,
01743                                                  data[0] == AdaptiveStructuralSymbols::HasInternalSubsetYes));
01744 
01745               if (doctype)
01746                 {
01747                   DBG(doctype);
01748                   DELETE(doctype);
01749                 }
01750 
01751               if (publicId)
01752                 {
01753                   DBG(publicId);
01754                   DELETE(publicId);
01755                 }
01756 
01757               if (systemId)
01758                 {
01759                   DBG(systemId);
01760                   DELETE(systemId);
01761                 }
01762 
01763               DBG("doctype start complete");
01764               RESET_BUFFER;
01765               RESET_STATE;
01766             }
01767           else
01768             if (data[i] == AdaptiveStructuralSymbols::EndOfBlock)
01769               {
01770                 //read target value
01771                 NEW(dataQueueItem, DataQueueItem);
01772                 dataQueueItem->type = Characters;
01773 
01774                 if (bufferLength)
01775                   {
01776                     if (buffer[0] == AdaptiveStructuralSymbols::EmptyString)
01777                       //we read an empty string
01778                       buffer[0] = 0;
01779                     else
01780                       buffer[bufferLength] = 0;
01781 
01782                     NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(buffer)+1]);
01783                     xmlchar_strcpy(dataQueueItem->data, buffer);
01784                   }
01785                 else
01786                   //data not present
01787                   dataQueueItem->data = 0;
01788 
01789                 dataQueue->enqueue(dataQueueItem);
01790 
01791                 RESET_BUFFER;
01792               }
01793             else
01794               {
01795                 //read next character
01796                 buffer[bufferLength] = data[i];
01797                 bufferLength++;
01798               }
01799 
01800           break;
01801 
01802         case AdaptiveStructuralSymbols::NACK:
01803           if (!inNACK)
01804             {
01805               DBG("NACK data");
01806 
01807               fibMask = 1 << (SIZEOF_CHAR*8 - 1);
01808               
01809               //decode symbol using Fibonacci code
01810               fibTmpChar = (unsigned char)data[i];
01811               DBG("SSS: " << (unsigned int)fibTmpChar);
01812 
01813               while (fibMask)
01814                 {
01815                   fibCode >>= 1;
01816                   fibBits++;
01817                   
01818                   if (fibTmpChar & fibMask)
01819                     {
01820                       fibCode = fibCode | ((unsigned long)1) << (SIZEOF_UNSIGNED_LONG*8-1);
01821                       
01822                       if (!fibLastWasOne)
01823                         fibLastWasOne = true;
01824                       else
01825                         {
01826                           //Fibonacci code is now complete
01827                           //--> decode the symbol number
01828                           fibCode >>= (SIZEOF_UNSIGNED_LONG*8 - fibBits);
01829                           clueEdge = Fibonacci::decode(fibCode);
01830                           fibFinished = true;
01831                           break;
01832                         }
01833                     }
01834                   else
01835                     fibLastWasOne = false;
01836                   
01837                   fibMask >>= 1;
01838                 }
01839 
01840               if (fibFinished)
01841                 {
01842                   inNACK = true;
01843 
01844                   DBG("############# clue edge: " << clueEdge);
01845                   readingNackData = true;
01846 
01847                   //complete Fibonacci code read and decoded
01848                   //reset fibCode, and fibBits
01849                   fibCode = 0;
01850                   fibBits = 0;
01851                   fibLastWasOne = false;
01852 
01853                   RESET_BUFFER;
01854                   RESET_STATE;
01855 
01856                   /*****/
01857                   //move clueEdge-1 edges forward
01858                   DBG("NACK --> moving");
01859 
01860                   ElementModel::NodeType res = ElementModel::ElementNode;                               
01861                   ElementModel::Node *node = 0;                                 
01862 
01863                   if (elementModelerStack.top()->modeler)
01864                     {
01865                       DBG("!before moving forward!:");
01866 //                    elementModelerStack.top()->modeler->print();
01867                     }
01868                                                                 
01869 
01870                   {
01871                     bool moved = false;
01872                     while (clueEdge > 1)                                
01873                       {
01874                         DBG("CURRENT VALUE OF CLUE EDGE: " << clueEdge);
01875 
01876                         DBG("before moving forward XX:");
01877 //                      elementModelerStack.top()->modeler->print();
01878                           
01879                         if (attrPos == 0)
01880                           {
01881                             DBG("We are entering the attribute section");
01882 
01883                             attrPos = 1;
01884                             clueEdge--;
01885 
01886                             if (clueEdge == 1)
01887                               {
01888                                 DBG("skipped the attribute node, because the element has no attributes");
01889                                 dataQueueItem = dataQueue->dequeue();
01890                                       
01891                                 //emit startElement event with no attributes
01892                                 //FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
01893                                 SAFE_CALL_EMITTER(startElement(userData, dataQueueItem->data, 0));
01894                                 elementModelerStack.top()->modeler->setAttributes(false);       //zz
01895                               }
01896                             continue;
01897 
01898                           }
01899                         else
01900                           {
01901                             //we are not in the attribute section
01902 
01903                             {
01904                               DBG("before moving forward:");
01905 //                            elementModelerStack.top()->modeler->print();
01906                               res = elementModelerStack.top()->modeler->moveForward(&node, saxEmitter, userData);       
01907                               DBG("after moving forward:");
01908 //                            elementModelerStack.top()->modeler->print();
01909 
01910 
01911                               //patch the element with no attributes and subsequent deterministic other things
01912                               if (attrPos == 1 && !moved)
01913                                 {
01914                                   DBG("DATA QUEUE COUNT: " << dataQueue->count());
01915 
01916                                   if (!dataQueue->isEmpty())
01917                                     {
01918                                       dataQueueItem = dataQueue->dequeue();
01919                                         
01920                                       //emit startElement event with no attributes
01921                                       //FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
01922                                       SAFE_CALL_EMITTER(startElement(userData, dataQueueItem->data, 0));
01923                                       elementModelerStack.top()->modeler->setAttributes(false); //zzz
01924                                     }
01925                                 }
01926 
01927                               //moving over the element's end?
01928                               if (res == ElementModel::EndNode)
01929                                 {
01930                                   DBG("moving over the element's end --> performing pop()");
01931                                   elementModelerStack.pop();
01932                                   elementModelerStack.top()->modeler->popCurrentNode();
01933                                 }
01934                               else
01935                                 /*jumped to other element model*/                               
01936                                 if (res == ElementModel::ElementNode)                   
01937                                   {                                                     
01938                                     ElementModelerInfo *eminfo;                         
01939                                     NEW(eminfo, ElementModelerInfo);                    
01940                                     eminfo->modeler = node->modeler;                            
01941                                     eminfo->building = false;                           
01942                                       
01943                                     elementModelerStack.push(eminfo);                   
01944                                     eminfo->modeler->resetCurrentNode();
01945 
01946                                     DBG("enqueueing the element name");
01947                                     NEW(dataQueueItem, DataQueueItem);
01948                                     dataQueueItem->type = Characters;
01949                                     NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(eminfo->modeler->getElementName())+1]);
01950                                     xmlchar_strcpy(dataQueueItem->data, eminfo->modeler->getElementName());
01951                                     dataQueue->enqueue(dataQueueItem);
01952 
01953                                     attrPos = 0;
01954                                   }
01955                         
01956                               moved = true;
01957                               clueEdge--;
01958                             }
01959                           }
01960 
01961                         DBG("current location:");
01962 //                      elementModelerStack.top()->modeler->print();
01963                       }
01964 
01965                     if (attrPos == 0)
01966                       {
01967                         DBG("We ended in the attribute section");
01968 
01969                         if (elementModelerStack.top()->modeler->hasAttributes())
01970                           {
01971                             DBG("Element should have the attributes... and HAS NOT");
01972                             attrPos = 1;        //we left the attribute node
01973                                 
01974                             elementModelerStack.top()->modeler->setAttributes(false);
01975                             DBG("element name : " << elementModelerStack.top()->modeler->getElementName());
01976                             DBG("the next prediction will be " << elementModelerStack.top()->modeler->hasAttributes());
01977                                 
01978                             DBG("DATA QUEUE COUNT: " << dataQueue->count());
01979                                 
01980 
01981                             dataQueueItem = dataQueue->dequeue();
01982                             //emit startElement event with no attributes
01983                             SAFE_CALL_EMITTER(startElement(userData, dataQueueItem->data, 0));
01984                             //FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
01985 
01986                             inAttr = false;
01987                             inAttrValue = false;
01988                             readingNackData = false;
01989                             inNACK = false;
01990                           }
01991                         else
01992                           {
01993                             DBG("Element should NOT have the attributes... and HAS");
01994                                 
01995                             attrPos = 0;        //we entered the attribute node
01996                                 
01997                             //the attribute stats will be updated in characters when reading the attributes!
01998 
01999                             DBG("element name : " << elementModelerStack.top()->modeler->getElementName());
02000                             DBG("the next prediction will be " << elementModelerStack.top()->modeler->hasAttributes());
02001                                 
02002                             DBG("DATA QUEUE COUNT: " << dataQueue->count());
02003                                 
02004                             if (dataQueue->isEmpty())
02005                               {
02006                                 FATAL("no element name present!");
02007                               }
02008                             else
02009                               {
02010                                 DBG("DATA QUEUE COUNT: " << dataQueue->count());
02011                               }
02012 
02013 
02014                             inAttr = true;
02015                             inAttrValue = false;
02016                             readingNackData = false;
02017                             inNACK = false;
02018                           }
02019                             
02020                       }
02021                     /******/
02022                       
02023                   }
02024                 }
02025             }
02026           //different edge data
02027           else
02028             {
02029               DBG("DifferentEdge data");
02030 
02031               fibMask = 1 << (SIZEOF_CHAR*8 - 1);
02032               
02033               //decode symbol using Fibonacci code
02034               fibTmpChar = (unsigned char)data[i];
02035 
02036               while (fibMask)
02037                 {
02038                   fibCode >>= 1;
02039                   fibBits++;
02040                   
02041                   if (fibTmpChar & fibMask)
02042                     {
02043                       fibCode = fibCode | ((unsigned long)1) << (SIZEOF_UNSIGNED_LONG*8-1);
02044                       
02045                       if (!fibLastWasOne)
02046                         fibLastWasOne = true;
02047                       else
02048                         {
02049                           //Fibonacci code is now complete
02050                           //--> decode the symbol number
02051                           fibCode >>= (SIZEOF_UNSIGNED_LONG*8 - fibBits);
02052                           differentEdge = Fibonacci::decode(fibCode);
02053                           fibFinished = true;
02054                           break;
02055                         }
02056                     }
02057                   else
02058                     fibLastWasOne = false;
02059                   
02060                   fibMask >>= 1;
02061                 }
02062 
02063               if (fibFinished)
02064                 {
02065                   DBG("############# different edge: " << differentEdge);
02066                   readingNackData = false;
02067                   inNACK = false;
02068 
02069                   //complete Fibonacci code read and decoded
02070                   //reset fibCode, and fibBits
02071                   fibCode = 0;
02072                   fibBits = 0;
02073                   fibLastWasOne = false;
02074 
02075                   RESET_BUFFER;
02076                   RESET_STATE;
02077 
02078                   //clue edge is now the chosen edge
02079                   clueEdge = 1;
02080 
02081 
02082                   /*****/
02083                   //move clueEdge-1 edges forward
02084                   DBG("DIFFERENT EDGE --> moving");
02085                   {
02086                     ElementModel::NodeType res = ElementModel::ElementNode;                             
02087                     ElementModel::Node *node = 0;                                       
02088                                                                 
02089                     DBG("before moving forward:");
02090 //                  elementModelerStack.top()->modeler->print();
02091                     res = elementModelerStack.top()->modeler->followEdge(differentEdge, &node, saxEmitter, userData);   
02092                     DBG("after moving forward:");
02093 //                  elementModelerStack.top()->modeler->print();
02094                                                                 
02095                     //moving over the element's end?
02096                     if (res == ElementModel::EndNode)
02097                       {
02098                         DBG("moving over the element's end --> performing pop()");
02099                         //SAFE_CALL_EMITTER(endElement(userData, elementModelerStack.top()->modeler->getElementName()));
02100                         elementModelerStack.pop();
02101                         elementModelerStack.top()->modeler->popCurrentNode();
02102                       }
02103                     else
02104                       /*jumped to other element model*/                         
02105                       if (res == ElementModel::ElementNode)                     
02106                         {                                                       
02107                           dataQueue->clear();   
02108 
02109                           ElementModelerInfo *eminfo;                           
02110                           NEW(eminfo, ElementModelerInfo);                      
02111                           eminfo->modeler = node->modeler;                              
02112                           eminfo->building = false;                             
02113                       
02114 
02115                           elementModelerStack.push(eminfo);                     
02116                           eminfo->modeler->resetCurrentNode();
02117 
02118                           DBG("enqueueing the element name");
02119                           NEW(dataQueueItem, DataQueueItem);
02120                           dataQueueItem->type = Characters;
02121                           NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(eminfo->modeler->getElementName())+1]);
02122                           xmlchar_strcpy(dataQueueItem->data, eminfo->modeler->getElementName());
02123                           dataQueue->enqueue(dataQueueItem);
02124                           
02125                           //we are in the attribute section
02126                           attrPos = 0;
02127 
02128 
02129                           if (!eminfo->modeler->hasAttributes())
02130                             {
02131                               //the element has no atributes (prediction, NACK can follow!)
02132                               //--> move throught the fictive attribute node
02133                               
02134                               inAttr = false;
02135                               DBG("DE: Element shouldn't have attributes");
02136                             }
02137                           else
02138                             {
02139                               //the element has attributes
02140                               //--> jump into the fictive attribute node
02141                               inAttr = true;
02142                               DBG("DE: Element should have attributes");
02143                             }
02144 
02145                         }               
02146 
02147                     DBG("current location:");
02148 //                  elementModelerStack.top()->modeler->print();
02149 
02150                     if (elementModelerStack.top()->modeler->currentNodeType() == ElementModel::CharactersNode)
02151                       {
02152                         //we ended in the characters node
02153                         //CHANGE_STATE(AdaptiveStructuralSymbols::Characters);
02154                         DBG("$$$$$$$$$$$$$$$$$$$$$$$");
02155                         nackEndedInCharacters = true;
02156                       }
02157                   }
02158                   /******/
02159 
02160                 }
02161             }
02162 
02163           break;
02164 
02165 
02166         case AdaptiveStructuralSymbols::NewElement:
02167           inProlog = false;
02168 
02169           if (data[i] == AdaptiveStructuralSymbols::EndOfBlock)
02170             {
02171               //decoding new element/attribute
02172 
02173               //add ending \0
02174               buffer[bufferLength] = 0;
02175               bufferLength++;
02176 
02177 
02178               if (inAttr)
02179                 {
02180                   //we are in the attribute section --> read the attribute name and value
02181                   DBG("NEW attribute " << buffer);
02182                   inAttrValue = true;
02183 
02184                   XmlChar *attrName;
02185                   NEW(attrName, XmlChar[xmlchar_strlen(buffer)+1]);
02186                   xmlchar_strcpy(attrName, buffer);
02187 
02188                   DBG("Attribute has this id: " << attributeCounter);
02189                   attributeNamesList->append(attrName);
02190                   attributeNames->insert(attributeCounter, attrName);
02191 
02192                   attributeNames->insert(attributeCounter, attrName);
02193                   attributeCounter++;
02194 
02195 
02196                   NEW(dataQueueItem, DataQueueItem);
02197                   dataQueueItem->type = Characters;
02198                   NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(buffer)+1]);
02199                   xmlchar_strcpy(dataQueueItem->data, buffer);
02200                   dataQueue->enqueue(dataQueueItem);
02201                   
02202                   RESET_STATE;
02203                   RESET_BUFFER;
02204                   continue;
02205                 }
02206               else
02207                 {
02208                   //move forward until:
02209                   // 1. attribute section --> inAttr = true OR
02210                   // 2. new node for new element
02211                   DBG("NEW element " << buffer);
02212 
02213                   //insert the name of the element onto the stack
02214                   NEW(dataQueueItem, DataQueueItem);
02215                   dataQueueItem->type = Characters;
02216                   NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(buffer)+1]);
02217                   xmlchar_strcpy(dataQueueItem->data, buffer);
02218                   dataQueue->enqueue(dataQueueItem);
02219 
02220 
02221                   ElementModelerInfo *emi;
02222 
02223                   if (elementModelerStack.isEmpty())
02224                     {
02225                       //first element processed
02226 
02227 
02228                       //save element name
02229                       NEW(elName, XmlChar[bufferLength]);
02230                       xmlchar_strcpy(elName, buffer);
02231                       elementNamesList->append(elName);
02232                       
02233                       //create an  element contex for the new element
02234                       NEW(ec, AdaptiveElementContext);
02235                       
02236                       //set unique element id
02237                       ec->name = elName;
02238                       
02239                       elementId = elementCounter;
02240 
02241                       DBG("NEW ELEMENT HAS THIS ID: " << elementId);
02242                       
02243                       //add new element context to the element hashtable
02244                       elements->insert(elementCounter, ec);
02245                       
02246                       //update the element counter
02247                       elementCounter++;
02248                       
02249                       elementStack->push(new unsigned long(elementId));
02250                       
02251                       //reset buffer
02252                       RESET_BUFFER;
02253                       
02254                       RESET_STATE;
02255                       
02256                       
02257                       NEW(ec->modeler, ElementModeler(elName));
02258                       elementModelerList.append(ec->modeler);
02259 
02260                       
02261                       NEW(emi, ElementModelerInfo);
02262                       emi->modeler = ec->modeler;
02263                       
02264                       //new elements always build
02265                       emi->building = true;
02266                       
02267 
02268                       DBG("initiating the root model");
02269                       elementModelerStack.push(emi);
02270                       DBG("items on the stack: " << elementModelerStack.count());
02271                       emi->modeler->resetCurrentNode();
02272                       clueEdge = 0;
02273 
02274                       if (!emi->modeler->hasAttributes())
02275                         {
02276                           //the element has no atributes (prediction, NACK can follow!)
02277                           //--> move throught the fictive attribute node
02278                           
02279                           inAttr = false;
02280                           DBG("1Element shouldn't have attributes");
02281                           attrPos = 0;
02282                         }
02283                       else
02284                         {
02285                           //the element has attributes
02286                           //--> jump into the fictive attribute node
02287                           inAttr = true;
02288                           DBG("1Element should have attributes");
02289                           attrPos = 0;
02290                         }
02291 
02292                     }
02293                   else
02294                     {
02295                       //move to the new element
02296 
02297                       /******/
02298                       //go as far as we can and then build a new element node
02299                       //only if not reading the nack information !
02300                       if (!readingNackData)
02301                         {
02302                           DBG("NEW ELEMENT --> moving");
02303 
02304 
02305                           ElementModel::NodeType res = ElementModel::ElementNode;                               
02306                           ElementModel::Node *node = 0;                                 
02307                           ElementModel::Node *cr;
02308                           bool moved = false;
02309                                 
02310                           bool leave = false;
02311                           while (res != ElementModel::NoNode)                           
02312                             {                           
02313 
02314                               //patch the first step
02315                               cr = elementModelerStack.top()->modeler->getCurrentNode();
02316                               if (moved && cr->type == ElementModel::StartNode && elementModelerStack.top()->modeler->hasAttributes())
02317                                 {
02318                                   DBG("We should stop in the attribute section!");
02319                                   DBG("Current element: " << elementModelerStack.top()->modeler->getElementName());
02320                                   inAttr = true;
02321                                   inAttrValue = true;
02322                                   moved = false;
02323 
02324                                   //we have to flip the attribute name and the name of the element (:-)
02325                                   dataQueueItem = dataQueue->dequeue();
02326                                   XmlChar *attrName = dataQueueItem->data;
02327 
02328                                   //enqueue the name of the element
02329                                   NEW(dataQueueItem, DataQueueItem);
02330                                   NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(elementModelerStack.top()->modeler->getElementName())+1]);
02331                                   xmlchar_strcpy(dataQueueItem->data, elementModelerStack.top()->modeler->getElementName());
02332                                   dataQueueItem->type = Characters;
02333                                   dataQueue->enqueue(dataQueueItem);
02334 
02335 
02336                                   //enqueue the name of the attribute
02337                                   NEW(dataQueueItem, DataQueueItem);
02338                                   dataQueueItem->type = Characters;
02339                                   dataQueueItem->data = attrName;
02340                                   dataQueue->enqueue(dataQueueItem);
02341 
02342 
02343                                   RESET_STATE;
02344                                   RESET_BUFFER;
02345 
02346                                   leave = true;
02347                                   break;
02348 
02349                                 }
02350 
02351                               attrPos = 1;      //at this point we are always after the attribute node
02352 
02353                               moved = true;
02354                               DBG("before moving forward:");
02355 //                            elementModelerStack.top()->modeler->print();
02356                               res = elementModelerStack.top()->modeler->moveForward(&node, saxEmitter, userData);       
02357                               DBG("after moving forward:");
02358 //                            elementModelerStack.top()->modeler->print();
02359                                                                 
02360                               //moving over the element's end?
02361                               if (res == ElementModel::EndNode)
02362                                 {
02363                                   DBG("moving over the element's end --> performing pop()");
02364                                   elementModelerStack.pop();
02365                                   elementModelerStack.top()->modeler->popCurrentNode();
02366                                 }
02367                               else
02368                                 /*jumped to other element model*/                               
02369                                 if (res == ElementModel::ElementNode)                   
02370                                   {                                                     
02371                                     ElementModelerInfo *eminfo;                         
02372                                     NEW(eminfo, ElementModelerInfo);                    
02373                                     eminfo->modeler = node->modeler;                            
02374                                     eminfo->building = false;                           
02375 
02376                                     elementModelerStack.push(eminfo);
02377                                     eminfo->modeler->resetCurrentNode();
02378 
02379                                     if (!eminfo->modeler->hasAttributes())
02380                                       {
02381                                         //the element has no atributes (prediction, NACK can follow!)
02382                                         //--> move throught the fictive attribute node
02383 
02384                                         inAttr = false;
02385                                         attrPos = 1;    //move behind the attribute section
02386 
02387                                         XmlChar *tmp;
02388                                         NEW(tmp, XmlChar[xmlchar_strlen(eminfo->modeler->getElementName())+1]);
02389                                         xmlchar_strcpy(tmp, eminfo->modeler->getElementName());
02390                             
02391                                         //emit startElement event with no attributes
02392                                         SAFE_CALL_EMITTER(startElement(userData, tmp, 0));
02393                                         eminfo->modeler->setAttributes(false);
02394                                       }
02395                                     else
02396                                       {
02397                                         //the element has attributes
02398                                         //--> jump into the fictive attribute node
02399                                         inAttr = true;
02400                                         DBG("2Element should have attributes");
02401                                         attrPos = 0;
02402                                       }
02403                                   }                                                     
02404                             }
02405 
02406                           if (leave)
02407                             continue;
02408 
02409                           //save element name
02410                           NEW(elName, XmlChar[bufferLength]);
02411                           xmlchar_strcpy(elName, buffer);
02412                           elementNamesList->append(elName);
02413                           
02414                           //DBG("!!!!!!!!! " << elName);
02415                           //create an  element contex for the new element
02416                           NEW(ec, AdaptiveElementContext);
02417                       
02418                           //set unique element id
02419                           ec->name = elName;
02420                           
02421                           elementId = elementCounter;
02422                           
02423                           DBG("NEW ELEMENT HAS THIS ID: " << elementId);
02424                           //add new element context to the element hashtable
02425                           elements->insert(elementCounter, ec);
02426                           
02427                           //update the element counter
02428                           elementCounter++;
02429                           
02430                           elementStack->push(new unsigned long(elementId));
02431                           
02432                           //reset buffer
02433                           RESET_BUFFER;
02434                           
02435                           RESET_STATE;
02436                       
02437                       
02438                           NEW(ec->modeler, ElementModeler(elName));
02439                           elementModelerList.append(ec->modeler);
02440                       
02441                           NEW(emi, ElementModelerInfo);
02442                           emi->modeler = ec->modeler;
02443                       
02444                           //new elements always build
02445                           emi->building = true;
02446 
02447                         }
02448                       else
02449                         {
02450                           readingNackData = false;
02451                           inNACK = false;
02452 
02453 
02454                           //save element name
02455                           NEW(elName, XmlChar[bufferLength]);
02456                           xmlchar_strcpy(elName, buffer);
02457                           elementNamesList->append(elName);
02458                           
02459                           //create an  element contex for the new element
02460                           NEW(ec, AdaptiveElementContext);
02461                       
02462                           //set unique element id
02463                           ec->name = elName;
02464                           
02465                           elementId = elementCounter;
02466                          
02467                           DBG("NEW ELEMENT HAS THIS ID: " << elementId);
02468  
02469                           //add new element context to the element hashtable
02470                           elements->insert(elementCounter, ec);
02471                           
02472                           //update the element counter
02473                           elementCounter++;
02474                           
02475                           elementStack->push(new unsigned long(elementId));
02476                           
02477                           //reset buffer
02478                           RESET_BUFFER;
02479                           
02480                           RESET_STATE;
02481                       
02482                       
02483                           NEW(ec->modeler, ElementModeler(elName));
02484                           elementModelerList.append(ec->modeler);
02485                       
02486                           NEW(emi, ElementModelerInfo);
02487                           emi->modeler = ec->modeler;
02488                       
02489                           //new elements always build
02490                           emi->building = true;
02491 
02492                         }
02493 
02494 
02495 
02496                       DBG("Appending new model:");
02497 //                    emi->modeler->print();
02498 
02499                       elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::ElementNode, &edge, &elts, emi->modeler);
02500                       DBG("After appending:");
02501 //                    elementModelerStack.top()->modeler->print();
02502 
02503 
02504                       DBG("Appended model:");
02505                       emi->modeler->resetCurrentNode();
02506 //                    emi->modeler->print();
02507 
02508                       elementModelerStack.push(emi);                    
02509                       DBG("items on the stack: " << elementModelerStack.count());
02510                       DBG("Top of the stack:");
02511 //                    elementModelerStack.top()->modeler->print();
02512 
02513                       if (!emi->modeler->hasAttributes())
02514                         {
02515                           //the element has no atributes (prediction, NACK can follow!)
02516                           //--> move throught the fictive attribute node
02517                           
02518                           inAttr = false;
02519                           DBG("3Element shouldn't have attributes");
02520                           attrPos = 0;
02521 
02522                         }
02523                       else
02524                         {
02525                           //the element has attributes
02526                           //--> jump into the fictive attribute node
02527                           inAttr = true;
02528                           DBG("3Element should have attributes");
02529                           attrPos = 0;
02530                         }
02531 
02532                     }
02533 
02534 
02535 
02536                 }
02537 
02538 
02539             }
02540           else
02541             {
02542               //read next character of the incomplete element name
02543               buffer[bufferLength] = data[i];
02544               bufferLength++;
02545             }
02546           break;
02547 
02548           //also used as XML declaration!
02549         case AdaptiveStructuralSymbols::KnownElement:
02550           if (inProlog)
02551             {
02552               //reading the XML declaration
02553 
02554               if (data[i] == AdaptiveStructuralSymbols::StandaloneYes || data[i] == AdaptiveStructuralSymbols::StandaloneNo
02555                   || (data[i] == AdaptiveStructuralSymbols::StandaloneNotSpecified && dataQueue->count() == 2))
02556                 {
02557                   //read standalone information
02558                   XmlChar *version;
02559                   XmlChar *encoding;
02560                   int standalone;
02561 
02562                   switch(data[i])
02563                     {
02564                     case AdaptiveStructuralSymbols::StandaloneYes:
02565                       standalone = 1;
02566                       break;
02567 
02568                     case AdaptiveStructuralSymbols::StandaloneNo:
02569                       standalone = 0;
02570                       break;
02571 
02572                     default:
02573                       standalone = -1;
02574                     }
02575 
02576                   version = dataQueue->dequeue()->data;
02577                   encoding = dataQueue->dequeue()->data;
02578 
02579                   SAFE_CALL_EMITTER(xmlDecl(userData, version, encoding, standalone));
02580 
02581                   if (version)
02582                     DELETE(version);
02583 
02584                   if (encoding)
02585                     DELETE(encoding);
02586 
02587                   RESET_STATE;
02588                   RESET_BUFFER;
02589                 }
02590               else
02591                 if (data[i] == AdaptiveStructuralSymbols::EndOfBlock)
02592                   {
02593                     //read version or encoding
02594                     NEW(dataQueueItem, DataQueueItem);
02595                     dataQueueItem->type = Characters;
02596 
02597                     if (bufferLength)
02598                       {
02599                         buffer[bufferLength] = 0;
02600                         NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(buffer)+1]);
02601                         xmlchar_strcpy(dataQueueItem->data, buffer);
02602                       }
02603                     else
02604                       //data not present (can occur for encoding)
02605                       dataQueueItem->data = 0;
02606 
02607                     dataQueue->enqueue(dataQueueItem);
02608 
02609                     RESET_BUFFER;
02610 
02611                   }
02612                 else
02613                   {
02614                     //read next character of xml declaration data
02615                     buffer[bufferLength] = data[i];
02616                     bufferLength++;
02617                   }
02618 
02619             }
02620           else
02621             {
02622 
02623               //known element started
02624               //--> read complete Fibonacci code of its id
02625 
02626               //construct reverse Fibonacci code (e.g. 1100101 instead of 1010011) from sequence of
02627               //following bytes
02628               fibMask = 1 << (SIZEOF_CHAR*8 - 1);
02629               
02630               //decode symbol using Fibonacci code
02631               fibTmpChar = (unsigned char)data[i];
02632 
02633               while (fibMask)
02634                 {
02635                   fibCode >>= 1;
02636                   fibBits++;
02637                   
02638                   if (fibTmpChar & fibMask)
02639                     {
02640                       fibCode = fibCode | ((unsigned long)1) << (SIZEOF_UNSIGNED_LONG*8-1);
02641                       
02642                       if (!fibLastWasOne)
02643                         fibLastWasOne = true;
02644                       else
02645                         {
02646                           //Fibonacci code is now complete
02647                           //--> decode the symbol number
02648                           fibCode >>= (SIZEOF_UNSIGNED_LONG*8 - fibBits);
02649                           elementId = Fibonacci::decode(fibCode);
02650                           fibFinished = true;
02651                           break;
02652                         }
02653                     }
02654                   else
02655                     fibLastWasOne = false;
02656                   
02657                   fibMask >>= 1;
02658                 }
02659 
02660               if (fibFinished)
02661                 {
02662                   //complete Fibonacci code read and decoded
02663                   //reset fibCode, and fibBits
02664                   fibCode = 0;
02665                   fibBits = 0;
02666                   fibLastWasOne = false;
02667 
02668                   RESET_BUFFER;
02669                   RESET_STATE;
02670 
02671 
02672 
02673                   if (inAttr)
02674                     {
02675                       //we are in the attribute section --> read the attribute name and value
02676                       DBG("KNOWN attribute " << elementId);
02677                       inAttrValue = true;
02678 
02679                       //get the name of the attribute from the attribute names hashtable
02680                       XmlChar *attrName = attributeNames->find(elementId);
02681                       CHECK_POINTER(attrName);
02682 
02683                       NEW(dataQueueItem, DataQueueItem);
02684                       dataQueueItem->type = Characters;
02685                       NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(attrName)+1]);
02686                       xmlchar_strcpy(dataQueueItem->data, attrName);
02687                       dataQueue->enqueue(dataQueueItem);
02688                       
02689                       continue;
02690                     }
02691                   else
02692                     {
02693                       //we read the id of the known element
02694 
02695                       //dataQueue->clear();     //!!! WORKAROUND because of the startelement with no attributes!
02696                       FINISH_START_ELEMENT_WITH_NO_ATTRIBUTES;
02697                       ec = elements->find(elementId);
02698 
02699                       DBG("KNOWN ELEMENT WITH THIS ID: " << elementId);
02700 
02701                       CHECK_POINTER(ec);
02702 
02703 
02704                       elementStack->push(new unsigned long(elementId));
02705 
02706                       DBG("DATA QUEUE COUNT: " << dataQueue->count());
02707 
02708                       //insert the name of the element onto the stack
02709                       NEW(dataQueueItem, DataQueueItem);
02710                       dataQueueItem->type = Characters;
02711                       NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(ec->name)+1]);
02712                       xmlchar_strcpy(dataQueueItem->data, ec->name);
02713                       dataQueue->enqueue(dataQueueItem);
02714                       DBG("Enqueued the name of the element:" << ec->name);
02715 
02716                       ElementModelerInfo *emi;
02717                       NEW(emi, ElementModelerInfo);
02718                       emi->modeler = ec->modeler;
02719 
02720                       /******/
02721                       //go as far as we can and then build a new element node
02722                       //only if not reading the nack information !
02723                       if (!readingNackData)
02724                         {
02725                           DBG("KNOWN ELEMENT " << ec->name << "--> moving");
02726 
02727                           ElementModel::NodeType res = ElementModel::ElementNode;                               
02728                           ElementModel::Node *node = 0;                                 
02729 
02730 
02731                           ElementModel::Node *cr;
02732                           bool moved = false;
02733                           bool leave = false;
02734 
02735                           while (res != ElementModel::NoNode)                           
02736                             {                           
02737                               //patch the first step
02738                               cr = elementModelerStack.top()->modeler->getCurrentNode();
02739                               if (moved && cr->type == ElementModel::StartNode && elementModelerStack.top()->modeler->hasAttributes())
02740                                 {
02741                                   DBG("We should stop in the attribute section!");
02742                                   DBG("Current element: " << elementModelerStack.top()->modeler->getElementName());
02743                                   inAttr = true;
02744                                   inAttrValue = true;
02745                                   moved = false;
02746 
02747                                   //we have to flip the attribute name and the name of the element (:-)
02748                                   dataQueueItem = dataQueue->dequeue();
02749                                   XmlChar *attrName = dataQueueItem->data;
02750 
02751                                   //enqueue the name of the element
02752                                   NEW(dataQueueItem, DataQueueItem);
02753                                   NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(elementModelerStack.top()->modeler->getElementName())+1]);
02754                                   xmlchar_strcpy(dataQueueItem->data, elementModelerStack.top()->modeler->getElementName());
02755                                   dataQueueItem->type = Characters;
02756                                   dataQueue->enqueue(dataQueueItem);
02757 
02758 
02759                                   //enqueue the name of the attribute
02760                                   NEW(dataQueueItem, DataQueueItem);
02761                                   dataQueueItem->type = Characters;
02762                                   dataQueueItem->data = attrName;
02763                                   dataQueue->enqueue(dataQueueItem);
02764 
02765 
02766                                   RESET_STATE;
02767                                   RESET_BUFFER;
02768 
02769                                   leave = true;
02770                                   break;
02771 
02772                                 }
02773 
02774                               attrPos = 1;      //at this point we are always after the attribute node
02775 
02776                               moved = true;
02777 
02778 
02779 
02780 
02781                               DBG("before moving forward:");
02782 //                            elementModelerStack.top()->modeler->print();
02783                               res = elementModelerStack.top()->modeler->moveForward(&node, saxEmitter, userData);       
02784                               DBG("after moving forward:");
02785 //                            elementModelerStack.top()->modeler->print();
02786                         
02787                               //moving over the element's end?
02788                               if (res == ElementModel::EndNode)
02789                                 {
02790                                   DBG("moving over the element's end --> performing pop()");
02791                                   elementModelerStack.pop();
02792                                   elementModelerStack.top()->modeler->popCurrentNode();
02793                                 }
02794                               else
02795                                 //jumped to other element model?
02796                                 if (res == ElementModel::ElementNode)                   
02797                                   {                                                     
02798                                     ElementModelerInfo *eminfo;                         
02799                                     NEW(eminfo, ElementModelerInfo);                    
02800                                     eminfo->modeler = node->modeler;                            
02801                                     eminfo->building = false;                           
02802                               
02803                                     elementModelerStack.push(eminfo);                   
02804                                     eminfo->modeler->resetCurrentNode();
02805 
02806 
02807                                     if (!eminfo->modeler->hasAttributes())
02808                                       {
02809                                         //the element has no atributes (prediction, NACK can follow!)
02810                                         //--> move throught the fictive attribute node
02811                                     
02812                                         inAttr = false;
02813 
02814                                         attrPos = 1;    //move behind the attribute section
02815 
02816                                         XmlChar *tmp;
02817                                         NEW(tmp, XmlChar[xmlchar_strlen(eminfo->modeler->getElementName())+1]);
02818                                         xmlchar_strcpy(tmp, eminfo->modeler->getElementName());
02819                             
02820                                         //emit startElement event with no attributes
02821                                         SAFE_CALL_EMITTER(startElement(userData, tmp, 0));
02822                                         eminfo->modeler->setAttributes(false);
02823 
02824                                       }
02825                                     else
02826                                       {
02827                                         //the element has attributes
02828                                         //--> jump into the fictive attribute node
02829                                         inAttr = true;
02830                                         DBG("4Element should have attributes");
02831                                         attrPos = 0;
02832                                       }
02833                                 
02834                                   }                                     
02835                             }
02836                           if (leave)
02837                             continue;
02838                         }
02839                       else
02840                         {
02841                           readingNackData = false;
02842                           inNACK = false;
02843                         }
02844 
02845 
02846 
02847 
02848                       DBG("Appending new model:");
02849 //                    emi->modeler->print();
02850 
02851                       emi->modeler->increaseRefCount();
02852               
02853                       elementModelerStack.top()->modeler->moveToDesiredNode(ElementModel::ElementNode, &edge, &elts, emi->modeler);
02854                       DBG("After appending:");
02855 //                    elementModelerStack.top()->modeler->print();
02856 
02857                       DBG("Appended model:");
02858                       emi->modeler->resetCurrentNode();
02859 //                    emi->modeler->print();
02860               
02861                       elementModelerStack.push(emi);                    
02862                       DBG("items on the stack: " << elementModelerStack.count());
02863                       DBG("Top of the stack:");
02864 //                    elementModelerStack.top()->modeler->print();
02865 
02866 
02867                       if (!emi->modeler->hasAttributes())
02868                         {
02869                           //the element has no atributes (prediction, NACK can follow!)
02870                           //--> move throught the fictive attribute node
02871                           
02872                           inAttr = false;
02873                           DBG("5Element shouldn't have attributes");
02874                           attrPos = 0;
02875                           DBG("DATA QUEUE COUNT: " << dataQueue->count());
02876                         }
02877                       else
02878                         {
02879                           //the element has attributes
02880                           //--> jump into the fictive attribute node
02881                           inAttr = true;
02882                           DBG("5Element should have attributes");
02883                           attrPos = 0;
02884                         }
02885                     }
02886 
02887                 }
02888             }
02889 
02890           break;
02891 
02892         case AdaptiveStructuralSymbols::Default:
02893           //default data
02894           if (data[i] == AdaptiveStructuralSymbols::EndOfBlock)
02895             {
02896 
02897               if (inComment)
02898                 {
02899                   //emit the comment data
02900                   inComment = false;
02901 
02902                   if (bufferLength)
02903                     //send data only if they aren't empty
02904                     SAFE_CALL_EMITTER(comment(userData, buffer));
02905 
02906                   RESET_BUFFER;
02907 
02908                   DBG("COMMENT ENDED");
02909                 }
02910               else
02911 
02912                 //CDATA is not allowed to occur before the root element!
02913                 //              if (inCDATA)
02914                 //                {
02915                 //                  //emit the CDATA data
02916                 //                  inCDATA = false;
02917 
02918                 //                  if (bufferLength)
02919                 //                    //send data only if they aren't empty
02920                 //                    SAFE_CALL_EMITTER(characterData(userData, buffer, bufferLength));
02921                         
02922                 //                  SAFE_CALL_EMITTER(endCDATASection(userData));
02923                 //                  RESET_BUFFER;
02924                 //                  DBG("CDATA ENDED");
02925                 //                }
02926                 //              else
02927                 if (inPI)
02928                   {
02929                     inPI--;
02930 
02931                     buffer[bufferLength] = data[i];
02932                     bufferLength++;
02933                     //complete PI read
02934                     if (!inPI)
02935                       {
02936                         //emit the PI
02937                         DBG("PI ENDED");
02938 
02939                         size_t k = 0;
02940 
02941                         while (buffer[k] != AdaptiveStructuralSymbols::EndOfBlock)
02942                           k++;
02943 
02944                         DBG("K = " << k << ", bufferLength = " << bufferLength);
02945                         if (k < bufferLength)
02946                           {
02947                             //both target and value
02948                             SAFE_CALL_EMITTER(processingInstruction(userData, buffer, buffer+k+1));
02949                           }
02950                         else
02951                           {
02952                             //no value
02953                             SAFE_CALL_EMITTER(processingInstruction(userData, buffer, 0));
02954                           }
02955 
02956                         RESET_BUFFER;
02957                       }
02958                   }
02959                 else
02960                   {
02961                     //emit the default data
02962                     SAFE_CALL_EMITTER(defaultHandler(userData, buffer, bufferLength));
02963                     RESET_STATE;
02964                     RESET_BUFFER;
02965                   }
02966             }
02967           else
02968             {
02969               switch (data[i])
02970                 {
02971                 case AdaptiveStructuralSymbols::Comment:
02972                   DBG("COMMENT");
02973                   inComment = true;
02974                   
02975                   //if there are any default data in the buffer, send them and reset the buffer
02976                   if (bufferLength)
02977                     //send data only if they aren't empty
02978                     SAFE_CALL_EMITTER(defaultHandler(userData, buffer, bufferLength));
02979                   
02980                   RESET_BUFFER;
02981                   break;
02982                   
02983                 case AdaptiveStructuralSymbols::PI:
02984                   DBG("PROCESSING INSTRUCTION");
02985                   inPI = 2;
02986                   
02987                   //if there are any default data in the buffer, send them and reset the buffer
02988                   if (bufferLength)
02989                     //send data only if they aren't empty
02990                     SAFE_CALL_EMITTER(defaultHandler(userData, buffer, bufferLength));
02991                   
02992                   RESET_BUFFER;
02993                   break;
02994 
02995                   //CDATA is not allowed to occur before the root element
02996                   //            case AdaptiveStructuralSymbols::CDATA:
02997                   //              DBG("CDATA");
02998                   //              inCDATA = true;
02999                   
03000                   //              //if there are any character data in the buffer, send them and reset the buffer
03001                   //              if (bufferLength)
03002                   //                //send data only if they aren't empty
03003                   //                SAFE_CALL_EMITTER(characterData(userData, buffer, bufferLength));
03004                   
03005                   //              RESET_BUFFER;
03006                   
03007                   //              SAFE_CALL_EMITTER(startCDATASection(userData));
03008                   //              break;
03009 
03010                 default:
03011                   //read next character of the default data
03012                   buffer[bufferLength] = data[i];
03013                   bufferLength++;
03014                   
03015                   if (bufferLength == XML_MODEL_BUFFER_DEFAULT_SIZE)
03016                     {
03017                       //data larger than the buffer --> split it
03018                       SAFE_CALL_EMITTER(defaultHandler(userData, buffer, bufferLength));
03019                       RESET_BUFFER;
03020                     }
03021                 }
03022             }
03023 
03024           break;
03025 
03026 
03027         case AdaptiveStructuralSymbols::EntityDecl:
03028           if (dataQueue->count() == 5)  //minus base!
03029             {
03030               //readind isparameterentity (the last record)
03031               XmlChar *entityName, *value, /* *base,*/ *publicId, *systemId, *notationName;
03032               int valueLength;
03033 
03034               entityName = dataQueue->dequeue()->data;
03035               value = dataQueue->dequeue()->data;
03036               //              base = dataQueue->dequeue()->data;
03037               systemId = dataQueue->dequeue()->data;
03038               publicId = dataQueue->dequeue()->data;
03039               notationName = dataQueue->dequeue()->data;
03040 
03041 
03042               if (!value)
03043                 valueLength = 0;
03044               else
03045                 valueLength = xmlchar_strlen(value);
03046 
03047               SAFE_CALL_EMITTER(entityDecl(userData, entityName, data[0] == AdaptiveStructuralSymbols::IsParameterEntityYes,
03048                                            value, valueLength, 0/*base*/, systemId, publicId, notationName));
03049 
03050               if (entityName)
03051                 DELETE(entityName);
03052 
03053               if (value)
03054                 DELETE(value);
03055 
03056               //              if (base)
03057               //                DELETE(base);
03058 
03059               if (systemId)
03060                 DELETE(systemId);
03061 
03062               if (publicId)
03063                 DELETE(publicId);
03064 
03065               if (notationName)
03066                 DELETE(notationName);
03067 
03068               RESET_BUFFER;
03069               RESET_STATE;
03070             }
03071           else
03072             if (data[i] == AdaptiveStructuralSymbols::EndOfBlock)
03073               {
03074                 //read target value
03075                 NEW(dataQueueItem, DataQueueItem);
03076                 dataQueueItem->type = Characters;
03077 
03078                 if (bufferLength)
03079                   {
03080                     if (buffer[0] == AdaptiveStructuralSymbols::EmptyString)
03081                       //we read an empty string
03082                       buffer[0] = 0;
03083                     else
03084                       buffer[bufferLength] = 0;
03085 
03086                     NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(buffer)+1]);
03087                     xmlchar_strcpy(dataQueueItem->data, buffer);
03088                   }
03089                 else
03090                   //data not present
03091                   dataQueueItem->data = 0;
03092 
03093                 dataQueue->enqueue(dataQueueItem);
03094 
03095                 RESET_BUFFER;
03096               }
03097             else
03098               {
03099                 //read next character
03100                 buffer[bufferLength] = data[i];
03101                 bufferLength++;
03102               }
03103 
03104           break;
03105 
03106         case AdaptiveStructuralSymbols::NotationDecl:
03107           if (data[i] == AdaptiveStructuralSymbols::EndOfBlock)
03108             {
03109               //read target value
03110               NEW(dataQueueItem, DataQueueItem);
03111               dataQueueItem->type = Characters;
03112               
03113               if (bufferLength)
03114                 {
03115                   if (buffer[0] == AdaptiveStructuralSymbols::EmptyString)
03116                     //we read an empty string
03117                     buffer[0] = 0;
03118                   else
03119                     buffer[bufferLength] = 0;
03120                   
03121                   NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(buffer)+1]);
03122                   xmlchar_strcpy(dataQueueItem->data, buffer);
03123                 }
03124               else
03125                 //data not present
03126                 dataQueueItem->data = 0;
03127               
03128               dataQueue->enqueue(dataQueueItem);
03129               
03130               RESET_BUFFER;
03131 
03132               //complete information read?
03133               if (dataQueue->count() == 3)      //minus base!
03134                 {
03135                   //readind isparameterentity (the last record)
03136                   XmlChar *notationName, /* *base,*/ *publicId, *systemId;
03137                   
03138                   notationName = dataQueue->dequeue()->data;
03139                   //          base = dataQueue->dequeue()->data;
03140                   systemId = dataQueue->dequeue()->data;
03141                   publicId = dataQueue->dequeue()->data;
03142                   
03143                   SAFE_CALL_EMITTER(notationDecl(userData, notationName, 0/*base*/, systemId, publicId));
03144 
03145                   if (notationName)
03146                     DELETE(notationName);
03147 
03148                   //              if (base)
03149                   //                DELETE(base);
03150 
03151                   if (systemId)
03152                     DELETE(systemId);
03153                   
03154                   if (publicId)
03155                     DELETE(publicId);
03156                   
03157                   if (notationName)
03158                     DELETE(notationName);
03159 
03160                   RESET_STATE;
03161                 }
03162             }
03163           else
03164             {
03165               //read next character
03166               buffer[bufferLength] = data[i];
03167               bufferLength++;
03168             }
03169 
03170           break;
03171 
03172         case AdaptiveStructuralSymbols::Characters:
03173           DBG("reading char " << (unsigned int)data[i]);
03174           //characters
03175           if (data[i] == AdaptiveStructuralSymbols::EmptyString)
03176             {
03177               //ignored
03178             }
03179           else
03180             if (inAttrValue)
03181               {
03182                 if (data[i] == AdaptiveStructuralSymbols::EndOfBlock ||
03183                     data[i] == AdaptiveStructuralSymbols::NewElement ||
03184                     data[i] == AdaptiveStructuralSymbols::KnownElement)
03185                   
03186                   {
03187                     //read the complete value of an attribute
03188                     inAttrValue = false;
03189 
03190                     if (!firstAttrRead)
03191                       {
03192                         firstAttrRead = true;
03193 
03194                         //update the attribute stats
03195                         elementModelerStack.top()->modeler->setAttributes(true);
03196                       }
03197 
03198                     buffer[bufferLength] = 0;
03199                     bufferLength++;
03200                     DBG("value of the attribute: " << buffer);
03201 
03202                     
03203 
03204                     NEW(dataQueueItem, DataQueueItem);
03205                     dataQueueItem->type = Characters;
03206                     NEW(dataQueueItem->data, XmlChar[xmlchar_strlen(buffer)+1]);
03207                     xmlchar_strcpy(dataQueueItem->data, buffer);
03208                     dataQueue->enqueue(dataQueueItem);
03209 
03210                     //we read the last attribute
03211                     if (data[i] == AdaptiveStructuralSymbols::EndOfBlock)
03212                       {
03213                         inAttr = false;
03214                         firstAttrRead = false;
03215                         attrPos = 1;    //we left the attribute node
03216                         DBG("last attribute value read");
03217                         RESET_STATE;
03218 
03219                         DBG("length of the data queue: " << dataQueue->count());
03220                         
03221                         const XmlChar **attrs;                          
03222                         const XmlChar *elementName;
03223                         size_t attrPos = 0;                                                     
03224                         
03225                         //dequeue the name of the element
03226                         dataQueueItem = dataQueue->dequeue();
03227                         elementName = dataQueueItem->data;
03228 
03229                         //get the attributes
03230                         NEW(attrs, const XmlChar *[dataQueue->count()+1]);                      
03231                         attrs[dataQueue->count()] = 0;                                          
03232                         /* attributes and the element name decoded */                           
03233                         while ((dataQueueItem = dataQueue->dequeue()))                          
03234                           {                                                                     
03235                             /* an attribute name or an attribute value */                       
03236                             attrs[attrPos] = dataQueueItem->data;                               
03237                             attrPos++;                                                          
03238                           }                                                                     
03239                         
03240                         //emit the startElement event
03241                         SAFE_CALL_EMITTER(startElement(userData, elementName, attrs));
03242 
03243                         DBG("-----");
03244                       }
03245                     else
03246                       {
03247                         //allow the change to newelement/knownelement
03248                         //in order to read the other attributes
03249                         if (data[i] == AdaptiveStructuralSymbols::NewElement)
03250                           state = AdaptiveStructuralSymbols::NewElement;
03251                         else
03252                           state = AdaptiveStructuralSymbols::KnownElement;
03253                       }
03254                     RESET_BUFFER;
03255                   }
03256                 else
03257                   {
03258                     //read the characters of the attribute value
03259                     DBG("attrval char: " << data[i]);
03260                     buffer[bufferLength] = data[i];
03261                     bufferLength++;
03262                   }
03263               }
03264             else
03265 
03266               if (data[i] == AdaptiveStructuralSymbols::EndOfBlock)
03267                 {
03268                   if (inComment)
03269                     {
03270                       //emit the comment data
03271                       inComment = false;
03272 
03273                       if (bufferLength)
03274                         //send data only if they aren't empty
03275                         SAFE_CALL_EMITTER(comment(userData, buffer));
03276 
03277                       RESET_BUFFER;
03278 
03279                       DBG("COMMENT ENDED");
03280                     }
03281                   else
03282                     if (inCDATA)
03283                       {
03284                         //emit the CDATA data
03285                         inCDATA = false;
03286 
03287                         if (bufferLength)
03288                           //send data only if they aren't empty
03289                           SAFE_CALL_EMITTER(characterData(userData, buffer, bufferLength));
03290                         
03291                         SAFE_CALL_EMITTER(endCDATASection(userData));
03292                         RESET_BUFFER;
03293                         DBG("CDATA ENDED");
03294                       }
03295                     else
03296                       if (inPI)
03297                         {
03298                           inPI--;
03299 
03300                           buffer[bufferLength] = data[i];
03301                           bufferLength++;
03302                           //complete PI read
03303                           if (!inPI)
03304                             {
03305                               //emit the PI
03306                               DBG("PI ENDED");
03307 
03308                               size_t k = 0;
03309 
03310                               while (buffer[k] != AdaptiveStructuralSymbols::EndOfBlock)
03311                                 k++;
03312 
03313                               DBG("K = " << k << ", bufferLength = " << bufferLength);
03314                               if (k < bufferLength)
03315                                 {
03316                                   //both target and value
03317                                   SAFE_CALL_EMITTER(processingInstruction(userData, buffer, buffer+k+1));
03318                                 }
03319                               else
03320                                 {
03321                                   //no value
03322                                   SAFE_CALL_EMITTER(processingInstruction(userData, buffer, 0));
03323                                 }
03324 
03325                               RESET_BUFFER;
03326                             }
03327                         }
03328                       else
03329                         {
03330                           //emit the character data and reset the state (character data end)
03331                           if (bufferLength)
03332                             //send data only if they aren't empty
03333                             {
03334                               buffer[bufferLength] = 0;
03335                               SAFE_CALL_EMITTER(characterData(userData, buffer, bufferLength));
03336                             }
03337                           
03338                           
03339                           RESET_STATE;
03340                           RESET_BUFFER;
03341                         }
03342                   
03343                 }
03344               else
03345                 {
03346                   DBG("Character: '" << data[i] << "' ASCII: "  << (unsigned int)data[i]);
03347 
03348                   switch (data[i])
03349                     {
03350                     case AdaptiveStructuralSymbols::Comment:
03351                       DBG("COMMENT");
03352                       inComment = true;
03353 
03354                       //if there are any character data in the buffer, send them and reset the buffer
03355                       if (bufferLength)
03356                         //send data only if they aren't empty
03357                         SAFE_CALL_EMITTER(characterData(userData, buffer, bufferLength));
03358 
03359                       RESET_BUFFER;
03360                       break;
03361 
03362                     case AdaptiveStructuralSymbols::PI:
03363                       DBG("PROCESSING INSTRUCTION");
03364                       inPI = 2;
03365 
03366                       //if there are any character data in the buffer, send them and reset the buffer
03367                       if (bufferLength)
03368                         //send data only if they aren't empty
03369                         SAFE_CALL_EMITTER(characterData(userData, buffer, bufferLength));
03370 
03371                       RESET_BUFFER;
03372                       break;
03373 
03374                     case AdaptiveStructuralSymbols::CDATA:
03375                       DBG("CDATA");
03376                       inCDATA = true;
03377 
03378                       //if there are any character data in the buffer, send them and reset the buffer
03379                       if (bufferLength)
03380                         //send data only if they aren't empty
03381                         SAFE_CALL_EMITTER(characterData(userData, buffer, bufferLength));
03382 
03383                       RESET_BUFFER;
03384 
03385                       SAFE_CALL_EMITTER(startCDATASection(userData));
03386                       break;
03387 
03388                     default:
03389                       buffer[bufferLength] = data[i];
03390                       bufferLength++;
03391                   
03392                       if (bufferLength == XML_MODEL_BUFFER_DEFAULT_SIZE)
03393                         {
03394                           //character data larger than the buffer --> split it
03395                           if (inComment)
03396                             {
03397                               //nothing to do, this should never happen!
03398                             }
03399 
03400                           SAFE_CALL_EMITTER(characterData(userData, buffer, bufferLength));
03401                           RESET_BUFFER;
03402                         }
03403                     }
03404                 }
03405           break;
03406 
03407         default:
03408           FATAL("default !!!");
03409         }
03410     }
03411 }

Generated on Wed Feb 5 10:43:02 2003 for Exalt by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002