package net.sf.jasperreports.engine.xml;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import net.sf.jasperreports.charts.JRAreaPlot;
import net.sf.jasperreports.charts.JRBar3DPlot;
import net.sf.jasperreports.charts.JRBarPlot;
import net.sf.jasperreports.charts.JRBubblePlot;
import net.sf.jasperreports.charts.JRCandlestickPlot;
import net.sf.jasperreports.charts.JRCategoryDataset;
import net.sf.jasperreports.charts.JRCategorySeries;
import net.sf.jasperreports.charts.JRChartAxis;
import net.sf.jasperreports.charts.JRDataRange;
import net.sf.jasperreports.charts.JRHighLowDataset;
import net.sf.jasperreports.charts.JRHighLowPlot;
import net.sf.jasperreports.charts.JRLinePlot;
import net.sf.jasperreports.charts.JRMeterPlot;
import net.sf.jasperreports.charts.JRMultiAxisPlot;
import net.sf.jasperreports.charts.JRPie3DPlot;
import net.sf.jasperreports.charts.JRPieDataset;
import net.sf.jasperreports.charts.JRPiePlot;
import net.sf.jasperreports.charts.JRScatterPlot;
import net.sf.jasperreports.charts.JRThermometerPlot;
import net.sf.jasperreports.charts.JRTimePeriodDataset;
import net.sf.jasperreports.charts.JRTimePeriodSeries;
import net.sf.jasperreports.charts.JRTimeSeries;
import net.sf.jasperreports.charts.JRTimeSeriesDataset;
import net.sf.jasperreports.charts.JRTimeSeriesPlot;
import net.sf.jasperreports.charts.JRValueDataset;
import net.sf.jasperreports.charts.JRValueDisplay;
import net.sf.jasperreports.charts.JRXyDataset;
import net.sf.jasperreports.charts.JRXySeries;
import net.sf.jasperreports.charts.JRXyzDataset;
import net.sf.jasperreports.charts.JRXyzSeries;
import net.sf.jasperreports.charts.util.JRMeterInterval;
import net.sf.jasperreports.crosstabs.JRCellContents;
import net.sf.jasperreports.crosstabs.JRCrosstab;
import net.sf.jasperreports.crosstabs.JRCrosstabBucket;
import net.sf.jasperreports.crosstabs.JRCrosstabCell;
import net.sf.jasperreports.crosstabs.JRCrosstabColumnGroup;
import net.sf.jasperreports.crosstabs.JRCrosstabDataset;
import net.sf.jasperreports.crosstabs.JRCrosstabMeasure;
import net.sf.jasperreports.crosstabs.JRCrosstabParameter;
import net.sf.jasperreports.crosstabs.JRCrosstabRowGroup;
import net.sf.jasperreports.crosstabs.design.JRDesignCrosstab;
import net.sf.jasperreports.engine.JRBand;
import net.sf.jasperreports.engine.JRBreak;
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartDataset;
import net.sf.jasperreports.engine.JRChartPlot;
import net.sf.jasperreports.engine.JRChild;
import net.sf.jasperreports.engine.JRDataset;
import net.sf.jasperreports.engine.JRDatasetParameter;
import net.sf.jasperreports.engine.JRDatasetRun;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRElementDataset;
import net.sf.jasperreports.engine.JRElementGroup;
import net.sf.jasperreports.engine.JREllipse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRFont;
import net.sf.jasperreports.engine.JRFrame;
import net.sf.jasperreports.engine.JRGraphicElement;
import net.sf.jasperreports.engine.JRGroup;
import net.sf.jasperreports.engine.JRHyperlink;
import net.sf.jasperreports.engine.JRHyperlinkParameter;
import net.sf.jasperreports.engine.JRImage;
import net.sf.jasperreports.engine.JRLine;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JRPropertiesHolder;
import net.sf.jasperreports.engine.JRPropertiesMap;
import net.sf.jasperreports.engine.JRPropertyExpression;
import net.sf.jasperreports.engine.JRQuery;
import net.sf.jasperreports.engine.JRRectangle;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JRReportFont;
import net.sf.jasperreports.engine.JRReportTemplate;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JRSortField;
import net.sf.jasperreports.engine.JRStaticText;
import net.sf.jasperreports.engine.JRStyle;
import net.sf.jasperreports.engine.JRStyleContainer;
import net.sf.jasperreports.engine.JRSubreport;
import net.sf.jasperreports.engine.JRSubreportParameter;
import net.sf.jasperreports.engine.JRSubreportReturnValue;
import net.sf.jasperreports.engine.JRTextElement;
import net.sf.jasperreports.engine.JRTextField;
import net.sf.jasperreports.engine.JRVariable;
import net.sf.jasperreports.engine.util.JRXmlWriteHelper;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.time.Day;

public class JRXmlWriter extends JRXmlBaseWriter {
  private JRReport report = null;
  
  private String encoding = null;
  
  private Map fontsMap = new HashMap();
  
  private XmlWriterVisitor xmlWriterVisitor = new XmlWriterVisitor(this);
  
  protected JRXmlWriter(JRReport report, String encoding) {
    this.report = report;
    this.encoding = encoding;
  }
  
  public static String writeReport(JRReport report, String encoding) {
    JRXmlWriter writer = new JRXmlWriter(report, encoding);
    StringWriter buffer = new StringWriter();
    try {
      writer.writeReport(buffer);
    } catch (IOException e) {
      throw new JRRuntimeException("Error writing report design.", e);
    } 
    return buffer.toString();
  }
  
  public static void writeReport(JRReport report, String destFileName, String encoding) throws JRException {
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(destFileName);
      Writer out = new OutputStreamWriter(fos, encoding);
      JRXmlWriter writer = new JRXmlWriter(report, encoding);
      writer.writeReport(out);
    } catch (IOException e) {
      throw new JRException("Error writing to file : " + destFileName, e);
    } finally {
      if (fos != null)
        try {
          fos.close();
        } catch (IOException e) {} 
    } 
  }
  
  public static void writeReport(JRReport report, OutputStream outputStream, String encoding) throws JRException {
    try {
      Writer out = new OutputStreamWriter(outputStream, encoding);
      JRXmlWriter writer = new JRXmlWriter(report, encoding);
      writer.writeReport(out);
    } catch (Exception e) {
      throw new JRException("Error writing to OutputStream : " + report.getName(), e);
    } 
  }
  
  protected void writeReport(Writer out) throws IOException {
    useWriter(new JRXmlWriteHelper(out));
    this.writer.writeProlog(this.encoding);
    this.writer.writePublicDoctype("jasperReport", "-//JasperReports//DTD JasperReport//EN", "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd");
    this.writer.startElement("jasperReport");
    this.writer.addEncodedAttribute("name", this.report.getName());
    this.writer.addEncodedAttribute("language", this.report.getLanguage(), "java");
    this.writer.addAttribute("columnCount", this.report.getColumnCount(), 1);
    this.writer.addAttribute("printOrder", this.report.getPrintOrder(), JRXmlConstants.getPrintOrderMap(), (byte)1);
    this.writer.addAttribute("pageWidth", this.report.getPageWidth());
    this.writer.addAttribute("pageHeight", this.report.getPageHeight());
    this.writer.addAttribute("orientation", this.report.getOrientation(), JRXmlConstants.getOrientationMap(), (byte)1);
    this.writer.addAttribute("whenNoDataType", this.report.getWhenNoDataType(), JRXmlConstants.getWhenNoDataTypeMap(), (byte)1);
    this.writer.addAttribute("columnWidth", this.report.getColumnWidth());
    this.writer.addAttribute("columnSpacing", this.report.getColumnSpacing(), 0);
    this.writer.addAttribute("leftMargin", this.report.getLeftMargin());
    this.writer.addAttribute("rightMargin", this.report.getRightMargin());
    this.writer.addAttribute("topMargin", this.report.getTopMargin());
    this.writer.addAttribute("bottomMargin", this.report.getBottomMargin());
    this.writer.addAttribute("isTitleNewPage", this.report.isTitleNewPage(), false);
    this.writer.addAttribute("isSummaryNewPage", this.report.isSummaryNewPage(), false);
    this.writer.addAttribute("isFloatColumnFooter", this.report.isFloatColumnFooter(), false);
    this.writer.addAttribute("scriptletClass", this.report.getScriptletClass());
    this.writer.addAttribute("formatFactoryClass", this.report.getFormatFactoryClass());
    this.writer.addEncodedAttribute("resourceBundle", this.report.getResourceBundle());
    this.writer.addAttribute("whenResourceMissingType", this.report.getWhenResourceMissingType(), JRXmlConstants.getWhenResourceMissingTypeMap(), (byte)1);
    this.writer.addAttribute("isIgnorePagination", this.report.isIgnorePagination(), false);
    writeProperties((JRPropertiesHolder)this.report);
    String[] imports = this.report.getImports();
    if (imports != null && imports.length > 0)
      for (int i = 0; i < imports.length; i++) {
        String value = imports[i];
        if (value != null) {
          this.writer.startElement("import");
          this.writer.addEncodedAttribute("value", value);
          this.writer.closeElement();
        } 
      }  
    writeTemplates();
    JRReportFont[] fonts = this.report.getFonts();
    if (fonts != null && fonts.length > 0)
      for (int i = 0; i < fonts.length; i++) {
        this.fontsMap.put(fonts[i].getName(), fonts[i]);
        writeReportFont(fonts[i]);
      }  
    JRStyle[] styles = this.report.getStyles();
    if (styles != null && styles.length > 0)
      for (int i = 0; i < styles.length; i++)
        writeStyle(styles[i]);  
    JRDataset[] datasets = this.report.getDatasets();
    if (datasets != null && datasets.length > 0)
      for (int i = 0; i < datasets.length; i++)
        writeDataset(datasets[i]);  
    writeDatasetContents(this.report.getMainDataset());
    if (this.report.getBackground() != null) {
      this.writer.startElement("background");
      writeBand(this.report.getBackground());
      this.writer.closeElement();
    } 
    if (this.report.getTitle() != null) {
      this.writer.startElement("title");
      writeBand(this.report.getTitle());
      this.writer.closeElement();
    } 
    if (this.report.getPageHeader() != null) {
      this.writer.startElement("pageHeader");
      writeBand(this.report.getPageHeader());
      this.writer.closeElement();
    } 
    if (this.report.getColumnHeader() != null) {
      this.writer.startElement("columnHeader");
      writeBand(this.report.getColumnHeader());
      this.writer.closeElement();
    } 
    if (this.report.getDetail() != null) {
      this.writer.startElement("detail");
      writeBand(this.report.getDetail());
      this.writer.closeElement();
    } 
    if (this.report.getColumnFooter() != null) {
      this.writer.startElement("columnFooter");
      writeBand(this.report.getColumnFooter());
      this.writer.closeElement();
    } 
    if (this.report.getPageFooter() != null) {
      this.writer.startElement("pageFooter");
      writeBand(this.report.getPageFooter());
      this.writer.closeElement();
    } 
    if (this.report.getLastPageFooter() != null) {
      this.writer.startElement("lastPageFooter");
      writeBand(this.report.getLastPageFooter());
      this.writer.closeElement();
    } 
    if (this.report.getSummary() != null) {
      this.writer.startElement("summary");
      writeBand(this.report.getSummary());
      this.writer.closeElement();
    } 
    if (this.report.getNoData() != null) {
      this.writer.startElement("noData");
      writeBand(this.report.getNoData());
      this.writer.closeElement();
    } 
    this.writer.closeElement();
    out.flush();
  }
  
  private void writeProperties(JRPropertiesHolder propertiesHolder) throws IOException {
    if (propertiesHolder.hasProperties()) {
      JRPropertiesMap propertiesMap = propertiesHolder.getPropertiesMap();
      String[] propertyNames = propertiesMap.getPropertyNames();
      if (propertyNames != null && propertyNames.length > 0)
        for (int i = 0; i < propertyNames.length; i++) {
          this.writer.startElement("property");
          this.writer.addEncodedAttribute("name", propertyNames[i]);
          String value = propertiesMap.getProperty(propertyNames[i]);
          if (value != null)
            this.writer.addEncodedAttribute("value", value); 
          this.writer.closeElement();
        }  
    } 
  }
  
  protected void writeTemplates() throws IOException {
    JRReportTemplate[] templates = this.report.getTemplates();
    if (templates != null)
      for (int i = 0; i < templates.length; i++) {
        JRReportTemplate template = templates[i];
        writeTemplate(template);
      }  
  }
  
  protected void writeTemplate(JRReportTemplate template) throws IOException {
    this.writer.writeExpression("template", template.getSourceExpression(), true, String.class.getName());
  }
  
  private void writeReportFont(JRReportFont font) throws IOException {
    this.writer.startElement("reportFont");
    this.writer.addEncodedAttribute("name", font.getName());
    this.writer.addAttribute("isDefault", font.isDefault());
    this.writer.addEncodedAttribute("fontName", font.getOwnFontName());
    this.writer.addAttribute("size", font.getOwnFontSize());
    this.writer.addAttribute("isBold", font.isOwnBold());
    this.writer.addAttribute("isItalic", font.isOwnItalic());
    this.writer.addAttribute("isUnderline", font.isOwnUnderline());
    this.writer.addAttribute("isStrikeThrough", font.isOwnStrikeThrough());
    this.writer.addEncodedAttribute("pdfFontName", font.getOwnPdfFontName());
    this.writer.addEncodedAttribute("pdfEncoding", font.getOwnPdfEncoding());
    this.writer.addAttribute("isPdfEmbedded", font.isOwnPdfEmbedded());
    this.writer.closeElement();
  }
  
  private void writeParameter(JRParameter parameter) throws IOException {
    this.writer.startElement("parameter");
    this.writer.addEncodedAttribute("name", parameter.getName());
    this.writer.addAttribute("class", parameter.getValueClassName());
    this.writer.addAttribute("isForPrompting", parameter.isForPrompting(), true);
    writeProperties((JRPropertiesHolder)parameter);
    this.writer.writeCDATAElement("parameterDescription", parameter.getDescription());
    this.writer.writeExpression("defaultValueExpression", parameter.getDefaultValueExpression(), false);
    this.writer.closeElement();
  }
  
  private void writeQuery(JRQuery query) throws IOException {
    this.writer.startElement("queryString");
    this.writer.addEncodedAttribute("language", query.getLanguage(), "sql");
    this.writer.writeCDATA(query.getText());
    this.writer.closeElement();
  }
  
  private void writeField(JRField field) throws IOException {
    this.writer.startElement("field");
    this.writer.addEncodedAttribute("name", field.getName());
    this.writer.addAttribute("class", field.getValueClassName());
    writeProperties((JRPropertiesHolder)field);
    this.writer.writeCDATAElement("fieldDescription", field.getDescription());
    this.writer.closeElement();
  }
  
  private void writeSortField(JRSortField sortField) throws IOException {
    this.writer.startElement("sortField");
    this.writer.addEncodedAttribute("name", sortField.getName());
    this.writer.addAttribute("order", sortField.getOrder(), JRXmlConstants.getSortOrderMap(), (byte)0);
    this.writer.closeElement();
  }
  
  private void writeVariable(JRVariable variable) throws IOException {
    this.writer.startElement("variable");
    this.writer.addEncodedAttribute("name", variable.getName());
    this.writer.addAttribute("class", variable.getValueClassName());
    this.writer.addAttribute("resetType", variable.getResetType(), JRXmlConstants.getResetTypeMap(), (byte)1);
    if (variable.getResetGroup() != null)
      this.writer.addEncodedAttribute("resetGroup", variable.getResetGroup().getName()); 
    this.writer.addAttribute("incrementType", variable.getIncrementType(), JRXmlConstants.getResetTypeMap(), (byte)5);
    if (variable.getIncrementGroup() != null)
      this.writer.addEncodedAttribute("incrementGroup", variable.getIncrementGroup().getName()); 
    this.writer.addAttribute("calculation", variable.getCalculation(), JRXmlConstants.getCalculationMap(), (byte)0);
    this.writer.addAttribute("incrementerFactoryClass", variable.getIncrementerFactoryClassName());
    this.writer.writeExpression("variableExpression", variable.getExpression(), false);
    this.writer.writeExpression("initialValueExpression", variable.getInitialValueExpression(), false);
    this.writer.closeElement();
  }
  
  private void writeGroup(JRGroup group) throws IOException {
    this.writer.startElement("group");
    this.writer.addEncodedAttribute("name", group.getName());
    this.writer.addAttribute("isStartNewColumn", group.isStartNewColumn(), false);
    this.writer.addAttribute("isStartNewPage", group.isStartNewPage(), false);
    this.writer.addAttribute("isResetPageNumber", group.isResetPageNumber(), false);
    this.writer.addAttribute("isReprintHeaderOnEachPage", group.isReprintHeaderOnEachPage(), false);
    this.writer.addAttributePositive("minHeightToStartNewPage", group.getMinHeightToStartNewPage());
    this.writer.writeExpression("groupExpression", group.getExpression(), false);
    if (group.getGroupHeader() != null) {
      this.writer.startElement("groupHeader");
      writeBand(group.getGroupHeader());
      this.writer.closeElement();
    } 
    if (group.getGroupFooter() != null) {
      this.writer.startElement("groupFooter");
      writeBand(group.getGroupFooter());
      this.writer.closeElement();
    } 
    this.writer.closeElement();
  }
  
  private void writeBand(JRBand band) throws IOException {
    this.writer.startElement("band");
    this.writer.addAttributePositive("height", band.getHeight());
    this.writer.addAttribute("isSplitAllowed", band.isSplitAllowed(), true);
    this.writer.writeExpression("printWhenExpression", band.getPrintWhenExpression(), false);
    List children = band.getChildren();
    if (children != null && children.size() > 0)
      for (int i = 0; i < children.size(); i++)
        ((JRChild)children.get(i)).visit(this.xmlWriterVisitor);  
    this.writer.closeElement();
  }
  
  public void writeElementGroup(JRElementGroup elementGroup) throws IOException {
    this.writer.startElement("elementGroup");
    List children = elementGroup.getChildren();
    if (children != null && children.size() > 0)
      for (int i = 0; i < children.size(); i++)
        ((JRChild)children.get(i)).visit(this.xmlWriterVisitor);  
    this.writer.closeElement();
  }
  
  public void writeBreak(JRBreak breakElement) throws IOException {
    this.writer.startElement("break");
    this.writer.addAttribute("type", breakElement.getType(), JRXmlConstants.getBreakTypeMap(), (byte)1);
    writeReportElement((JRElement)breakElement);
    this.writer.closeElement();
  }
  
  public void writeLine(JRLine line) throws IOException {
    this.writer.startElement("line");
    this.writer.addAttribute("direction", line.getDirection(), JRXmlConstants.getDirectionMap(), (byte)1);
    writeReportElement((JRElement)line);
    writeGraphicElement((JRGraphicElement)line);
    this.writer.closeElement();
  }
  
  private void writeReportElement(JRElement element) throws IOException {
    this.writer.startElement("reportElement");
    this.writer.addEncodedAttribute("key", element.getKey());
    writeStyleReferenceAttr((JRStyleContainer)element);
    this.writer.addAttribute("positionType", element.getPositionType(), JRXmlConstants.getPositionTypeMap(), (byte)2);
    this.writer.addAttribute("stretchType", element.getStretchType(), JRXmlConstants.getStretchTypeMap(), (byte)0);
    this.writer.addAttribute("isPrintRepeatedValues", element.isPrintRepeatedValues(), true);
    this.writer.addAttribute("mode", element.getOwnMode(), JRXmlConstants.getModeMap());
    this.writer.addAttribute("x", element.getX());
    this.writer.addAttribute("y", element.getY());
    this.writer.addAttribute("width", element.getWidth());
    this.writer.addAttribute("height", element.getHeight());
    this.writer.addAttribute("isRemoveLineWhenBlank", element.isRemoveLineWhenBlank(), false);
    this.writer.addAttribute("isPrintInFirstWholeBand", element.isPrintInFirstWholeBand(), false);
    this.writer.addAttribute("isPrintWhenDetailOverflows", element.isPrintWhenDetailOverflows(), false);
    if (element.getPrintWhenGroupChanges() != null)
      this.writer.addEncodedAttribute("printWhenGroupChanges", element.getPrintWhenGroupChanges().getName()); 
    this.writer.addAttribute("forecolor", element.getOwnForecolor());
    this.writer.addAttribute("backcolor", element.getOwnBackcolor());
    writeProperties((JRPropertiesHolder)element);
    writePropertyExpressions(element.getPropertyExpressions());
    this.writer.writeExpression("printWhenExpression", element.getPrintWhenExpression(), false);
    this.writer.closeElement();
  }
  
  protected void writePropertyExpressions(JRPropertyExpression[] propertyExpressions) throws IOException {
    if (propertyExpressions != null)
      for (int i = 0; i < propertyExpressions.length; i++)
        writePropertyExpression(propertyExpressions[i]);  
  }
  
  protected void writePropertyExpression(JRPropertyExpression propertyExpression) throws IOException {
    String expressionText = propertyExpression.getValueExpression().getText();
    this.writer.writeCDATAElement("propertyExpression", expressionText, "name", propertyExpression.getName());
  }
  
  private void writeGraphicElement(JRGraphicElement element) throws IOException {
    this.writer.startElement("graphicElement");
    this.writer.addAttribute("fill", element.getOwnFill(), JRXmlConstants.getFillMap());
    writePen(element.getLinePen());
    this.writer.closeElement(true);
  }
  
  public void writeRectangle(JRRectangle rectangle) throws IOException {
    this.writer.startElement("rectangle");
    this.writer.addAttribute("radius", rectangle.getOwnRadius());
    writeReportElement((JRElement)rectangle);
    writeGraphicElement((JRGraphicElement)rectangle);
    this.writer.closeElement();
  }
  
  public void writeEllipse(JREllipse ellipse) throws IOException {
    this.writer.startElement("ellipse");
    writeReportElement((JRElement)ellipse);
    writeGraphicElement((JRGraphicElement)ellipse);
    this.writer.closeElement();
  }
  
  public void writeImage(JRImage image) throws IOException {
    this.writer.startElement("image");
    this.writer.addAttribute("scaleImage", image.getOwnScaleImage(), JRXmlConstants.getScaleImageMap());
    this.writer.addAttribute("hAlign", image.getOwnHorizontalAlignment(), JRXmlConstants.getHorizontalAlignMap());
    this.writer.addAttribute("vAlign", image.getOwnVerticalAlignment(), JRXmlConstants.getVerticalAlignMap());
    this.writer.addAttribute("isUsingCache", image.isOwnUsingCache());
    this.writer.addAttribute("isLazy", image.isLazy(), false);
    this.writer.addAttribute("onErrorType", image.getOnErrorType(), JRXmlConstants.getOnErrorTypeMap(), (byte)1);
    this.writer.addAttribute("evaluationTime", image.getEvaluationTime(), JRXmlConstants.getEvaluationTimeMap(), (byte)1);
    if (image.getEvaluationGroup() != null)
      this.writer.addEncodedAttribute("evaluationGroup", image.getEvaluationGroup().getName()); 
    this.writer.addEncodedAttribute("hyperlinkType", image.getLinkType());
    this.writer.addAttribute("hyperlinkTarget", image.getHyperlinkTarget(), JRXmlConstants.getHyperlinkTargetMap(), (byte)1);
    this.writer.addAttribute("bookmarkLevel", image.getBookmarkLevel(), 0);
    writeReportElement((JRElement)image);
    writeBox(image.getLineBox());
    writeGraphicElement((JRGraphicElement)image);
    this.writer.writeExpression("imageExpression", image.getExpression(), true);
    this.writer.writeExpression("anchorNameExpression", image.getAnchorNameExpression(), false);
    this.writer.writeExpression("hyperlinkReferenceExpression", image.getHyperlinkReferenceExpression(), false);
    this.writer.writeExpression("hyperlinkAnchorExpression", image.getHyperlinkAnchorExpression(), false);
    this.writer.writeExpression("hyperlinkPageExpression", image.getHyperlinkPageExpression(), false);
    this.writer.writeExpression("hyperlinkTooltipExpression", image.getHyperlinkTooltipExpression(), false);
    writeHyperlinkParameters(image.getHyperlinkParameters());
    this.writer.closeElement();
  }
  
  public void writeStaticText(JRStaticText staticText) throws IOException {
    this.writer.startElement("staticText");
    writeReportElement((JRElement)staticText);
    writeBox(staticText.getLineBox());
    writeTextElement((JRTextElement)staticText);
    this.writer.writeCDATAElement("text", staticText.getText());
    this.writer.closeElement();
  }
  
  private void writeTextElement(JRTextElement textElement) throws IOException {
    this.writer.startElement("textElement");
    this.writer.addAttribute("textAlignment", textElement.getOwnHorizontalAlignment(), JRXmlConstants.getHorizontalAlignMap());
    this.writer.addAttribute("verticalAlignment", textElement.getOwnVerticalAlignment(), JRXmlConstants.getVerticalAlignMap());
    this.writer.addAttribute("rotation", textElement.getOwnRotation(), JRXmlConstants.getRotationMap());
    this.writer.addAttribute("lineSpacing", textElement.getOwnLineSpacing(), JRXmlConstants.getLineSpacingMap());
    this.writer.addAttribute("markup", textElement.getOwnMarkup());
    writeFont((JRFont)textElement);
    this.writer.closeElement();
  }
  
  private void writeFont(JRFont font) throws IOException {
    if (font != null) {
      this.writer.startElement("font");
      if (font.getReportFont() != null) {
        JRFont baseFont = (JRFont)this.fontsMap.get(font.getReportFont().getName());
        if (baseFont != null) {
          this.writer.addEncodedAttribute("reportFont", font.getReportFont().getName());
        } else {
          throw new JRRuntimeException("Referenced report font not found : " + font.getReportFont().getName());
        } 
      } 
      this.writer.addEncodedAttribute("fontName", font.getOwnFontName());
      this.writer.addAttribute("size", font.getOwnFontSize());
      this.writer.addAttribute("isBold", font.isOwnBold());
      this.writer.addAttribute("isItalic", font.isOwnItalic());
      this.writer.addAttribute("isUnderline", font.isOwnUnderline());
      this.writer.addAttribute("isStrikeThrough", font.isOwnStrikeThrough());
      this.writer.addEncodedAttribute("pdfFontName", font.getOwnPdfFontName());
      this.writer.addEncodedAttribute("pdfEncoding", font.getOwnPdfEncoding());
      this.writer.addAttribute("isPdfEmbedded", font.isOwnPdfEmbedded());
      this.writer.closeElement(true);
    } 
  }
  
  public void writeTextField(JRTextField textField) throws IOException {
    this.writer.startElement("textField");
    this.writer.addAttribute("isStretchWithOverflow", textField.isStretchWithOverflow(), false);
    this.writer.addAttribute("evaluationTime", textField.getEvaluationTime(), JRXmlConstants.getEvaluationTimeMap(), (byte)1);
    if (textField.getEvaluationGroup() != null)
      this.writer.addEncodedAttribute("evaluationGroup", textField.getEvaluationGroup().getName()); 
    this.writer.addEncodedAttribute("pattern", textField.getOwnPattern());
    this.writer.addAttribute("isBlankWhenNull", textField.isOwnBlankWhenNull());
    this.writer.addEncodedAttribute("hyperlinkType", textField.getLinkType());
    this.writer.addAttribute("hyperlinkTarget", textField.getHyperlinkTarget(), JRXmlConstants.getHyperlinkTargetMap(), (byte)1);
    this.writer.addAttribute("bookmarkLevel", textField.getBookmarkLevel(), 0);
    writeReportElement((JRElement)textField);
    writeBox(textField.getLineBox());
    writeTextElement((JRTextElement)textField);
    this.writer.writeExpression("textFieldExpression", textField.getExpression(), true);
    this.writer.writeExpression("anchorNameExpression", textField.getAnchorNameExpression(), false);
    this.writer.writeExpression("hyperlinkReferenceExpression", textField.getHyperlinkReferenceExpression(), false);
    this.writer.writeExpression("hyperlinkAnchorExpression", textField.getHyperlinkAnchorExpression(), false);
    this.writer.writeExpression("hyperlinkPageExpression", textField.getHyperlinkPageExpression(), false);
    this.writer.writeExpression("hyperlinkTooltipExpression", textField.getHyperlinkTooltipExpression(), false);
    writeHyperlinkParameters(textField.getHyperlinkParameters());
    this.writer.closeElement();
  }
  
  public void writeSubreport(JRSubreport subreport) throws IOException {
    this.writer.startElement("subreport");
    this.writer.addAttribute("isUsingCache", subreport.isOwnUsingCache());
    writeReportElement((JRElement)subreport);
    this.writer.writeExpression("parametersMapExpression", subreport.getParametersMapExpression(), false);
    JRSubreportParameter[] parameters = subreport.getParameters();
    if (parameters != null && parameters.length > 0)
      for (int i = 0; i < parameters.length; i++)
        writeSubreportParameter(parameters[i]);  
    this.writer.writeExpression("connectionExpression", subreport.getConnectionExpression(), false);
    this.writer.writeExpression("dataSourceExpression", subreport.getDataSourceExpression(), false);
    JRSubreportReturnValue[] returnValues = subreport.getReturnValues();
    if (returnValues != null && returnValues.length > 0)
      for (int i = 0; i < returnValues.length; i++)
        writeSubreportReturnValue(returnValues[i]);  
    this.writer.writeExpression("subreportExpression", subreport.getExpression(), true);
    this.writer.closeElement();
  }
  
  private void writeSubreportParameter(JRSubreportParameter subreportParameter) throws IOException {
    this.writer.startElement("subreportParameter");
    this.writer.addEncodedAttribute("name", subreportParameter.getName());
    this.writer.writeExpression("subreportParameterExpression", subreportParameter.getExpression(), false);
    this.writer.closeElement();
  }
  
  private void writeDatasetParameter(JRDatasetParameter datasetParameter) throws IOException {
    this.writer.startElement("datasetParameter");
    this.writer.addEncodedAttribute("name", datasetParameter.getName());
    this.writer.writeExpression("datasetParameterExpression", datasetParameter.getExpression(), false);
    this.writer.closeElement();
  }
  
  private void writeChart(JRChart chart) throws IOException {
    this.writer.startElement("chart");
    this.writer.addAttribute("isShowLegend", chart.isShowLegend(), true);
    this.writer.addAttribute("evaluationTime", chart.getEvaluationTime(), JRXmlConstants.getEvaluationTimeMap(), (byte)1);
    if (chart.getEvaluationTime() == 5)
      this.writer.addEncodedAttribute("evaluationGroup", chart.getEvaluationGroup().getName()); 
    this.writer.addEncodedAttribute("hyperlinkType", chart.getLinkType());
    this.writer.addAttribute("hyperlinkTarget", chart.getHyperlinkTarget(), JRXmlConstants.getHyperlinkTargetMap(), (byte)1);
    this.writer.addAttribute("bookmarkLevel", chart.getBookmarkLevel(), 0);
    this.writer.addAttribute("customizerClass", chart.getCustomizerClass());
    this.writer.addAttribute("renderType", chart.getRenderType());
    writeReportElement((JRElement)chart);
    writeBox(chart.getLineBox());
    this.writer.startElement("chartTitle");
    this.writer.addAttribute("position", chart.getTitlePosition(), JRXmlConstants.getChartEdgeMap(), (byte)1);
    this.writer.addAttribute("color", chart.getOwnTitleColor());
    writeFont(chart.getTitleFont());
    if (chart.getTitleExpression() != null)
      this.writer.writeExpression("titleExpression", chart.getTitleExpression(), false); 
    this.writer.closeElement();
    this.writer.startElement("chartSubtitle");
    this.writer.addAttribute("color", chart.getOwnSubtitleColor());
    writeFont(chart.getSubtitleFont());
    if (chart.getSubtitleExpression() != null)
      this.writer.writeExpression("subtitleExpression", chart.getSubtitleExpression(), false); 
    this.writer.closeElement();
    this.writer.startElement("chartLegend");
    if (chart.getOwnLegendColor() != null)
      this.writer.addAttribute("textColor", chart.getOwnLegendColor()); 
    if (chart.getOwnLegendBackgroundColor() != null)
      this.writer.addAttribute("backgroundColor", chart.getOwnLegendBackgroundColor()); 
    this.writer.addAttribute("position", chart.getLegendPosition(), JRXmlConstants.getChartEdgeMap(), (byte)2);
    writeFont(chart.getLegendFont());
    this.writer.closeElement();
    this.writer.writeExpression("anchorNameExpression", chart.getAnchorNameExpression(), false);
    this.writer.writeExpression("hyperlinkReferenceExpression", chart.getHyperlinkReferenceExpression(), false);
    this.writer.writeExpression("hyperlinkAnchorExpression", chart.getHyperlinkAnchorExpression(), false);
    this.writer.writeExpression("hyperlinkPageExpression", chart.getHyperlinkPageExpression(), false);
    this.writer.writeExpression("hyperlinkTooltipExpression", chart.getHyperlinkTooltipExpression(), false);
    writeHyperlinkParameters(chart.getHyperlinkParameters());
    this.writer.closeElement();
  }
  
  private void writeElementDataset(JRElementDataset dataset) throws IOException {
    this.writer.startElement("dataset");
    this.writer.addAttribute("resetType", dataset.getResetType(), JRXmlConstants.getResetTypeMap(), (byte)1);
    if (dataset.getResetType() == 4)
      this.writer.addEncodedAttribute("resetGroup", dataset.getResetGroup().getName()); 
    this.writer.addAttribute("incrementType", dataset.getIncrementType(), JRXmlConstants.getResetTypeMap(), (byte)5);
    if (dataset.getIncrementType() == 4)
      this.writer.addEncodedAttribute("incrementGroup", dataset.getIncrementGroup().getName()); 
    this.writer.writeExpression("incrementWhenExpression", dataset.getIncrementWhenExpression(), false);
    JRDatasetRun datasetRun = dataset.getDatasetRun();
    if (datasetRun != null)
      writeDatasetRun(datasetRun); 
    this.writer.closeElement(true);
  }
  
  private void writeCategoryDataSet(JRCategoryDataset dataset) throws IOException {
    this.writer.startElement("categoryDataset");
    writeElementDataset((JRElementDataset)dataset);
    JRCategorySeries[] categorySeries = dataset.getSeries();
    if (categorySeries != null && categorySeries.length > 0)
      for (int i = 0; i < categorySeries.length; i++)
        writeCategorySeries(categorySeries[i]);  
    this.writer.closeElement();
  }
  
  private void writeTimeSeriesDataset(JRTimeSeriesDataset dataset) throws IOException {
    this.writer.startElement("timeSeriesDataset");
    if (dataset.getTimePeriod() != null && !Day.class.getName().equals(dataset.getTimePeriod().getName()))
      this.writer.addAttribute("timePeriod", JRXmlConstants.getTimePeriodName(dataset.getTimePeriod())); 
    writeElementDataset((JRElementDataset)dataset);
    JRTimeSeries[] timeSeries = dataset.getSeries();
    if (timeSeries != null && timeSeries.length > 0)
      for (int i = 0; i < timeSeries.length; i++)
        writeTimeSeries(timeSeries[i]);  
    this.writer.closeElement();
  }
  
  private void writeTimePeriodDataset(JRTimePeriodDataset dataset) throws IOException {
    this.writer.startElement("timePeriodDataset");
    writeElementDataset((JRElementDataset)dataset);
    JRTimePeriodSeries[] timePeriodSeries = dataset.getSeries();
    if (timePeriodSeries != null && timePeriodSeries.length > 0)
      for (int i = 0; i < timePeriodSeries.length; i++)
        writeTimePeriodSeries(timePeriodSeries[i]);  
    this.writer.closeElement();
  }
  
  private void writeCategorySeries(JRCategorySeries categorySeries) throws IOException {
    this.writer.startElement("categorySeries");
    this.writer.writeExpression("seriesExpression", categorySeries.getSeriesExpression(), false);
    this.writer.writeExpression("categoryExpression", categorySeries.getCategoryExpression(), false);
    this.writer.writeExpression("valueExpression", categorySeries.getValueExpression(), false);
    this.writer.writeExpression("labelExpression", categorySeries.getLabelExpression(), false);
    writeHyperlink("itemHyperlink", categorySeries.getItemHyperlink());
    this.writer.closeElement();
  }
  
  private void writeXyzDataset(JRXyzDataset dataset) throws IOException {
    this.writer.startElement("xyzDataset");
    writeElementDataset((JRElementDataset)dataset);
    JRXyzSeries[] series = dataset.getSeries();
    if (series != null && series.length > 0)
      for (int i = 0; i < series.length; i++)
        writeXyzSeries(series[i]);  
    this.writer.closeElement();
  }
  
  private void writeXyzSeries(JRXyzSeries series) throws IOException {
    this.writer.startElement("xyzSeries");
    this.writer.writeExpression("seriesExpression", series.getSeriesExpression(), false);
    this.writer.writeExpression("xValueExpression", series.getXValueExpression(), false);
    this.writer.writeExpression("yValueExpression", series.getYValueExpression(), false);
    this.writer.writeExpression("zValueExpression", series.getZValueExpression(), false);
    writeHyperlink("itemHyperlink", series.getItemHyperlink());
    this.writer.closeElement();
  }
  
  private void writeXySeries(JRXySeries xySeries) throws IOException {
    this.writer.startElement("xySeries");
    this.writer.writeExpression("seriesExpression", xySeries.getSeriesExpression(), false);
    this.writer.writeExpression("xValueExpression", xySeries.getXValueExpression(), false);
    this.writer.writeExpression("yValueExpression", xySeries.getYValueExpression(), false);
    this.writer.writeExpression("labelExpression", xySeries.getLabelExpression(), false);
    writeHyperlink("itemHyperlink", xySeries.getItemHyperlink());
    this.writer.closeElement();
  }
  
  private void writeXyDataset(JRXyDataset dataset) throws IOException {
    this.writer.startElement("xyDataset");
    writeElementDataset((JRElementDataset)dataset);
    JRXySeries[] xySeries = dataset.getSeries();
    if (xySeries != null && xySeries.length > 0)
      for (int i = 0; i < xySeries.length; i++)
        writeXySeries(xySeries[i]);  
    this.writer.closeElement();
  }
  
  private void writeTimeSeries(JRTimeSeries timeSeries) throws IOException {
    this.writer.startElement("timeSeries");
    this.writer.writeExpression("seriesExpression", timeSeries.getSeriesExpression(), false);
    this.writer.writeExpression("timePeriodExpression", timeSeries.getTimePeriodExpression(), false);
    this.writer.writeExpression("valueExpression", timeSeries.getValueExpression(), false);
    this.writer.writeExpression("labelExpression", timeSeries.getLabelExpression(), false);
    writeHyperlink("itemHyperlink", timeSeries.getItemHyperlink());
    this.writer.closeElement();
  }
  
  private void writeTimePeriodSeries(JRTimePeriodSeries timePeriodSeries) throws IOException {
    this.writer.startElement("timePeriodSeries");
    this.writer.writeExpression("seriesExpression", timePeriodSeries.getSeriesExpression(), false);
    this.writer.writeExpression("startDateExpression", timePeriodSeries.getStartDateExpression(), false);
    this.writer.writeExpression("endDateExpression", timePeriodSeries.getEndDateExpression(), false);
    this.writer.writeExpression("valueExpression", timePeriodSeries.getValueExpression(), false);
    this.writer.writeExpression("labelExpression", timePeriodSeries.getLabelExpression(), false);
    writeHyperlink("itemHyperlink", timePeriodSeries.getItemHyperlink());
    this.writer.closeElement();
  }
  
  public void writePieDataset(JRPieDataset dataset) throws IOException {
    this.writer.startElement("pieDataset");
    writeElementDataset((JRElementDataset)dataset);
    this.writer.writeExpression("keyExpression", dataset.getKeyExpression(), false);
    this.writer.writeExpression("valueExpression", dataset.getValueExpression(), false);
    this.writer.writeExpression("labelExpression", dataset.getLabelExpression(), false);
    writeHyperlink("sectionHyperlink", dataset.getSectionHyperlink());
    this.writer.closeElement();
  }
  
  public void writeValueDataset(JRValueDataset dataset) throws IOException {
    this.writer.startElement("valueDataset");
    writeElementDataset((JRElementDataset)dataset);
    this.writer.writeExpression("valueExpression", dataset.getValueExpression(), false);
    this.writer.closeElement();
  }
  
  public void writeValueDisplay(JRValueDisplay valueDisplay) throws IOException {
    this.writer.startElement("valueDisplay");
    this.writer.addAttribute("color", valueDisplay.getColor());
    this.writer.addAttribute("mask", valueDisplay.getMask());
    writeFont(valueDisplay.getFont());
    this.writer.closeElement();
  }
  
  public void writeDataRange(JRDataRange dataRange) throws IOException {
    this.writer.startElement("dataRange");
    this.writer.writeExpression("lowExpression", dataRange.getLowExpression(), false);
    this.writer.writeExpression("highExpression", dataRange.getHighExpression(), false);
    this.writer.closeElement();
  }
  
  private void writeMeterInterval(JRMeterInterval interval) throws IOException {
    this.writer.startElement("meterInterval");
    this.writer.addAttribute("label", interval.getLabel());
    this.writer.addAttribute("color", interval.getBackgroundColor());
    this.writer.addAttribute("alpha", interval.getAlpha());
    writeDataRange(interval.getDataRange());
    this.writer.closeElement();
  }
  
  private void writeSeriesColors(SortedSet seriesColors) throws IOException {
    if (seriesColors == null || seriesColors.size() == 0)
      return; 
    JRChartPlot.JRSeriesColor[] colors = (JRChartPlot.JRSeriesColor[])seriesColors.toArray((Object[])new JRChartPlot.JRSeriesColor[0]);
    for (int i = 0; i < colors.length; i++) {
      this.writer.startElement("seriesColor");
      this.writer.addAttribute("seriesOrder", colors[i].getSeriesOrder());
      this.writer.addAttribute("color", colors[i].getColor());
      this.writer.closeElement();
    } 
  }
  
  private void writeChartAxis(JRChartAxis chartAxis) throws IOException {
    this.writer.startElement("axis");
    this.writer.addAttribute("position", chartAxis.getPosition(), JRXmlConstants.getAxisPositionMap(), (byte)1);
    writeChartTag(chartAxis.getChart());
    this.writer.closeElement();
  }
  
  private void writePlot(JRChartPlot plot) throws IOException {
    this.writer.startElement("plot");
    this.writer.addAttribute("backcolor", plot.getOwnBackcolor());
    this.writer.addAttribute("orientation", plot.getOrientation(), JRXmlConstants.getPlotOrientationMap(), PlotOrientation.VERTICAL);
    this.writer.addAttribute("backgroundAlpha", plot.getBackgroundAlpha(), 1.0F);
    this.writer.addAttribute("foregroundAlpha", plot.getForegroundAlpha(), 1.0F);
    this.writer.addAttribute("labelRotation", plot.getLabelRotation(), 0.0D);
    writeSeriesColors(plot.getSeriesColors());
    this.writer.closeElement();
  }
  
  public void writePieChart(JRChart chart) throws IOException {
    this.writer.startElement("pieChart");
    writeChart(chart);
    writePieDataset((JRPieDataset)chart.getDataset());
    JRPiePlot plot = (JRPiePlot)chart.getPlot();
    this.writer.startElement("piePlot");
    this.writer.addAttribute("isCircular", plot.isCircular(), false);
    writePlot(chart.getPlot());
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  public void writePie3DChart(JRChart chart) throws IOException {
    this.writer.startElement("pie3DChart");
    writeChart(chart);
    writePieDataset((JRPieDataset)chart.getDataset());
    JRPie3DPlot plot = (JRPie3DPlot)chart.getPlot();
    this.writer.startElement("pie3DPlot");
    this.writer.addAttribute("depthFactor", plot.getDepthFactor(), 0.2D);
    this.writer.addAttribute("isCircular", plot.isCircular(), false);
    writePlot(chart.getPlot());
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  public void writeAxisFormat(String axisFormatElementName, JRFont axisLabelFont, Color axisLabelColor, JRFont axisTickLabelFont, Color axisTickLabelColor, String axisTickLabelMask, Color axisLineColor) throws IOException {
    if (axisLabelFont == null && axisLabelColor == null && axisTickLabelFont == null && axisTickLabelColor == null && axisLineColor == null)
      return; 
    this.writer.startElement(axisFormatElementName);
    this.writer.startElement("axisFormat");
    this.writer.addAttribute("labelColor", axisLabelColor);
    this.writer.addAttribute("tickLabelColor", axisTickLabelColor);
    this.writer.addAttribute("tickLabelMask", axisTickLabelMask);
    this.writer.addAttribute("axisLineColor", axisLineColor);
    if (axisLabelFont != null) {
      this.writer.startElement("labelFont");
      writeFont(axisLabelFont);
      this.writer.closeElement();
    } 
    if (axisTickLabelFont != null) {
      this.writer.startElement("tickLabelFont");
      writeFont(axisTickLabelFont);
      this.writer.closeElement();
    } 
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  private void writeBarPlot(JRBarPlot plot) throws IOException {
    this.writer.startElement("barPlot");
    this.writer.addAttribute("isShowLabels", plot.isShowLabels(), false);
    this.writer.addAttribute("isShowTickLabels", plot.isShowTickLabels(), true);
    this.writer.addAttribute("isShowTickMarks", plot.isShowTickMarks(), true);
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("categoryAxisLabelExpression", plot.getCategoryAxisLabelExpression(), false);
    writeAxisFormat("categoryAxisFormat", plot.getCategoryAxisLabelFont(), plot.getOwnCategoryAxisLabelColor(), plot.getCategoryAxisTickLabelFont(), plot.getOwnCategoryAxisTickLabelColor(), plot.getCategoryAxisTickLabelMask(), plot.getOwnCategoryAxisLineColor());
    this.writer.writeExpression("valueAxisLabelExpression", plot.getValueAxisLabelExpression(), false);
    writeAxisFormat("valueAxisFormat", plot.getValueAxisLabelFont(), plot.getOwnValueAxisLabelColor(), plot.getValueAxisTickLabelFont(), plot.getOwnValueAxisTickLabelColor(), plot.getValueAxisTickLabelMask(), plot.getOwnValueAxisLineColor());
    this.writer.closeElement();
  }
  
  private void writeBubblePlot(JRBubblePlot plot) throws IOException {
    this.writer.startElement("bubblePlot");
    this.writer.addAttribute("scaleType", plot.getScaleType(), JRXmlConstants.getScaleTypeMap());
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("xAxisLabelExpression", plot.getXAxisLabelExpression(), false);
    writeAxisFormat("xAxisFormat", plot.getXAxisLabelFont(), plot.getOwnXAxisLabelColor(), plot.getXAxisTickLabelFont(), plot.getOwnXAxisTickLabelColor(), plot.getXAxisTickLabelMask(), plot.getOwnXAxisLineColor());
    this.writer.writeExpression("yAxisLabelExpression", plot.getYAxisLabelExpression(), false);
    writeAxisFormat("yAxisFormat", plot.getYAxisLabelFont(), plot.getOwnYAxisLabelColor(), plot.getYAxisTickLabelFont(), plot.getOwnYAxisTickLabelColor(), plot.getYAxisTickLabelMask(), plot.getOwnYAxisLineColor());
    this.writer.closeElement();
  }
  
  private void writeLinePlot(JRLinePlot plot) throws IOException {
    this.writer.startElement("linePlot");
    this.writer.addAttribute("isShowLines", plot.isShowLines(), true);
    this.writer.addAttribute("isShowShapes", plot.isShowShapes(), true);
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("categoryAxisLabelExpression", plot.getCategoryAxisLabelExpression(), false);
    writeAxisFormat("categoryAxisFormat", plot.getCategoryAxisLabelFont(), plot.getOwnCategoryAxisLabelColor(), plot.getCategoryAxisTickLabelFont(), plot.getOwnCategoryAxisTickLabelColor(), plot.getCategoryAxisTickLabelMask(), plot.getOwnCategoryAxisLineColor());
    this.writer.writeExpression("valueAxisLabelExpression", plot.getValueAxisLabelExpression(), false);
    writeAxisFormat("valueAxisFormat", plot.getValueAxisLabelFont(), plot.getOwnValueAxisLabelColor(), plot.getValueAxisTickLabelFont(), plot.getOwnValueAxisTickLabelColor(), plot.getValueAxisTickLabelMask(), plot.getOwnValueAxisLineColor());
    this.writer.closeElement();
  }
  
  private void writeTimeSeriesPlot(JRTimeSeriesPlot plot) throws IOException {
    this.writer.startElement("timeSeriesPlot");
    this.writer.addAttribute("isShowLines", plot.isShowLines(), true);
    this.writer.addAttribute("isShowShapes", plot.isShowShapes(), true);
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("timeAxisLabelExpression", plot.getTimeAxisLabelExpression(), false);
    writeAxisFormat("timeAxisFormat", plot.getTimeAxisLabelFont(), plot.getOwnTimeAxisLabelColor(), plot.getTimeAxisTickLabelFont(), plot.getOwnTimeAxisTickLabelColor(), plot.getTimeAxisTickLabelMask(), plot.getOwnTimeAxisLineColor());
    this.writer.writeExpression("valueAxisLabelExpression", plot.getValueAxisLabelExpression(), false);
    writeAxisFormat("valueAxisFormat", plot.getValueAxisLabelFont(), plot.getOwnValueAxisLabelColor(), plot.getValueAxisTickLabelFont(), plot.getOwnValueAxisTickLabelColor(), plot.getValueAxisTickLabelMask(), plot.getOwnValueAxisLineColor());
    this.writer.closeElement();
  }
  
  public void writeBar3DPlot(JRBar3DPlot plot) throws IOException {
    this.writer.startElement("bar3DPlot");
    this.writer.addAttribute("isShowLabels", plot.isShowLabels(), false);
    this.writer.addAttribute("xOffset", plot.getXOffset(), 12.0D);
    this.writer.addAttribute("yOffset", plot.getYOffset(), 8.0D);
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("categoryAxisLabelExpression", plot.getCategoryAxisLabelExpression(), false);
    writeAxisFormat("categoryAxisFormat", plot.getCategoryAxisLabelFont(), plot.getOwnCategoryAxisLabelColor(), plot.getCategoryAxisTickLabelFont(), plot.getOwnCategoryAxisTickLabelColor(), plot.getCategoryAxisTickLabelMask(), plot.getOwnCategoryAxisLineColor());
    this.writer.writeExpression("valueAxisLabelExpression", plot.getValueAxisLabelExpression(), false);
    writeAxisFormat("valueAxisFormat", plot.getValueAxisLabelFont(), plot.getOwnValueAxisLabelColor(), plot.getValueAxisTickLabelFont(), plot.getOwnValueAxisTickLabelColor(), plot.getValueAxisTickLabelMask(), plot.getOwnValueAxisLineColor());
    this.writer.closeElement();
  }
  
  public void writeBarChart(JRChart chart) throws IOException {
    this.writer.startElement("barChart");
    writeChart(chart);
    writeCategoryDataSet((JRCategoryDataset)chart.getDataset());
    writeBarPlot((JRBarPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeBar3DChart(JRChart chart) throws IOException {
    this.writer.startElement("bar3DChart");
    writeChart(chart);
    writeCategoryDataSet((JRCategoryDataset)chart.getDataset());
    writeBar3DPlot((JRBar3DPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeBubbleChart(JRChart chart) throws IOException {
    this.writer.startElement("bubbleChart");
    writeChart(chart);
    writeXyzDataset((JRXyzDataset)chart.getDataset());
    writeBubblePlot((JRBubblePlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeStackedBarChart(JRChart chart) throws IOException {
    this.writer.startElement("stackedBarChart");
    writeChart(chart);
    writeCategoryDataSet((JRCategoryDataset)chart.getDataset());
    writeBarPlot((JRBarPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeStackedBar3DChart(JRChart chart) throws IOException {
    this.writer.startElement("stackedBar3DChart");
    writeChart(chart);
    writeCategoryDataSet((JRCategoryDataset)chart.getDataset());
    writeBar3DPlot((JRBar3DPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeLineChart(JRChart chart) throws IOException {
    this.writer.startElement("lineChart");
    writeChart(chart);
    writeCategoryDataSet((JRCategoryDataset)chart.getDataset());
    writeLinePlot((JRLinePlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeTimeSeriesChart(JRChart chart) throws IOException {
    this.writer.startElement("timeSeriesChart");
    writeChart(chart);
    writeTimeSeriesDataset((JRTimeSeriesDataset)chart.getDataset());
    writeTimeSeriesPlot((JRTimeSeriesPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeHighLowDataset(JRHighLowDataset dataset) throws IOException {
    this.writer.startElement("highLowDataset");
    writeElementDataset((JRElementDataset)dataset);
    this.writer.writeExpression("seriesExpression", dataset.getSeriesExpression(), false);
    this.writer.writeExpression("dateExpression", dataset.getDateExpression(), false);
    this.writer.writeExpression("highExpression", dataset.getHighExpression(), false);
    this.writer.writeExpression("lowExpression", dataset.getLowExpression(), false);
    this.writer.writeExpression("openExpression", dataset.getOpenExpression(), false);
    this.writer.writeExpression("closeExpression", dataset.getCloseExpression(), false);
    this.writer.writeExpression("volumeExpression", dataset.getVolumeExpression(), false);
    writeHyperlink("itemHyperlink", dataset.getItemHyperlink());
    this.writer.closeElement();
  }
  
  public void writeHighLowChart(JRChart chart) throws IOException {
    this.writer.startElement("highLowChart");
    writeChart(chart);
    writeHighLowDataset((JRHighLowDataset)chart.getDataset());
    JRHighLowPlot plot = (JRHighLowPlot)chart.getPlot();
    this.writer.startElement("highLowPlot");
    this.writer.addAttribute("isShowOpenTicks", plot.isShowOpenTicks(), true);
    this.writer.addAttribute("isShowCloseTicks", plot.isShowCloseTicks(), true);
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("timeAxisLabelExpression", plot.getTimeAxisLabelExpression(), false);
    writeAxisFormat("timeAxisFormat", plot.getTimeAxisLabelFont(), plot.getOwnTimeAxisLabelColor(), plot.getTimeAxisTickLabelFont(), plot.getOwnTimeAxisTickLabelColor(), plot.getTimeAxisTickLabelMask(), plot.getOwnTimeAxisLineColor());
    this.writer.writeExpression("valueAxisLabelExpression", plot.getValueAxisLabelExpression(), false);
    writeAxisFormat("valueAxisFormat", plot.getValueAxisLabelFont(), plot.getOwnValueAxisLabelColor(), plot.getValueAxisTickLabelFont(), plot.getOwnValueAxisTickLabelColor(), plot.getValueAxisTickLabelMask(), plot.getOwnValueAxisLineColor());
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  public void writeCandlestickChart(JRChart chart) throws IOException {
    this.writer.startElement("candlestickChart");
    writeChart(chart);
    writeHighLowDataset((JRHighLowDataset)chart.getDataset());
    JRCandlestickPlot plot = (JRCandlestickPlot)chart.getPlot();
    this.writer.startElement("candlestickPlot");
    this.writer.addAttribute("isShowVolume", plot.isShowVolume(), true);
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("timeAxisLabelExpression", plot.getTimeAxisLabelExpression(), false);
    writeAxisFormat("timeAxisFormat", plot.getTimeAxisLabelFont(), plot.getOwnTimeAxisLabelColor(), plot.getTimeAxisTickLabelFont(), plot.getOwnTimeAxisTickLabelColor(), plot.getTimeAxisTickLabelMask(), plot.getOwnTimeAxisLineColor());
    this.writer.writeExpression("valueAxisLabelExpression", plot.getValueAxisLabelExpression(), false);
    writeAxisFormat("valueAxisFormat", plot.getValueAxisLabelFont(), plot.getOwnValueAxisLabelColor(), plot.getValueAxisTickLabelFont(), plot.getOwnValueAxisTickLabelColor(), plot.getValueAxisTickLabelMask(), plot.getOwnValueAxisLineColor());
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  private void writeAreaPlot(JRAreaPlot plot) throws IOException {
    this.writer.startElement("areaPlot");
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("categoryAxisLabelExpression", plot.getCategoryAxisLabelExpression(), false);
    writeAxisFormat("categoryAxisFormat", plot.getCategoryAxisLabelFont(), plot.getOwnCategoryAxisLabelColor(), plot.getCategoryAxisTickLabelFont(), plot.getOwnCategoryAxisTickLabelColor(), plot.getCategoryAxisTickLabelMask(), plot.getOwnCategoryAxisLineColor());
    this.writer.writeExpression("valueAxisLabelExpression", plot.getValueAxisLabelExpression(), false);
    writeAxisFormat("valueAxisFormat", plot.getValueAxisLabelFont(), plot.getOwnValueAxisLabelColor(), plot.getValueAxisTickLabelFont(), plot.getOwnValueAxisTickLabelColor(), plot.getValueAxisTickLabelMask(), plot.getOwnValueAxisLineColor());
    this.writer.closeElement();
  }
  
  public void writeAreaChart(JRChart chart) throws IOException {
    this.writer.startElement("areaChart");
    writeChart(chart);
    writeCategoryDataSet((JRCategoryDataset)chart.getDataset());
    writeAreaPlot((JRAreaPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  private void writeScatterPlot(JRScatterPlot plot) throws IOException {
    this.writer.startElement("scatterPlot");
    this.writer.addAttribute("isShowLines", plot.isShowLines(), true);
    this.writer.addAttribute("isShowShapes", plot.isShowShapes(), true);
    writePlot((JRChartPlot)plot);
    this.writer.writeExpression("xAxisLabelExpression", plot.getXAxisLabelExpression(), false);
    writeAxisFormat("xAxisFormat", plot.getXAxisLabelFont(), plot.getOwnXAxisLabelColor(), plot.getXAxisTickLabelFont(), plot.getOwnXAxisTickLabelColor(), plot.getXAxisTickLabelMask(), plot.getOwnXAxisLineColor());
    this.writer.writeExpression("yAxisLabelExpression", plot.getYAxisLabelExpression(), false);
    writeAxisFormat("yAxisFormat", plot.getYAxisLabelFont(), plot.getOwnYAxisLabelColor(), plot.getYAxisTickLabelFont(), plot.getOwnYAxisTickLabelColor(), plot.getYAxisTickLabelMask(), plot.getOwnYAxisLineColor());
    this.writer.closeElement();
  }
  
  public void writeScatterChart(JRChart chart) throws IOException {
    this.writer.startElement("scatterChart");
    writeChart(chart);
    writeXyDataset((JRXyDataset)chart.getDataset());
    writeScatterPlot((JRScatterPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeXyAreaChart(JRChart chart) throws IOException {
    this.writer.startElement("xyAreaChart");
    writeChart(chart);
    writeXyDataset((JRXyDataset)chart.getDataset());
    writeAreaPlot((JRAreaPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeXyBarChart(JRChart chart) throws IOException {
    this.writer.startElement("xyBarChart");
    writeChart(chart);
    JRChartDataset dataset = chart.getDataset();
    if (dataset.getDatasetType() == 6) {
      writeTimeSeriesDataset((JRTimeSeriesDataset)dataset);
    } else if (dataset.getDatasetType() == 5) {
      writeTimePeriodDataset((JRTimePeriodDataset)dataset);
    } else if (dataset.getDatasetType() == 3) {
      writeXyDataset((JRXyDataset)dataset);
    } 
    writeBarPlot((JRBarPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeXyLineChart(JRChart chart) throws IOException {
    this.writer.startElement("xyLineChart");
    writeChart(chart);
    writeXyDataset((JRXyDataset)chart.getDataset());
    writeLinePlot((JRLinePlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeMeterChart(JRChart chart) throws IOException {
    this.writer.startElement("meterChart");
    writeChart(chart);
    writeValueDataset((JRValueDataset)chart.getDataset());
    JRMeterPlot plot = (JRMeterPlot)chart.getPlot();
    this.writer.startElement("meterPlot");
    this.writer.addAttribute("shape", plot.getShape(), JRXmlConstants.getMeterShapeMap(), (byte)2);
    this.writer.addAttribute("angle", plot.getMeterAngle());
    this.writer.addAttribute("units", plot.getUnits());
    this.writer.addAttribute("tickInterval", plot.getTickInterval());
    this.writer.addAttribute("meterColor", plot.getMeterBackgroundColor());
    this.writer.addAttribute("needleColor", plot.getNeedleColor());
    this.writer.addAttribute("tickColor", plot.getTickColor());
    writePlot(chart.getPlot());
    writeValueDisplay(plot.getValueDisplay());
    writeDataRange(plot.getDataRange());
    List intervals = plot.getIntervals();
    if (intervals != null) {
      Iterator iter = intervals.iterator();
      while (iter.hasNext()) {
        JRMeterInterval meterInterval = iter.next();
        writeMeterInterval(meterInterval);
      } 
    } 
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  public void writeThermometerChart(JRChart chart) throws IOException {
    this.writer.startElement("thermometerChart");
    writeChart(chart);
    writeValueDataset((JRValueDataset)chart.getDataset());
    JRThermometerPlot plot = (JRThermometerPlot)chart.getPlot();
    this.writer.startElement("thermometerPlot");
    this.writer.addAttribute("valueLocation", plot.getValueLocation(), JRXmlConstants.getThermometerValueLocationMap(), (byte)3);
    this.writer.addAttribute("isShowValueLines", plot.isShowValueLines());
    this.writer.addAttribute("mercuryColor", plot.getMercuryColor());
    writePlot(chart.getPlot());
    writeValueDisplay(plot.getValueDisplay());
    writeDataRange(plot.getDataRange());
    if (plot.getLowRange() != null) {
      this.writer.startElement("lowRange");
      writeDataRange(plot.getLowRange());
      this.writer.closeElement();
    } 
    if (plot.getMediumRange() != null) {
      this.writer.startElement("mediumRange");
      writeDataRange(plot.getMediumRange());
      this.writer.closeElement();
    } 
    if (plot.getHighRange() != null) {
      this.writer.startElement("highRange");
      writeDataRange(plot.getHighRange());
      this.writer.closeElement();
    } 
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  public void writeMultiAxisChart(JRChart chart) throws IOException {
    this.writer.startElement("multiAxisChart");
    writeChart(chart);
    JRMultiAxisPlot plot = (JRMultiAxisPlot)chart.getPlot();
    this.writer.startElement("multiAxisPlot");
    writePlot(chart.getPlot());
    List axes = plot.getAxes();
    if (axes != null) {
      Iterator iter = axes.iterator();
      while (iter.hasNext()) {
        JRChartAxis chartAxis = iter.next();
        writeChartAxis(chartAxis);
      } 
    } 
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  public void writeStackedAreaChart(JRChart chart) throws IOException {
    this.writer.startElement("stackedAreaChart");
    writeChart(chart);
    writeCategoryDataSet((JRCategoryDataset)chart.getDataset());
    writeAreaPlot((JRAreaPlot)chart.getPlot());
    this.writer.closeElement();
  }
  
  public void writeChartTag(JRChart chart) throws IOException {
    switch (chart.getChartType()) {
      case 1:
        writeAreaChart(chart);
        return;
      case 3:
        writeBarChart(chart);
        return;
      case 2:
        writeBar3DChart(chart);
        return;
      case 4:
        writeBubbleChart(chart);
        return;
      case 5:
        writeCandlestickChart(chart);
        return;
      case 6:
        writeHighLowChart(chart);
        return;
      case 7:
        writeLineChart(chart);
        return;
      case 17:
        writeMeterChart(chart);
        return;
      case 19:
        writeMultiAxisChart(chart);
        return;
      case 9:
        writePieChart(chart);
        return;
      case 8:
        writePie3DChart(chart);
        return;
      case 10:
        writeScatterChart(chart);
        return;
      case 12:
        writeStackedBarChart(chart);
        return;
      case 11:
        writeStackedBar3DChart(chart);
        return;
      case 18:
        writeThermometerChart(chart);
        return;
      case 16:
        writeTimeSeriesChart(chart);
        return;
      case 13:
        writeXyAreaChart(chart);
        return;
      case 14:
        writeXyBarChart(chart);
        return;
      case 15:
        writeXyLineChart(chart);
        return;
      case 20:
        writeStackedAreaChart(chart);
        return;
    } 
    throw new JRRuntimeException("Chart type not supported.");
  }
  
  private void writeSubreportReturnValue(JRSubreportReturnValue returnValue) throws IOException {
    this.writer.startElement("returnValue");
    this.writer.addEncodedAttribute("subreportVariable", returnValue.getSubreportVariable());
    this.writer.addEncodedAttribute("toVariable", returnValue.getToVariable());
    this.writer.addAttribute("calculation", returnValue.getCalculation(), JRXmlConstants.getCalculationMap(), (byte)0);
    this.writer.addAttribute("incrementerFactoryClass", returnValue.getIncrementerFactoryClassName());
    this.writer.closeElement();
  }
  
  public void writeCrosstab(JRCrosstab crosstab) throws IOException {
    this.writer.startElement("crosstab");
    this.writer.addAttribute("isRepeatColumnHeaders", crosstab.isRepeatColumnHeaders(), true);
    this.writer.addAttribute("isRepeatRowHeaders", crosstab.isRepeatRowHeaders(), true);
    this.writer.addAttribute("columnBreakOffset", crosstab.getColumnBreakOffset(), 10);
    this.writer.addAttribute("runDirection", crosstab.getRunDirection(), JRXmlConstants.getRunDirectionMap(), (byte)0);
    writeReportElement((JRElement)crosstab);
    JRCrosstabParameter[] parameters = crosstab.getParameters();
    if (parameters != null)
      for (int m = 0; m < parameters.length; m++) {
        if (!parameters[m].isSystemDefined())
          writeCrosstabParameter(parameters[m]); 
      }  
    this.writer.writeExpression("parametersMapExpression", crosstab.getParametersMapExpression(), false);
    writeCrosstabDataset(crosstab);
    writeCrosstabHeaderCell(crosstab);
    JRCrosstabRowGroup[] rowGroups = crosstab.getRowGroups();
    for (int i = 0; i < rowGroups.length; i++)
      writeCrosstabRowGroup(rowGroups[i]); 
    JRCrosstabColumnGroup[] columnGroups = crosstab.getColumnGroups();
    for (int j = 0; j < columnGroups.length; j++)
      writeCrosstabColumnGroup(columnGroups[j]); 
    JRCrosstabMeasure[] measures = crosstab.getMeasures();
    for (int k = 0; k < measures.length; k++)
      writeCrosstabMeasure(measures[k]); 
    if (crosstab instanceof JRDesignCrosstab) {
      List cellsList = ((JRDesignCrosstab)crosstab).getCellsList();
      for (Iterator it = cellsList.iterator(); it.hasNext(); ) {
        JRCrosstabCell cell = it.next();
        writeCrosstabCell(cell);
      } 
    } else {
      JRCrosstabCell[][] cells = crosstab.getCells();
      Set cellsSet = new HashSet();
      for (int m = cells.length - 1; m >= 0; m--) {
        for (int n = (cells[m]).length - 1; n >= 0; n--) {
          JRCrosstabCell cell = cells[m][n];
          if (cell != null && cellsSet.add(cell))
            writeCrosstabCell(cell); 
        } 
      } 
    } 
    writeCrosstabWhenNoDataCell(crosstab);
    this.writer.closeElement();
  }
  
  private void writeCrosstabDataset(JRCrosstab crosstab) throws IOException {
    JRCrosstabDataset dataset = crosstab.getDataset();
    this.writer.startElement("crosstabDataset");
    this.writer.addAttribute("isDataPreSorted", dataset.isDataPreSorted(), false);
    writeElementDataset((JRElementDataset)dataset);
    this.writer.closeElement(true);
  }
  
  private void writeCrosstabWhenNoDataCell(JRCrosstab crosstab) throws IOException {
    JRCellContents whenNoDataCell = crosstab.getWhenNoDataCell();
    if (whenNoDataCell != null) {
      this.writer.startElement("whenNoDataCell");
      writeCellContents(whenNoDataCell);
      this.writer.closeElement();
    } 
  }
  
  private void writeCrosstabHeaderCell(JRCrosstab crosstab) throws IOException {
    JRCellContents headerCell = crosstab.getHeaderCell();
    if (headerCell != null) {
      this.writer.startElement("crosstabHeaderCell");
      writeCellContents(headerCell);
      this.writer.closeElement();
    } 
  }
  
  protected void writeCrosstabRowGroup(JRCrosstabRowGroup group) throws IOException {
    this.writer.startElement("rowGroup");
    this.writer.addEncodedAttribute("name", group.getName());
    this.writer.addAttribute("width", group.getWidth());
    this.writer.addAttribute("totalPosition", group.getTotalPosition(), JRXmlConstants.getCrosstabTotalPositionMap(), (byte)0);
    this.writer.addAttribute("headerPosition", group.getPosition(), JRXmlConstants.getCrosstabRowPositionMap(), (byte)1);
    writeBucket(group.getBucket());
    JRCellContents header = group.getHeader();
    this.writer.startElement("crosstabRowHeader");
    writeCellContents(header);
    this.writer.closeElement();
    JRCellContents totalHeader = group.getTotalHeader();
    this.writer.startElement("crosstabTotalRowHeader");
    writeCellContents(totalHeader);
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  protected void writeCrosstabColumnGroup(JRCrosstabColumnGroup group) throws IOException {
    this.writer.startElement("columnGroup");
    this.writer.addEncodedAttribute("name", group.getName());
    this.writer.addAttribute("height", group.getHeight());
    this.writer.addAttribute("totalPosition", group.getTotalPosition(), JRXmlConstants.getCrosstabTotalPositionMap(), (byte)0);
    this.writer.addAttribute("headerPosition", group.getPosition(), JRXmlConstants.getCrosstabColumnPositionMap(), (byte)1);
    writeBucket(group.getBucket());
    JRCellContents header = group.getHeader();
    this.writer.startElement("crosstabColumnHeader");
    writeCellContents(header);
    this.writer.closeElement();
    JRCellContents totalHeader = group.getTotalHeader();
    this.writer.startElement("crosstabTotalColumnHeader");
    writeCellContents(totalHeader);
    this.writer.closeElement();
    this.writer.closeElement();
  }
  
  protected void writeBucket(JRCrosstabBucket bucket) throws IOException {
    this.writer.startElement("bucket");
    this.writer.addAttribute("order", bucket.getOrder(), JRXmlConstants.getCrosstabBucketOrderMap(), (byte)1);
    this.writer.writeExpression("bucketExpression", bucket.getExpression(), true);
    this.writer.writeExpression("comparatorExpression", bucket.getComparatorExpression(), false);
    this.writer.closeElement();
  }
  
  protected void writeCrosstabMeasure(JRCrosstabMeasure measure) throws IOException {
    this.writer.startElement("measure");
    this.writer.addEncodedAttribute("name", measure.getName());
    this.writer.addAttribute("class", measure.getValueClassName());
    this.writer.addAttribute("calculation", measure.getCalculation(), JRXmlConstants.getCalculationMap(), (byte)0);
    this.writer.addAttribute("percentageOf", measure.getPercentageOfType(), JRXmlConstants.getCrosstabPercentageMap(), (byte)0);
    this.writer.addAttribute("percentageCalculatorClass", measure.getPercentageCalculatorClassName());
    this.writer.writeExpression("measureExpression", measure.getValueExpression(), false);
    this.writer.closeElement();
  }
  
  protected void writeCrosstabCell(JRCrosstabCell cell) throws IOException {
    this.writer.startElement("crosstabCell");
    this.writer.addAttribute("width", cell.getWidth());
    this.writer.addAttribute("height", cell.getHeight());
    this.writer.addEncodedAttribute("rowTotalGroup", cell.getRowTotalGroup());
    this.writer.addEncodedAttribute("columnTotalGroup", cell.getColumnTotalGroup());
    writeCellContents(cell.getContents());
    this.writer.closeElement();
  }
  
  protected void writeCellContents(JRCellContents contents) throws IOException {
    if (contents != null) {
      this.writer.startElement("cellContents");
      this.writer.addAttribute("backcolor", contents.getBackcolor());
      this.writer.addAttribute("mode", contents.getMode(), JRXmlConstants.getModeMap());
      writeStyleReferenceAttr((JRStyleContainer)contents);
      writeBox(contents.getLineBox());
      List children = contents.getChildren();
      if (children != null)
        for (Iterator it = children.iterator(); it.hasNext();)
          ((JRChild)it.next()).visit(this.xmlWriterVisitor);  
      this.writer.closeElement();
    } 
  }
  
  protected void writeCrosstabParameter(JRCrosstabParameter parameter) throws IOException {
    this.writer.startElement("crosstabParameter");
    this.writer.addEncodedAttribute("name", parameter.getName());
    this.writer.addAttribute("class", parameter.getValueClassName(), "java.lang.String");
    this.writer.writeExpression("parameterValueExpression", parameter.getExpression(), false);
    this.writer.closeElement();
  }
  
  public void writeDataset(JRDataset dataset) throws IOException {
    this.writer.startElement("subDataset");
    this.writer.addEncodedAttribute("name", dataset.getName());
    this.writer.addAttribute("scriptletClass", dataset.getScriptletClass());
    this.writer.addEncodedAttribute("resourceBundle", dataset.getResourceBundle());
    this.writer.addAttribute("whenResourceMissingType", dataset.getWhenResourceMissingType(), JRXmlConstants.getWhenResourceMissingTypeMap(), (byte)1);
    writeProperties((JRPropertiesHolder)dataset);
    writeDatasetContents(dataset);
    this.writer.closeElement();
  }
  
  protected void writeDatasetContents(JRDataset dataset) throws IOException {
    JRParameter[] parameters = dataset.getParameters();
    if (parameters != null && parameters.length > 0)
      for (int i = 0; i < parameters.length; i++) {
        if (!parameters[i].isSystemDefined())
          writeParameter(parameters[i]); 
      }  
    if (dataset.getQuery() != null)
      writeQuery(dataset.getQuery()); 
    JRField[] fields = dataset.getFields();
    if (fields != null && fields.length > 0)
      for (int i = 0; i < fields.length; i++)
        writeField(fields[i]);  
    JRSortField[] sortFields = dataset.getSortFields();
    if (sortFields != null && sortFields.length > 0)
      for (int i = 0; i < sortFields.length; i++)
        writeSortField(sortFields[i]);  
    JRVariable[] variables = dataset.getVariables();
    if (variables != null && variables.length > 0)
      for (int i = 0; i < variables.length; i++) {
        if (!variables[i].isSystemDefined())
          writeVariable(variables[i]); 
      }  
    this.writer.writeExpression("filterExpression", dataset.getFilterExpression(), false);
    JRGroup[] groups = dataset.getGroups();
    if (groups != null && groups.length > 0)
      for (int i = 0; i < groups.length; i++)
        writeGroup(groups[i]);  
  }
  
  protected void writeDatasetRun(JRDatasetRun datasetRun) throws IOException {
    this.writer.startElement("datasetRun");
    this.writer.addEncodedAttribute("subDataset", datasetRun.getDatasetName());
    this.writer.writeExpression("parametersMapExpression", datasetRun.getParametersMapExpression(), false);
    JRDatasetParameter[] parameters = datasetRun.getParameters();
    if (parameters != null && parameters.length > 0)
      for (int i = 0; i < parameters.length; i++)
        writeDatasetParameter(parameters[i]);  
    this.writer.writeExpression("connectionExpression", datasetRun.getConnectionExpression(), false);
    this.writer.writeExpression("dataSourceExpression", datasetRun.getDataSourceExpression(), false);
    this.writer.closeElement();
  }
  
  public void writeFrame(JRFrame frame) throws IOException {
    this.writer.startElement("frame");
    writeReportElement((JRElement)frame);
    writeBox(frame.getLineBox());
    List children = frame.getChildren();
    if (children != null)
      for (Iterator it = children.iterator(); it.hasNext();)
        ((JRChild)it.next()).visit(this.xmlWriterVisitor);  
    this.writer.closeElement();
  }
  
  protected void writeHyperlinkParameters(JRHyperlinkParameter[] parameters) throws IOException {
    if (parameters != null)
      for (int i = 0; i < parameters.length; i++) {
        JRHyperlinkParameter parameter = parameters[i];
        writeHyperlinkParameter(parameter);
      }  
  }
  
  protected void writeHyperlinkParameter(JRHyperlinkParameter parameter) throws IOException {
    if (parameter != null) {
      this.writer.startElement("hyperlinkParameter");
      this.writer.addEncodedAttribute("name", parameter.getName());
      this.writer.writeExpression("hyperlinkParameterExpression", parameter.getValueExpression(), true, String.class.getName());
      this.writer.closeElement();
    } 
  }
  
  protected void writeHyperlink(String tagName, JRHyperlink hyperlink) throws IOException {
    if (hyperlink != null) {
      this.writer.startElement(tagName);
      this.writer.addEncodedAttribute("hyperlinkType", hyperlink.getLinkType());
      this.writer.addAttribute("hyperlinkTarget", hyperlink.getHyperlinkTarget(), JRXmlConstants.getHyperlinkTargetMap(), (byte)1);
      this.writer.writeExpression("hyperlinkReferenceExpression", hyperlink.getHyperlinkReferenceExpression(), false);
      this.writer.writeExpression("hyperlinkAnchorExpression", hyperlink.getHyperlinkAnchorExpression(), false);
      this.writer.writeExpression("hyperlinkPageExpression", hyperlink.getHyperlinkPageExpression(), false);
      this.writer.writeExpression("hyperlinkTooltipExpression", hyperlink.getHyperlinkTooltipExpression(), false);
      writeHyperlinkParameters(hyperlink.getHyperlinkParameters());
      this.writer.closeElement();
    } 
  }
  
  protected boolean toWriteConditionalStyles() {
    return true;
  }
}
