View Javadoc
1   /*
2    * This file is a part of the SchemaSpy project (http://schemaspy.sourceforge.net).
3    * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 John Currier
4    *
5    * SchemaSpy is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * SchemaSpy is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package net.sourceforge.schemaspy.view;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import net.sourceforge.schemaspy.model.Table;
24  import net.sourceforge.schemaspy.util.Dot;
25  import net.sourceforge.schemaspy.util.LineWriter;
26  
27  public class HtmlTableDiagrammer extends HtmlDiagramFormatter {
28      private static HtmlTableDiagrammer instance = new HtmlTableDiagrammer();
29  
30      private HtmlTableDiagrammer() {
31      }
32  
33      public static HtmlTableDiagrammer getInstance() {
34          return instance;
35      }
36  
37      public boolean write(Table table, File diagramDir, LineWriter html) {
38          File oneDegreeDotFile = new File(diagramDir, table.getName() + ".1degree.dot");
39          File oneDegreeDiagramFile = new File(diagramDir, table.getName() + ".1degree.png");
40          File twoDegreesDotFile = new File(diagramDir, table.getName() + ".2degrees.dot");
41          File twoDegreesDiagramFile = new File(diagramDir, table.getName() + ".2degrees.png");
42          File impliedDotFile = new File(diagramDir, table.getName() + ".implied2degrees.dot");
43          File impliedDiagramFile = new File(diagramDir, table.getName() + ".implied2degrees.png");
44  
45          try {
46              Dot dot = getDot();
47              if (dot == null)
48                  return false;
49  
50              String map = dot.generateDiagram(oneDegreeDotFile, oneDegreeDiagramFile);
51  
52              html.write("<br><form action='get'><b>Close relationships");
53              if (twoDegreesDotFile.exists()) {
54                  html.writeln("</b><span class='degrees' id='degrees' title='Detail diminishes with increased separation from " + table.getName() + "'>");
55                  html.write("&nbsp;within <label for='oneDegree'><input type='radio' name='degrees' id='oneDegree' checked>one</label>");
56                  html.write("  <label for='twoDegrees'><input type='radio' name='degrees' id='twoDegrees'>two degrees</label> of separation");
57                  html.write("</span><b>:</b>");
58                  html.writeln("</form>");
59              } else {
60                  html.write(":</b></form>");
61              }
62              html.write(map);
63              map = null;
64              html.writeln("  <a name='diagram'><img id='oneDegreeImg' src='../diagrams/" + encodeHref( oneDegreeDiagramFile.getName() ) + "' usemap='#oneDegreeRelationshipsDiagram' class='diagram' border='0' alt='' align='left'></a>");
65  
66              if (impliedDotFile.exists()) {
67                  html.writeln(dot.generateDiagram(impliedDotFile, impliedDiagramFile));
68                  html.writeln("  <a name='diagram'><img id='impliedTwoDegreesImg' src='../diagrams/" + encodeHref( impliedDiagramFile.getName() ) + "' usemap='#impliedTwoDegreesRelationshipsDiagram' class='diagram' border='0' alt='' align='left'></a>");
69              } else {
70                  impliedDotFile.delete();
71                  impliedDiagramFile.delete();
72              }
73              if (twoDegreesDotFile.exists()) {
74                  html.writeln(dot.generateDiagram(twoDegreesDotFile, twoDegreesDiagramFile));
75                  html.writeln("  <a name='diagram'><img id='twoDegreesImg' src='../diagrams/" + encodeHref (twoDegreesDiagramFile.getName() ) + "' usemap='#twoDegreesRelationshipsDiagram' class='diagram' border='0' alt='' align='left'></a>");
76              } else {
77                  twoDegreesDotFile.delete();
78                  twoDegreesDiagramFile.delete();
79              }
80          } catch (Dot.DotFailure dotFailure) {
81              System.err.println(dotFailure);
82              return false;
83          } catch (IOException ioExc) {
84              ioExc.printStackTrace();
85              return false;
86          }
87  
88          return true;
89      }
90  }