MyFileSinkGraphML.java 7.6 KB

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