Open
Graph Drawing
Framework

 v.2012.05
 

XmlParser.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_XML_PARSER_H
00048 #define OGDF_XML_PARSER_H
00049 
00050 
00051 #include <ogdf/basic/Hashing.h>
00052 #include <ogdf/basic/String.h>
00053 #include <ogdf/basic/GraphAttributes.h>
00054 #include <ogdf/fileformats/XmlObject.h>
00055 #include <ogdf/basic/SList.h>
00056 
00057 
00058 namespace ogdf {
00059 
00060 
00061 
00062 //---------------------------------------------------------
00063 // XmlParser
00064 // reads XML file and constructs XML parse tree
00065 //---------------------------------------------------------
00066 class OGDF_EXPORT XmlParser {
00067     Hashing<String,int> m_hashTable; // hash table for tags
00068     int m_num;
00069 
00070     istream *m_is;
00071     bool m_error;
00072     String m_errorString;
00073 
00074     char *m_rLineBuffer, *m_lineBuffer, *m_pCurrent, *m_pStore, m_cStore;
00075 
00076     int         m_intSymbol;    // integer attribute
00077     double      m_doubleSymbol; // double attribute
00078     const char *m_stringSymbol; // string attribute
00079     char       *m_keyName;      // Tag name
00080     XmlKey      m_keySymbol;    // Tag name and Attribute Name in Hash Table
00081     String      m_longString;
00082     bool        m_eoTag;        // end of Tag recognized
00083 
00084     XmlObject *m_objectTree; // root node of XML parse tree
00085 
00086     bool m_doCheck;
00087 
00088     SList<char*> m_objectBody;
00089 
00090 public:
00091     // predefined id constants for all used keys
00092     enum PredefinedKey { idPredefKey = 0, labelPredefKey, CreatorPredefKey,
00093         namePredefKey, graphPredefKey, versionPredefKey, directedPredefKey,
00094         nodePredefKey, edgePredefKey, graphicsPredefKey, xPredefKey,
00095         yPredefKey, wPredefKey, hPredefKey, nodetypePredefKey, edgetypePredefKey,
00096         typePredefKey, widthPredefKey, heightPredefKey, sizePredefKey,
00097         positionPredefKey, pathPredefKey,
00098         sourcePredefKey, targetPredefKey, sensePredefKey, arrowPredefKey, LinePredefKey,
00099         pointPredefKey, NEXTPREDEFKEY };
00100 
00101     // construction: creates object tree
00102     // sets m_error flag if an error occured
00103     XmlParser(const char *fileName, bool doCheck = false);
00104     XmlParser(istream &is, bool doCheck = false);
00105 
00106     // destruction: destroys object tree
00107     ~XmlParser();
00108 
00109     // returns root object
00110     XmlObject *root() { return m_objectTree; }
00111 
00112     // id of a string in hash table; -1 if not contained
00113     int getId(const String &tag) const {
00114         HashElement<String,int> *it = m_hashTable.lookup(tag);
00115         return (it != 0) ? it->info() : -1;
00116     }
00117 
00118     // returns id of object
00119     int id(XmlObject *object) const { return object->m_key->info(); }
00120 
00121     // true <=> an error in XML files has been detected
00122     bool error() const { return m_error; }
00123     // returns error message
00124     const String &errorString() const { return m_errorString; }
00125 
00126     // creates graph from XML parse tree
00127     bool read(Graph &G);
00128     // creates attributed graph from XML parse tree
00129     bool read(Graph &G, GraphAttributes &AG);
00130 
00131 private:
00132     void createObjectTree(istream &is, bool doCheck);
00133     void initPredefinedKeys();
00134     void setError(const char *errorString);
00135 
00136     XmlObject *parseList(XmlObjectType closingKey, XmlObjectType errorKey, const char *objectBodyName);
00137     XmlObjectType getNextSymbol();
00138     bool getLine();
00139 
00140     XmlKey hashString(const String &str);
00141 
00142     XmlObject *getNodeIdRange(int &minId,int &maxId, 
00143                               int &nodetypeCount,
00144                               XmlObject *graphObject);
00145 
00146     bool makeIdMap(int maxId,
00147                    Array<char*> &idMap, 
00148                    int nodetypeCount,
00149                    Array<char*> & typeName,
00150                    Array<double> &typeWidth,
00151                    Array<double> &typeHeight, 
00152                    XmlObject *graphObject);
00153 
00154     void closeLabels(Array<char*> idMap, Array<char*>  typeName);
00155 
00156     void readLineAttribute(XmlObject *object, DPolyline &dpl);
00157 
00158     void destroyObjectList(XmlObject *object);
00159 
00160     void indent(ostream &os, int d);
00161     void output(ostream &os, XmlObject *object, int d);
00162 
00163 };
00164 
00165 
00166 } // end namespace ogdf
00167 
00168 #endif