]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/GraphPrinter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / GraphPrinter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.impl;\r
13 \r
14 import java.io.DataOutput;\r
15 import java.io.DataOutputStream;\r
16 import java.io.FileNotFoundException;\r
17 import java.io.FileOutputStream;\r
18 import java.io.IOException;\r
19 \r
20 public class GraphPrinter {\r
21 \r
22     public static String LOG_FILE = "d:\\graph.dot";\r
23     \r
24     public static boolean LOG = true;\r
25 \r
26     static Object loggerCreationLock = new Object();\r
27     static GraphPrinter logger = null; \r
28 \r
29     DataOutput log;\r
30 \r
31     public GraphPrinter() {\r
32         if(LOG) {\r
33             try {\r
34                 FileOutputStream stream = new FileOutputStream(LOG_FILE);\r
35                 log = new DataOutputStream(stream);\r
36             } catch (FileNotFoundException e) {\r
37                 e.printStackTrace();\r
38             }\r
39         }\r
40     }\r
41 \r
42     public static GraphPrinter getInstance() {\r
43         if(logger == null) {\r
44             synchronized (loggerCreationLock) {\r
45                 if(logger == null)\r
46                     logger = new GraphPrinter();        \r
47             }                    \r
48         }\r
49         return logger;\r
50     }\r
51 \r
52     public void begin(String filename) {\r
53         if(LOG) {\r
54             try {\r
55                 FileOutputStream stream = new FileOutputStream(LOG_FILE);\r
56                 log = new DataOutputStream(stream);\r
57                 try {\r
58                     synchronized(log) {\r
59                         log.writeBytes("digraph test {\n");\r
60                     }\r
61                 } catch(IOException e) {\r
62                     e.printStackTrace();\r
63                 }\r
64             } catch (FileNotFoundException e) {\r
65                 e.printStackTrace();\r
66             }\r
67         }\r
68     }\r
69 \r
70     public void finish() {\r
71         if(LOG) {\r
72             try {\r
73                 synchronized(log) {\r
74                     log.writeBytes("}\n");\r
75                 }\r
76             } catch(IOException e) {\r
77                 e.printStackTrace();\r
78             }\r
79             log = null;\r
80         }\r
81     }\r
82 \r
83     private String escape(String input) {\r
84         return input.replace("[", "_").replace("]", "_").replace(":", "_").replace("/", "_").replace("@", "_").replace(".", "_").replace(" ", "_").replace("#", "_").replace("-", "_");\r
85     }\r
86     \r
87     public void log(String start, String end) {\r
88 \r
89         try {\r
90             synchronized(log) {\r
91                 log.writeBytes(escape(start.toString()));\r
92                 log.writeBytes(" -> ");\r
93                 log.writeBytes(escape(end.toString()));\r
94                 log.writeBytes("\n");\r
95             }\r
96         } catch(IOException e) {\r
97             e.printStackTrace();\r
98         }\r
99         \r
100     }\r
101     \r
102 }\r