001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.utils;
020
021import java.util.*;
022
023/**
024 * Abstract class for formatters that print the contents of a {@link java.util.Properties Properties} object as a string.
025 * @author Laurent Cohen
026 */
027public abstract class PropertiesTableFormat {
028  /**
029   * Contains the formatted text.
030   */
031  protected StringBuffer sb = new StringBuffer();
032  /**
033   * The title of the whole document.
034   */
035  protected String docTitle = null;
036
037  /**
038   * Initialize this formatter with the specified title.
039   * @param docTitle the title of the whole document.
040   */
041  public PropertiesTableFormat(final String docTitle) {
042    this.docTitle = docTitle;
043  }
044
045  /**
046   * Get the formatted text.
047   * @return the text as a string.
048   */
049  public String getText() {
050    return sb.toString();
051  }
052
053  /**
054   * Write the prologue for the formatted text.
055   */
056  public void start() {
057  }
058
059  /**
060   * Write the epilogue for the formatted text.
061   */
062  public void end() {
063  }
064
065  /**
066   * Generate the formatted text for a set of properties.
067   * @param props the set of properties to format.
068   * @param title the title of the formatted table.
069   */
070  public void formatTable(final Properties props, final String title) {
071    Set<String> orderedProps = new TreeSet<>();
072    Enumeration en = props.propertyNames();
073    while (en.hasMoreElements()) orderedProps.add((String) en.nextElement());
074    tableStart(title);
075    for (String name: orderedProps) {
076      rowStart();
077      cellStart();
078      sb.append(formatName(name));
079      cellEnd();
080      cellSeparator();
081      cellStart();
082      sb.append(props.getProperty(name));
083      cellEnd();
084      rowEnd();
085    }
086    tableEnd();
087  }
088
089  /**
090   * Write the prologue of a table.
091   * @param title the title for the table.
092   */
093  public void tableStart(final String title) {
094  }
095
096  /**
097   * Write the prologue of a table.
098   */
099  public void tableEnd() {
100  }
101
102  /**
103   * Write the prologue of a table row.
104   */
105  public void rowStart() {
106  }
107
108  /**
109   * Write the prologue of a table row.
110   */
111  public void rowEnd() {
112  }
113
114  /**
115   * Write the prologue of a table cell.
116   */
117  public void cellStart() {
118  }
119
120  /**
121   * Write the prologue of a table cell.
122   */
123  public void cellEnd() {
124  }
125
126  /**
127   * Write the separator between 2 cells.
128   */
129  public void cellSeparator() {
130  }
131
132  /**
133   * Format the specified property name if needed.
134   * @param name the name to format.
135   * @return the formatted name as a string.
136   */
137  public String formatName(final String name) {
138    return name;
139  }
140
141  /**
142   * Print the specified string.
143   * @param message the string to print.
144   */
145  public void print(final String message) {
146    sb.append(message);
147  }
148}