KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > xml > JRStyleFactory


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.xml;
29
30 import java.awt.Color JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import net.sf.jasperreports.engine.JRStyle;
34 import net.sf.jasperreports.engine.JasperPrint;
35 import net.sf.jasperreports.engine.design.JRDesignStyle;
36 import net.sf.jasperreports.engine.design.JasperDesign;
37
38 import org.xml.sax.Attributes JavaDoc;
39
40 /**
41  * @author Ionut Nedelcu (ionutned@users.sourceforge.net)
42  * @version $Id: JRStyleFactory.java 1413 2006-09-28 13:47:40 +0300 (Thu, 28 Sep 2006) teodord $
43  */

44 public class JRStyleFactory extends JRBaseFactory
45 {
46     private static final String JavaDoc ATTRIBUTE_name = "name";
47     private static final String JavaDoc ATTRIBUTE_isDefault = "isDefault";
48     private static final String JavaDoc ATTRIBUTE_mode = "mode";
49     private static final String JavaDoc ATTRIBUTE_forecolor = "forecolor";
50     private static final String JavaDoc ATTRIBUTE_backcolor = "backcolor";
51     private static final String JavaDoc ATTRIBUTE_style = "style";
52
53     private static final String JavaDoc ATTRIBUTE_pen = "pen";
54     private static final String JavaDoc ATTRIBUTE_fill = "fill";
55
56     private static final String JavaDoc ATTRIBUTE_radius = "radius";
57
58     private static final String JavaDoc ATTRIBUTE_scaleImage = "scaleImage";
59     // these are inherited by both images and texts.
60
private static final String JavaDoc ATTRIBUTE_hAlign = "hAlign";
61     private static final String JavaDoc ATTRIBUTE_vAlign = "vAlign";
62
63     private static final String JavaDoc ATTRIBUTE_border = "border";
64     private static final String JavaDoc ATTRIBUTE_borderColor = "borderColor";
65     private static final String JavaDoc ATTRIBUTE_padding = "padding";
66     private static final String JavaDoc ATTRIBUTE_topBorder = "topBorder";
67     private static final String JavaDoc ATTRIBUTE_topBorderColor = "topBorderColor";
68     private static final String JavaDoc ATTRIBUTE_topPadding = "topPadding";
69     private static final String JavaDoc ATTRIBUTE_leftBorder = "leftBorder";
70     private static final String JavaDoc ATTRIBUTE_leftBorderColor = "leftBorderColor";
71     private static final String JavaDoc ATTRIBUTE_leftPadding = "leftPadding";
72     private static final String JavaDoc ATTRIBUTE_bottomBorder = "bottomBorder";
73     private static final String JavaDoc ATTRIBUTE_bottomBorderColor = "bottomBorderColor";
74     private static final String JavaDoc ATTRIBUTE_bottomPadding = "bottomPadding";
75     private static final String JavaDoc ATTRIBUTE_rightBorder = "rightBorder";
76     private static final String JavaDoc ATTRIBUTE_rightBorderColor = "rightBorderColor";
77     private static final String JavaDoc ATTRIBUTE_rightPadding = "rightPadding";
78
79     private static final String JavaDoc ATTRIBUTE_rotation = "rotation";
80     private static final String JavaDoc ATTRIBUTE_lineSpacing = "lineSpacing";
81     private static final String JavaDoc ATTRIBUTE_isStyledText = "isStyledText";
82     private static final String JavaDoc ATTRIBUTE_pattern = "pattern";
83     private static final String JavaDoc ATTRIBUTE_isBlankWhenNull = "isBlankWhenNull";
84
85     private static final String JavaDoc ATTRIBUTE_fontName = "fontName";
86     private static final String JavaDoc ATTRIBUTE_isBold = "isBold";
87     private static final String JavaDoc ATTRIBUTE_isItalic = "isItalic";
88     private static final String JavaDoc ATTRIBUTE_isUnderline = "isUnderline";
89     private static final String JavaDoc ATTRIBUTE_isStrikeThrough = "isStrikeThrough";
90     private static final String JavaDoc ATTRIBUTE_fontSize = "fontSize";
91     private static final String JavaDoc ATTRIBUTE_pdfFontName = "pdfFontName";
92     private static final String JavaDoc ATTRIBUTE_pdfEncoding = "pdfEncoding";
93     private static final String JavaDoc ATTRIBUTE_isPdfEmbedded = "isPdfEmbedded";
94
95
96     /**
97      *
98      */

99     public Object JavaDoc createObject(Attributes JavaDoc atts)
100     {
101         JRDesignStyle style = new JRDesignStyle();
102
103         // get style name
104
style.setName(atts.getValue(ATTRIBUTE_name));
105
106         String JavaDoc isDefault = atts.getValue(ATTRIBUTE_isDefault);
107         if (isDefault != null && isDefault.length() > 0)
108         {
109             style.setDefault(Boolean.valueOf(isDefault).booleanValue());
110         }
111
112         // get parent style
113
if (atts.getValue(ATTRIBUTE_style) != null)
114         {
115             JRXmlLoader xmlLoader = null;
116             JRPrintXmlLoader printXmlLoader = null;
117             Map JavaDoc stylesMap = null;
118
119             Object JavaDoc loader = digester.peek(digester.getCount() - 1);
120             if (loader instanceof JRXmlLoader)
121             {
122                 xmlLoader = (JRXmlLoader)loader;
123                 JasperDesign jasperDesign = (JasperDesign)digester.peek(digester.getCount() - 2);
124                 stylesMap = jasperDesign.getStylesMap();
125             }
126             else
127             {
128                 printXmlLoader = (JRPrintXmlLoader)loader;
129                 JasperPrint jasperPrint = (JasperPrint)digester.peek(digester.getCount() - 2);
130                 stylesMap = jasperPrint.getStylesMap();
131             }
132
133             if ( !stylesMap.containsKey(atts.getValue(ATTRIBUTE_style)) )
134             {
135                 if (printXmlLoader == null)
136                 {
137                     xmlLoader.addError(new Exception JavaDoc("Unknown report style : " + atts.getValue(ATTRIBUTE_style)));
138                 }
139                 else
140                 {
141                     printXmlLoader.addError(new Exception JavaDoc("Unknown report style : " + atts.getValue(ATTRIBUTE_style)));
142                 }
143             }
144
145             style.setParentStyle((JRStyle) stylesMap.get(atts.getValue(ATTRIBUTE_style)));
146         }
147
148
149         // get JRElement attributes
150
Byte JavaDoc mode = (Byte JavaDoc)JRXmlConstants.getModeMap().get(atts.getValue(ATTRIBUTE_mode));
151         if (mode != null)
152         {
153             style.setMode(mode);
154         }
155
156         String JavaDoc forecolor = atts.getValue(ATTRIBUTE_forecolor);
157         style.setForecolor(JRXmlConstants.getColor(forecolor, null));
158
159         String JavaDoc backcolor = atts.getValue(ATTRIBUTE_backcolor);
160         style.setBackcolor(JRXmlConstants.getColor(backcolor, null));
161
162
163
164         // get graphic element attributes
165
Byte JavaDoc pen = (Byte JavaDoc)JRXmlConstants.getPenMap().get(atts.getValue(ATTRIBUTE_pen));
166         style.setPen(pen);
167
168         Byte JavaDoc fill = (Byte JavaDoc)JRXmlConstants.getFillMap().get(atts.getValue(ATTRIBUTE_fill));
169         style.setFill(fill);
170
171
172
173         // get rectangle attributes
174
String JavaDoc radius = atts.getValue(ATTRIBUTE_radius);
175         if (radius != null && radius.length() > 0)
176         {
177             style.setRadius(Integer.parseInt(radius));
178         }
179
180
181
182         // get image attributes
183
Byte JavaDoc scaleImage = (Byte JavaDoc)JRXmlConstants.getScaleImageMap().get(atts.getValue(ATTRIBUTE_scaleImage));
184         if (scaleImage != null)
185         {
186             style.setScaleImage(scaleImage);
187         }
188
189         Byte JavaDoc horizontalAlignment = (Byte JavaDoc)JRXmlConstants.getHorizontalAlignMap().get(atts.getValue(ATTRIBUTE_hAlign));
190         if (horizontalAlignment != null)
191         {
192             style.setHorizontalAlignment(horizontalAlignment);
193         }
194
195         Byte JavaDoc verticalAlignment = (Byte JavaDoc)JRXmlConstants.getVerticalAlignMap().get(atts.getValue(ATTRIBUTE_vAlign));
196         if (verticalAlignment != null)
197         {
198             style.setVerticalAlignment(verticalAlignment);
199         }
200
201
202         // get box attributes
203
Byte JavaDoc border = (Byte JavaDoc)JRXmlConstants.getPenMap().get(atts.getValue(ATTRIBUTE_border));
204         if (border != null)
205         {
206             style.setBorder(border);
207         }
208
209         Color JavaDoc borderColor = JRXmlConstants.getColor(atts.getValue(ATTRIBUTE_borderColor), null);
210         if (borderColor != null)
211         {
212             style.setBorderColor(borderColor);
213         }
214
215         String JavaDoc padding = atts.getValue(ATTRIBUTE_padding);
216         if (padding != null && padding.length() > 0)
217         {
218             style.setPadding(Integer.parseInt(padding));
219         }
220
221         border = (Byte JavaDoc)JRXmlConstants.getPenMap().get(atts.getValue(ATTRIBUTE_topBorder));
222         if (border != null)
223         {
224             style.setTopBorder(border);
225         }
226
227         borderColor = JRXmlConstants.getColor(atts.getValue(ATTRIBUTE_topBorderColor), Color.black);
228         if (borderColor != null)
229         {
230             style.setTopBorderColor(borderColor);
231         }
232
233         padding = atts.getValue(ATTRIBUTE_topPadding);
234         if (padding != null && padding.length() > 0)
235         {
236             style.setTopPadding(Integer.parseInt(padding));
237         }
238
239         border = (Byte JavaDoc)JRXmlConstants.getPenMap().get(atts.getValue(ATTRIBUTE_leftBorder));
240         if (border != null)
241         {
242             style.setLeftBorder(border);
243         }
244
245         borderColor = JRXmlConstants.getColor(atts.getValue(ATTRIBUTE_leftBorderColor), Color.black);
246         if (borderColor != null)
247         {
248             style.setLeftBorderColor(borderColor);
249         }
250
251         padding = atts.getValue(ATTRIBUTE_leftPadding);
252         if (padding != null && padding.length() > 0)
253         {
254             style.setLeftPadding(Integer.parseInt(padding));
255         }
256
257         border = (Byte JavaDoc)JRXmlConstants.getPenMap().get(atts.getValue(ATTRIBUTE_bottomBorder));
258         if (border != null)
259         {
260             style.setBottomBorder(border);
261         }
262
263         borderColor = JRXmlConstants.getColor(atts.getValue(ATTRIBUTE_bottomBorderColor), Color.black);
264         if (borderColor != null)
265         {
266             style.setBottomBorderColor(borderColor);
267         }
268
269         padding = atts.getValue(ATTRIBUTE_bottomPadding);
270         if (padding != null && padding.length() > 0)
271         {
272             style.setBottomPadding(Integer.parseInt(padding));
273         }
274
275         border = (Byte JavaDoc)JRXmlConstants.getPenMap().get(atts.getValue(ATTRIBUTE_rightBorder));
276         if (border != null)
277         {
278             style.setRightBorder(border);
279         }
280
281         borderColor = JRXmlConstants.getColor(atts.getValue(ATTRIBUTE_rightBorderColor), Color.black);
282         if (borderColor != null)
283         {
284             style.setRightBorderColor(borderColor);
285         }
286
287         padding = atts.getValue(ATTRIBUTE_rightPadding);
288         if (padding != null && padding.length() > 0)
289         {
290             style.setRightPadding(Integer.parseInt(padding));
291         }
292
293
294
295         Byte JavaDoc rotation = (Byte JavaDoc)JRXmlConstants.getRotationMap().get(atts.getValue(ATTRIBUTE_rotation));
296         if (rotation != null)
297         {
298             style.setRotation(rotation);
299         }
300
301         Byte JavaDoc lineSpacing = (Byte JavaDoc)JRXmlConstants.getLineSpacingMap().get(atts.getValue(ATTRIBUTE_lineSpacing));
302         if (lineSpacing != null)
303         {
304             style.setLineSpacing(lineSpacing);
305         }
306
307         String JavaDoc isStyledText = atts.getValue(ATTRIBUTE_isStyledText);
308         if (isStyledText != null && isStyledText.length() > 0)
309         {
310             style.setStyledText(Boolean.valueOf(isStyledText));
311         }
312
313         style.setPattern(atts.getValue(ATTRIBUTE_pattern));
314
315         String JavaDoc isBlankWhenNull = atts.getValue(ATTRIBUTE_isBlankWhenNull);
316         if (isBlankWhenNull != null && isBlankWhenNull.length() > 0)
317         {
318             style.setBlankWhenNull(Boolean.valueOf(isBlankWhenNull));
319         }
320
321         if (atts.getValue(ATTRIBUTE_fontName) != null)
322             style.setFontName(atts.getValue(ATTRIBUTE_fontName));
323
324         if (atts.getValue(ATTRIBUTE_isBold) != null)
325             style.setBold(Boolean.valueOf(atts.getValue(ATTRIBUTE_isBold)));
326
327         if (atts.getValue(ATTRIBUTE_isItalic) != null)
328             style.setItalic(Boolean.valueOf(atts.getValue(ATTRIBUTE_isItalic)));
329
330         if (atts.getValue(ATTRIBUTE_isUnderline) != null)
331             style.setUnderline(Boolean.valueOf(atts.getValue(ATTRIBUTE_isUnderline)));
332
333         if (atts.getValue(ATTRIBUTE_isStrikeThrough) != null)
334             style.setStrikeThrough(Boolean.valueOf(atts.getValue(ATTRIBUTE_isStrikeThrough)));
335
336         if (atts.getValue(ATTRIBUTE_fontSize) != null)
337             style.setFontSize(Integer.valueOf(atts.getValue(ATTRIBUTE_fontSize)));
338
339         if (atts.getValue(ATTRIBUTE_pdfFontName) != null)
340             style.setPdfFontName(atts.getValue(ATTRIBUTE_pdfFontName));
341
342         if (atts.getValue(ATTRIBUTE_pdfEncoding) != null)
343             style.setPdfEncoding(atts.getValue(ATTRIBUTE_pdfEncoding));
344
345         if (atts.getValue(ATTRIBUTE_isPdfEmbedded) != null)
346             style.setPdfEmbedded(Boolean.valueOf(atts.getValue(ATTRIBUTE_isPdfEmbedded)));
347
348
349         return style;
350     }
351 }
352
Popular Tags