elementfactory.java

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

JAVA
1,039
字号
   * @throws SAXException if there is a SAX problem.
   */
  private void startStringField(final Attributes atts) throws SAXException
  {
    final TextFieldElementFactory factory = new TextFieldElementFactory();
    getTextFieldElementAttributes(atts, factory);
    textElementFactory = factory;
  }

  /**
   * Creates a number element (a text element that displays a numerical value).
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startNumberField(final Attributes atts) throws SAXException
  {
    final NumberFieldElementFactory factory = new NumberFieldElementFactory();
    factory.setFormatString(atts.getValue(FORMAT_ATT));
    getTextFieldElementAttributes(atts, factory);
    textElementFactory = factory;
  }

  /**
   * Creates a date element (a text element that displays a date value).
   *
   * @param atts  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startDateField(final Attributes atts) throws SAXException
  {
    final DateFieldElementFactory factory = new DateFieldElementFactory();
    factory.setFormatString(atts.getValue(FORMAT_ATT));
    getTextFieldElementAttributes(atts, factory);
    textElementFactory = factory;
  }

  /**
   * Ends a label tag, sets the static text for the label which was build during the
   * parsing. The label is added to the current band.
   */
  private void endLabel()
  {
    final LabelElementFactory factory = (LabelElementFactory) textElementFactory;
    factory.setText(getCurrentText());
    clearCurrentText();
    getCurrentBand().addElement(factory.createElement());
    textElementFactory = null;
  }

  /**
   * Creates a resource label element, an text element with an static datasource attached.
   *
   * @param attrs  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startResourceLabel(final Attributes attrs)
      throws SAXException
  {
    final ResourceLabelElementFactory factory = new ResourceLabelElementFactory();
    textElementFactory = factory;
    getTextElementAttributes(attrs, factory);

    factory.setNullString(attrs.getValue(NULLSTRING_ATT));
    String resourceBase = attrs.getValue(RESOURCEBASE_ATTR);
    if (resourceBase == null)
    {
      final ReportConfiguration config = getReport().getReportConfiguration();
      resourceBase = config.getConfigProperty(ReportConfiguration.REPORT_RESOURCE_BUNDLE_KEY);
      if (resourceBase == null)
      {
        throw new SAXException("Resourcebase is not defined for this report. " +
            "Use the configuration key 'org.jfree.report.ResourceBundle' to define it.");
      }
    }
    factory.setResourceBase(resourceBase);
    clearCurrentText();
  }

  /**
   * Creates a resource field element.
   *
   * @param attrs  the attributes.
   *
   * @throws SAXException if there is a SAX problem.
   */
  private void startResourceField(final Attributes attrs)
      throws SAXException
  {
    final ResourceFieldElementFactory factory = new ResourceFieldElementFactory();
    getTextFieldElementAttributes(attrs, factory);

    String resourceBase = attrs.getValue(RESOURCEBASE_ATTR);
    if (resourceBase == null)
    {
      final ReportConfiguration config = getReport().getReportConfiguration();
      resourceBase = config.getConfigProperty(ReportConfiguration.REPORT_RESOURCE_BUNDLE_KEY);
      if (resourceBase == null)
      {
        throw new SAXException("Resourcebase is not defined for this report. " +
            "Use the configuration key 'org.jfree.report.ResourceBundle' to define it.");
      }
    }
    factory.setResourceBase(resourceBase);
    clearCurrentText();
  }

  /**
   * Ends a resource label tag, sets the static key for the resource label,
   * which was build during the parsing. The label is added to the current band.
   */
  private void endResourceLabel()
  {
    final ResourceLabelElementFactory factory = (ResourceLabelElementFactory) textElementFactory;
    factory.setResourceKey(getCurrentText());
    getCurrentBand().addElement(factory.createElement());
    textElementFactory = null;
  }

  /**
   * Ends the resource field and adds it to the current band.
   */
  private void endResourceField()
  {
    getCurrentBand().addElement(textElementFactory.createElement());
    textElementFactory = null;
  }


  /**
   * Ends the String field and adds it to the current band.
   */
  private void endStringField()
  {
    getCurrentBand().addElement(textElementFactory.createElement());
    textElementFactory = null;
  }

  /**
   * Ends the number field and adds it to the current band.
   */
  private void endNumberField()
  {
    getCurrentBand().addElement(textElementFactory.createElement());
    textElementFactory = null;
  }

  /**
   * Ends the date field and adds it to the current band.
   */
  private void endDateField()
  {
    getCurrentBand().addElement(textElementFactory.createElement());
    textElementFactory = null;
  }

  /**
   * Reads the attributes that are base for all text-elements, as
   * name, x, y, width, height, font, fontstyle, fontsize and alignment.
   *
   * @param atts  the attributes.
   * @param factory the text element factory that should produce the text element.
   * @throws SAXException if there is a SAX problem.
   */
  private void getTextElementAttributes
      (final Attributes atts, final TextElementFactory factory) throws SAXException
  {
    factory.setName(atts.getValue(NAME_ATT));
    factory.setAbsolutePosition(getElementPosition(atts));
    factory.setMinimumSize(getElementDimension(atts));
    if (atts.getValue(COLOR_ATT) != null)
    {
      factory.setColor(ParserUtil.parseColor(atts.getValue(COLOR_ATT)));
    }
    factory.setDynamicHeight(parseBoolean(atts.getValue(DYNAMIC_ATT)));
    factory.setHorizontalAlignment(parseTextAlignment(atts.getValue(ALIGNMENT_ATT)));
    factory.setVerticalAlignment(parseTextVerticalAlignment(atts.getValue(VALIGNMENT_ATT)));
    factory.setBold(parseBoolean(atts.getValue(FS_BOLD)));
    factory.setEmbedFont(parseBoolean(atts.getValue(FS_EMBEDDED)));
    factory.setEncoding(atts.getValue(FS_ENCODING));
    factory.setFontName(atts.getValue(FONT_NAME_ATT));
    factory.setFontSize(parseInteger(atts.getValue(FONT_SIZE_ATT)));
    factory.setItalic(parseBoolean(atts.getValue(FS_ITALIC)));
    factory.setLineHeight(parseFloat(atts.getValue(LINEHEIGHT)));
    factory.setStrikethrough(parseBoolean(atts.getValue(FS_STRIKETHR)));
    factory.setUnderline(parseBoolean(atts.getValue(FS_UNDERLINE)));
    factory.setReservedLiteral(atts.getValue(RESERVED_LITERAL_ATT));
    factory.setTrimTextContent(parseBoolean(atts.getValue(TRIM_TEXT_CONTENT_ATT)));
    parseSimpleFontStyle(atts.getValue(FONT_STYLE_ATT), factory);


  }

  /**
   * Reads an attribute as float and returns <code>def</code> if that fails.
   *
   * @param value the attribute value.
   * @return the float value.
   * @throws SAXException if an parse error occured.
   */
  private Float parseFloat(final String value) throws SAXException
  {
    if (value == null)
    {
      return null;
    }
    try
    {
      return new Float(value);
    }
    catch (Exception ex)
    {
      throw new SAXException("Failed to parse value", ex);
    }
  }

  /**
   * Reads the attributes that are base for all text-field elements, as
   * name, x, y, width, height, font, fontstyle, fontsize and alignment.
   *
   * @param atts  the attributes.
   * @param factory the text element factory that should produce the text element.
   * @throws SAXException if there is a SAX problem.
   */
  private void getTextFieldElementAttributes
      (final Attributes atts, final TextFieldElementFactory factory) throws SAXException
  {
    getTextElementAttributes(atts, factory);
    factory.setNullString(atts.getValue(NULLSTRING_ATT));
    final String textElementSourceName = atts.getValue(FIELDNAME_ATT);
    if (textElementSourceName == null)
    {
      throw new ParseException("The fieldname-attribute is required.", getLocator());
    }
    factory.setFieldname(textElementSourceName);

  }

  /**
   * Reads an attribute as int and returns <code>def</code> if that fails.
   *
   * @param val the attribute value.
   *
   * @return the int value.
   * @throws SAXException if an parse error occured.
   */
  private Integer parseInteger(final String val) throws SAXException
  {
    if (val == null)
    {
      return null;
    }
    try
    {
      return new Integer(val);
    }
    catch (NumberFormatException e)
    {
      throw new SAXException("Failed to parse value", e);
    }
  }

  /**
   * Parses the text looking for a text alignment, which is one of "left", "center" or "right".
   * <p>
   * The method returns one of the values:  Element.LEFT, Element.CENTER and Element.RIGHT.
   *
   * @param alignment  the alignment.
   * @return an alignment code.
   */
  private ElementAlignment parseTextAlignment(final String alignment)
  {
    ElementAlignment elementAlignment = null;
    if (alignment != null)
    {
      if (alignment.equals("left"))
      {
        elementAlignment = ElementAlignment.LEFT;
      }
      if (alignment.equals("center"))
      {
        elementAlignment = ElementAlignment.CENTER;
      }
      if (alignment.equals("right"))
      {
        elementAlignment = ElementAlignment.RIGHT;
      }
    }
    return elementAlignment;
  }

  /**
   * Parses the text looking for a text alignment, which is one of "top", "middle"/"center"
   * or "bottom".
   * <p>
   * The method returns one of the values:  Element.TOP, Element.BOTTOM and Element.MIDDLE.
   *
   * @param alignment  the alignment.
   * @return an alignment code.
   */
  private ElementAlignment parseTextVerticalAlignment
      (final String alignment)
  {
    ElementAlignment elementAlignment = null;
    if (alignment != null)
    {
      if (alignment.equals("top"))
      {
        elementAlignment = ElementAlignment.TOP;
      }
      if ((alignment.equals("center")) || (alignment.equals("middle")))
      {
        elementAlignment = ElementAlignment.MIDDLE;
      }
      if (alignment.equals("bottom"))
      {
        elementAlignment = ElementAlignment.BOTTOM;
      }
    }
    return elementAlignment;
  }

  /**
   * Handles the end of a band definition.
   *
   * @see org.jfree.report.ReportFooter
   *
   * @throws SAXException if a parser error occurs.
   */
  private void endBand() throws SAXException
  {
    if (subbandActive == false)
    {
      getParser().popFactory().endElement(BAND_TAG);
    }
    else
    {
      subbandActive = false;
    }
  }
}

⌨️ 快捷键说明

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