Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added -template parameter. #18

Merged
merged 5 commits into from Dec 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/main/java/org/schemaspy/Config.java
Expand Up @@ -85,6 +85,7 @@ public class Config
private Integer port;
private String server;
private String meta;
private String templateDirectory;
private Pattern tableInclusions;
private Pattern tableExclusions;
private Pattern columnExclusions;
Expand Down Expand Up @@ -280,6 +281,16 @@ public String getMeta() {
return meta;
}

public String getTemplateDirectory() {
if (templateDirectory == null)
templateDirectory = pullParam("-template");
if (templateDirectory == null)
// default template dir = resources/layout/
templateDirectory = getClass().getResource("/layout").getPath();

return templateDirectory;
}

public void setDbType(String dbType) {
this.dbType = dbType;
}
Expand Down Expand Up @@ -582,7 +593,7 @@ public String getCss() {
if (css == null) {
css = pullParam("-css");
if (css == null)
css = "layout/schemaSpy.css";
css = "schemaSpy.css";
}
return css;
}
Expand Down Expand Up @@ -1839,6 +1850,12 @@ public List<String> asList() throws IOException {
params.add("-meta");
params.add(value);
}

value = getTemplateDirectory();
if (value != null) {
params.add("-template");
params.add(value);
}
if (getGraphvizDir() != null) {
params.add("-gv");
params.add(getGraphvizDir().toString());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/schemaspy/view/HtmlAnomaliesPage.java
Expand Up @@ -72,7 +72,7 @@ public void write(Database database, Collection<Table> tables, List<? extends Fo
scopes.put("uniqueNullables", uniqueNullables);

MustacheWriter mw = new MustacheWriter( outputDir, scopes, getPathToRoot(), database.getName(), false);
mw.write("layout/anomalies.html", "anomalies.html", "anomalies.js");
mw.write("anomalies.html", "anomalies.html", "anomalies.js");
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/schemaspy/view/HtmlColumnsPage.java
Expand Up @@ -147,7 +147,7 @@ public void write(Database database, Collection<Table> tables, ColumnInfo column
scopes.put("columns", tableColumns);

MustacheWriter mw = new MustacheWriter(outputDir, scopes, getPathToRoot(), database.getName(), false);
mw.write("layout/column.html", "columns.html", "column.js");
mw.write("column.html", "columns.html", "column.js");
}

public void writeMainTableHeader(boolean hasTableIds, ColumnInfo selectedColumn, LineWriter out) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/schemaspy/view/HtmlComponentPage.java
Expand Up @@ -65,7 +65,7 @@ public void write(Database database, Collection<Table> tables, File outputDir) t
scopes.put("database", database);

MustacheWriter mw = new MustacheWriter(outputDir, scopes, "", database.getName(), false);
mw.write("layout/components.html", "components.html", "components.js");
mw.write("components.html", "components.html", "components.js");
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/schemaspy/view/HtmlConstraintsPage.java
Expand Up @@ -60,7 +60,7 @@ public void write(Database database, List<ForeignKeyConstraint> constraints, Col
scopes.put("tables", tables);

MustacheWriter mw = new MustacheWriter( outputDir, scopes, getPathToRoot(), database.getName(), false);
mw.write("layout/constraint.html", "constraints.html", "constraint.js");
mw.write("constraint.html", "constraints.html", "constraint.js");
}

private void writeHeader(Database database, LineWriter html) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/schemaspy/view/HtmlMainIndexPage.java
Expand Up @@ -79,7 +79,7 @@ public int compare(Table table1, Table table2) {
scopes.put("databaseName", databaseName);

MustacheWriter mw = new MustacheWriter(outputDir, scopes, "", database.getName(), false);
mw.write("layout/main.html", "index.html", "main.js");
mw.write("main.html", "index.html", "main.js");
}

private String getDatabaseDescription(Database db) {
Expand Down
Expand Up @@ -78,7 +78,7 @@ public void write(File outputDir, String dbName, List<String> populatedSchemas,


MustacheWriter mw = new MustacheWriter(outputDir, scopes, "", dbName, true);
mw.write("layout/multi.html", "index.html", "");
mw.write("multi.html", "index.html", "");
}

// private void writeHeader(String databaseName, DatabaseMetaData meta4, int numberOfSchemas, boolean showIds9, String aSchema, LineWriter html) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/schemaspy/view/HtmlOrphansPage.java
Expand Up @@ -112,7 +112,7 @@ public int compare(Object t1, Object t2) {
scopes.put("maps", maps);

MustacheWriter mw = new MustacheWriter(outputDir, scopes, getPathToRoot(), db.getName(), false);
mw.write("layout/orphans.html", "orphans.html", "");
mw.write("orphans.html", "orphans.html", "");

return true;
} finally {
Expand Down
Expand Up @@ -122,7 +122,7 @@ public boolean write(Database db, File diagramDir, String dotBaseFilespec, boole
scopes.put("diagrams", diagrams);

MustacheWriter mw = new MustacheWriter(outputDir, scopes, getPathToRoot(), db.getName(), false);
mw.write("layout/relationships.html", "relationships.html", "relationships.js");
mw.write("relationships.html", "relationships.html", "relationships.js");
return true;
} catch (Dot.DotFailure dotFailure) {
System.err.println(dotFailure);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/schemaspy/view/HtmlTablePage.java
Expand Up @@ -111,7 +111,7 @@ public void writeMainTable(Database db, Table table, File outputDir, WriteStats
scopes.put("definitionExists", definitionExists(table));
System.out.println("Table -> "+table.getName());
MustacheWriter mw = new MustacheWriter(outputDir, scopes, getPathToRoot(), db.getName(), false);
mw.write("layout/tables/table.html", Markdown.pagePath(table.getName()), "table.js");
mw.write("tables/table.html", Markdown.pagePath(table.getName()), "table.js");
}


Expand Down
22 changes: 11 additions & 11 deletions src/main/java/org/schemaspy/view/MustacheWriter.java
Expand Up @@ -22,6 +22,7 @@ public class MustacheWriter {
private String rootPathtoHome;
private String databaseName;
private boolean isMultipleSchemas;
private String templateDirectory = Config.getInstance().getTemplateDirectory();

public MustacheWriter(File outputDir, HashMap<String, Object> scopes, String rootPath, String databaseName, boolean isMultipleSchemas) {
this.outputDir = outputDir;
Expand All @@ -46,11 +47,11 @@ public void write(String templatePath, String destination, String scriptFileName
FileUtils fileUtils = new FileUtils();

HashMap<String, Object> mainScope = new HashMap<String, Object>();
//URL containerTemplate = getClass().getResource("/layout/container.html");
//URL containerTemplate = getClass().getResource(Paths.get(templateDirectory,"container.html").toString());
// URL template = getClass().getResource(templatePath);

try {
Mustache mustache = mf.compile(getReader(templatePath),"template");
Mustache mustache = mf.compile(getReader(new File(templateDirectory,templatePath).toString()),"template");
mustache.execute(result, scopes).flush();

mainScope.put("databaseName", databaseName);
Expand All @@ -62,7 +63,7 @@ public void write(String templatePath, String destination, String scriptFileName
Version version = new Version();
mainScope.put("version", version.getVersion());

Mustache mustacheContent = contentMf.compile(getReader("layout/container.html"), "container");
Mustache mustacheContent = contentMf.compile(getReader(new File(templateDirectory,"container.html").toString()), "container");
mustacheContent.execute(content, mainScope).flush();

File destinationFile = new File(outputDir, destination);
Expand All @@ -74,14 +75,13 @@ public void write(String templatePath, String destination, String scriptFileName
}

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

InputStream cssStream = StyleSheet.class.getClassLoader().getResourceAsStream(fileName);
InputStream cssStream = null;
if (new File(fileName).exists()){
cssStream = new FileInputStream(fileName);
}else if (new File(System.getProperty("user.dir"), fileName).exists()){
cssStream = new FileInputStream(fileName);
}

if (cssStream == null)
throw new ParseException("Unable to find requested file: " + fileName);
String inputStream = IOUtils.toString(cssStream, "UTF-8").toString();
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/org/schemaspy/view/StyleSheet.java
Expand Up @@ -20,17 +20,21 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import org.apache.commons.io.IOUtils;
import org.schemaspy.Config;
import org.schemaspy.model.InvalidConfigurationException;
import org.schemaspy.util.LineWriter;
Expand Down Expand Up @@ -117,7 +121,7 @@ else if (id.equals("a:visited"))
public static StyleSheet getInstance() throws ParseException {
if (instance == null) {
try {
instance = new StyleSheet(new BufferedReader(getReader(Config.getInstance().getCss())));
instance = new StyleSheet(new BufferedReader(getReader(new File(Config.getInstance().getTemplateDirectory(),Config.getInstance().getCss()).toString())));
} catch (IOException exc) {
throw new ParseException(exc);
}
Expand All @@ -141,18 +145,17 @@ public static StyleSheet getInstance() throws ParseException {
* @throws IOException
*/
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);
InputStream cssStream = null;
if (new File(cssName).exists()){
cssStream = new FileInputStream(cssName);
}else if (new File(System.getProperty("user.dir"), cssName).exists()){
cssStream = new FileInputStream(cssName);
}

return new InputStreamReader(cssStream);
if (cssStream == null)
throw new ParseException("Unable to find requested file: " + cssName);
String inputStream = IOUtils.toString(cssStream, "UTF-8").toString();
return new StringReader(inputStream);
}

private Map<String, String> parseAttributes(String data) {
Expand Down Expand Up @@ -258,7 +261,7 @@ public static class MissingCssPropertyException extends InvalidConfigurationExce
* @param propName name of the missing property in that section
*/
public MissingCssPropertyException(String cssSection, String propName) {
super("Required property '" + propName + "' was not found for the definition of '" + cssSection + "' in " + Config.getInstance().getCss());
super("Required property '" + propName + "' was not found for the definition of '" + cssSection + "' in " + new File(Config.getInstance().getTemplateDirectory(),Config.getInstance().getCss()).toString());
}
}

Expand Down