001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
019:
020: import java.util.Hashtable;
021:
022: import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
023: import org.apache.cocoon.components.elementprocessor.types.NumericResult;
024: import org.apache.cocoon.components.elementprocessor.ElementProcessor;
025: import org.apache.cocoon.components.elementprocessor.types.Attribute;
026:
027: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
028: import org.apache.poi.hssf.util.Region;
029:
030: import java.io.IOException;
031:
032: /**
033: * No-op implementation of ElementProcessor to handle the
034: * "StyleRegion" tag
035: *
036: * This element is a container of other elements and has four
037: * attributes that define the boundaries of the region.
038: *
039: * @author Marc Johnson (marc_johnson27591@hotmail.com)
040: * @author Andrew C. Oliver (acoliver2@users.sourceforge.net)
041: * @version CVS $Id: EPStyleRegion.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class EPStyleRegion extends BaseElementProcessor {
044: private static final String _start_col_attribute = "startCol";
045: private static final String _start_row_attribute = "startRow";
046: private static final String _end_col_attribute = "endCol";
047: private static final String _end_row_attribute = "endRow";
048: private NumericResult _start_col;
049: private NumericResult _start_row;
050: private NumericResult _end_col;
051: private NumericResult _end_row;
052:
053: private HSSFCellStyle _style;
054: private Hashtable colorhash;
055:
056: private boolean invalid;
057:
058: //kludge constant to fix gnumeric's love of declaring large stlye regions
059: //for the blank sections of the sheet w/no apparent purpose that we can
060: //
061: private int MAX_AREA = 2001;
062:
063: /**
064: * constructor
065: */
066: public EPStyleRegion() {
067: super (null);
068: _start_col = null;
069: _start_row = null;
070: _end_col = null;
071: _end_row = null;
072: }
073:
074: /**
075: * Override of Initialize() implementation
076: * @param attributes the array of Attribute instances; may be empty, will
077: * never be null
078: * @param parent the parent ElementProcessor; may be null
079: * @exception IOException if anything is wrong
080: */
081: public void initialize(final Attribute[] attributes,
082: final ElementProcessor parent) throws IOException {
083: super .initialize(attributes, parent);
084:
085: Region region = new Region(getStartRow(),
086: (short) getStartCol(), getEndRow(), (short) getEndCol());
087:
088: // if (region.getRowFrom() == 0 &&
089: // region.getColumnFrom() ==0)
090: // getLogger().debug("got 0,0");
091:
092: getLogger().debug("region area is " + region.getArea());
093: if (region.getArea() < MAX_AREA) {
094: //protect against stupid mega regions
095: //of generally NOTHING and no real
096: //puprose created by gnumeric
097: getLogger().debug("region added");
098: _style = getSheet().addStyleRegion(region); //test
099: } else {
100: invalid = true;
101: }
102: colorhash = ((EPStyles) parent).getColorHash();
103: }
104:
105: /**
106: * @return start row
107: * @exception IOException
108: */
109: public int getStartRow() throws IOException {
110: if (_start_row == null) {
111: _start_row = NumericConverter
112: .extractNonNegativeInteger(getValue(_start_row_attribute));
113: }
114: return _start_row.intValue();
115: }
116:
117: /**
118: * @return start column
119: * @exception IOException
120: */
121: public int getStartCol() throws IOException {
122: if (_start_col == null) {
123: _start_col = NumericConverter
124: .extractNonNegativeInteger(getValue(_start_col_attribute));
125: }
126: return _start_col.intValue();
127: }
128:
129: /**
130: * @return end row
131: * @exception IOException
132: */
133: public int getEndRow() throws IOException {
134: if (_end_row == null) {
135: _end_row = NumericConverter
136: .extractNonNegativeInteger(getValue(_end_row_attribute));
137: }
138: return _end_row.intValue();
139: }
140:
141: /**
142: * @return end column
143: * @exception IOException
144: */
145: public int getEndCol() throws IOException {
146: if (_end_col == null) {
147: _end_col = NumericConverter
148: .extractNonNegativeInteger(getValue(_end_col_attribute));
149: }
150: return _end_col.intValue();
151: }
152:
153: /**
154: * @return HSSFCellStyle associated with this style region.
155: */
156: public HSSFCellStyle getStyle() {
157: return _style;
158: }
159:
160: /**
161: * @return instance created in the EPStyles instance from
162: * HSSFColor.getTripletHash();
163: * @see org.apache.poi.hssf.util.HSSFColor#getTripletHash()
164: */
165: public Hashtable getColorHash() {
166: return colorhash;
167: }
168:
169: /**
170: * @return validity (used to determine whether this is a big wasteful
171: * region with no purpose (gnumeric does this
172: */
173: public boolean isValid() {
174: return (!invalid);
175: }
176:
177: } // end public class EPStyleRegion
|