Open
Graph Drawing
Framework

 v.2012.05
 

GmlParser.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_GML_PARSER_H
00048 #define OGDF_GML_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/cluster/ClusterGraph.h>
00055 #include <ogdf/cluster/ClusterGraphAttributes.h>
00056 
00057 
00058 namespace ogdf {
00059 
00060 
00061 typedef HashElement<String,int> *GmlKey;
00062 enum GmlObjectType { gmlIntValue, gmlDoubleValue, gmlStringValue, gmlListBegin,
00063     gmlListEnd, gmlKey, gmlEOF, gmlError };
00064 
00065 
00066 //---------------------------------------------------------
00067 // GmlObject
00068 // represents node in GML parse tree
00069 //---------------------------------------------------------
00070 struct OGDF_EXPORT GmlObject {
00071     GmlObject *m_pBrother; // brother of node in tree
00072     GmlKey m_key; // tag of node
00073     GmlObjectType m_valueType; // type of node
00074 
00075     // the entry in the union is selected according to m_valueType:
00076     //   gmlIntValue -> m_intValue
00077     //   gmlDoubleValue -> m_doubleValue
00078     //   gmlStringValue -> m_stringValue
00079     //   gmlListBegin -> m_pFirstSon (in case of a list, m_pFirstSon is pointer
00080     //     to first son and the sons are chained by m_pBrother)
00081     union {
00082         int m_intValue;
00083         double m_doubleValue;
00084         const char *m_stringValue;
00085         GmlObject *m_pFirstSon;
00086     };
00087 
00088     // construction
00089     GmlObject(GmlKey key, int intValue) : m_pBrother(0), m_key(key),
00090         m_valueType(gmlIntValue), m_intValue(intValue)  { }
00091 
00092     GmlObject(GmlKey key, double doubleValue) : m_pBrother(0), m_key(key),
00093         m_valueType(gmlDoubleValue), m_doubleValue(doubleValue)  { }
00094 
00095     GmlObject(GmlKey key, const char *stringValue) : m_pBrother(0), m_key(key),
00096         m_valueType(gmlStringValue), m_stringValue(stringValue)  { }
00097 
00098     GmlObject(GmlKey key) : m_pBrother(0), m_key(key),
00099         m_valueType(gmlListBegin), m_pFirstSon(0)  { }
00100 
00101     OGDF_NEW_DELETE
00102 };
00103 
00104 
00105 //---------------------------------------------------------
00106 // GmlParser
00107 // reads GML file and constructs GML parse tree
00108 //---------------------------------------------------------
00109 class OGDF_EXPORT GmlParser {
00110     Hashing<String,int> m_hashTable; // hash table for tags
00111     int m_num;
00112 
00113     istream *m_is;
00114     bool m_error;
00115     String m_errorString;
00116 
00117     char *m_rLineBuffer, *m_lineBuffer, *m_pCurrent, *m_pStore, m_cStore;
00118 
00119     int m_intSymbol;
00120     double m_doubleSymbol;
00121     const char *m_stringSymbol;
00122     GmlKey m_keySymbol;
00123     String m_longString;
00124 
00125     GmlObject *m_objectTree; // root node of GML parse tree
00126 
00127     bool m_doCheck;
00128     Array<node> m_mapToNode;
00129     GmlObject  *m_graphObject;
00130 
00131 public:
00132     // predefined id constants for all used keys
00133     enum PredefinedKey { idPredefKey = 0, labelPredefKey, CreatorPredefKey,
00134         namePredefKey, graphPredefKey, versionPredefKey, directedPredefKey,
00135         nodePredefKey, edgePredefKey, graphicsPredefKey, xPredefKey,
00136         yPredefKey, wPredefKey, hPredefKey, typePredefKey, widthPredefKey,
00137         sourcePredefKey, targetPredefKey, arrowPredefKey, LinePredefKey,
00138         pointPredefKey, generalizationPredefKey, subGraphPredefKey, fillPredefKey, clusterPredefKey,
00139         rootClusterPredefKey, vertexPredefKey, colorPredefKey, 
00140         heightPredefKey, stipplePredefKey, patternPredefKey, 
00141         linePredefKey, lineWidthPredefKey, templatePredefKey,
00142         edgeWeightPredefKey, NEXTPREDEFKEY };
00143 
00144     // construction: creates object tree
00145     // sets m_error flag if an error occured
00146     GmlParser(const char *fileName, bool doCheck = false);
00147     GmlParser(istream &is, bool doCheck = false);
00148 
00149     // destruction: destroys object tree
00150     ~GmlParser();
00151 
00152     // returns id of object
00153     int id(GmlObject *object) const { return object->m_key->info(); }
00154 
00155     // true <=> an error in GML files has been detected
00156     bool error() const { return m_error; }
00157     // returns error message
00158     const String &errorString() const { return m_errorString; }
00159 
00160     // creates graph from GML parse tree
00161     bool read(Graph &G);
00162     // creates attributed graph from GML parse tree
00163     bool read(Graph &G, GraphAttributes &AG);
00164     //creates clustergraph from GML parse tree
00165     //bool read(Graph &G, ClusterGraph & CG);
00166     //read only cluster part of object tree and create cluster graph structure
00167     bool readCluster(Graph &G, ClusterGraph& CG);
00168     //the same with attributes 
00169     bool readAttributedCluster(Graph &G, ClusterGraph& CG,
00170                                        ClusterGraphAttributes& ACG);
00171 
00172 protected:
00173 
00174     //read all cluster tree information
00175     bool clusterRead(GmlObject* rootCluster, 
00176                      ClusterGraph& CG);
00177     //with attributes
00178     bool attributedClusterRead(GmlObject* rootCluster, 
00179                             ClusterGraph& CG, 
00180                             ClusterGraphAttributes& ACG);
00181     //recursively read cluster subtree information
00182     bool recursiveClusterRead(GmlObject* clusterObject,
00183                          ClusterGraph& CG,
00184                          cluster c);
00185     bool recursiveAttributedClusterRead(GmlObject* clusterObject,
00186                                 ClusterGraph& CG,
00187                                 ClusterGraphAttributes& ACG,
00188                                 cluster c);
00189     bool readClusterAttributes(GmlObject* cGraphics,
00190                                       cluster c,
00191                                       ClusterGraphAttributes& ACG);
00192 
00193 private:
00194     void doInit(istream &is, bool doCheck);
00195     void createObjectTree(istream &is, bool doCheck);
00196     void initPredefinedKeys();
00197     void setError(const char *errorString);
00198 
00199     GmlObject *parseList(GmlObjectType closingKey, GmlObjectType errorKey);
00200     GmlObjectType getNextSymbol();
00201     bool getLine();
00202 
00203     GmlKey hashString(const String &str);
00204 
00205     GmlObject *getNodeIdRange(int &minId,int &maxId);
00206     void readLineAttribute(GmlObject *object, DPolyline &dpl);
00207 
00208     void destroyObjectList(GmlObject *object);
00209 
00210     void indent(ostream &os, int d);
00211     void output(ostream &os, GmlObject *object, int d);
00212 
00213 };
00214 
00215 
00216 } // end namespace ogdf
00217 
00218 #endif