001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.xml;
029:
030: import java.util.Collection;
031: import java.util.Map;
032:
033: import net.sf.jasperreports.engine.JRStyle;
034: import net.sf.jasperreports.engine.design.JRDesignElement;
035: import net.sf.jasperreports.engine.design.JRDesignGroup;
036: import net.sf.jasperreports.engine.design.JasperDesign;
037:
038: import org.xml.sax.Attributes;
039:
040: /**
041: * @author Teodor Danciu (teodord@users.sourceforge.net)
042: * @version $Id: JRElementFactory.java 1759 2007-06-20 16:47:34Z lucianc $
043: */
044: public class JRElementFactory extends JRBaseFactory {
045:
046: /**
047: *
048: */
049: public Object createObject(Attributes atts) {
050: JRXmlLoader xmlLoader = (JRXmlLoader) digester.peek(digester
051: .getCount() - 1);
052: Collection groupReprintedElements = xmlLoader
053: .getGroupReprintedElements();
054:
055: JRDesignElement element = (JRDesignElement) digester.peek();
056:
057: element.setKey(atts.getValue(JRXmlConstants.ATTRIBUTE_key));
058:
059: Byte positionType = (Byte) JRXmlConstants
060: .getPositionTypeMap()
061: .get(
062: atts
063: .getValue(JRXmlConstants.ATTRIBUTE_positionType));
064: if (positionType != null) {
065: element.setPositionType(positionType.byteValue());
066: }
067:
068: Byte stretchType = (Byte) JRXmlConstants
069: .getStretchTypeMap()
070: .get(
071: atts
072: .getValue(JRXmlConstants.ATTRIBUTE_stretchType));
073: if (stretchType != null) {
074: element.setStretchType(stretchType.byteValue());
075: }
076:
077: String isPrintRepeatedValues = atts
078: .getValue(JRXmlConstants.ATTRIBUTE_isPrintRepeatedValues);
079: if (isPrintRepeatedValues != null
080: && isPrintRepeatedValues.length() > 0) {
081: element.setPrintRepeatedValues(Boolean.valueOf(
082: isPrintRepeatedValues).booleanValue());
083: }
084:
085: Byte mode = (Byte) JRXmlConstants.getModeMap().get(
086: atts.getValue(JRXmlConstants.ATTRIBUTE_mode));
087: if (mode != null) {
088: element.setMode(mode);
089: }
090:
091: String x = atts.getValue(JRXmlConstants.ATTRIBUTE_x);
092: if (x != null && x.length() > 0) {
093: element.setX(Integer.parseInt(x));
094: }
095:
096: String y = atts.getValue(JRXmlConstants.ATTRIBUTE_y);
097: if (y != null && y.length() > 0) {
098: element.setY(Integer.parseInt(y));
099: }
100:
101: String width = atts.getValue(JRXmlConstants.ATTRIBUTE_width);
102: if (width != null && width.length() > 0) {
103: element.setWidth(Integer.parseInt(width));
104: }
105:
106: String height = atts.getValue(JRXmlConstants.ATTRIBUTE_height);
107: if (height != null && height.length() > 0) {
108: element.setHeight(Integer.parseInt(height));
109: }
110:
111: String isRemoveLineWhenBlank = atts
112: .getValue(JRXmlConstants.ATTRIBUTE_isRemoveLineWhenBlank);
113: if (isRemoveLineWhenBlank != null
114: && isRemoveLineWhenBlank.length() > 0) {
115: element.setRemoveLineWhenBlank(Boolean.valueOf(
116: isRemoveLineWhenBlank).booleanValue());
117: }
118:
119: String isPrintInFirstWholeBand = atts
120: .getValue(JRXmlConstants.ATTRIBUTE_isPrintInFirstWholeBand);
121: if (isPrintInFirstWholeBand != null
122: && isPrintInFirstWholeBand.length() > 0) {
123: element.setPrintInFirstWholeBand(Boolean.valueOf(
124: isPrintInFirstWholeBand).booleanValue());
125: }
126:
127: String isPrintWhenDetailOverflows = atts
128: .getValue(JRXmlConstants.ATTRIBUTE_isPrintWhenDetailOverflows);
129: if (isPrintWhenDetailOverflows != null
130: && isPrintWhenDetailOverflows.length() > 0) {
131: element.setPrintWhenDetailOverflows(Boolean.valueOf(
132: isPrintWhenDetailOverflows).booleanValue());
133: }
134:
135: String groupName = atts
136: .getValue(JRXmlConstants.ATTRIBUTE_printWhenGroupChanges);
137: if (groupName != null) {
138: JRDesignGroup group = new JRDesignGroup();
139: group.setName(groupName);
140: element.setPrintWhenGroupChanges(group);
141: groupReprintedElements.add(element);
142: }
143:
144: String forecolor = atts
145: .getValue(JRXmlConstants.ATTRIBUTE_forecolor);
146: if (forecolor != null && forecolor.length() > 0) {
147: element.setForecolor(JRXmlConstants.getColor(forecolor,
148: null));
149: }
150:
151: String backcolor = atts
152: .getValue(JRXmlConstants.ATTRIBUTE_backcolor);
153: if (backcolor != null && backcolor.length() > 0) {
154: element.setBackcolor(JRXmlConstants.getColor(backcolor,
155: null));
156: }
157:
158: String styleName = atts
159: .getValue(JRXmlConstants.ATTRIBUTE_style);
160: if (styleName != null) {
161: JasperDesign jasperDesign = (JasperDesign) digester
162: .peek(digester.getCount() - 2);
163: Map stylesMap = jasperDesign.getStylesMap();
164:
165: if (stylesMap.containsKey(styleName)) {
166: JRStyle style = (JRStyle) stylesMap.get(styleName);
167: element.setStyle(style);
168: } else {
169: element.setStyleNameReference(styleName);
170: }
171: }
172:
173: return element;
174: }
175: }
|