schemacrawler.tools.text.base.BaseFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of schemacrawler Show documentation
Show all versions of schemacrawler Show documentation
SchemaCrawler is an open-source Java API that makes working with database metadata as easy as working with plain old Java objects. SchemaCrawler is also a database schema discovery and comprehension, and schema documentation tool. You can search for database schema objects using regular expressions, and output the schema and data in a readable text format. The output is designed to be diff-ed against other database schemas.
package schemacrawler.tools.text.base;
import static java.util.Objects.requireNonNull;
import static sf.util.Utility.isLowerCase;
import java.io.PrintWriter;
import schemacrawler.schema.Column;
import schemacrawler.schemacrawler.SchemaCrawlerException;
import schemacrawler.tools.options.OutputFormat;
import schemacrawler.tools.options.OutputOptions;
import schemacrawler.tools.options.OutputWriter;
import schemacrawler.tools.options.TextOutputFormat;
import schemacrawler.tools.text.utility.HtmlFormattingHelper;
import schemacrawler.tools.text.utility.PlainTextFormattingHelper;
import schemacrawler.tools.text.utility.TextFormattingHelper;
import schemacrawler.tools.traversal.TraversalHandler;
public abstract class BaseFormatter
implements TraversalHandler
{
protected final O options;
protected final OutputOptions outputOptions;
protected final PrintWriter out;
protected final TextFormattingHelper formattingHelper;
protected final boolean printVerboseDatabaseInfo;
protected BaseFormatter(final O options,
final boolean printVerboseDatabaseInfo,
final OutputOptions outputOptions)
throws SchemaCrawlerException
{
this.options = requireNonNull(options, "Options not provided");
this.outputOptions = requireNonNull(outputOptions,
"Output options not provided");
this.printVerboseDatabaseInfo = !options.isNoInfo()
&& printVerboseDatabaseInfo;
final OutputFormat outputFormat = outputOptions.getOutputFormat();
if (outputFormat == TextOutputFormat.html)
{
formattingHelper = new HtmlFormattingHelper((TextOutputFormat) outputFormat);
}
else
{
formattingHelper = new PlainTextFormattingHelper((TextOutputFormat) outputFormat);
}
out = new PrintWriter(new OutputWriter(outputOptions,
options.isAppendOutput()), true);
}
protected String columnNullable(final String columnTypeName,
final boolean isNullable)
{
return isNullable? "": isLowerCase(columnTypeName)? " not null"
: " NOT NULL";
}
protected boolean isColumnSignificant(final Column column)
{
return column != null
&& (column.isPartOfPrimaryKey() || column.isPartOfForeignKey() || column
.isPartOfIndex());
}
}