elementfactory.java

来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 1,039 行 · 第 1/3 页

JAVA
1,039
字号
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner;
 *
 * (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -------------------
 * ElementFactory.java
 * -------------------
 * (C)opyright 2002, 2003, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: ElementFactory.java,v 1.15 2003/11/07 18:33:57 taqua Exp $
 *
 * Changes
 * -------
 * 10-May-2002 : Initial version
 * 16-May-2002 : parseStroke added for line width
 * 22-May-2002 : Structured parsing functions: all tags have a startXXX and endXXX function
 * 30-Jun-2002 : Added support for ImageField, ImageFunction
 * 10-Jul-2002 : Added support for ImageURLField, ImageURLFunction
 * 31-Aug-2002 : Element-creation uses the ItemFactory, removed references to deprecated
 *               Element-types
 */
package org.jfree.report.modules.parser.simple;

import java.awt.Color;
import java.awt.Stroke;
import java.awt.geom.Dimension2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import org.jfree.report.Band;
import org.jfree.report.ElementAlignment;
import org.jfree.report.ShapeElement;
import org.jfree.report.elementfactory.DateFieldElementFactory;
import org.jfree.report.elementfactory.DrawableFieldElementFactory;
import org.jfree.report.elementfactory.ImageFieldElementFactory;
import org.jfree.report.elementfactory.ImageURLFieldElementFactory;
import org.jfree.report.elementfactory.LabelElementFactory;
import org.jfree.report.elementfactory.NumberFieldElementFactory;
import org.jfree.report.elementfactory.ResourceFieldElementFactory;
import org.jfree.report.elementfactory.ResourceLabelElementFactory;
import org.jfree.report.elementfactory.ShapeFieldElementFactory;
import org.jfree.report.elementfactory.StaticImageURLElementFactory;
import org.jfree.report.elementfactory.StaticShapeElementFactory;
import org.jfree.report.elementfactory.TextElementFactory;
import org.jfree.report.elementfactory.TextFieldElementFactory;
import org.jfree.report.layout.StaticLayoutManager;
import org.jfree.report.modules.parser.base.ReportParser;
import org.jfree.report.modules.parser.base.ReportParserUtil;
import org.jfree.report.style.ElementStyleSheet;
import org.jfree.report.util.CharacterEntityParser;
import org.jfree.report.util.ReportConfiguration;
import org.jfree.ui.FloatDimension;
import org.jfree.xml.ParseException;
import org.jfree.xml.ParserUtil;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * The ElementFactory is responsible for creating ReportElements and is called by the
 * ReportDefinitionContentHandler. For details on the format of the parser have a look
 * at the DTD supplied in the distribution or on http://jfreereport.sourceforge.net/
 * <p>
 * This factory uses the deprecated element classes. These classes will get not extension
 * for new features and as soon as the discrepancy between implemented and possible features
 * gets too huge, this parser will be discontinued.
 *
 * @author Thomas Morgner
 */
public class ElementFactory extends AbstractReportDefinitionHandler implements ReportDefinitionTags
{
  /** A constant defining the name of the dynamic attribute. */
  public static final String DYNAMIC_ATT = "dynamic";
  /** A constant defining the name of the reserved-literal attribute. */
  public static final String RESERVED_LITERAL_ATT = "reserved-literal";
  /** A constant defining the name of the trim-text-content attribute. */
  public static final String TRIM_TEXT_CONTENT_ATT = "trim-text-content";

  /** Storage for the current CDATA. */
  private final StringBuffer currentText;

  /** The current band, where created elements are added to. */
  private final Band currentBand;

  /** A flag indicating whether this factory current works on an subband. */
  private boolean subbandActive;

  /** The current text element factory used to produce the next element. */
  private TextElementFactory textElementFactory;

  /** The character entity parser. */
  private final CharacterEntityParser entityParser;

  /**
   * Creates a new ElementFactory. The factory queries the current Band of the ReportFactory
   * and will add created element to this band. If unknown end-Tags are encountered, the
   * parsing for elements will stop and the previous handler will be activated.
   *
   * @param parser the used parser to coordinate the parsing process.
   * @param finishTag the finish tag, that should trigger the deactivation of this parser.
   * @param band the band that should be defined.
   *
   * @throws NullPointerException if the finishTag or the parser are null.
   */
  public ElementFactory(final ReportParser parser,
                        final String finishTag, final Band band)
  {
    super(parser, finishTag);
    this.currentBand = band;
    this.currentText = new StringBuffer();
    this.entityParser = CharacterEntityParser.createXMLEntityParser();
  }

  /**
   * SAX-Handler function that is forwarded from the ReportDefinitionContentHandler.
   * StartTag-occurences of element definitions get handled by this factory. If an unknown
   * element is encountered, a SAXException is thrown.
   * <p>
   * The elements parsed in this factory denote base usecases. Element creation is
   * delegated to the element factories in the package org.jfree.repor.elementfactory
   *
   * @param qName  the element name.
   * @param atts  the element attributes.
   *
   * @throws SAXException if an unknown tag is encountered.
   */
  public void startElement(final String qName, final Attributes atts)
      throws SAXException
  {
    if (textElementFactory != null)
    {
      throw new IllegalStateException(qName);
    }
    final String elementName = qName.toLowerCase().trim();

    // *** LABEL ***
    if (elementName.equals(LABEL_TAG))
    {
      startLabel(atts);
    }
    else if (elementName.equals(RESOURCELABEL_TAG))
    {
      startResourceLabel(atts);
    }
    else if (elementName.equals(RESOURCEFIELD_TAG))
    {
      startResourceField(atts);
    }
    else if (elementName.equals(DRAWABLE_FIELD_TAG))
    {
      startDrawableField(atts);
    }
    else if (elementName.equals(IMAGEREF_TAG))
    {
      startImageRef(atts);
    }
    else if (elementName.equals(IMAGEFIELD_TAG))
    {
      startImageField(atts);
    }
    else if (elementName.equals(IMAGEURLFIELD_TAG))
    {
      startImageURLField(atts);
    }
    else if (elementName.equals(LINE_TAG))
    {
      startLine(atts);
    }
    else if (elementName.equals(SHAPE_FIELD_TAG))
    {
      startShapeField(atts);
    }
    else if (elementName.equals(STRING_FIELD_TAG))
    {
      startStringField(atts);
    }
    else if (elementName.equals(NUMBER_FIELD_TAG))
    {
      startNumberField(atts);
    }
    else if (elementName.equals(DATE_FIELD_TAG))
    {
      startDateField(atts);
    }
    else if (elementName.equals(RECTANGLE_TAG))
    {
      startRectangle(atts);
    }
    else if (elementName.equals(BAND_TAG))
    {
      startBand(atts);
    }
  }

  /**
   * Returns the current band, which receives the parsed elements.
   *
   * @return the current band.
   */
  protected Band getCurrentBand()
  {
    return currentBand;
  }

  /**
   * Removes all text from the textbuffer at the end of a CDATA section.
   */
  private void clearCurrentText()
  {
    this.currentText.delete(0, currentText.length());
  }

  /**
   * Returns the current text of the textbuffer.
   *
   * @return the current text.
   */
  protected String getCurrentText()
  {
    return entityParser.decodeEntities(currentText.toString());
  }

  /**
   * Receives some (or all) of the text in the current element.
   *
   * @param ch  character buffer.
   * @param start  the start index.
   * @param length  the length of the valid character data.
   */
  public void characters(final char[] ch, final int start, final int length)
  {
    if (this.currentText != null)
    {
      this.currentText.append(String.copyValueOf(ch, start, length));
    }
  }

  /**
   * SAX handler function that is forwarded from the ReportDefinitionContentHandler.
   * If an unknown element is encountered, the previous handler gets activated.
   *
   * @param qName  the element name.
   *
   * @throws SAXException if an unknown tag is encountered.
   */
  public void endElement(final String qName)
      throws SAXException
  {
    final String elementName = qName.toLowerCase().trim();

    // *** LABEL ***
    if (elementName.equals(LABEL_TAG))
    {
      endLabel();
    }
    else if (elementName.equals(RESOURCELABEL_TAG))
    {
      endResourceLabel();
    }
    else if (elementName.equals(RESOURCEFIELD_TAG))
    {
      endResourceField();
    }
    else if (elementName.equals(DRAWABLE_FIELD_TAG) ||
        elementName.equals(IMAGEREF_TAG) ||
        elementName.equals(IMAGEFIELD_TAG) ||
        elementName.equals(IMAGEURLFIELD_TAG) ||
        elementName.equals(LINE_TAG) ||
        elementName.equals(SHAPE_FIELD_TAG) ||
        elementName.equals(RECTANGLE_TAG))
    {
      // do nothing ..
    }
    else if (elementName.equals(STRING_FIELD_TAG))
    {
      endStringField();
    }
    else if (elementName.equals(NUMBER_FIELD_TAG))
    {
      endNumberField();
    }
    else if (elementName.equals(DATE_FIELD_TAG))
    {
      endDateField();
    }
    else if (elementName.equals(BAND_TAG))
    {
      endBand();
    }
    else if (elementName.equals(getFinishTag()))
    {
      getParser().popFactory().endElement(qName);
      return;
    }
    else
    {
      throw new ParseException("Invalid tag: " + qName, getLocator());
    }
  }

  /**
   * Handles the start of a new sub-band.
   * @param attr the attribute set of the band-element.
   * @throws SAXException if an error occurs.
   */
  private void startBand(final Attributes attr) throws SAXException
  {
    // create the report header...
    final Band band = new Band();

    final String name = attr.getValue(NAME_ATT);
    if (name != null)
    {
      band.setName(name);
    }
    band.getStyle().setStyleProperty
        (StaticLayoutManager.ABSOLUTE_POS, getElementPosition(attr));

    String widthValue = attr.getValue("width");
    if (widthValue == null)
    {
      widthValue = "0";
    }
    String heightValue = attr.getValue("height");
    if (heightValue == null)
    {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?