Skip to content

Commit

Permalink
Refactored the way StyleSheet gets initialized so explicit initializa…
Browse files Browse the repository at this point in the history
…tion isn't required (or missed).
  • Loading branch information
johncurrier committed Sep 18, 2008
1 parent 2dc51a4 commit 699d0f3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
3 changes: 3 additions & 0 deletions src/net/sourceforge/schemaspy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public static void main(String[] argv) throws Exception {
} catch (StyleSheet.MissingCssPropertyException badCss) {
System.err.println();
System.err.println(badCss.getClass().getSimpleName() + ": " + badCss.getMessage());
} catch (StyleSheet.ParseException badCss) {
System.err.println();
System.err.println(badCss.getClass().getSimpleName() + ": " + badCss.getMessage());
} catch (Exception exc) {
exc.printStackTrace();
}
Expand Down
23 changes: 0 additions & 23 deletions src/net/sourceforge/schemaspy/SchemaAnalyzer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package net.sourceforge.schemaspy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
Expand Down Expand Up @@ -135,9 +130,6 @@ public int analyze(Config config) throws Exception {
yankParam(args, "-s"); // param will be replaced by something appropriate
args.remove("-all"); // param will be replaced by something appropriate

if (config.isHtmlGenerationEnabled())
StyleSheet.init(new BufferedReader(getStyleSheet(config.getCss())));

String schemaSpec = config.getSchemaSpec();
if (schemaSpec == null)
schemaSpec = properties.getProperty("schemaSpec", ".*");
Expand All @@ -153,7 +145,6 @@ public int analyze(Config config) throws Exception {
if (config.isHtmlGenerationEnabled()) {
new File(outputDir, "tables").mkdirs();
new File(outputDir, "diagrams/summary").mkdirs();
StyleSheet.init(new BufferedReader(getStyleSheet(config.getCss())));

System.out.println("Connected to " + meta.getDatabaseProductName() + " - " + meta.getDatabaseProductVersion());
if (schemaMeta != null && schemaMeta.getFile() != null) {
Expand Down Expand Up @@ -572,18 +563,4 @@ private static void yankParam(List<String> args, String paramId) {
args.remove(paramIndex);
}
}

private static Reader getStyleSheet(String cssName) throws IOException {
File cssFile = new File(cssName);
if (cssFile.exists())
return new FileReader(cssFile);
cssFile = new File(System.getProperty("user.dir"), cssName);
if (cssFile.exists())
return new FileReader(cssFile);

InputStream cssStream = StyleSheet.class.getClassLoader().getResourceAsStream(cssName);
if (cssStream == null)
throw new IllegalStateException("Unable to find requested style sheet: " + cssName);
return new InputStreamReader(cssStream);
}
}
54 changes: 42 additions & 12 deletions src/net/sourceforge/schemaspy/view/StyleSheet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package net.sourceforge.schemaspy.view;

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -76,12 +82,31 @@ else if (id.equals("a:visited"))
}
}

public static StyleSheet getInstance() {
public static StyleSheet getInstance() throws ParseException {
if (instance == null) {
try {
instance = new StyleSheet(new BufferedReader(getReader(Config.getInstance().getCss())));
} catch (IOException exc) {
throw new ParseException(exc);
}
}

return instance;
}

public static void init(BufferedReader cssReader) throws IOException {
instance = new StyleSheet(cssReader);

private static Reader getReader(String cssName) throws IOException {
File cssFile = new File(cssName);
if (cssFile.exists())
return new FileReader(cssFile);
cssFile = new File(System.getProperty("user.dir"), cssName);
if (cssFile.exists())
return new FileReader(cssFile);

InputStream cssStream = StyleSheet.class.getClassLoader().getResourceAsStream(cssName);
if (cssStream == null)
throw new ParseException("Unable to find requested style sheet: " + cssName);

return new InputStreamReader(cssStream);
}

private Map<String, String> parseAttributes(String data) {
Expand Down Expand Up @@ -170,18 +195,23 @@ public String getLinkVisitedColor() {
return linkVisitedColor;
}

public int getOffsetOf(String id) {
int offset = ids.indexOf(id.toLowerCase());
if (offset == -1)
throw new IllegalArgumentException(id);
return offset;
}

public class MissingCssPropertyException extends RuntimeException {
public static class MissingCssPropertyException extends RuntimeException {
private static final long serialVersionUID = 1L;

public MissingCssPropertyException(String cssSection, String propName) {
super("Required property '" + propName + "' was not found for the definition of '" + cssSection + "' in " + Config.getInstance().getCss());
}
}

public static class ParseException extends RuntimeException {
private static final long serialVersionUID = 1L;

public ParseException(Exception cause) {
super(cause);
}

public ParseException(String msg) {
super(msg);
}
}
}

0 comments on commit 699d0f3

Please sign in to comment.