reportdescriptionwriter.java

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

JAVA
533
字号
    }
    p.setAttribute("type", element.getContentType());
    writeTag(writer, BandHandler.ELEMENT_TAG, p, OPEN);

    final ElementStyleSheet styleSheet = element.getStyle();
    if (isStyleSheetEmpty(styleSheet) == false)
    {
      final CommentHintPath stylePath = newPath.getInstance();
      stylePath.addName(ElementHandler.STYLE_TAG);
      writeComment(writer, stylePath, CommentHandler.OPEN_TAG_COMMENT);
      writeTag(writer, ElementHandler.STYLE_TAG);

      final StyleWriter styleWriter =
          new StyleWriter(getReportWriter(), element.getStyle(),
              parent.getBandDefaults(), getIndentLevel(), stylePath);
      styleWriter.write(writer);
      writeComment(writer, stylePath, CommentHandler.CLOSE_TAG_COMMENT);
      writeCloseTag(writer, ElementHandler.STYLE_TAG);
    }

    writeDataSourceForElement(element, writer, newPath);

    writeComment(writer, newPath, CommentHandler.CLOSE_TAG_COMMENT);
    writeCloseTag(writer, BandHandler.ELEMENT_TAG);
  }

  /**
   * Writes the datasource- or template-tag for an given element.
   *
   * @param element the element, which should be written.
   * @param writer the writer that should receive the contents.
   * @param path the comment hint path used to read the ext-parser comments.
   * @throws ReportWriterException if there is a problem writing the report
   * @throws IOException if there is an IO error.
   */
  protected void writeDataSourceForElement
      (final Element element, final Writer writer, final CommentHintPath path)
      throws ReportWriterException, IOException
  {
    if ((element.getDataSource() instanceof EmptyDataSource))
    {
      return;
    }
    if (element.getDataSource() instanceof Template == false)
    {
      writeDataSource(writer, element.getDataSource(), path);
      return;
    }

    final TemplateCollector tc = getReportWriter().getTemplateCollector();

    final Template template = (Template) element.getDataSource();

    // the template description of the element template will get the
    // template name as its name.
    final TemplateDescription templateDescription =
        tc.getDescription(template);

    if (templateDescription == null)
    {
      throw new ReportWriterException("Unknown template type: " + templateDescription);
    }

    // create the parent description before the template description is filled.
    TemplateDescription parentTemplate = (TemplateDescription) templateDescription.getInstance();

    try
    {
      templateDescription.setParameterFromObject(template);
    }
    catch (ObjectFactoryException ofe)
    {
      throw new ReportWriterException("Error while preparing the template", ofe);
    }

    // seek the parent template. If that fails for any reason, we fall back to
    // the root template description, we safed earlier ...
    final String templateExtends = (String) getReport().getReportBuilderHints().getHint
        (element, "ext.parser.template-reference", String.class);
    if (templateExtends != null)
    {
      final TemplateDescription parent =
          TemplatesWriter.getTemplateDescription(getReportWriter(), templateExtends);
      if (parent != null)
      {
        parentTemplate = parent;
      }
    }

    final CommentHintPath templatePath = path.getInstance();
    templatePath.addName(TemplatesHandler.TEMPLATE_TAG);

    final TemplateWriter templateWriter = new TemplateWriter
        (getReportWriter(), getIndentLevel(),
            templateDescription, parentTemplate, templatePath);
    templateWriter.write(writer);
  }

  /**
   * Writes a data source to a character stream writer.
   *
   * @param writer  the character stream writer.
   * @param datasource  the datasource.
   * @param path the comment hint path used to read the ext-parser comments.
   *
   * @throws IOException if there is an I/O problem.
   * @throws ReportWriterException if there is a problem writing the report.
   */
  private void writeDataSource(final Writer writer, final DataSource datasource,
                               final CommentHintPath path)
      throws IOException, ReportWriterException
  {
    ObjectDescription od =
        getReportWriter().getClassFactoryCollector().getDescriptionForClass(datasource.getClass());
    if (od == null)
    {
      od = getReportWriter().getClassFactoryCollector().
          getSuperClassObjectDescription(datasource.getClass(), null);
    }
    if (od == null)
    {
      throw new ReportWriterException("Unable to resolve DataSource: " + datasource.getClass());
    }

    final DataSourceCollector dataSourceCollector = getReportWriter().getDataSourceCollector();
    final String dsname = dataSourceCollector.getDataSourceName(od);
    if (dsname == null)
    {
      throw new ReportWriterException("No name for DataSource " + datasource);
    }

    final CommentHintPath dataSourcePath = path.getInstance();
    dataSourcePath.addName(DataSourceHandler.DATASOURCE_TAG);
    writeComment(writer, dataSourcePath, CommentHandler.OPEN_TAG_COMMENT);
    writeTag(writer, DataSourceHandler.DATASOURCE_TAG, "type", dsname, OPEN);

    final DataSourceWriter dsWriter =
        new DataSourceWriter(getReportWriter(), datasource, od, getIndentLevel(), dataSourcePath);
    dsWriter.write(writer);

    writeComment(writer, dataSourcePath, CommentHandler.CLOSE_TAG_COMMENT);
    writeCloseTag(writer, DataSourceHandler.DATASOURCE_TAG);
  }

  /**
   * Writes groups to a character stream writer.
   *
   * @param writer  the character stream writer.
   *
   * @throws IOException if there is an I/O problem.
   * @throws ReportWriterException if there is a problem writing the report.
   */
  private void writeGroups(final Writer writer)
      throws IOException, ReportWriterException
  {
    final CommentHintPath groupsPath = REPORT_DESCRIPTION_HINT_PATH.getInstance();
    groupsPath.addName(ReportDescriptionHandler.GROUPS_TAG);
    writeComment(writer, groupsPath, CommentHandler.OPEN_TAG_COMMENT);
    writeTag(writer, ReportDescriptionHandler.GROUPS_TAG);

    //logComment = true;
    final int groupSize = getReport().getGroupCount();
    for (int i = 0; i < groupSize; i++)
    {
      final Group g = getReport().getGroup(i);
      if (isGroupEmpty(g) == true)
      {
        continue;
      }

      final CommentHintPath groupPath = groupsPath.getInstance();
      groupPath.addName(g);
      writeComment(writer, groupPath, CommentHandler.OPEN_TAG_COMMENT);
      writeTag(writer, GroupsHandler.GROUP_TAG, "name", g.getName(), OPEN);

      final List fields = g.getFields();
      if (fields.isEmpty() == false)
      {
        final CommentHintPath fieldsPath = groupPath.getInstance();
        fieldsPath.addName(GroupHandler.FIELDS_TAG);
        writeComment(writer, fieldsPath, CommentHandler.OPEN_TAG_COMMENT);
        writeTag(writer, GroupHandler.FIELDS_TAG);

        for (int f = 0; f < fields.size(); f++)
        {
          final String field = (String) fields.get(f);
          final CommentHintPath fieldPath = fieldsPath.getInstance();
          fieldPath.addName(field);
          writeComment(writer, fieldPath, CommentHandler.OPEN_TAG_COMMENT);
          writeTag(writer, GroupHandler.FIELD_TAG);
          writer.write(normalize(field));
          writeCloseTag(writer, GroupHandler.FIELD_TAG);
        }
        writeComment(writer, fieldsPath, CommentHandler.CLOSE_TAG_COMMENT);
        writeCloseTag(writer, GroupHandler.FIELDS_TAG);
      }

      writeBand(writer, GroupHandler.GROUP_HEADER_TAG, g.getHeader(), null, groupPath);
      writeBand(writer, GroupHandler.GROUP_FOOTER_TAG, g.getFooter(), null, groupPath);

      writeComment(writer, groupPath, CommentHandler.CLOSE_TAG_COMMENT);
      writeCloseTag(writer, GroupsHandler.GROUP_TAG);
    }

    writeComment(writer, groupsPath, CommentHandler.CLOSE_TAG_COMMENT);
    writeCloseTag(writer, ReportDescriptionHandler.GROUPS_TAG);
    // logComment = false;
  }

  /**
   * Checks, whether a given group is empty. Returns true, if the group does not define
   * any fields and only contains the default bands.
   *
   * @param g the group that should be tested
   * @return true, if the group is empty, false otherwise.
   */
  private boolean isGroupEmpty(final Group g)
  {
    if (g.getFields().isEmpty() == false)
    {
      return false;
    }
    if (isBandEmpty(g.getFooter()) == false)
    {
      return false;
    }
    if (isBandEmpty(g.getHeader()) == false)
    {
      return false;
    }
    return true;
  }

  /**
   * Checks whether the given band is empty.
   * <p>
   * A band is considered empty, if it does only define the a band layoutmanager
   * and no other style definition.
   *
   * @param b the band that should be tested.
   * @return true, if the band does not contain child elements and does not define a
   * specific style.
   */
  private boolean isBandEmpty(final Band b)
  {
    if (b.getElementCount() != 0)
    {
      return false;
    }
    final Iterator it = b.getStyle().getDefinedPropertyNames();
    if (it.hasNext() == false)
    {
      return false;
    }
    final StyleKey o = (StyleKey) it.next();
    if (o.equals(BandLayoutManager.LAYOUTMANAGER))
    {
      if (it.hasNext() == false)
      {
        return true;
      }
    }
    return false;
  }
}

⌨️ 快捷键说明

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