Open
Graph Drawing
Framework

 v.2012.05
 

DinoXmlParser.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 
00007  ***************************************************************/
00008  
00043 #ifdef _MSC_VER
00044 #pragma once
00045 #endif
00046 
00047 #ifndef OGDF_DINO_XML_PARSER_H
00048 #define OGDF_DINO_XML_PARSER_H
00049 
00050 #include <ogdf/basic/Stack.h>
00051 #include <ogdf/basic/String.h>
00052 #include <ogdf/basic/Hashing.h>
00053 #include <ogdf/basic/GraphAttributes.h>
00054 #include <ogdf/fileformats/DinoXmlScanner.h>
00055 
00056 
00057 
00058 namespace ogdf {
00059 
00060     //---------------------------------------------------------
00061     // H a s h e d S t r i n g
00062     //---------------------------------------------------------
00063 
00064     typedef HashElement<String,int> HashedString;
00065 
00066     //---------------------------------------------------------
00067     // X m l A t t r i b u t e O b j e c t
00068     //---------------------------------------------------------
00071     struct OGDF_EXPORT XmlAttributeObject {
00072         
00076         HashedString *m_pAttributeName;
00077         
00081         HashedString *m_pAttributeValue;
00082 
00084         XmlAttributeObject *m_pNextAttribute;
00085 
00087         XmlAttributeObject(HashedString *name, HashedString *value) :
00088             m_pAttributeName(name),
00089             m_pAttributeValue(value),
00090             m_pNextAttribute(0)
00091             {};
00092 
00094         ~XmlAttributeObject(){};
00095 
00097         bool m_valid;
00098 
00100         const String& getName() const {
00101             return m_pAttributeName->key(); 
00102         }
00103 
00104         const String& getValue() const {
00105             return m_pAttributeValue->key();    
00106         }
00107 
00108         const bool& valid() const {
00109             return m_valid;
00110         }
00111 
00113         void setValid() {
00114             m_valid = true;
00115         }
00116 
00117         void setInvalid() {
00118             m_valid = false;    
00119         }
00120 
00121         // Overloaded new and delete operators
00122         OGDF_NEW_DELETE
00123 
00124     }; // struct XmlAttributeObject
00125 
00126     //---------------------------------------------------------
00127     // X m l T a g O b j e c t
00128     //---------------------------------------------------------
00131     struct OGDF_EXPORT XmlTagObject {
00132 
00136         HashedString *m_pTagName;
00137 
00142         XmlAttributeObject *m_pFirstAttribute;
00143 
00147         HashedString *m_pTagValue;
00148 
00156         XmlTagObject *m_pFirstSon;
00157             
00161         XmlTagObject *m_pBrother;
00162 
00164         XmlTagObject(HashedString *name) :
00165             m_pTagName(name),
00166             m_pFirstAttribute(0),
00167             m_pTagValue(0),
00168             m_pFirstSon(0),
00169             m_pBrother(0),
00170             m_valid(0)
00171             {};
00172 
00174         ~XmlTagObject(){};
00175 
00177         mutable bool m_valid;
00178         
00180         int m_depth;
00181         
00184          int m_line;
00185         
00186         public:
00187 
00194         bool isLeaf() const;
00195 
00203         bool findSonXmlTagObjectByName( const String sonsName,
00204                                         XmlTagObject *&son) const;
00205 
00213         bool findSonXmlTagObjectByName( const String sonsName,
00214                                         List<XmlTagObject*> &sons) const;
00215 
00224         bool hasMoreSonXmlTagObject(const List<String> &sonNamesToIgnore) const;
00225 
00230         bool findXmlAttributeObjectByName(const String attName,
00231                                            XmlAttributeObject*& attribute) const;
00232         
00239         bool isAttributeLess() const;
00240 
00242         const bool& valid() const {
00243             return m_valid; 
00244         }
00245 
00246         const String& getName() const {
00247             return m_pTagName->key();   
00248         }
00249 
00250         const String& getValue() const {
00251             return m_pTagValue->key();  
00252         }
00253 
00255         void setValid() const {
00256             m_valid = true;
00257         }
00258 
00259         void setInvalid() {
00260             m_valid = false;
00261         }
00262         
00263         /* get for depth of xml-tag-object */
00264         const int& getDepth() const {
00265             return m_depth; 
00266         }
00267         
00268         /* setter for new depth */
00269         void setDepth(int newDepth){
00270             m_depth = newDepth;
00271         }
00272 
00273 
00274         /* get for line of xml-tag-object */
00275         const int& getLine() const {
00276             return m_line;  
00277         }
00278         
00279         /* setter for line */
00280         void setLine(int line) {
00281             m_line = line;
00282         }
00283 
00284         // Overloaded new and delete operators
00285         OGDF_NEW_DELETE
00286 
00287     }; // struct XmlTagObject
00288 
00289     //---------------------------------------------------------
00290     // D i n o X m l P a r s e r 
00291     //---------------------------------------------------------
00297   class OGDF_EXPORT DinoXmlParser {
00298 
00299       friend ostream &operator<<(ostream&, const DinoXmlParser &);
00300 
00301     private:
00302 
00304         XmlTagObject *m_pRootTag;
00305 
00307         DinoXmlScanner *m_pScanner;
00308 
00314         Hashing<String,int> m_hashTable; 
00315 
00320         int m_hashTableInfoIndex;
00321 
00323         int m_recursionDepth;
00325         Stack<String> m_tagObserver;
00326 
00327 
00328     public:
00329         
00333         DinoXmlParser(const char *fileName);
00334 
00336         ~DinoXmlParser();
00337 
00340         void addNewHashElement(const String &key, int info){
00341             OGDF_ASSERT(info >= m_hashTableInfoIndex)
00342             m_hashTable.fastInsert(key, info);
00343             m_hashTableInfoIndex = info + 1;
00344         }
00345 
00349         void createParseTree();
00350 
00352         const XmlTagObject &getRootTag() const {
00353             return *m_pRootTag;
00354         }
00355 
00363         bool traversePath(const XmlTagObject &startTag, 
00364                           const Array<int> &infoIndexPath,
00365                           const XmlTagObject *&targetTag) const;
00366 
00373         bool findSonXmlTagObject(const XmlTagObject &father, 
00374                                  int sonInfoIndex,
00375                                  const XmlTagObject *&son) const;
00376 
00383         bool findBrotherXmlTagObject(const XmlTagObject &currentTag, 
00384                                      int brotherInfoIndex,
00385                                      const XmlTagObject *&brother) const;
00386 
00393         bool findXmlAttributeObject(const XmlTagObject &currentTag, 
00394                                     int attributeInfoIndex,
00395                                     const XmlAttributeObject *&attribute) const;
00396         
00400         inline int getInputFileLineCounter() const {
00401             return m_pScanner->getInputFileLineCounter();
00402         }
00403 
00405         void printHashTable(ostream &os);
00406 
00407     private:
00408 
00410         void destroyParseTree(XmlTagObject *root);
00411 
00417         XmlTagObject* parse();
00418 
00420         void appendAttributeObject(XmlTagObject *tagObject,
00421                                    XmlAttributeObject *attributeObject);
00422         
00424         void appendSonTagObject(XmlTagObject *currentTagObject, 
00425                                 XmlTagObject *sonTagObject);
00426 
00433         HashedString *hashString(const String &str);
00434 
00438         void printXmlTagObjectTree(ostream &os, const XmlTagObject &rootObject, 
00439                                    int indent = 0) const;
00440 
00442         void printSpaces(ostream &os, int nOfSpaces) const;
00443 
00444     }; // class DinoXmlParser
00445 
00447     ostream &operator<<(ostream &os, const DinoXmlParser &parser);
00448 
00449 } // end namespace ogdf
00450 
00451 #endif