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: package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
018:
019: import org.apache.cocoon.components.elementprocessor.types.Attribute;
020: import org.apache.cocoon.components.elementprocessor.ElementProcessor;
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.types.Validator;
025:
026: import org.apache.poi.hssf.util.HSSFColor;
027: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
028:
029: import java.io.IOException;
030: import java.util.Hashtable;
031:
032: /**
033: * No-op implementation of ElementProcessor to handle the "Top" tag
034: *
035: * This element has two attributes and no content.
036: *
037: * This element is not used in HSSFSerializer 1.0
038: *
039: * @author Marc Johnson (marc_johnson27591@hotmail.com)
040: * @author Andrew C. Oliver (acoliver2@users.sourceforge.net)
041: * @version CVS $Id: EPTop.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class EPTop extends BaseElementProcessor {
044: private NumericResult _style;
045: private ColorCode _color;
046: private boolean _color_fetched;
047: private static final String _style_attribute = "Style";
048: private static final String _color_attribute = "Color";
049: private static final Validator _style_validator = new Validator() {
050: public IOException validate(final Number number) {
051: return BorderStyle.isValid(number.intValue()) ? null
052: : new IOException("\"" + number
053: + "\" is not a legal value");
054: }
055: };
056:
057: /**
058: * constructor
059: */
060: public EPTop() {
061: super (null);
062: _style = null;
063: _color = null;
064: _color_fetched = false;
065: }
066:
067: /**
068: * Override of Initialize() implementation
069: * @param attributes the array of Attribute instances; may be empty, will
070: * never be null
071: * @param parent the parent ElementProcessor; may be null
072: * @exception IOException if anything is wrong
073: */
074: public void initialize(final Attribute[] attributes,
075: final ElementProcessor parent) throws IOException {
076: super .initialize(attributes, parent);
077: EPStyle pstyle = (EPStyle) getAncestor(EPStyle.class);
078: if (pstyle != null && pstyle.isValid()) {
079: Hashtable colorhash = pstyle.getColorHash();
080: HSSFColor color = null;
081:
082: HSSFCellStyle style = pstyle.getStyle(); //oops
083: // a
084: // little
085: // confusing
086: //below is the style attribute
087: //this is an HSSFCellStyle
088: //associated with EPStyle
089: style.setBorderTop((short) getStyle());
090:
091: ColorCode colorCode = getColor();
092: if (colorCode != null) {
093: color = (HSSFColor) colorhash.get(colorCode.toString());
094: }
095: if (color == null)
096: color = new HSSFColor.BLACK();
097:
098: style.setTopBorderColor(color.getIndex());
099: }
100:
101: }
102:
103: /**
104: * @return style as an int from BorderStyle
105: * @exception IOException
106: */
107: public int getStyle() throws IOException {
108: if (_style == null) {
109: _style = NumericConverter.extractInteger(
110: getValue(_style_attribute), _style_validator);
111: }
112: return _style.intValue();
113: }
114:
115: /**
116: * @return color (may be null)
117: * @exception IOException
118: */
119: public ColorCode getColor() throws IOException {
120: if (!_color_fetched) {
121: String colorString = getValue(_color_attribute);
122:
123: if (colorString != null) {
124: _color = new ColorCode(colorString);
125: }
126: _color_fetched = true;
127: }
128: return _color;
129: }
130: } // end public class EPTop
|