1. Makes vertical text orientation possible.
2. Caches fonts used in workbook, since there could be an error in MS Excel that maximal numer of fonts used in workbook is exceeded.
Please, if there is a POI-block developer on the list, could you look at that code and think if it is good enough to include it in next Cocoon release?
Regards, Krystian
-- Krystian Nowak [EMAIL PROTECTED] =========================================== Poznan Supercomputing and Networking Center Poland, 60-814 Poznan, Zwierzyniecka 20 tel. (+48 61) 8582159 fax. (+48 61) 8582151 http://www.man.poznan.pl =========================================== BlueEyes - Human-Operator Monitoring System http://www.blueeyes.prv.pl http://www.cs.put.poznan.pl/csidc/2001 ===========================================
Index: EPFont.java
===================================================================
RCS file:
/home/cvspublic/cocoon-2.1/src/blocks/poi/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/elements/EPFont.java,v
retrieving revision 1.6
diff -u -r1.6 EPFont.java
--- EPFont.java 5 Mar 2004 13:02:03 -0000 1.6
+++ EPFont.java 9 Feb 2005 16:01:00 -0000
@@ -16,21 +16,14 @@
package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
-import org.apache.cocoon.components.elementprocessor.types.Attribute;
-import org.apache.cocoon.components.elementprocessor.ElementProcessor;
+import java.io.*;
+import java.util.*;
-import org.apache.cocoon.components.elementprocessor.types.BooleanConverter;
-import org.apache.cocoon.components.elementprocessor.types.BooleanResult;
-import org.apache.cocoon.components.elementprocessor.types.NumericConverter;
-import org.apache.cocoon.components.elementprocessor.types.NumericResult;
-import org.apache.cocoon.components.elementprocessor.types.Validator;
-
-import org.apache.poi.hssf.usermodel.HSSFCellStyle;
-import org.apache.poi.hssf.usermodel.HSSFFont;
-import org.apache.poi.hssf.util.HSSFColor;
-
-import java.io.IOException;
-import java.util.Hashtable;
+import org.apache.cocoon.components.elementprocessor.*;
+import
org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements.fontcache.*;
+import org.apache.cocoon.components.elementprocessor.types.*;
+import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.hssf.util.*;
/**
* No-op implementation of ElementProcessor to handle the "Font" tag
@@ -42,16 +35,19 @@
*
* @author Marc Johnson ([EMAIL PROTECTED])
* @author Andrew C. Oliver ([EMAIL PROTECTED])
+ * @author Krystian Nowak (krystian _a_t_ man _d_o_t_ poznan _d_o_t_ pl)
* @version CVS $Id: EPFont.java,v 1.6 2004/03/05 13:02:03 bdelacretaz Exp $
*/
public class EPFont extends BaseElementProcessor {
+ private static final String DEFAULT_EXCEL_FONT = "Arial";
private NumericResult _unit;
private BooleanResult _bold;
private BooleanResult _italic;
private NumericResult _underline;
private BooleanResult _strike_through;
private String _font;
- private HSSFFont hssfFont;
+ private HSSFCellStyle _style;
+ private Key _fontCacheKey;
private static final String _unit_attribute = "Unit";
private static final String _bold_attribute = "Bold";
private static final String _italic_attribute = "Italic";
@@ -76,6 +72,8 @@
_underline = null;
_strike_through = null;
_font = null;
+ _style = null;
+ _fontCacheKey = null;
}
/**
@@ -92,30 +90,24 @@
if (pstyle.isValid()) {
Hashtable colorhash = pstyle.getColorHash();
HSSFColor color = null;
-
- HSSFCellStyle style = pstyle.getStyle();
- //style.setFillForegroundColor(
- Workbook workbook = getWorkbook();
- HSSFFont font = workbook.createFont();
- style.setFont(font);
- font.setFontHeightInPoints((short)getUnit());
- //font.setFontName(getFont());
- font.setItalic(getItalic());
- if (getBold()) {
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- } else {
- font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
- }
- font.setUnderline((byte)getUnderline());
- font.setStrikeout(getStrikeThrough());
-
+ _style = pstyle.getStyle();
+
color = (HSSFColor)colorhash.get(
pstyle.getForegroundColor().toString());
//System.out.println(pstyle.getForegroundColor().toString());
if (color == null)
color = new HSSFColor.BLACK();
- font.setColor(color.getIndex());
- hssfFont = font;
+
+ //style.setFillForegroundColor(
+
+ _fontCacheKey = new Key();
+ _fontCacheKey.setUnit((short)getUnit());
+ //key.setName(getFont()); // later
+ _fontCacheKey.setItalic(getItalic());
+ _fontCacheKey.setBold(getBold());
+ _fontCacheKey.setUnderline((byte)getUnderline());
+ _fontCacheKey.setStrikeThrough(getStrikeThrough());
+ _fontCacheKey.setColor(color.getIndex());
}
}
@@ -192,22 +184,23 @@
return _font;
}
- private HSSFFont getHSSFFont() {
- return hssfFont;
- }
-
/**
* push the data into the font
* @exception IOException
*/
-
public void endProcessing() throws IOException {
- String thefont = getFont();
- if (thefont != null && !thefont.trim().equals("")
- && getHSSFFont() != null) {
- getHSSFFont().setFontName(thefont);
- } else if (getHSSFFont() != null) {
- getHSSFFont().setFontName("Arial"); //default excel font
+ String theFontName = getFont();
+ if((theFontName == null ) || (theFontName.trim().equals(""))) {
+ _fontCacheKey.setName(DEFAULT_EXCEL_FONT);
+ } else {
+ _fontCacheKey.setName(theFontName.trim());
+ }
+
+ Cache fontCache = getWorkbook().getFontCache();
+ HSSFFont font = fontCache.getFont(_fontCacheKey);
+
+ if(font != null) {
+ _style.setFont(font);
}
}
} // end public class EPFont
Index: EPStyle.java
===================================================================
RCS file:
/home/cvspublic/cocoon-2.1/src/blocks/poi/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/elements/EPStyle.java,v
retrieving revision 1.7
diff -u -r1.7 EPStyle.java
--- EPStyle.java 5 Mar 2004 13:02:04 -0000 1.7
+++ EPStyle.java 9 Feb 2005 16:01:00 -0000
@@ -41,6 +41,7 @@
*
* @author Marc Johnson ([EMAIL PROTECTED])
* @author Andrew C. Oliver ([EMAIL PROTECTED])
+ * @author Krystian Nowak (krystian _a_t_ man _d_o_t_ poznan _d_o_t_ pl)
* @version CVS $Id: EPStyle.java,v 1.7 2004/03/05 13:02:04 bdelacretaz Exp $
*/
public class EPStyle extends BaseElementProcessor {
@@ -116,6 +117,11 @@
convertVAlignment(getVerticalAlignment().getCode());
style.setVerticalAlignment(cnvvalign);
style.setFillPattern((short)getShade());
+ style.setRotation(convertStyleOrientation(
+ isStyleOrientationHoriz(),
+ isStyleOrientationVertHorizText(),
+ isStyleOrientationVertVertText(),
+ isStyleOrientationVertVertText2()));
Workbook workbook = getWorkbook();
HSSFDataFormat dataformat = workbook.createDataFormat();
@@ -549,4 +555,18 @@
return retval;
}
+ /**
+ * deal with mismatch between gnumeric style orientation and Excel cell
style rotation
+ */
+ private short convertStyleOrientation(
+ boolean horiz, boolean vert_horiz_text, boolean vert_vert_text,
+ boolean vert_vert_text2) {
+
+ if(horiz && (!vert_horiz_text) && (!vert_vert_text) &&
(!vert_vert_text2)) {
+ return 0;
+ } else {
+ return 90;
+ }
+ }
+
} // end public class EPStyle
Index: Workbook.java
===================================================================
RCS file:
/home/cvspublic/cocoon-2.1/src/blocks/poi/java/org/apache/cocoon/components/elementprocessor/impl/poi/hssf/elements/Workbook.java,v
retrieving revision 1.7
diff -u -r1.7 Workbook.java
--- Workbook.java 5 Mar 2004 13:02:04 -0000 1.7
+++ Workbook.java 9 Feb 2005 16:01:00 -0000
@@ -16,23 +16,19 @@
package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.poi.hssf.usermodel.HSSFCellStyle;
-import org.apache.poi.hssf.usermodel.HSSFDataFormat;
-import org.apache.poi.hssf.usermodel.HSSFFont;
-import org.apache.poi.hssf.usermodel.HSSFSheet;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import java.io.*;
+import java.util.*;
+
+import
org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements.fontcache.*;
+import org.apache.poi.hssf.usermodel.*;
+import org.apache.poi.poifs.filesystem.*;
/**
* internal representation of a Workbook
*
* @author Marc Johnson ([EMAIL PROTECTED])
* @author Andrew C. Oliver ([EMAIL PROTECTED])
+ * @author Krystian Nowak (krystian _a_t_ man _d_o_t_ poznan _d_o_t_ pl)
* @version CVS $Id: Workbook.java,v 1.7 2004/03/05 13:02:04 bdelacretaz Exp $
*/
@@ -42,6 +38,7 @@
private int _sheet_index;
private final static int REPEAT_CAPACITY = 91;
private Map _repeat;
+ private Cache _fontCache = new Cache(new WorkbookFontFactory(this));
/**
* Constructor Workbook
@@ -123,6 +120,10 @@
HSSFFont font = _workbook.createFont();
return font;
}
+
+ Cache getFontCache() {
+ return _fontCache;
+ }
HSSFWorkbook getWorkbook() {
return _workbook;
Index: WorkbookFontFactory.java
===================================================================
RCS file: WorkbookFontFactory.java
diff -N WorkbookFontFactory.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ WorkbookFontFactory.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,44 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Created on 2005-02-09
+ *
+ */
+package org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements;
+
+import
org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements.fontcache.*;
+import org.apache.poi.hssf.usermodel.*;
+
+
+/**
+ * @author Krystian Nowak (krystian _a_t_ man _d_o_t_ poznan _d_o_t_ pl)
+ */
+public class WorkbookFontFactory
+ implements FontFactory
+{
+ private Workbook _workbook;
+
+ WorkbookFontFactory(Workbook workbook)
+ {
+ _workbook = workbook;
+ }
+
+ public HSSFFont createFont()
+ {
+ return _workbook.createFont();
+ }
+}
Index: fontcache/Cache.java
===================================================================
RCS file: fontcache/Cache.java
diff -N fontcache/Cache.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ fontcache/Cache.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,83 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * Created on 2005-02-09
+ *
+ */
+package
org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements.fontcache;
+
+import org.apache.commons.logging.*;
+import org.apache.poi.hssf.usermodel.*;
+
+import java.util.*;
+
+
+/**
+ * @author Krystian Nowak (krystian _a_t_ man _d_o_t_ poznan _d_o_t_ pl)
+ */
+public class Cache
+{
+ private FontFactory fontFactory;
+ private Map map = new HashMap();
+ private Log log = LogFactory.getLog(Cache.class);
+
+ public Cache(FontFactory fontFactory)
+ {
+ this.fontFactory = fontFactory;
+ }
+
+ public HSSFFont getFont(Key key)
+ {
+ HSSFFont font = (HSSFFont)map.get(key);
+
+ if (font == null) {
+ log.debug("Cache miss with key " + key);
+ font = createFont(key);
+ map.put(key, font);
+ }
+ else {
+ log.debug("Cache hit with key " + key);
+ }
+
+ return font;
+ }
+
+
+ private HSSFFont createFont(Key key)
+ {
+ HSSFFont font = fontFactory.createFont();
+
+ font.setFontHeightInPoints(key.getUnit());
+
+ font.setItalic(key.isItalic());
+
+ if (key.isBold()) {
+ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
+ }
+ else {
+ font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
+ }
+
+ font.setUnderline(key.getUnderline());
+ font.setStrikeout(key.isStrikeThrough());
+
+ font.setColor(key.getColor());
+
+ font.setFontName(key.getName());
+
+ return font;
+ }
+}
Index: fontcache/FontFactory.java
===================================================================
RCS file: fontcache/FontFactory.java
diff -N fontcache/FontFactory.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ fontcache/FontFactory.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,32 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Created on 2005-02-09
+ *
+ */
+package
org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements.fontcache;
+
+import org.apache.poi.hssf.usermodel.*;
+
+
+/**
+ * @author Krystian Nowak (krystian _a_t_ man _d_o_t_ poznan _d_o_t_ pl)
+ */
+public interface FontFactory
+{
+ HSSFFont createFont();
+}
Index: fontcache/Key.java
===================================================================
RCS file: fontcache/Key.java
diff -N fontcache/Key.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ fontcache/Key.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,161 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * Created on 2005-02-09
+ *
+ */
+package
org.apache.cocoon.components.elementprocessor.impl.poi.hssf.elements.fontcache;
+
+import org.apache.commons.lang.builder.*;
+
+
+/**
+ * @author Krystian Nowak (krystian _a_t_ man _d_o_t_ poznan _d_o_t_ pl)
+ */
+public class Key
+{
+ private String name;
+ private short unit;
+ private boolean bold;
+ private boolean italic;
+ private byte underline;
+ private boolean strikeThrough;
+ private short color;
+
+ public boolean isBold()
+ {
+ return bold;
+ }
+
+
+ public void setBold(boolean bold)
+ {
+ this.bold = bold;
+ }
+
+
+ public short getColor()
+ {
+ return color;
+ }
+
+
+ public void setColor(short color)
+ {
+ this.color = color;
+ }
+
+
+ public boolean isItalic()
+ {
+ return italic;
+ }
+
+
+ public void setItalic(boolean italic)
+ {
+ this.italic = italic;
+ }
+
+
+ public String getName()
+ {
+ return name;
+ }
+
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+
+ public boolean isStrikeThrough()
+ {
+ return strikeThrough;
+ }
+
+
+ public void setStrikeThrough(boolean strikeThrough)
+ {
+ this.strikeThrough = strikeThrough;
+ }
+
+
+ public byte getUnderline()
+ {
+ return underline;
+ }
+
+
+ public void setUnderline(byte underline)
+ {
+ this.underline = underline;
+ }
+
+
+ public short getUnit()
+ {
+ return unit;
+ }
+
+
+ public void setUnit(short unit)
+ {
+ this.unit = unit;
+ }
+
+
+ public boolean equals(Object obj)
+ {
+ if (!(obj instanceof Key)) {
+ return false;
+ }
+
+ if (this == obj) {
+ return true;
+ }
+
+ Key other = (Key)obj;
+
+ return new EqualsBuilder().append(name, other.getName())
+ .append(unit, other.getUnit())
+ .append(bold, other.isBold())
+ .append(italic, other.isItalic())
+ .append(underline, other.getUnderline())
+ .append(
+ strikeThrough, other.isStrikeThrough())
+ .append(color, other.getColor()).isEquals();
+ }
+
+
+ public int hashCode()
+ {
+ return new HashCodeBuilder().append(name).append(unit).append(bold)
+ .append(italic).append(underline)
+ .append(strikeThrough).append(color)
+ .toHashCode();
+ }
+
+
+ public String toString()
+ {
+ return new ToStringBuilder(this).append(name).append(unit).append(bold)
+ .append(italic).append(underline)
+ .append(strikeThrough).append(color)
+ .toString();
+ }
+}
