6 * $Date: 2012-07-07 00:03:48 +0200 (Sa, 07. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration of classes GmlObject and GmlParser.
12 * \author Carsten Gutwenger
15 * This file is part of the Open Graph Drawing Framework (OGDF).
19 * See README.txt in the root directory of the OGDF installation for details.
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * Version 2 or 3 as published by the Free Software Foundation;
25 * see the file LICENSE.txt included in the packaging of this file
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the Free
37 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
40 * \see http://www.gnu.org/copyleft/gpl.html
41 ***************************************************************/
48 #ifndef OGDF_GML_PARSER_H
49 #define OGDF_GML_PARSER_H
52 #include <ogdf/basic/Hashing.h>
53 #include <ogdf/basic/String.h>
54 #include <ogdf/basic/GraphAttributes.h>
55 #include <ogdf/cluster/ClusterGraph.h>
56 #include <ogdf/cluster/ClusterGraphAttributes.h>
62 typedef HashElement
<String
,int> *GmlKey
;
63 enum GmlObjectType
{ gmlIntValue
, gmlDoubleValue
, gmlStringValue
, gmlListBegin
,
64 gmlListEnd
, gmlKey
, gmlEOF
, gmlError
};
67 //---------------------------------------------------------
69 // represents node in GML parse tree
70 //---------------------------------------------------------
71 struct OGDF_EXPORT GmlObject
{
72 GmlObject
*m_pBrother
; // brother of node in tree
73 GmlKey m_key
; // tag of node
74 GmlObjectType m_valueType
; // type of node
76 // the entry in the union is selected according to m_valueType:
77 // gmlIntValue -> m_intValue
78 // gmlDoubleValue -> m_doubleValue
79 // gmlStringValue -> m_stringValue
80 // gmlListBegin -> m_pFirstSon (in case of a list, m_pFirstSon is pointer
81 // to first son and the sons are chained by m_pBrother)
85 const char *m_stringValue
;
86 GmlObject
*m_pFirstSon
;
90 GmlObject(GmlKey key
, int intValue
) : m_pBrother(0), m_key(key
),
91 m_valueType(gmlIntValue
), m_intValue(intValue
) { }
93 GmlObject(GmlKey key
, double doubleValue
) : m_pBrother(0), m_key(key
),
94 m_valueType(gmlDoubleValue
), m_doubleValue(doubleValue
) { }
96 GmlObject(GmlKey key
, const char *stringValue
) : m_pBrother(0), m_key(key
),
97 m_valueType(gmlStringValue
), m_stringValue(stringValue
) { }
99 GmlObject(GmlKey key
) : m_pBrother(0), m_key(key
),
100 m_valueType(gmlListBegin
), m_pFirstSon(0) { }
106 //---------------------------------------------------------
108 // reads GML file and constructs GML parse tree
109 //---------------------------------------------------------
110 class OGDF_EXPORT GmlParser
{
111 Hashing
<String
,int> m_hashTable
; // hash table for tags
116 String m_errorString
;
118 char *m_rLineBuffer
, *m_lineBuffer
, *m_pCurrent
, *m_pStore
, m_cStore
;
121 double m_doubleSymbol
;
122 const char *m_stringSymbol
;
126 GmlObject
*m_objectTree
; // root node of GML parse tree
129 Array
<node
> m_mapToNode
;
130 GmlObject
*m_graphObject
;
133 // predefined id constants for all used keys
134 enum PredefinedKey
{ idPredefKey
= 0, labelPredefKey
, CreatorPredefKey
,
135 namePredefKey
, graphPredefKey
, versionPredefKey
, directedPredefKey
,
136 nodePredefKey
, edgePredefKey
, graphicsPredefKey
, xPredefKey
,
137 yPredefKey
, wPredefKey
, hPredefKey
, typePredefKey
, widthPredefKey
,
138 sourcePredefKey
, targetPredefKey
, arrowPredefKey
, LinePredefKey
,
139 pointPredefKey
, generalizationPredefKey
, subGraphPredefKey
, fillPredefKey
, clusterPredefKey
,
140 rootClusterPredefKey
, vertexPredefKey
, colorPredefKey
,
141 heightPredefKey
, stipplePredefKey
, patternPredefKey
,
142 linePredefKey
, lineWidthPredefKey
, templatePredefKey
,
143 edgeWeightPredefKey
, NEXTPREDEFKEY
};
145 // construction: creates object tree
146 // sets m_error flag if an error occured
147 GmlParser(const char *fileName
, bool doCheck
= false);
148 GmlParser(istream
&is
, bool doCheck
= false);
150 // destruction: destroys object tree
153 // returns id of object
154 int id(GmlObject
*object
) const { return object
->m_key
->info(); }
156 // true <=> an error in GML files has been detected
157 bool error() const { return m_error
; }
158 // returns error message
159 const String
&errorString() const { return m_errorString
; }
161 // creates graph from GML parse tree
163 // creates attributed graph from GML parse tree
164 bool read(Graph
&G
, GraphAttributes
&AG
);
165 //creates clustergraph from GML parse tree
166 //bool read(Graph &G, ClusterGraph & CG);
167 //read only cluster part of object tree and create cluster graph structure
168 bool readCluster(Graph
&G
, ClusterGraph
& CG
);
169 //the same with attributes
170 bool readAttributedCluster(
173 ClusterGraphAttributes
& ACG
);
177 //read all cluster tree information
179 GmlObject
* rootCluster
,
183 bool attributedClusterRead(
184 GmlObject
* rootCluster
,
186 ClusterGraphAttributes
& ACG
);
188 //recursively read cluster subtree information
189 bool recursiveClusterRead(
190 GmlObject
* clusterObject
,
194 bool recursiveAttributedClusterRead(
195 GmlObject
* clusterObject
,
197 ClusterGraphAttributes
& ACG
,
200 bool readClusterAttributes(
201 GmlObject
* cGraphics
,
203 ClusterGraphAttributes
& ACG
);
206 void doInit(istream
&is
, bool doCheck
);
207 void createObjectTree(istream
&is
, bool doCheck
);
208 void initPredefinedKeys();
209 void setError(const char *errorString
);
211 GmlObject
*parseList(GmlObjectType closingKey
, GmlObjectType errorKey
);
212 GmlObjectType
getNextSymbol();
215 GmlKey
hashString(const String
&str
);
217 GmlObject
*getNodeIdRange(int &minId
,int &maxId
);
218 void readLineAttribute(GmlObject
*object
, DPolyline
&dpl
);
220 void destroyObjectList(GmlObject
*object
);
222 void indent(ostream
&os
, int d
);
223 void output(ostream
&os
, GmlObject
*object
, int d
);
228 } // end namespace ogdf