1 28 package net.sf.jasperreports.engine.xml; 29 30 import java.awt.Color ; 31 import java.util.Map ; 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 ; 39 40 44 public class JRStyleFactory extends JRBaseFactory 45 { 46 private static final String ATTRIBUTE_name = "name"; 47 private static final String ATTRIBUTE_isDefault = "isDefault"; 48 private static final String ATTRIBUTE_mode = "mode"; 49 private static final String ATTRIBUTE_forecolor = "forecolor"; 50 private static final String ATTRIBUTE_backcolor = "backcolor"; 51 private static final String ATTRIBUTE_style = "style"; 52 53 private static final String ATTRIBUTE_pen = "pen"; 54 private static final String ATTRIBUTE_fill = "fill"; 55 56 private static final String ATTRIBUTE_radius = "radius"; 57 58 private static final String ATTRIBUTE_scaleImage = "scaleImage"; 59 private static final String ATTRIBUTE_hAlign = "hAlign"; 61 private static final String ATTRIBUTE_vAlign = "vAlign"; 62 63 private static final String ATTRIBUTE_border = "border"; 64 private static final String ATTRIBUTE_borderColor = "borderColor"; 65 private static final String ATTRIBUTE_padding = "padding"; 66 private static final String ATTRIBUTE_topBorder = "topBorder"; 67 private static final String ATTRIBUTE_topBorderColor = "topBorderColor"; 68 private static final String ATTRIBUTE_topPadding = "topPadding"; 69 private static final String ATTRIBUTE_leftBorder = "leftBorder"; 70 private static final String ATTRIBUTE_leftBorderColor = "leftBorderColor"; 71 private static final String ATTRIBUTE_leftPadding = "leftPadding"; 72 private static final String ATTRIBUTE_bottomBorder = "bottomBorder"; 73 private static final String ATTRIBUTE_bottomBorderColor = "bottomBorderColor"; 74 private static final String ATTRIBUTE_bottomPadding = "bottomPadding"; 75 private static final String ATTRIBUTE_rightBorder = "rightBorder"; 76 private static final String ATTRIBUTE_rightBorderColor = "rightBorderColor"; 77 private static final String ATTRIBUTE_rightPadding = "rightPadding"; 78 79 private static final String ATTRIBUTE_rotation = "rotation"; 80 private static final String ATTRIBUTE_lineSpacing = "lineSpacing"; 81 private static final String ATTRIBUTE_isStyledText = "isStyledText"; 82 private static final String ATTRIBUTE_pattern = "pattern"; 83 private static final String ATTRIBUTE_isBlankWhenNull = "isBlankWhenNull"; 84 85 private static final String ATTRIBUTE_fontName = "fontName"; 86 private static final String ATTRIBUTE_isBold = "isBold"; 87 private static final String ATTRIBUTE_isItalic = "isItalic"; 88 private static final String ATTRIBUTE_isUnderline = "isUnderline"; 89 private static final String ATTRIBUTE_isStrikeThrough = "isStrikeThrough"; 90 private static final String ATTRIBUTE_fontSize = "fontSize"; 91 private static final String ATTRIBUTE_pdfFontName = "pdfFontName"; 92 private static final String ATTRIBUTE_pdfEncoding = "pdfEncoding"; 93 private static final String ATTRIBUTE_isPdfEmbedded = "isPdfEmbedded"; 94 95 96 99 public Object createObject(Attributes atts) 100 { 101 JRDesignStyle style = new JRDesignStyle(); 102 103 style.setName(atts.getValue(ATTRIBUTE_name)); 105 106 String isDefault = atts.getValue(ATTRIBUTE_isDefault); 107 if (isDefault != null && isDefault.length() > 0) 108 { 109 style.setDefault(Boolean.valueOf(isDefault).booleanValue()); 110 } 111 112 if (atts.getValue(ATTRIBUTE_style) != null) 114 { 115 JRXmlLoader xmlLoader = null; 116 JRPrintXmlLoader printXmlLoader = null; 117 Map stylesMap = null; 118 119 Object 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 ("Unknown report style : " + atts.getValue(ATTRIBUTE_style))); 138 } 139 else 140 { 141 printXmlLoader.addError(new Exception ("Unknown report style : " + atts.getValue(ATTRIBUTE_style))); 142 } 143 } 144 145 style.setParentStyle((JRStyle) stylesMap.get(atts.getValue(ATTRIBUTE_style))); 146 } 147 148 149 Byte mode = (Byte )JRXmlConstants.getModeMap().get(atts.getValue(ATTRIBUTE_mode)); 151 if (mode != null) 152 { 153 style.setMode(mode); 154 } 155 156 String forecolor = atts.getValue(ATTRIBUTE_forecolor); 157 style.setForecolor(JRXmlConstants.getColor(forecolor, null)); 158 159 String backcolor = atts.getValue(ATTRIBUTE_backcolor); 160 style.setBackcolor(JRXmlConstants.getColor(backcolor, null)); 161 162 163 164 Byte pen = (Byte )JRXmlConstants.getPenMap().get(atts.getValue(ATTRIBUTE_pen)); 166 style.setPen(pen); 167 168 Byte fill = (Byte )JRXmlConstants.getFillMap().get(atts.getValue(ATTRIBUTE_fill)); 169 style.setFill(fill); 170 171 172 173 String radius = atts.getValue(ATTRIBUTE_radius); 175 if (radius != null && radius.length() > 0) 176 { 177 style.setRadius(Integer.parseInt(radius)); 178 } 179 180 181 182 Byte scaleImage = (Byte )JRXmlConstants.getScaleImageMap().get(atts.getValue(ATTRIBUTE_scaleImage)); 184 if (scaleImage != null) 185 { 186 style.setScaleImage(scaleImage); 187 } 188 189 Byte horizontalAlignment = (Byte )JRXmlConstants.getHorizontalAlignMap().get(atts.getValue(ATTRIBUTE_hAlign)); 190 if (horizontalAlignment != null) 191 { 192 style.setHorizontalAlignment(horizontalAlignment); 193 } 194 195 Byte verticalAlignment = (Byte )JRXmlConstants.getVerticalAlignMap().get(atts.getValue(ATTRIBUTE_vAlign)); 196 if (verticalAlignment != null) 197 { 198 style.setVerticalAlignment(verticalAlignment); 199 } 200 201 202 Byte border = (Byte )JRXmlConstants.getPenMap().get(atts.getValue(ATTRIBUTE_border)); 204 if (border != null) 205 { 206 style.setBorder(border); 207 } 208 209 Color borderColor = JRXmlConstants.getColor(atts.getValue(ATTRIBUTE_borderColor), null); 210 if (borderColor != null) 211 { 212 style.setBorderColor(borderColor); 213 } 214 215 String padding = atts.getValue(ATTRIBUTE_padding); 216 if (padding != null && padding.length() > 0) 217 { 218 style.setPadding(Integer.parseInt(padding)); 219 } 220 221 border = (Byte )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 )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 )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 )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 rotation = (Byte )JRXmlConstants.getRotationMap().get(atts.getValue(ATTRIBUTE_rotation)); 296 if (rotation != null) 297 { 298 style.setRotation(rotation); 299 } 300 301 Byte lineSpacing = (Byte )JRXmlConstants.getLineSpacingMap().get(atts.getValue(ATTRIBUTE_lineSpacing)); 302 if (lineSpacing != null) 303 { 304 style.setLineSpacing(lineSpacing); 305 } 306 307 String 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 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 |