--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.impl;\r
+\r
+import java.io.DataOutput;\r
+import java.io.DataOutputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+public class GraphPrinter {\r
+\r
+ public static String LOG_FILE = "d:\\graph.dot";\r
+ \r
+ public static boolean LOG = true;\r
+\r
+ static Object loggerCreationLock = new Object();\r
+ static GraphPrinter logger = null; \r
+\r
+ DataOutput log;\r
+\r
+ public GraphPrinter() {\r
+ if(LOG) {\r
+ try {\r
+ FileOutputStream stream = new FileOutputStream(LOG_FILE);\r
+ log = new DataOutputStream(stream);\r
+ } catch (FileNotFoundException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+\r
+ public static GraphPrinter getInstance() {\r
+ if(logger == null) {\r
+ synchronized (loggerCreationLock) {\r
+ if(logger == null)\r
+ logger = new GraphPrinter(); \r
+ } \r
+ }\r
+ return logger;\r
+ }\r
+\r
+ public void begin(String filename) {\r
+ if(LOG) {\r
+ try {\r
+ FileOutputStream stream = new FileOutputStream(LOG_FILE);\r
+ log = new DataOutputStream(stream);\r
+ try {\r
+ synchronized(log) {\r
+ log.writeBytes("digraph test {\n");\r
+ }\r
+ } catch(IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ } catch (FileNotFoundException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+\r
+ public void finish() {\r
+ if(LOG) {\r
+ try {\r
+ synchronized(log) {\r
+ log.writeBytes("}\n");\r
+ }\r
+ } catch(IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ log = null;\r
+ }\r
+ }\r
+\r
+ private String escape(String input) {\r
+ return input.replace("[", "_").replace("]", "_").replace(":", "_").replace("/", "_").replace("@", "_").replace(".", "_").replace(" ", "_").replace("#", "_").replace("-", "_");\r
+ }\r
+ \r
+ public void log(String start, String end) {\r
+\r
+ try {\r
+ synchronized(log) {\r
+ log.writeBytes(escape(start.toString()));\r
+ log.writeBytes(" -> ");\r
+ log.writeBytes(escape(end.toString()));\r
+ log.writeBytes("\n");\r
+ }\r
+ } catch(IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ }\r
+ \r
+}\r