~brian-thomason/+junk/jasperreports3.7

« back to all changes in this revision

Viewing changes to src/net/sf/jasperreports/engine/xml/JRXmlBaseWriter.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:51:16 UTC
  • Revision ID: brian.thomason@canonical.com-20111220175116-apwo6unuaedvgzo3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * JasperReports - Free Java Reporting Library.
 
3
 * Copyright (C) 2001 - 2009 Jaspersoft Corporation. All rights reserved.
 
4
 * http://www.jaspersoft.com
 
5
 *
 
6
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 
7
 * the following license terms apply:
 
8
 *
 
9
 * This program is part of JasperReports.
 
10
 *
 
11
 * JasperReports is free software: you can redistribute it and/or modify
 
12
 * it under the terms of the GNU Lesser General Public License as published by
 
13
 * the Free Software Foundation, either version 3 of the License, or
 
14
 * (at your option) any later version.
 
15
 *
 
16
 * JasperReports is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
19
 * GNU Lesser General Public License for more details.
 
20
 * 
 
21
 * You should have received a copy of the GNU Lesser General Public License
 
22
 * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
 
23
 */
 
24
package net.sf.jasperreports.engine.xml;
 
25
 
 
26
import java.io.IOException;
 
27
 
 
28
import net.sf.jasperreports.engine.JRConditionalStyle;
 
29
import net.sf.jasperreports.engine.JRLineBox;
 
30
import net.sf.jasperreports.engine.JRPen;
 
31
import net.sf.jasperreports.engine.JRStyle;
 
32
import net.sf.jasperreports.engine.JRStyleContainer;
 
33
import net.sf.jasperreports.engine.util.JRXmlWriteHelper;
 
34
import net.sf.jasperreports.engine.util.XmlNamespace;
 
35
 
 
36
 
 
37
/**
 
38
 * Base XML writer.
 
39
 * 
 
40
 * @author Lucian Chirita (lucianc@users.sourceforge.net)
 
41
 * @version $Id: JRXmlBaseWriter.java 3794 2010-05-13 07:47:16Z teodord $
 
42
 */
 
43
public abstract class JRXmlBaseWriter
 
44
{
 
45
        
 
46
        protected JRXmlWriteHelper writer;
 
47
        
 
48
        /**
 
49
         * Sets the XML write helper.
 
50
         * 
 
51
         * @param aWriter the XML write helper
 
52
         */
 
53
        protected void useWriter(JRXmlWriteHelper aWriter)
 
54
        {
 
55
                this.writer = aWriter;
 
56
        }
 
57
        
 
58
        /**
 
59
         * Writes a style.
 
60
         * 
 
61
         * @param style the style to write.
 
62
         * @throws IOException
 
63
         */
 
64
        protected void writeStyle(JRStyle style) throws IOException
 
65
        {
 
66
                writer.startElement(JRXmlConstants.ELEMENT_style);
 
67
                writer.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_name, style.getName());
 
68
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_isDefault, style.isDefault(), false);
 
69
                writeStyleReferenceAttr(style);
 
70
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_mode, style.getOwnModeValue());
 
71
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_forecolor, style.getOwnForecolor());
 
72
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_backcolor, style.getOwnBackcolor());
 
73
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_fill, style.getOwnFillValue());
 
74
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_radius, style.getOwnRadius());
 
75
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_scaleImage, style.getOwnScaleImageValue());
 
76
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_hAlign, style.getOwnHorizontalAlignmentValue());
 
77
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_vAlign, style.getOwnVerticalAlignmentValue());
 
78
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_rotation, style.getOwnRotationValue());
 
79
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_lineSpacing, style.getOwnLineSpacingValue());
 
80
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_markup, style.getOwnMarkup());
 
81
                writer.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_pattern, style.getOwnPattern());
 
82
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_isBlankWhenNull, style.isOwnBlankWhenNull());
 
83
                
 
84
                writer.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_fontName, style.getOwnFontName());
 
85
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_fontSize, style.getOwnFontSize());
 
86
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_isBold, style.isOwnBold());
 
87
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_isItalic, style.isOwnItalic());
 
88
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_isUnderline, style.isOwnUnderline());
 
89
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_isStrikeThrough, style.isOwnStrikeThrough());
 
90
                writer.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_pdfFontName, style.getOwnPdfFontName());
 
91
                writer.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_pdfEncoding, style.getOwnPdfEncoding());
 
92
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_isPdfEmbedded, style.isOwnPdfEmbedded());
 
93
 
 
94
                writePen(style.getLinePen());
 
95
                writeBox(style.getLineBox());
 
96
                
 
97
                if (toWriteConditionalStyles())
 
98
                {
 
99
                        JRConditionalStyle[] conditionalStyles = style.getConditionalStyles();
 
100
                        if (!(style instanceof JRConditionalStyle) && conditionalStyles != null)
 
101
                        {
 
102
                                for (int i = 0; i < conditionalStyles.length; i++)
 
103
                                {
 
104
                                        writeConditionalStyle(conditionalStyles[i]);
 
105
                                }
 
106
                        }
 
107
                }
 
108
 
 
109
                writer.closeElement();
 
110
        }
 
111
 
 
112
        public void writeStyleReferenceAttr(JRStyleContainer styleContainer)
 
113
        {
 
114
                if (!(styleContainer instanceof JRConditionalStyle))
 
115
                {
 
116
                        if (styleContainer.getStyle() != null)
 
117
                        {
 
118
                                writer.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_style, styleContainer.getStyle().getName());
 
119
                        }
 
120
                        else if (styleContainer.getStyleNameReference() != null)
 
121
                        {
 
122
                                writer.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_style, styleContainer.getStyleNameReference());
 
123
                        }
 
124
                }
 
125
        }
 
126
 
 
127
        /**
 
128
         * Decides whether conditional styles are to be written.
 
129
         * 
 
130
         * @return whether conditional styles are to be written
 
131
         */
 
132
        protected abstract boolean toWriteConditionalStyles();
 
133
 
 
134
        /**
 
135
         * Writes a conditional style.
 
136
         * 
 
137
         * @param style the conditional style
 
138
         * @throws IOException
 
139
         */
 
140
        protected void writeConditionalStyle(JRConditionalStyle style) throws IOException
 
141
        {
 
142
                writer.startElement(JRXmlConstants.ELEMENT_conditionalStyle);
 
143
                writer.writeExpression(JRXmlConstants.ELEMENT_conditionExpression, style.getConditionExpression(), false);
 
144
                writeStyle(style);
 
145
                writer.closeElement();
 
146
        }
 
147
 
 
148
        /**
 
149
         *
 
150
         */
 
151
        protected void writePen(JRPen pen) throws IOException
 
152
        {
 
153
                writePen(JRXmlConstants.ELEMENT_pen, pen);
 
154
        }
 
155
 
 
156
        /**
 
157
         *
 
158
         */
 
159
        private void writePen(String element, JRPen pen) throws IOException
 
160
        {
 
161
                writer.startElement(element);
 
162
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_lineWidth, pen.getOwnLineWidth());
 
163
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_lineStyle, pen.getOwnLineStyleValue());
 
164
                writer.addAttribute(JRXmlConstants.ATTRIBUTE_lineColor, pen.getOwnLineColor());
 
165
                writer.closeElement(true);
 
166
        }
 
167
 
 
168
        public void writeBox(JRLineBox box) throws IOException
 
169
        {
 
170
                writeBox(box, null);
 
171
        }
 
172
        
 
173
        /**
 
174
         *
 
175
         */
 
176
        public void writeBox(JRLineBox box, XmlNamespace namespace) throws IOException
 
177
        {
 
178
                if (box != null)
 
179
                {
 
180
                        writer.startElement(JRXmlConstants.ELEMENT_box, namespace);
 
181
                        
 
182
                        writer.addAttribute(JRXmlConstants.ATTRIBUTE_padding, box.getOwnPadding());
 
183
                        writer.addAttribute(JRXmlConstants.ATTRIBUTE_topPadding, box.getOwnTopPadding());
 
184
                        writer.addAttribute(JRXmlConstants.ATTRIBUTE_leftPadding, box.getOwnLeftPadding());
 
185
                        writer.addAttribute(JRXmlConstants.ATTRIBUTE_bottomPadding, box.getOwnBottomPadding());
 
186
                        writer.addAttribute(JRXmlConstants.ATTRIBUTE_rightPadding, box.getOwnRightPadding());
 
187
 
 
188
                        writePen(JRXmlConstants.ELEMENT_pen, box.getPen());
 
189
                        writePen(JRXmlConstants.ELEMENT_topPen, box.getTopPen());
 
190
                        writePen(JRXmlConstants.ELEMENT_leftPen, box.getLeftPen());
 
191
                        writePen(JRXmlConstants.ELEMENT_bottomPen, box.getBottomPen());
 
192
                        writePen(JRXmlConstants.ELEMENT_rightPen, box.getRightPen());
 
193
 
 
194
                        writer.closeElement(true);
 
195
                }
 
196
        }
 
197
 
 
198
}