1   /*
2    * PROJECT   : DAR Runtime and Tools
3    * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org
4    * LICENSE   : GNU LESSER GENERAL PUBLIC LICENSE
5    *             Version 2.1, February 1999
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package org.enableit.db.darrt.visualise;
22  
23  import javax.xml.parsers.DocumentBuilder;
24  import javax.xml.parsers.DocumentBuilderFactory;
25  import javax.xml.parsers.FactoryConfigurationError;
26  import javax.xml.parsers.ParserConfigurationException;
27  
28  import org.apache.log4j.Logger;
29  import org.enableit.db.beans.Column;
30  import org.enableit.db.beans.Table;
31  import org.enableit.db.darrt.AbstractSchemaHandler;
32  import org.w3c.dom.DOMImplementation;
33  import org.w3c.dom.Document;
34  import org.w3c.dom.DocumentFragment;
35  import org.w3c.dom.Element;
36  
37  
38  /***
39   * Visualize a DAR schema.
40   *
41   * Not thread-safe, if used in multi-threaded environment should be
42   * externally synchronized.
43   *
44   * @author $Author: tim $
45   */
46  public class SchemaVisualiser extends AbstractSchemaHandler {
47      /***
48       * The Log4J <code>Logger</code> doing the logging.
49       */
50      private static Logger logger = Logger.getLogger(SchemaVisualiser.class);
51  
52      /***
53       * Width of single text character.
54       */
55      private static final int fontWidth = 10;
56  
57      /***
58       * Height of text.
59       */
60      private static final int fontHeight = 12;
61  
62      /***
63       * Space before and after text lines.
64       */
65      private static final int textSpacing = 5;
66  
67      /***
68       * The SVG namespace for generated documents.
69       */
70      public static final String SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg";
71  
72      /***
73       * CVS info ABOUT this class and its current version
74       */
75      public static final String ABOUT = "$Id: SchemaVisualiser.java,v 1.6 2004/03/20 05:23:38 tim Exp $";
76  
77      /*
78       * Properties
79       */
80  
81      /***
82       * Max. width required by image.
83       */
84      private int maxX = 0;
85  
86      /***
87       * Y co-ord for next text line.
88       */
89      private int y = 0;
90  
91      /***
92       * The DOM implementation we are using.
93       */
94      private DOMImplementation impl;
95  
96      /***
97       * JAXP document builder to create new DOMs.
98       */
99      private DocumentBuilder docBuilder;
100 
101     /*
102      * Constructors
103      */
104 
105     /***
106      * Default Constructor
107      */
108     public SchemaVisualiser()
109         throws FactoryConfigurationError, ParserConfigurationException {
110         super();
111 
112         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
113 
114         docBuilder = factory.newDocumentBuilder();
115     }
116 
117     /*
118      * Methods
119      */
120 
121     /***
122      * Converts a Table object into an SVG representation.
123      * Note that the representation takes no account of the positioning of
124      * any other objects, just the one provided.
125      *
126      * @param table Table to create visualisation of.
127      */
128     public Document toSVG(Table table) {
129         logger.info("METHOD_ENTRY: toSVG");
130 
131         // Create a document to append elements to 
132         impl = docBuilder.getDOMImplementation();
133 
134         String svgNS = SVG_NAMESPACE_URI;
135         Document doc = impl.createDocument(svgNS, "svg", null);
136         Element svgRoot = doc.getDocumentElement();
137 
138         // Title element 
139         Element title = doc.createElementNS(svgNS, "title");
140 
141         title.appendChild(doc.createTextNode("Data diagram: " + table.getName()));
142         svgRoot.appendChild(title);
143 
144         // Add the table element
145         doc = toSVG(doc, table);
146 
147         // set the width and height attribute on the root svg element
148         svgRoot.setAttributeNS(null, "width",
149             String.valueOf((2 * textSpacing) + (maxX * fontWidth)));
150         svgRoot.setAttributeNS(null, "height", String.valueOf(y + textSpacing));
151 
152         //svgRoot.setAttributeNS(null, "viewBox", "0 0 450 500");
153         logger.info("METHOD_EXIT: toSVG");
154 
155         return doc;
156     }
157 
158     /***
159      * Converts a Table object into an SVG representation.
160      * Note that the representation takes no account of the positioning of
161      * any other objects, just the one provided.
162      *
163      * @param doc Pre-existing Document to append to.
164      * @param table Table to create visualisation of.
165      */
166     public Document toSVG(Document doc, Table table) {
167         logger.info("METHOD_ENTRY: toSVG");
168 
169         // Create a fragment to attach to
170         DocumentFragment fragment = doc.createDocumentFragment();
171 
172         // set up co-ordinate system (in pixels?) for now just relative to 0,0
173         int xOrigin = 0;
174         int yOrigin = 0;
175         int x = 0;
176 
177         maxX = 0;
178         y = 0;
179 
180         // Create table name text 
181         x += textSpacing;
182         y += (fontHeight + textSpacing);
183 
184         Element tableName = doc.createElementNS(SVG_NAMESPACE_URI, "text");
185 
186         tableName.setAttributeNS(null, "x", String.valueOf(x));
187         tableName.setAttributeNS(null, "y", String.valueOf(y));
188         maxX = table.getName().length();
189         tableName.appendChild(doc.createTextNode(table.getName()));
190         fragment.appendChild(tableName);
191 
192         // Create table attributes text 
193         y += textSpacing;
194 
195         for (int i = 0; i < table.getColumnCount(); i++) {
196             y += (fontHeight + textSpacing);
197 
198             Element colName = doc.createElementNS(SVG_NAMESPACE_URI, "text");
199 
200             colName.setAttributeNS(null, "x", String.valueOf(x));
201             colName.setAttributeNS(null, "y", String.valueOf(y));
202 
203             Column col = table.getColumn(i);
204             String colText = col.getColName() + " " + col.getColType();
205 
206             if (colText.length() > maxX) {
207                 maxX = colText.length();
208             }
209 
210             colName.appendChild(doc.createTextNode(colText));
211             fragment.appendChild(colName);
212         }
213 
214         // Create line to separate table name from attributes
215         y += (fontHeight + textSpacing);
216 
217         Element line = doc.createElementNS(SVG_NAMESPACE_URI, "line");
218 
219         line.setAttributeNS(null, "x1", String.valueOf(xOrigin));
220         line.setAttributeNS(null, "y1",
221             String.valueOf(yOrigin + fontHeight + (2 * textSpacing)));
222         line.setAttributeNS(null, "x2",
223             String.valueOf((2 * textSpacing) + (maxX * fontWidth)));
224         line.setAttributeNS(null, "y2",
225             String.valueOf(yOrigin + fontHeight + (2 * textSpacing)));
226         line.setAttributeNS(null, "stroke", "black");
227         fragment.appendChild(line);
228 
229         // Create rectangle for background of table
230         Element rectangle = doc.createElementNS(SVG_NAMESPACE_URI, "rect");
231 
232         rectangle.setAttributeNS(null, "x", String.valueOf(xOrigin));
233         rectangle.setAttributeNS(null, "y", String.valueOf(yOrigin));
234         rectangle.setAttributeNS(null, "width",
235             String.valueOf((2 * textSpacing) + (maxX * fontWidth)));
236         rectangle.setAttributeNS(null, "height", String.valueOf(y + textSpacing));
237         rectangle.setAttributeNS(null, "stroke", "black");
238         rectangle.setAttributeNS(null, "fill", "none");
239         fragment.appendChild(rectangle);
240 
241         // append to the doc root element
242         doc.getDocumentElement().appendChild(fragment);
243 
244         logger.info("METHOD_EXIT: toSVG");
245 
246         return doc;
247     }
248 }
This page was automatically generated by Maven