Package org.pentaho.reporting.engine.classic.core.modules.output.table.html.helper

Source Code of org.pentaho.reporting.engine.classic.core.modules.output.table.html.helper.StyleBuilder$StyleCarrier

/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program 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.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2001 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors..  All rights reserved.
*/

package org.pentaho.reporting.engine.classic.core.modules.output.table.html.helper;

import java.io.IOException;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

import org.pentaho.reporting.engine.classic.core.ClassicEngineBoot;
import org.pentaho.reporting.engine.classic.core.layout.model.BorderEdge;
import org.pentaho.reporting.engine.classic.core.modules.output.table.html.util.HtmlColors;
import org.pentaho.reporting.engine.classic.core.modules.output.table.html.HtmlPrinter;
import org.pentaho.reporting.engine.classic.core.modules.output.table.html.util.HtmlEncoderUtil;
import org.pentaho.reporting.engine.classic.core.style.BorderStyle;
import org.pentaho.reporting.engine.classic.core.util.geom.StrictGeomUtility;
import org.pentaho.reporting.libraries.base.util.LFUMap;
import org.pentaho.reporting.libraries.base.util.LinkedMap;
import org.pentaho.reporting.libraries.base.util.ObjectUtilities;
import org.pentaho.reporting.libraries.base.util.StringBufferWriter;
import org.pentaho.reporting.libraries.base.util.StringUtils;

public final class StyleBuilder
{
  public static class StyleCarrier
  {
    private String key;
    private String value;
    private String unit;

    protected StyleCarrier(final String key, final String value, final String unit)
    {
      if (key == null)
      {
        throw new NullPointerException();
      }
      this.key = key;
      this.value = value;
      this.unit = unit;
    }

    public String getUnit()
    {
      return unit;
    }

    public String getKey()
    {
      return key;
    }

    public String getValue()
    {
      return value;
    }


    public boolean equals(final Object o)
    {
      if (this == o)
      {
        return true;
      }
      if (o == null || getClass() != o.getClass())
      {
        return false;
      }

      final StyleCarrier that = (StyleCarrier) o;

      if (!key.equals(that.key))
      {
        return false;
      }
      if (ObjectUtilities.equal(value, that.value) == false)
      {
        return false;
      }
      if (ObjectUtilities.equal(unit, that.unit) == false)
      {
        return false;
      }
      return true;
    }

    public int hashCode()
    {
      int result;
      result = key.hashCode();
      result = 31 * result + (value != null ? value.hashCode() : 0);
      result = 31 * result + (unit != null ? unit.hashCode() : 0);
      return result;
    }


    public String toString()
    {
      return "org.pentaho.reporting.engine.classic.core.modules.output.table.html.helper.StyleBuilder.StyleCarrier{" +
          "key='" + key + '\'' +
          ", value='" + value + '\'' +
          ", unit='" + unit + '\'' +
          '}';
    }
  }

  public static final String INDENT = "    ";

  private LinkedMap styles;
  private LFUMap cachedBorderStyle;
  private String lineSeparator;
  private StringBuffer buffer;

  private NumberFormat pointConverter;
  private boolean safariLengthFix;

  public StyleBuilder()
  {
    this.lineSeparator = StringUtils.getLineSeparator();
    this.styles = new LinkedMap(1024, 0.75f);
    this.cachedBorderStyle = new LFUMap(30);
    this.buffer = new StringBuffer(100);

    if ("true".equals(ClassicEngineBoot.getInstance().getGlobalConfig().getConfigProperty
        ("org.pentaho.reporting.engine.classic.core.modules.output.table.html.SafariLengthHack")))
    {
      pointConverter = new DecimalFormat("0", new DecimalFormatSymbols(Locale.US));
      safariLengthFix = true;
    }
    else
    {
      safariLengthFix = false;
      pointConverter = new DecimalFormat("0.####", new DecimalFormatSymbols(Locale.US));
    }
  }

  public void clear()
  {
    this.styles.clear();
  }

  public void append(final String key, final String value)
  {
    final StyleCarrier newCarrier = new StyleCarrier(key, HtmlEncoderUtil.encodeCSS(value), null);
    styles.put(key, newCarrier);
  }

  public void appendRaw(final String key, final String value)
  {
    final StyleCarrier newCarrier = new StyleCarrier(key, value, null);
    styles.put(key, newCarrier);
  }

  public void append(final String key, final String value, final String unit)
  {
    final StyleCarrier newCarrier = new StyleCarrier(key, HtmlEncoderUtil.encodeCSS(value), unit);
    styles.put(key, newCarrier);
  }

  /**
   * Appends the style to the list. If the replace value is <code>false</code> and the list already contains the key, it
   * will not be replaced.
   */
  public void append(final String key, final String value, final boolean replace)
  {
    if (replace == false)
    {
      final Object stylePos = styles.get(key);
      if (stylePos != null)
      {
        return;
      }
    }

    final StyleCarrier newCarrier = new StyleCarrier(key, HtmlEncoderUtil.encodeCSS(value), null);
    styles.put(key, newCarrier);
  }

  /**
   * Appends the style to the list. If the replace value if <code>false</code> and the list already contains the key, it
   * will not be replaced.
   */
  public void append(final String key, final String value, final String unit, final boolean replace)
  {
    if (replace == false)
    {
      final Object stylePos = styles.get(key);
      if (stylePos != null)
      {
        return;
      }
    }

    final StyleCarrier newCarrier = new StyleCarrier(key, HtmlEncoderUtil.encodeCSS(value), unit);
    styles.put(key, newCarrier);
  }

  public String toString()
  {
    return toString(true);
  }

  public void print(final Writer writer, final boolean compact) throws IOException
  {
    // we are usign a linked list now, so a iterator is more efficient than a for loop ...
    final Object[] values = styles.values();
    boolean first = true;
    for (int i = 0; i < values.length; i++)
    {
      if (first == false)
      {
        writer.write("; ");
      }
      final StyleCarrier sc = (StyleCarrier) values[i];
      if (compact == false)
      {
        if (first == false)
        {
          writer.write(lineSeparator);
        }
        writer.write(StyleBuilder.INDENT);
      }

      writer.write(sc.getKey());
      writer.write(": ");
      writer.write(sc.getValue());
      final String unit = sc.getUnit();
      if (unit != null)
      {
        writer.write(unit);
      }
      first = false;
    }
  }

  public String toString(final boolean compact)
  {
    buffer.delete(0, buffer.length());
    final StringBufferWriter writer = new StringBufferWriter(buffer);
    try
    {
      print(writer, compact);
    }
    catch (IOException e)
    {
      // will not happen ..
      throw new IllegalStateException("How can a fully buffered writer cause a IO exception?");
    }
    return buffer.toString();
  }


  public String printEdgeAsCSS(final BorderEdge edge)
  {
    final BorderStyle borderStyle = edge.getBorderStyle();
    final long width = edge.getWidth();
    if (BorderStyle.NONE.equals(borderStyle) || width <= 0)
    {
      return "none";
    }

    final Object cached = cachedBorderStyle.get(edge);
    if (cached != null)
    {
      return cached.toString();
    }

    final String value =
        (pointConverter.format(HtmlPrinter.fixLengthForSafari(StrictGeomUtility.toExternalValue(width),
        safariLengthFix)) +
        "pt " + borderStyle.toString() + ' ' + HtmlColors.getColorString(edge.getColor()));
    cachedBorderStyle.put(edge, value);
    return value;
  }

  public NumberFormat getPointConverter()
  {
    return pointConverter;
  }

  /**
   * @return the style carriers as array.
   */
  public StyleCarrier[] toArray()
  {
    final StyleCarrier[] data = new StyleCarrier[styles.size()];
    return (StyleCarrier[]) styles.values(data);
  }

}
TOP

Related Classes of org.pentaho.reporting.engine.classic.core.modules.output.table.html.helper.StyleBuilder$StyleCarrier

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.