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 org.apache.cocoon.components.elementprocessor.types.Attribute;
021: import org.apache.cocoon.components.elementprocessor.ElementProcessor;
022:
023: import org.apache.cocoon.components.elementprocessor.types.BooleanConverter;
024: import org.apache.cocoon.components.elementprocessor.types.BooleanResult;
025: import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
026: import org.apache.cocoon.components.elementprocessor.types.NumericResult;
027: import org.apache.cocoon.components.elementprocessor.types.Validator;
028:
029: import org.apache.poi.hssf.usermodel.HSSFCellStyle;
030: import org.apache.poi.hssf.usermodel.HSSFFont;
031: import org.apache.poi.hssf.util.HSSFColor;
032:
033: import java.io.IOException;
034: import java.util.Hashtable;
035:
036: /**
037: * No-op implementation of ElementProcessor to handle the "Font" tag
038: *
039: * This element has five attributes and also holds the name of the
040: * font as element content.
041: *
042: * This element is not used in HSSFSerializer 1.0
043: *
044: * @author Marc Johnson (marc_johnson27591@hotmail.com)
045: * @author Andrew C. Oliver (acoliver2@users.sourceforge.net)
046: * @version CVS $Id: EPFont.java 433543 2006-08-22 06:22:54Z crossley $
047: */
048: public class EPFont extends BaseElementProcessor {
049: private NumericResult _unit;
050: private BooleanResult _bold;
051: private BooleanResult _italic;
052: private NumericResult _underline;
053: private BooleanResult _strike_through;
054: private String _font;
055: private HSSFFont hssfFont;
056: private static final String _unit_attribute = "Unit";
057: private static final String _bold_attribute = "Bold";
058: private static final String _italic_attribute = "Italic";
059: private static final String _underline_attribute = "Underline";
060: private static final String _strike_through_attribute = "StrikeThrough";
061: private static final Validator _underline_type_validator = new Validator() {
062: public IOException validate(final Number number) {
063: return UnderlineType.isValid(number.intValue()) ? null
064: : new IOException("\"" + number
065: + "\" is not a legal value");
066: }
067: };
068:
069: /**
070: * constructor
071: */
072: public EPFont() {
073: super (null);
074: _unit = null;
075: _bold = null;
076: _italic = null;
077: _underline = null;
078: _strike_through = null;
079: _font = null;
080: }
081:
082: /**
083: * Override of Initialize() implementation
084: * @param attributes the array of Attribute instances; may be empty, will
085: * never be null
086: * @param parent the parent ElementProcessor; may be null
087: * @exception IOException if anything is wrong
088: */
089: public void initialize(final Attribute[] attributes,
090: final ElementProcessor parent) throws IOException {
091: super .initialize(attributes, parent);
092: EPStyle pstyle = (EPStyle) parent;
093: if (pstyle.isValid()) {
094: Hashtable colorhash = pstyle.getColorHash();
095: HSSFColor color = null;
096:
097: HSSFCellStyle style = pstyle.getStyle();
098: //style.setFillForegroundColor(
099: Workbook workbook = getWorkbook();
100: HSSFFont font = workbook.createFont();
101: style.setFont(font);
102: font.setFontHeightInPoints((short) getUnit());
103: //font.setFontName(getFont());
104: font.setItalic(getItalic());
105: if (getBold()) {
106: font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
107: } else {
108: font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
109: }
110: font.setUnderline((byte) getUnderline());
111: font.setStrikeout(getStrikeThrough());
112:
113: color = (HSSFColor) colorhash.get(pstyle
114: .getForegroundColor().toString());
115: //System.out.println(pstyle.getForegroundColor().toString());
116: if (color == null)
117: color = new HSSFColor.BLACK();
118: font.setColor(color.getIndex());
119: hssfFont = font;
120: }
121: }
122:
123: /**
124: * @return unit
125: * @exception IOException
126: */
127: public double getUnit() throws IOException {
128: if (_unit == null) {
129: _unit = NumericConverter
130: .extractDouble(getValue(_unit_attribute));
131: }
132: return _unit.doubleValue();
133: }
134:
135: /**
136: * @return bold
137: * @exception IOException
138: */
139: public boolean getBold() throws IOException {
140: if (_bold == null) {
141: _bold = BooleanConverter
142: .extractBoolean(getValue(_bold_attribute));
143: }
144: return _bold.booleanValue();
145: }
146:
147: /**
148: * @return italic
149: * @exception IOException
150: */
151: public boolean getItalic() throws IOException {
152: if (_italic == null) {
153: _italic = BooleanConverter
154: .extractBoolean(getValue(_italic_attribute));
155: }
156: return _italic.booleanValue();
157: }
158:
159: /**
160: * @return underline as one of the public variables in UnderlineType
161: * @exception IOException
162: */
163: public int getUnderline() throws IOException {
164: if (_underline == null) {
165: _underline = NumericConverter.extractInteger(
166: getValue(_underline_attribute),
167: _underline_type_validator);
168: }
169: return _underline.intValue();
170: }
171:
172: /**
173: * @return strikeThrough
174: * @exception IOException
175: */
176:
177: public boolean getStrikeThrough() throws IOException {
178: if (_strike_through == null) {
179: _strike_through = BooleanConverter
180: .extractBoolean(getValue(_strike_through_attribute));
181: }
182: return _strike_through.booleanValue();
183: }
184:
185: /**
186: * @return name of the font
187: */
188:
189: public String getFont() {
190: if (_font == null) {
191: try {
192: _font = getData();
193: } catch (NullPointerException ignored) { /* VOID */
194: }
195: }
196:
197: return _font;
198: }
199:
200: private HSSFFont getHSSFFont() {
201: return hssfFont;
202: }
203:
204: /**
205: * push the data into the font
206: * @exception IOException
207: */
208:
209: public void endProcessing() throws IOException {
210: String thefont = getFont();
211: if (thefont != null && !thefont.trim().equals("")
212: && getHSSFFont() != null) {
213: getHSSFFont().setFontName(thefont);
214: } else if (getHSSFFont() != null) {
215: getHSSFFont().setFontName("Arial"); //default excel font
216: }
217: }
218: } // end public class EPFont
|