📄 elementfactory.java
字号:
/**
* ========================================
* 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.27.2.1 2003/08/24 14:18:10 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 com.jrefinery.report.io.simple;
import java.awt.Color;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.net.URL;
import com.jrefinery.report.Band;
import com.jrefinery.report.DrawableElement;
import com.jrefinery.report.ElementAlignment;
import com.jrefinery.report.ImageElement;
import com.jrefinery.report.ItemFactory;
import com.jrefinery.report.ShapeElement;
import com.jrefinery.report.TextElement;
import com.jrefinery.report.filter.DataRowDataSource;
import com.jrefinery.report.filter.DrawableFilter;
import com.jrefinery.report.targets.style.ElementStyleSheet;
import com.jrefinery.report.util.CharacterEntityParser;
import com.jrefinery.report.util.Log;
import com.jrefinery.report.util.ReportConfiguration;
import org.jfree.xml.ParseException;
import org.jfree.xml.Parser;
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
{
/** Storage for the current CDATA. */
private StringBuffer currentText;
/** The current band, where created elements are added to. */
private Band currentBand;
/** the fontfactory used to fill TextElements font definitions. */
private FontFactory fontFactory;
/** The text element name. */
private String textElementName;
/** The text element bounds. */
private Rectangle2D textElementBounds;
/** The text element alignment. */
private int textElementAlignment;
/** The text element vertical alignment. */
private int textElementVerticalAlignment;
/** The text element color. */
private Color textElementColor;
/** The text element null string. */
private String textElementNullString;
/** The text element source name. */
private String textElementSourceName;
/** The text element format string. */
private String textElementFormatString;
/** The resource base. */
private String resourceBase;
/** Dynamic flag. */
private boolean textElementDynamic;
/** Font information. */
private FontFactory.FontInformation textElementFont;
/** The character entity parser. */
private 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 Parser parser, final String finishTag, final Band band)
{
super(parser, finishTag);
this.currentBand = band;
this.currentText = new StringBuffer();
fontFactory = new FontFactory();
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 ItemFactory
*
* @param qName the element name.
* @param atts the element attributes.
*
* @throws SAXException if an unknown tag is encountered.
*
* @see com.jrefinery.report.ItemFactory
*/
public void startElement(final String qName, final Attributes atts)
throws SAXException
{
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(IMAGEFUNCTION_TAG))
{
Log.warn("The usage of the image-function tag is deprecated. " +
"Use the image-element instead.");
startImageFunction(atts);
}
else if (elementName.equals(IMAGEURLFIELD_TAG))
{
startImageURLField(atts);
}
else if (elementName.equals(IMAGEURLFUNCTION_TAG))
{
Log.warn("The usage of the image-url-function tag is deprecated. " +
"Use the image-url-element instead.");
startImageURLFunction(atts);
}
else if (elementName.equals(LINE_TAG))
{
startLine(atts);
}
else if (elementName.equals(SHAPE_FIELD_TAG))
{
startShapeField(atts);
}
else if (elementName.equals(GENERAL_FIELD_TAG))
{
throw new ParseException
("The usage of general field is deprecated, use string field instead", getLocator());
}
else if (elementName.equals(STRING_FIELD_TAG))
{
startStringField(atts);
}
else if (elementName.equals(MULTILINE_FIELD_TAG))
{
Log.warn("The usage of the mutiline-field tag is deprecated. " +
"Use the string-element instead.");
startMultilineField(atts);
}
else if (elementName.equals(NUMBER_FIELD_TAG))
{
startNumberField(atts);
}
else if (elementName.equals(DATE_FIELD_TAG))
{
startDateField(atts);
}
else if (elementName.equals(STRING_FUNCTION_TAG))
{
Log.warn("The usage of the string-function tag is deprecated. " +
"Use the string-element instead.");
startStringFunction(atts);
}
else if (elementName.equals(NUMBER_FUNCTION_TAG))
{
Log.warn("The usage of the number-function tag is deprecated. " +
"Use the number-element instead.");
startNumberFunction(atts);
}
else if (elementName.equals(DATE_FUNCTION_TAG))
{
Log.warn("The usage of the date-function tag is deprecated. " +
"Use the date-element instead.");
startDateFunction(atts);
}
else if (elementName.equals(RECTANGLE_TAG))
{
startRectangle(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.
*/
protected 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))
{
endDrawableField();
}
else if (elementName.equals(IMAGEREF_TAG))
{
endImageRef();
}
else if (elementName.equals(IMAGEFUNCTION_TAG))
{
endImageFunction();
}
else if (elementName.equals(IMAGEFIELD_TAG))
{
endImageField();
}
else if (elementName.equals(IMAGEURLFUNCTION_TAG))
{
endImageURLFunction();
}
else if (elementName.equals(IMAGEURLFIELD_TAG))
{
endImageURLField();
}
else if (elementName.equals(LINE_TAG))
{
endLine();
}
else if (elementName.equals(SHAPE_FIELD_TAG))
{
endShapeField();
}
else if (elementName.equals(GENERAL_FIELD_TAG))
{
throw new ParseException
("The general element is deprecated, use string-field instead.", getLocator());
}
else if (elementName.equals(STRING_FIELD_TAG))
{
endStringField();
}
else if (elementName.equals(MULTILINE_FIELD_TAG))
{
endMultilineField();
}
else if (elementName.equals(NUMBER_FIELD_TAG))
{
endNumberField();
}
else if (elementName.equals(DATE_FIELD_TAG))
{
endDateField();
}
else if (elementName.equals(STRING_FUNCTION_TAG))
{
endStringFunction();
}
else if (elementName.equals(NUMBER_FUNCTION_TAG))
{
endNumberFunction();
}
else if (elementName.equals(DATE_FUNCTION_TAG))
{
endDateFunction();
}
else if (elementName.equals(RECTANGLE_TAG))
{
endRectangle();
}
else if (elementName.equals(getFinishTag()))
{
getParser().popFactory().endElement(qName);
}
else
{
throw new ParseException("Invalid tag: " + qName, getLocator());
}
}
/**
* Create a ImageElement with an static ImageDataSource. The ImageData is read from
* the supplied URL (attribute "src") in conjunction with the contentbase defined in the
* ReportDefinitionContentHandler.
*
* @param atts the attributes.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -