1 28 package net.sf.jasperreports.engine.xml; 29 30 import java.util.Collection ; 31 import java.util.Map ; 32 33 import net.sf.jasperreports.engine.JRStyle; 34 import net.sf.jasperreports.engine.design.JRDesignElement; 35 import net.sf.jasperreports.engine.design.JRDesignGroup; 36 import net.sf.jasperreports.engine.design.JasperDesign; 37 38 import org.xml.sax.Attributes ; 39 40 41 45 public class JRElementFactory extends JRBaseFactory 46 { 47 48 49 52 private static final String ATTRIBUTE_key = "key"; 53 private static final String ATTRIBUTE_positionType = "positionType"; 54 private static final String ATTRIBUTE_stretchType = "stretchType"; 55 private static final String ATTRIBUTE_isPrintRepeatedValues = "isPrintRepeatedValues"; 56 private static final String ATTRIBUTE_mode = "mode"; 57 private static final String ATTRIBUTE_x = "x"; 58 private static final String ATTRIBUTE_y = "y"; 59 private static final String ATTRIBUTE_width = "width"; 60 private static final String ATTRIBUTE_height = "height"; 61 private static final String ATTRIBUTE_isRemoveLineWhenBlank = "isRemoveLineWhenBlank"; 62 private static final String ATTRIBUTE_isPrintInFirstWholeBand = "isPrintInFirstWholeBand"; 63 private static final String ATTRIBUTE_isPrintWhenDetailOverflows = "isPrintWhenDetailOverflows"; 64 private static final String ATTRIBUTE_printWhenGroupChanges = "printWhenGroupChanges"; 65 private static final String ATTRIBUTE_forecolor = "forecolor"; 66 private static final String ATTRIBUTE_backcolor = "backcolor"; 67 private static final String ATTRIBUTE_style = "style"; 68 69 70 73 public Object createObject(Attributes atts) 74 { 75 JRXmlLoader xmlLoader = (JRXmlLoader)digester.peek(digester.getCount() - 1); 76 JasperDesign jasperDesign = (JasperDesign)digester.peek(digester.getCount() - 2); 77 Collection groupReprintedElements = xmlLoader.getGroupReprintedElements(); 78 79 JRDesignElement element = (JRDesignElement)digester.peek(); 80 81 element.setKey(atts.getValue(ATTRIBUTE_key)); 82 83 Byte positionType = (Byte )JRXmlConstants.getPositionTypeMap().get(atts.getValue(ATTRIBUTE_positionType)); 84 if (positionType != null) 85 { 86 element.setPositionType(positionType.byteValue()); 87 } 88 89 Byte stretchType = (Byte )JRXmlConstants.getStretchTypeMap().get(atts.getValue(ATTRIBUTE_stretchType)); 90 if (stretchType != null) 91 { 92 element.setStretchType(stretchType.byteValue()); 93 } 94 95 String isPrintRepeatedValues = atts.getValue(ATTRIBUTE_isPrintRepeatedValues); 96 if (isPrintRepeatedValues != null && isPrintRepeatedValues.length() > 0) 97 { 98 element.setPrintRepeatedValues(Boolean.valueOf(isPrintRepeatedValues).booleanValue()); 99 } 100 101 Byte mode = (Byte )JRXmlConstants.getModeMap().get(atts.getValue(ATTRIBUTE_mode)); 102 if (mode != null) 103 { 104 element.setMode(mode); 105 } 106 107 String x = atts.getValue(ATTRIBUTE_x); 108 if (x != null && x.length() > 0) 109 { 110 element.setX(Integer.parseInt(x)); 111 } 112 113 String y = atts.getValue(ATTRIBUTE_y); 114 if (y != null && y.length() > 0) 115 { 116 element.setY(Integer.parseInt(y)); 117 } 118 119 String width = atts.getValue(ATTRIBUTE_width); 120 if (width != null && width.length() > 0) 121 { 122 element.setWidth(Integer.parseInt(width)); 123 } 124 125 String height = atts.getValue(ATTRIBUTE_height); 126 if (height != null && height.length() > 0) 127 { 128 element.setHeight(Integer.parseInt(height)); 129 } 130 131 String isRemoveLineWhenBlank = atts.getValue(ATTRIBUTE_isRemoveLineWhenBlank); 132 if (isRemoveLineWhenBlank != null && isRemoveLineWhenBlank.length() > 0) 133 { 134 element.setRemoveLineWhenBlank(Boolean.valueOf(isRemoveLineWhenBlank).booleanValue()); 135 } 136 137 String isPrintInFirstWholeBand = atts.getValue(ATTRIBUTE_isPrintInFirstWholeBand); 138 if (isPrintInFirstWholeBand != null && isPrintInFirstWholeBand.length() > 0) 139 { 140 element.setPrintInFirstWholeBand(Boolean.valueOf(isPrintInFirstWholeBand).booleanValue()); 141 } 142 143 String isPrintWhenDetailOverflows = atts.getValue(ATTRIBUTE_isPrintWhenDetailOverflows); 144 if (isPrintWhenDetailOverflows != null && isPrintWhenDetailOverflows.length() > 0) 145 { 146 element.setPrintWhenDetailOverflows(Boolean.valueOf(isPrintWhenDetailOverflows).booleanValue()); 147 } 148 149 String groupName = atts.getValue(ATTRIBUTE_printWhenGroupChanges); 150 if (groupName != null) 151 { 152 JRDesignGroup group = new JRDesignGroup(); 153 group.setName(groupName); 154 element.setPrintWhenGroupChanges(group); 155 groupReprintedElements.add(element); 156 } 157 158 String forecolor = atts.getValue(ATTRIBUTE_forecolor); 159 if (forecolor != null && forecolor.length() > 0) 160 { 161 element.setForecolor(JRXmlConstants.getColor(forecolor, null)); 162 } 163 164 String backcolor = atts.getValue(ATTRIBUTE_backcolor); 165 if (backcolor != null && backcolor.length() > 0) 166 { 167 element.setBackcolor(JRXmlConstants.getColor(backcolor, null)); 168 } 169 170 if (atts.getValue(ATTRIBUTE_style) != null) 171 { 172 Map stylesMap = jasperDesign.getStylesMap(); 173 174 if ( !stylesMap.containsKey(atts.getValue(ATTRIBUTE_style)) ) 175 { 176 xmlLoader.addError(new Exception ("Unknown report style : " + atts.getValue(ATTRIBUTE_style))); 177 } 178 179 element.setStyle((JRStyle) stylesMap.get(atts.getValue(ATTRIBUTE_style))); 180 } 181 182 return element; 183 } 184 } 185 | Popular Tags |