MyFileSinkGraphML.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package de.tu_darmstadt.informatik.tk.scopviz.io;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import org.graphstream.graph.Graph;
  7. import org.graphstream.stream.file.FileSinkGraphML;
  8. import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
  9. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyEdge;
  10. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
  11. import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyNode;
  12. public class MyFileSinkGraphML extends FileSinkGraphML {
  13. public boolean isWritingMultigraph = false;
  14. private void print(String format, Object... args) throws IOException {
  15. output.write(String.format(format, args));
  16. }
  17. @Override
  18. protected void exportGraph(Graph g) {
  19. try {
  20. int attribute = 0;
  21. HashMap<String, String> nodeAttributes = new HashMap<String, String>();
  22. HashMap<String, String> edgeAttributes = new HashMap<String, String>();
  23. HashMap<String, String> graphAttributes = new HashMap<String, String>();
  24. Debug.out(g.getAttributeCount());
  25. for (String j : g.getAttributeKeySet()) {
  26. if (!graphAttributes.containsKey(j)) {
  27. Object gValue = g.getAttribute(j);
  28. String gType;
  29. if (gValue == null)
  30. continue;
  31. String gId = String.format("attr%04X", attribute++);
  32. if (gValue instanceof Boolean)
  33. gType = "boolean";
  34. else if (gValue instanceof Long)
  35. gType = "long";
  36. else if (gValue instanceof Integer)
  37. gType = "int";
  38. else if (gValue instanceof Double)
  39. gType = "double";
  40. else if (gValue instanceof Float)
  41. gType = "float";
  42. else
  43. gType = "string";
  44. graphAttributes.put(j, gId);
  45. print("\t<key id=\"%s\" for=\"graph\" attr.name=\"%s\" attr.type=\"%s\"/>\n", gId,
  46. escapeXmlString(j), gType);
  47. }
  48. }
  49. for (MyNode n : g.<MyNode>getEachNode()) {
  50. for (String k : n.getAttributeKeySet()) {
  51. // AttributeFiltering
  52. if (k.equals("ui.j2dsk") || k.equals("ui.class") || k.equals("ui.pie-values")) {
  53. continue;
  54. }
  55. Class<? extends Object> c = n.getAttribute(k).getClass();
  56. if (!c.isPrimitive() && !(c == String.class) && !(c == Character.class) && !(c == Boolean.class)
  57. && !(c == Integer.class) && !(c == Long.class) && !(c == Short.class) && !(c == Byte.class)
  58. && !(c == Float.class) && !(c == Double.class)) {
  59. Debug.out("Could not parse an Attribute because it is not Primitive or a String \n\t"
  60. + "(Attribute: " + k + ", Value: " + n.getAttribute(k) + ", from Node: " + n
  61. + ", Type: " + c + ") ", 2);
  62. continue;
  63. }
  64. if (!nodeAttributes.containsKey(k)) {
  65. Object value = n.getAttribute(k);
  66. String type;
  67. if (value == null)
  68. continue;
  69. String id = String.format("attr%04X", attribute++);
  70. if (value instanceof Boolean)
  71. type = "boolean";
  72. else if (value instanceof Long)
  73. type = "long";
  74. else if (value instanceof Integer)
  75. type = "int";
  76. else if (value instanceof Double)
  77. type = "double";
  78. else if (value instanceof Float)
  79. type = "float";
  80. else
  81. type = "string";
  82. nodeAttributes.put(k, id);
  83. print("\t<key id=\"%s\" for=\"node\" attr.name=\"%s\" attr.type=\"%s\"/>\n", id,
  84. escapeXmlString(k), type);
  85. }
  86. }
  87. }
  88. for (MyEdge n : g.<MyEdge>getEachEdge()) {
  89. for (String k : n.getAttributeKeySet()) {
  90. // AttributeFiltering
  91. if (k.equals("ui.j2dsk")) {
  92. continue;
  93. }
  94. Class<? extends Object> c = n.getAttribute(k).getClass();
  95. if (!c.isPrimitive() && !(c == String.class) && !(c == Character.class) && !(c == Boolean.class)
  96. && !(c == Integer.class) && !(c == Long.class) && !(c == Short.class) && !(c == Byte.class)
  97. && !(c == Float.class) && !(c == Double.class)) {
  98. Debug.out("Could not parse an Attribute because it is not Primitive or a String \n\t"
  99. + "(Attribute: " + k + ", Value: " + n.getAttribute(k) + ", from Edge: " + n
  100. + ", Type: " + c + ") ", 2);
  101. continue;
  102. }
  103. if (!edgeAttributes.containsKey(k)) {
  104. Object value = n.getAttribute(k);
  105. String type;
  106. if (value == null)
  107. continue;
  108. String id = String.format("attr%04X", attribute++);
  109. if (value instanceof Boolean)
  110. type = "boolean";
  111. else if (value instanceof Long)
  112. type = "long";
  113. else if (value instanceof Integer)
  114. type = "int";
  115. else if (value instanceof Double)
  116. type = "double";
  117. else if (value instanceof Float)
  118. type = "float";
  119. else
  120. type = "string";
  121. edgeAttributes.put(k, id);
  122. print("\t<key id=\"%s\" for=\"edge\" attr.name=\"%s\" attr.type=\"%s\"/>\n", id,
  123. escapeXmlString(k), type);
  124. }
  125. }
  126. }
  127. print("\t<graph id=\"%s\" edgedefault=\"undirected\">\n", escapeXmlString(g.getId()));
  128. for (String k : g.getAttributeKeySet()) {
  129. print("\t\t\t<data key=\"%s\">%s</data>\n", graphAttributes.get(k),
  130. escapeXmlString(g.getAttribute(k).toString()));
  131. }
  132. for (MyNode n : g.<MyNode>getEachNode()) {
  133. print("\t\t<node id=\"%s\">\n", n.getId());
  134. for (String k : n.getAttributeKeySet()) {
  135. if (k.equals("ui.j2dsk") || k.equals("ui.class") || k.equals("ui.pie-values")) {
  136. continue;
  137. }
  138. Class<? extends Object> c = n.getAttribute(k).getClass();
  139. if (!c.isPrimitive() && !(c == String.class) && !(c == Character.class) && !(c == Boolean.class)
  140. && !(c == Integer.class) && !(c == Long.class) && !(c == Short.class) && !(c == Byte.class)
  141. && !(c == Float.class) && !(c == Double.class)) {
  142. continue;
  143. }
  144. print("\t\t\t<data key=\"%s\">%s</data>\n", nodeAttributes.get(k),
  145. escapeXmlString(n.getAttribute(k).toString()));
  146. }
  147. print("\t\t</node>\n");
  148. }
  149. for (MyEdge e : g.<MyEdge>getEachEdge()) {
  150. print("\t\t<edge id=\"%s\" source=\"%s\" target=\"%s\" directed=\"%s\">\n", e.getId(),
  151. e.getSourceNode().getId(), e.getTargetNode().getId(), e.isDirected());
  152. for (String k : e.getAttributeKeySet()) {
  153. if (k.equals("ui.j2dsk") || k.equals("ui.class") || k.equals("ui.pie-values")) {
  154. continue;
  155. }
  156. Class<? extends Object> c = e.getAttribute(k).getClass();
  157. if (!c.isPrimitive() && !(c == String.class) && !(c == Character.class) && !(c == Boolean.class)
  158. && !(c == Integer.class) && !(c == Long.class) && !(c == Short.class) && !(c == Byte.class)
  159. && !(c == Float.class) && !(c == Double.class)) {
  160. continue;
  161. }
  162. print("\t\t\t<data key=\"%s\">%s</data>\n", edgeAttributes.get(k),
  163. escapeXmlString(e.getAttribute(k).toString()));
  164. }
  165. print("\t\t</edge>\n");
  166. }
  167. print("\t</graph>\n");
  168. } catch (IOException e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. private static String escapeXmlString(String s) {
  173. // why do you make me do this graphstream???
  174. return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;").replace("'",
  175. "&apos;");
  176. }
  177. /**
  178. *
  179. * @param graphs
  180. */
  181. public void exportGraphs(LinkedList<MyGraph> graphs, String fileName) {
  182. Iterator<MyGraph> graphIter = graphs.iterator();
  183. while (graphIter.hasNext()) {
  184. if (graphIter.next().isComposite()) {
  185. graphIter.remove();
  186. }
  187. }
  188. try {
  189. begin(fileName);
  190. isWritingMultigraph = true;
  191. for (MyGraph g : graphs) {
  192. exportGraph(g);
  193. }
  194. isWritingMultigraph = false;
  195. end();
  196. } catch (IOException e) {
  197. e.printStackTrace();
  198. }
  199. }
  200. }