All Downloads are FREE. Search and download functionalities are using the official Maven repository.

schemacrawler.tools.text.base.BaseDotFormatter Maven / Gradle / Ivy

/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2020, Sualeh Fatehi .
All rights reserved.
------------------------------------------------------------------------

SchemaCrawler is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

SchemaCrawler and the accompanying materials are made available under
the terms of the Eclipse Public License v1.0, GNU General Public License
v3 or GNU Lesser General Public License v3.

You may elect to redistribute this code under any of these licenses.

The Eclipse Public License is available at:
http://www.eclipse.org/legal/epl-v10.html

The GNU General Public License v3 and the GNU Lesser General Public
License v3 are available at:
http://www.gnu.org/licenses/

========================================================================
*/

package schemacrawler.tools.text.base;


import static sf.util.IOUtility.readResourceFully;
import static sf.util.Utility.isBlank;

import java.util.Map;
import java.util.Map.Entry;

import schemacrawler.SchemaCrawlerInfo;
import schemacrawler.schema.CrawlInfo;
import schemacrawler.schema.DatabaseInfo;
import schemacrawler.schema.JdbcDriverInfo;
import schemacrawler.schemacrawler.SchemaCrawlerException;
import schemacrawler.tools.integration.graph.GraphOptions;
import schemacrawler.tools.options.OutputOptions;
import schemacrawler.tools.options.TextOutputFormat;
import schemacrawler.tools.text.utility.html.Alignment;
import schemacrawler.tools.text.utility.html.TableCell;
import schemacrawler.tools.text.utility.html.TableRow;
import sf.util.Color;

/**
 * Text formatting of schema.
 *
 * @author Sualeh Fatehi
 */
public abstract class BaseDotFormatter
  extends BaseFormatter
{

  protected BaseDotFormatter(final GraphOptions options,
                             final boolean printVerboseDatabaseInfo,
                             final OutputOptions outputOptions,
                             final String identifierQuoteString)
    throws SchemaCrawlerException
  {
    super(options,
          printVerboseDatabaseInfo,
          outputOptions,
          identifierQuoteString);
  }

  @Override
  public void begin()
  {
    final String header = makeGraphvizHeader();
    formattingHelper
      .append(header)
      .println();
  }

  @Override
  public void end()
    throws SchemaCrawlerException
  {
    formattingHelper
      .append("}")
      .println();

    super.end();
  }

  @Override
  public void handle(final CrawlInfo crawlInfo)
  {
    if (crawlInfo == null)
    {
      return;
    }

    TableRow row;

    if (outputOptions.hasTitle())
    {
      final String title = outputOptions.getTitle();
      row = new TableRow(TextOutputFormat.html);
      row.add(newTableCell(title, Alignment.left, true, Color.white, 2));

      formattingHelper
        .append(row.toString())
        .println();
    }

    if (options.isNoInfo())
    {
      return;
    }

    if (!options.isNoSchemaCrawlerInfo())
    {
      row = new TableRow(TextOutputFormat.html);
      row.add(newTableCell("generated by",
                           Alignment.right,
                           false,
                           Color.white,
                           1));
      row.add(newTableCell(crawlInfo.getSchemaCrawlerInfo(),
                           Alignment.left,
                           false,
                           Color.white,
                           1));

      formattingHelper
        .append(row.toString())
        .println();

      row = new TableRow(TextOutputFormat.html);
      row.add(newTableCell("generated on",
                           Alignment.right,
                           false,
                           Color.white,
                           1));
      row.add(newTableCell(formatTimestamp(crawlInfo.getCrawlTimestamp()),
                           Alignment.left,
                           false,
                           Color.white,
                           1));

      formattingHelper
        .append(row.toString())
        .println();
    }

    if (options.isShowDatabaseInfo())
    {
      row = new TableRow(TextOutputFormat.html);
      row.add(newTableCell("database version",
                           Alignment.right,
                           false,
                           Color.white,
                           1));
      row.add(newTableCell(crawlInfo.getDatabaseInfo(),
                           Alignment.left,
                           false,
                           Color.white,
                           1));

      formattingHelper
        .append(row.toString())
        .println();
    }
  }

  @Override
  public void handle(final DatabaseInfo dbInfo)
  {
    // No-op
  }

  @Override
  public void handle(final JdbcDriverInfo driverInfo)
  {
    // No-op
  }

  @Override
  public void handle(final SchemaCrawlerInfo schemaCrawlerInfo)
  {
    // No-op
  }

  @Override
  public void handleHeaderEnd()
    throws SchemaCrawlerException
  {
    if (options.isNoInfo() && !outputOptions.hasTitle())
    {
      return;
    }

    formattingHelper
      .append("      ")
      .println();
    formattingHelper
      .append("    >")
      .println();
    formattingHelper
      .append("  ];")
      .println();
    formattingHelper.println();
  }

  @Override
  public void handleHeaderStart()
    throws SchemaCrawlerException
  {
    if (options.isNoInfo() && !outputOptions.hasTitle())
    {
      return;
    }

    formattingHelper
      .append("  /* ")
      .append("Title Block")
      .append(" -=-=-=-=-=-=-=-=-=-=-=-=-=- */")
      .println();
    formattingHelper
      .append("  graph [ ")
      .println();
    formattingHelper
      .append("    label=<")
      .println();
    formattingHelper
      .append(
        "      ")
      .println();
  }

  @Override
  public void handleInfoEnd()
    throws SchemaCrawlerException
  {
    // No-op
  }

  @Override
  public void handleInfoStart()
    throws SchemaCrawlerException
  {
    // No-op
  }

  protected TableCell newTableCell(final String text,
                                   final Alignment align,
                                   final boolean emphasizeText,
                                   final Color bgColor,
                                   final int colspan)
  {
    return new TableCell(text,
                         true,
                         0,
                         align,
                         emphasizeText,
                         "",
                         bgColor,
                         colspan,
                         TextOutputFormat.html);
  }

  private String makeGraphvizAttributes(final Map graphvizAttributes,
                                        final String prefix)
  {
    final StringBuilder buffer = new StringBuilder();
    for (final Entry entry : graphvizAttributes.entrySet())
    {
      final String[] key = entry
        .getKey()
        .split("\\.");
      if (key.length == 2 && key[0].equals(prefix))
      {
        buffer
          .append("    ")
          .append(key[1])
          .append("=")
          .append("\"")
          .append(entry.getValue())
          .append("\"")
          .append("\n");
      }
    }
    return buffer.toString();
  }

  private String makeGraphvizHeader()
  {
    final Map graphvizAttributes =
      options.getGraphvizAttributes();
    final String graphvizHeaderTemplate = readResourceFully("/dot.header.txt");
    final String graphvizHeader = String.format(graphvizHeaderTemplate,
                                                makeGraphvizAttributes(
                                                  graphvizAttributes,
                                                  "graph"),
                                                makeGraphvizAttributes(
                                                  graphvizAttributes,
                                                  "node"),
                                                makeGraphvizAttributes(
                                                  graphvizAttributes,
                                                  "edge"));
    return graphvizHeader;
  }

}