DotFileFormatHtml.java

  1. /*
  2.  * Copyright © 2014 - 2021 Leipzig University (Database Research Group)
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.gradoop.flink.io.impl.dot.functions;

  17. import com.google.common.base.Preconditions;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.gradoop.common.model.api.entities.Element;
  20. import org.gradoop.common.model.impl.pojo.EPGMVertex;
  21. import org.gradoop.common.model.impl.properties.Properties;
  22. import org.gradoop.common.model.impl.properties.Property;
  23. import org.gradoop.flink.model.impl.layouts.transactional.tuples.GraphTransaction;

  24. import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;

  25. /**
  26.  * Converts a GraphTransaction to the following .dot format:
  27.  * <p>{@code
  28.  *   digraph 0
  29.  *   {
  30.  *   gradoopId1 [label=<<table>...</table>>];
  31.  *   gradoopId2 [label=<<table>...</table>>];
  32.  *   gradoopID3;
  33.  *   gradoopID4;
  34.  *   gradoopId1->gradoopId2 [label=<<table>...</table>>];
  35.  *   gradoopId2->gradoopId1 [label=<<table>...</table>>];
  36.  *   gradoopId3->gradoopId4;
  37.  *   }
  38.  *   }
  39.  * </p>
  40.  */
  41. public class DotFileFormatHtml extends AbstractDotFileFormat {

  42.   /**
  43.    * color color for header background and properties text
  44.    */
  45.   private String color;

  46.   /**
  47.    * Constructor
  48.    *
  49.    * @param printGraphHead true, iff graph head data shall be attached to the output
  50.    * @param color the color for header background and properties text
  51.    */
  52.   public DotFileFormatHtml(boolean printGraphHead, String color) {
  53.     setPrintGraphHead(printGraphHead);
  54.     this.color = Preconditions.checkNotNull(color, "Color was null");
  55.   }

  56.   /**
  57.    * Adds vertex information to the specified builder.
  58.    *
  59.    * {@code vertexId [label="label", property1="value1", ...];}
  60.    *
  61.    * @param transaction graph transaction
  62.    * @param builder string builder to append
  63.    * @param suffix id suffix specific for the current {@link GraphTransaction}
  64.    */
  65.   @Override
  66.   void writeVertices(GraphTransaction transaction, StringBuilder builder, String suffix) {
  67.     for (EPGMVertex vertex: transaction.getVertices()) {
  68.       // writes for each vertex:
  69.       // "v1234",
  70.       builder.append(VERTEX_ID_PREFIX)
  71.         .append(vertex.getId())
  72.         .append(suffix)
  73.         .append(" [shape=Mrecord, ");

  74.       writeLabel(builder, vertex);

  75.       // writes:
  76.       // ";"
  77.       builder.append("];\n");
  78.     }
  79.   }

  80.   /**
  81.    * Writes the specified label and properties as DOT HTML label string (table)
  82.    *
  83.    * output: {@code label=<<table>...</table>>}
  84.    *
  85.    * @param builder string builder to append
  86.    * @param elem graph element with id, label and properties
  87.    */
  88.   @Override
  89.   void writeLabel(StringBuilder builder, Element elem) {
  90.     String label = elem.getLabel();
  91.     String id = elem.getId().toString();
  92.     Properties properties = elem.getProperties();
  93.     String lbl = StringUtils.isEmpty(label) ? id : label;

  94.     if (properties != null && properties.size() > 0) {
  95.       // writes properties as rows in html table
  96.       //write white on black label/id as header
  97.       builder.append("label=<")
  98.         .append("<font color=\"").append(color).append("\">")
  99.         .append("<table border=\"0\" cellborder=\"0\" cellpadding=\"3\">")
  100.         .append("<tr><td colspan=\"2\" bgcolor=\"")
  101.         .append(color)
  102.         .append("\"><font color=\"white\">")
  103.         .append(escapeHtml4(lbl))
  104.         .append("</font></td></tr>");

  105.       for (Property property : properties) {
  106.         builder.append("<tr><td>")
  107.           .append(escapeHtml4(property.getKey()))
  108.           .append("</td><td>")
  109.           .append(escapeHtml4(property.getValue().toString()))
  110.           .append("</td></tr>");
  111.       }
  112.       builder.append("</table></font>>");
  113.     } else {
  114.       //write id/label as node label in dot
  115.       builder.append("label=\"")
  116.         .append(escapeHtml4(lbl))
  117.         .append("\"");
  118.     }
  119.   }
  120. }