1 16 17 package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements; 18 19 import org.apache.cocoon.components.elementprocessor.types.Attribute; 20 import org.apache.cocoon.components.elementprocessor.ElementProcessor; 21 22 import org.apache.cocoon.components.elementprocessor.types.BooleanConverter; 23 import org.apache.cocoon.components.elementprocessor.types.BooleanResult; 24 import org.apache.cocoon.components.elementprocessor.types.NumericConverter; 25 import org.apache.cocoon.components.elementprocessor.types.NumericResult; 26 import org.apache.cocoon.components.elementprocessor.types.Validator; 27 28 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 29 import org.apache.poi.hssf.usermodel.HSSFFont; 30 import org.apache.poi.hssf.util.HSSFColor; 31 32 import java.io.IOException ; 33 import java.util.Hashtable ; 34 35 47 public class EPFont extends BaseElementProcessor { 48 private NumericResult _unit; 49 private BooleanResult _bold; 50 private BooleanResult _italic; 51 private NumericResult _underline; 52 private BooleanResult _strike_through; 53 private String _font; 54 private HSSFFont hssfFont; 55 private static final String _unit_attribute = "Unit"; 56 private static final String _bold_attribute = "Bold"; 57 private static final String _italic_attribute = "Italic"; 58 private static final String _underline_attribute = "Underline"; 59 private static final String _strike_through_attribute = "StrikeThrough"; 60 private static final Validator _underline_type_validator = 61 new Validator() { 62 public IOException validate(final Number number) { 63 return UnderlineType.isValid(number.intValue()) ? null 64 : new IOException ("\"" + number + "\" is not a legal value"); 65 } 66 }; 67 68 71 public EPFont() { 72 super(null); 73 _unit = null; 74 _bold = null; 75 _italic = null; 76 _underline = null; 77 _strike_through = null; 78 _font = null; 79 } 80 81 88 public void initialize(final Attribute[] attributes, 89 final ElementProcessor parent) throws IOException { 90 super.initialize(attributes, parent); 91 EPStyle pstyle = (EPStyle)parent; 92 if (pstyle.isValid()) { 93 Hashtable colorhash = pstyle.getColorHash(); 94 HSSFColor color = null; 95 96 HSSFCellStyle style = pstyle.getStyle(); 97 Workbook workbook = getWorkbook(); 99 HSSFFont font = workbook.createFont(); 100 style.setFont(font); 101 font.setFontHeightInPoints((short)getUnit()); 102 font.setItalic(getItalic()); 104 if (getBold()) { 105 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 106 } else { 107 font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); 108 } 109 font.setUnderline((byte)getUnderline()); 110 font.setStrikeout(getStrikeThrough()); 111 112 color = (HSSFColor)colorhash.get( 113 pstyle.getForegroundColor().toString()); 114 if (color == null) 116 color = new HSSFColor.BLACK(); 117 font.setColor(color.getIndex()); 118 hssfFont = font; 119 } 120 } 121 122 126 public double getUnit() throws IOException { 127 if (_unit == null) { 128 _unit = NumericConverter.extractDouble(getValue(_unit_attribute)); 129 } 130 return _unit.doubleValue(); 131 } 132 133 137 public boolean getBold() throws IOException { 138 if (_bold == null) { 139 _bold = BooleanConverter.extractBoolean(getValue(_bold_attribute)); 140 } 141 return _bold.booleanValue(); 142 } 143 144 148 public boolean getItalic() throws IOException { 149 if (_italic == null) { 150 _italic = 151 BooleanConverter.extractBoolean(getValue(_italic_attribute)); 152 } 153 return _italic.booleanValue(); 154 } 155 156 160 public int getUnderline() throws IOException { 161 if (_underline == null) { 162 _underline = NumericConverter.extractInteger( 163 getValue(_underline_attribute), _underline_type_validator); 164 } 165 return _underline.intValue(); 166 } 167 168 172 173 public boolean getStrikeThrough() throws IOException { 174 if (_strike_through == null) { 175 _strike_through = BooleanConverter.extractBoolean( 176 getValue(_strike_through_attribute)); 177 } 178 return _strike_through.booleanValue(); 179 } 180 181 184 185 public String getFont() { 186 if (_font == null) { 187 try { 188 _font = getData(); 189 } catch (NullPointerException ignored) { } 190 } 191 192 return _font; 193 } 194 195 private HSSFFont getHSSFFont() { 196 return hssfFont; 197 } 198 199 203 204 public void endProcessing() throws IOException { 205 String thefont = getFont(); 206 if (thefont != null && !thefont.trim().equals("") 207 && getHSSFFont() != null) { 208 getHSSFFont().setFontName(thefont); 209 } else if (getHSSFFont() != null) { 210 getHSSFFont().setFontName("Arial"); } 212 } 213 } | Popular Tags |