GraphMLReader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #ifndef GRAPHMLREADER_H
  2. #define GRAPHMLREADER_H
  3. #include <qxml.h>
  4. #include <QXmlSchemaValidator>
  5. #include <QXmlSchema>
  6. #include <GraphLayoutLibrary_global.h>
  7. #include <ReaderWriterUtilities/AttributeConstants.h>
  8. #include <Common/BoostGraphWrapper.h>
  9. #include "dirtynode.h"
  10. #include "dirtyedge.h"
  11. #include "MessageHandler.h"
  12. #include <LayoutException/LayoutFileIOException.h>
  13. /** @name Datatype for key
  14. * datatype for key
  15. */
  16. typedef enum Keytype
  17. {
  18. INTIGER_TYPE = 0,
  19. DOUBLE_TYPE,
  20. BOOLEAN_TYPE,
  21. QSTRING_TYPE
  22. }KeyType;
  23. /** @brief Datatype for information about key
  24. */
  25. typedef struct KeyInfo
  26. {
  27. QString sKeyId;
  28. KeyType keyType;
  29. QString sKeyName;
  30. QString sDomain;
  31. QString sDefaultValue;
  32. }KeyInfo;
  33. /** @brief Datatype for information about data
  34. */
  35. typedef struct DataInfo
  36. {
  37. QString sKeyId;
  38. QString sKeyValue;
  39. QString sDomainID;
  40. QString sDomain;
  41. }DataInfo;
  42. /** @brief Datatype for storing vertex and edge properties
  43. */
  44. typedef struct EntityProperty
  45. {
  46. int iCoordX;
  47. int iCoordY;
  48. int iHeight;
  49. int iWidth;
  50. bool bIsBidirectional;
  51. EntityProperty()
  52. {
  53. iCoordX = 0;
  54. iCoordY = 0;
  55. iHeight = 0;
  56. iWidth = 0;
  57. bIsBidirectional = false;
  58. }
  59. }EntityProperty;
  60. /** @brief Datatype for storing graph properties
  61. */
  62. typedef struct GraphProperty
  63. {
  64. int iCoordX;
  65. int iCoordY;
  66. int iHeight;
  67. int iWidth;
  68. GraphProperty()
  69. {
  70. iCoordX = 0;
  71. iCoordY = 0;
  72. iHeight = 0;
  73. iWidth = 0;
  74. }
  75. }GraphProperty;
  76. /** @brief Map of vertices to their IDs
  77. */
  78. typedef QMap<QString , VertexDescriptor> VertexIdToVertexDescriptor;
  79. /** @brief Map of vertices to their Dummy nodes
  80. */
  81. typedef QMap<VertexDescriptor,bool> VertexDescriptorToIsDummyNode;
  82. /** @brief Map of key-information to IDs of their keys
  83. */
  84. typedef QMap<QString , KeyInfo> mapKeyIdToKeyInfo;
  85. /**
  86. Class GraphMLReader :- It is about implementing Graphml reader's functionality using default handler
  87. provided by xml parsing in qt with SAX approach.
  88. Added 'GRAPHLAYOUTLIBRARYSHARED_EXPORT' before class name inorder to get access to Client.
  89. This ensures that the right macro is seen by both library and clients.
  90. */
  91. class GRAPHLAYOUTLIBRARYSHARED_EXPORT GraphMLReader : public QXmlDefaultHandler
  92. {
  93. private:
  94. // Counter variables used to maintain state within handler functions
  95. unsigned int m_iNodeStartAt; /*!< Unsigned integer to store index of first node. Counter variable used to maintain state within handler functions. */
  96. unsigned int m_iNodeEndAt; /*!< Unsigned integer to store index of last node. Counter variable used to maintain state within handler functions. */
  97. unsigned int m_iGraphStartAt; /*!< Unsigned integer to store index of first graph. Counter variable used to maintain state within handler functions. */
  98. unsigned int m_iGraphEndAt; /*!< Unsigned integer to store index of last graph. Counter variable used to maintain state within handler functions. */
  99. unsigned int m_iNode_GraphFoundCounter; /*!< Unsigned integer to store count of graphs. Counter variable used to maintain state within handler functions. */
  100. DirtyNode m_dirtyNode; /*!< DirtyNode to store the node properties */
  101. DirtyEdge m_dirtyEdge; /*!< DirtyEdge to store the edge properties */
  102. VertexIdToVertexDescriptor mapVertexIdToVertexDescriptorObj; /*!< map of node id to node descriptor object */
  103. VertexDescriptorToIsDummyNode mapVertexDescriptorToIsDummyNode; /*!< map of whether the node is dummy or not */
  104. mapKeyIdToKeyInfo m_mapKeyIdToKeyInfo; /*!< map of key id to key info */
  105. /* data variables to store the information for the various tags as a domain
  106. *and its information as a domain information.*/
  107. QString m_sCurrentText; /*!< String to store a tag as text*/
  108. QString m_sCurrentKeyId; /*!< String to store id*/
  109. QString m_sCurrentDomainId; /*!< String to store tag-id*/
  110. QString m_sCurrentDomain; /*!< String to store tag*/
  111. QString m_sDefaultValue; /*!< String to store default value of a tag*/
  112. // data variables to store the information about the key maping properties to various entities.
  113. vector<KeyInfo> m_vecKeyInfo; /*!< Vector of KeyInfo to store list of keys*/
  114. int m_iCurrentKeyValue; /*!< Integer to store numeric keys value*/
  115. bool m_bCurrentKeyValue; /*!< Boolean to store true/false keys value*/
  116. double m_dCurrentKeyValue; /*!< Double to store decimal keys value*/
  117. QString m_sCurrentKeyValue; /*!< String to store keys value read from file*/
  118. // Structue for storing data values
  119. EntityProperty structEntityProperty; /*!< EntityProperty to store properties of an entity */
  120. GraphProperty structGraphProperty; /*!< GraphProperty to store properties of a graph */
  121. KeyInfo structKeyInfo; /*!< Integer to store information of key */
  122. // Error
  123. QString m_sErrorText; /*!< String to store error */
  124. bool m_bGraphMLTag; /*!< Boolean to track start/end of graphML tag */
  125. // for graph after node bounding in single pass parser
  126. bool m_bGraphTag; /*!< Boolean to track start/end of graph tag */
  127. // for the bend points
  128. // edge flag
  129. bool m_bEdgeTag; /*!< Boolean to track start/end of edge tag */
  130. bool m_bEdgeDataTag; /*!< Boolean to track start/end of edge-data tag */
  131. // data values validation(using flags).
  132. // for graph
  133. bool m_bIsGraphCoordXSet; /*!< Boolean to track whether valid value for x-coordinte of graph is present */
  134. bool m_bIsGraphCoordYSet; /*!< Boolean to track whether valid value for y-coordinte of graph is present */
  135. bool m_bIsGraphHeightSet; /*!< Boolean to track whether valid value for height of graph is present */
  136. bool m_bIsGraphWidthSet; /*!< Boolean to track whether valid value for width of graph is present */
  137. // for nodes
  138. bool m_bIsNodeCoordXSet; /*!< Boolean to track whether valid value for x-coordinte of nodes is present */
  139. bool m_bIsNodeCoordYSet; /*!< Boolean to track whether valid value for y-coordinte of nodes is present */
  140. bool m_bIsNodeHeightSet; /*!< Boolean to track whether valid value for height of nodes is present */
  141. bool m_bIsNodeWidthSet; /*!< Boolean to track whether valid value for width of nodes is present */
  142. // for edge
  143. bool m_bIsEdgeDirectionSet; /*!< Boolean to track whether valid direction of edges is present */
  144. // Private Helper Functions
  145. /** @name Helper
  146. * The methods under this section are helper functions of GraphMLReader.
  147. */
  148. /**
  149. This method adds a vertex to the current graph.
  150. @pre sVertexId != EMPTY_STRING_VALUE
  151. @pre mapVertexIdToVertexDescriptor.contains(sVertexId) != true
  152. @param sVertexId
  153. @return none
  154. @throw none
  155. */
  156. void addVertexToSubgraph(QString sVertexId);
  157. /**
  158. This method adds a edge to the main graph.
  159. @pre sSourceVertexId != EMPTY_STRING_VALUE
  160. @pre sTargetVertexId != EMPTY_STRING_VALUE
  161. @param sSourceVertexId
  162. @param sTargetVertexId
  163. @param sEdgeId
  164. @return none
  165. @throw MemoryException
  166. -# NULL_POINTER_EXCEPTION if referenced entity is null
  167. @throw LayoutException
  168. -# INVALID_PARAMETER if unknown parameter is passed
  169. */
  170. void addEdgeToMainGraph(QString sSourceVertexId, QString sTargetVertexId,QString sEdgeId);
  171. /**
  172. This method calls QXmlDefaultHandler's handler to start processing of graphML tags
  173. @pre none
  174. @param sNamespaceURI
  175. @param sTagElement
  176. @param sName
  177. @param attribute
  178. @return true if processed elements else false
  179. @throw MemoryException
  180. -# NULL_POINTER_EXCEPTION if referenced entity is null
  181. @throw LayoutException
  182. -# INVALID_PARAMETER if unknown parameter is passed
  183. -# INVALID_ATTRIBUTE_VALUE if invalid attribute value is returned.
  184. @throw FileIOException
  185. -# INVALID_FILE_FORMAT if other file than graphml is imported
  186. */
  187. bool startElement( const QString&, const QString&, const QString&,
  188. const QXmlAttributes& attribute);
  189. /**
  190. This method gets the text values.
  191. @pre none
  192. @param sCurrentString
  193. @return true if processed text value else false
  194. @throw none
  195. */
  196. bool characters(const QString &sCurrentString);
  197. /**
  198. This method calls QXmlDefaultHandler's handler to start coresponding element from startElement of graphML tags
  199. @pre none
  200. @param sNamespaceURI
  201. @param sTagElement
  202. @param sName
  203. @return true if processed element else false
  204. @throw MemoryException
  205. -# NULL_POINTER_EXCEPTION if referenced entity is null
  206. @throw LayoutException
  207. -# INVALID_PARAMETER if unknown parameter is passed
  208. -# INVALID_ATTRIBUTE_VALUE if invalid attribute value is returned.
  209. @throw FileIOException
  210. -# INVALID_FILE_FORMAT if other file than graphml is imported
  211. */
  212. bool endElement( const QString&, const QString&, const QString&);
  213. /**
  214. This method gets the error text value
  215. @pre errorText should be already written.
  216. @return error string value
  217. @throw none
  218. */
  219. QString errorString() const;
  220. /**
  221. This method converts the text values into integer value.
  222. @pre sKeyValue != empty
  223. @param sKeyValue
  224. @return none
  225. @throw none
  226. */
  227. void convertTextToInt(QString sKeyValue);
  228. /**
  229. This method converts the text values into boolean value.
  230. @pre sKeyValue != empty
  231. @param sKeyValue
  232. @return none
  233. @throw none
  234. */
  235. void convertTextToBool(QString sKeyValue);
  236. /**
  237. This method converts the text values into double value.
  238. @pre sKeyValue != empty
  239. @param sKeyValue
  240. @return none
  241. @throw none
  242. */
  243. void convertTextToDouble(QString sKeyValue);
  244. /**
  245. this method checks whether the input graphML file is valid or not.
  246. * @brief validateGraphML
  247. *
  248. * @pre sGraphMLInstanceFileName != empty
  249. *
  250. * @param sGraphMLInstanceFileName
  251. *
  252. * @return none
  253. *
  254. * @throw none
  255. */
  256. void validateGraphML(QString sGraphMLInstanceFileName);
  257. /**
  258. *this method returns whether the file is supported by library or not.
  259. *@pre !sGraphMLFileName.empty()
  260. *
  261. *@param file name in string
  262. *
  263. *@return true if file supported otherwise false.
  264. *
  265. *@throw none
  266. */
  267. bool isSupportedGraphmlFile(QString sGraphMLFileName);
  268. public:
  269. /** @name Creators
  270. * The methods under this section are responsible for constructing or
  271. * destructing an instance of type LayoutException.
  272. */
  273. //@{
  274. /**
  275. Constructs new object of GraphMLReader.
  276. @pre none
  277. @param none
  278. @return none
  279. @throw none
  280. */
  281. GraphMLReader();
  282. /**
  283. Destroys object of GraphMLReader.
  284. @pre none
  285. @param none
  286. @return none
  287. @throw none
  288. */
  289. ~GraphMLReader();
  290. //@}
  291. /** @name Modifiers
  292. * The methods under this section are responsible for modifying
  293. * an instance of GraphMLReader.
  294. */
  295. //@{
  296. /**
  297. *this method returns the graph object which stores the graph contents with subgraph hierarchy.
  298. *@pre file != NULL
  299. *
  300. *@param pointer to file
  301. *
  302. *@return subgraph reference
  303. *
  304. *@throw none
  305. */
  306. SubGraph& readGraphML(QFile *file);
  307. //@}
  308. };
  309. #endif // XMLSAXHANDLER_H