]> source.dussan.org Git - poi.git/commitdiff
1. initial support for rich text in xssf"
authorYegor Kozlov <yegor@apache.org>
Thu, 18 Sep 2008 07:42:40 +0000 (07:42 +0000)
committerYegor Kozlov <yegor@apache.org>
Thu, 18 Sep 2008 07:42:40 +0000 (07:42 +0000)
2. cleaned common ooxml-ole2 interfaces, removed ole2-specific stuff
3. added new examples from the quick guide
4. misc xssf refactoring, tending to use enums intstead of final static constants
5. refactored XSSFFont
6. included test-ooxml and jar-ooxml in the dist target, they are part of release and should be there

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@696584 13f79535-47bb-0310-9956-ffa450edef68

34 files changed:
build.xml
src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java [new file with mode: 0755]
src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateNewSpreadsheet.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java [new file with mode: 0755]
src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java
src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java [new file with mode: 0755]
src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/Comment.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Color.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Font.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/RichTextString.java
src/ooxml/java/org/apache/poi/POIXMLDocument.java
src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java [new file with mode: 0755]
src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java
src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java [new file with mode: 0755]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellBorder.java
src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFColor.java
src/ooxml/java/org/apache/poi/xssf/util/CTFontWrapper.java
src/ooxml/java/org/apache/poi/xssf/util/IndexedColors.java [deleted file]
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFont.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java [new file with mode: 0755]
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

index d2472c2dc305dd60c3b4611db113c79ce16a5453..b9c94f171ca53e2a99ecea2aaedd6b5f1ff7d719 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -555,8 +555,8 @@ under the License.
        <delete file="${version.java}" />
   </target>
 
-  <target name="test" depends="test-main,test-scratchpad,test-contrib"
-    description="Tests main, contrib and scratchpad"/>
+  <target name="test" depends="test-main,test-scratchpad,test-contrib,test-ooxml"
+    description="Tests main, contrib, scratchpad and ooxml"/>
 
     <target name="-test-main-check">
         <uptodate property="main.test.notRequired" targetfile="${main.testokfile}">
@@ -1206,7 +1206,7 @@ FORREST_HOME environment variable!</echo>
             </manifest>        
         </jar>
        </target>
-    <target name="jar" depends="compile, compile-version, jar-14" description="Creates jar files for distribution">
+    <target name="jar" depends="compile, compile-version, jar-14, jar-ooxml" description="Creates jar files for distribution">
         <jar destfile="${dist.dir}/${jar.name}-${version.id}-${DSTAMP}.jar">
                        <fileset dir="${main.output.dir}" />
                        <fileset dir="legal/" />
diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateCell.java
new file mode 100755 (executable)
index 0000000..3e49aa2
--- /dev/null
@@ -0,0 +1,76 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import java.io.FileOutputStream;
+import java.util.Date;
+
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+/**
+ *
+ */
+public class CreateCell {
+
+
+       public static void main(String[]args) throws Exception {
+        Workbook wb = new XSSFWorkbook();
+        CreationHelper creationHelper = wb.getCreationHelper();
+        Sheet sheet = wb.createSheet("new sheet");
+
+        // Create a row and put some cells in it. Rows are 0 based.
+        Row row = sheet.createRow((short)0);
+        // Create a cell and put a value in it.
+        Cell cell = row.createCell((short)0);
+        cell.setCellValue(1);
+
+        //numeric value
+        row.createCell(1).setCellValue(1.2);
+
+        //plain string value
+        row.createCell(2).setCellValue("This is a string cell");
+
+        //rich text string
+        RichTextString str = creationHelper.createRichTextString("Apache");
+        Font font = wb.createFont();
+        font.setItalic(true);
+        font.setUnderline(Font.U_SINGLE);
+        str.applyFont(font);
+        row.createCell(3).setCellValue(str);
+
+        //boolean value
+        row.createCell(4).setCellValue(true);
+
+        //formula
+        row.createCell(5).setCellFormula("SUM(A1:B1)");
+
+        //date
+        CellStyle style = wb.createCellStyle();
+        style.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm"));
+        cell = row.createCell(6);
+        cell.setCellValue(new Date());
+        cell.setCellStyle(style);
+
+        // Write the output to a file
+        FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx");
+        wb.write(fileOut);
+        fileOut.close();
+       }
+}
index afef75271593b4dc7c4bf2660c1d80ad3f51b8e9..62664f2dcaa3ef4744ab94cb469617f13c1adae3 100644 (file)
@@ -46,7 +46,7 @@ public class CreateNewSpreadsheet {
                
                s2.getRow(2).createCell(1).setCellValue(createHelper.createRichTextString("Sheet 2"));
 
-               
+/*
                // Comment
                Comment comment = ((XSSFSheet)s1).createComment();
 //             HSSFPatriarch patriach = (HSSFPatriarch)s1.createDrawingPatriarch();
@@ -62,7 +62,7 @@ public class CreateNewSpreadsheet {
                hyperlink.setLabel("Link to POI");
                s1.getRow(1).createCell(1).setHyperlink(hyperlink);
                s1.getRow(1).getCell(1).setCellValue(createHelper.createRichTextString("Link to POI"));
-               
+*/
                // Save
                FileOutputStream fout = new FileOutputStream("NewFile.xlsx");
                wb.write(fout);
diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/CreateUserDefinedDataFormats.java
new file mode 100755 (executable)
index 0000000..9905a9a
--- /dev/null
@@ -0,0 +1,65 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel.examples;
+
+import java.io.FileOutputStream;
+
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.DataFormat;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+/**
+ * How to set user-defined date formats
+ */
+public class CreateUserDefinedDataFormats {
+
+
+    public static void main(String[]args) throws Exception {
+        Workbook wb = new XSSFWorkbook();
+        Sheet sheet = wb.createSheet("format sheet");
+        CellStyle style;
+        DataFormat format = wb.createDataFormat();
+        Row row;
+        Cell cell;
+        short rowNum = 0;
+        short colNum = 0;
+
+        row = sheet.createRow(rowNum++);
+        cell = row.createCell(colNum);
+        cell.setCellValue(11111.25);
+        style = wb.createCellStyle();
+        style.setDataFormat(format.getFormat("0.0"));
+        cell.setCellStyle(style);
+
+        row = sheet.createRow(rowNum++);
+        cell = row.createCell(colNum);
+        cell.setCellValue(11111.25);
+        style = wb.createCellStyle();
+        style.setDataFormat(format.getFormat("#,##0.0000"));
+        cell.setCellStyle(style);
+
+        FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx");
+        wb.write(fileOut);
+        fileOut.close();
+    }
+
+}
index f70596f9496c173559c713fc8b8d38f9d19528d2..baca54c2cabed726b64cb8d0f5233889602c2bf4 100755 (executable)
@@ -17,7 +17,7 @@
 package org.apache.poi.xssf.usermodel.examples;\r
 \r
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
-import org.apache.poi.xssf.util.IndexedColors;\r
+import org.apache.poi.xssf.usermodel.IndexedColors;\r
 import org.apache.poi.ss.usermodel.*;\r
 \r
 import java.io.FileOutputStream;\r
@@ -40,13 +40,13 @@ public class WorkingWithBorders {
         // Style the cell with borders all around.\r
         CellStyle style = wb.createCellStyle();\r
         style.setBorderBottom(CellStyle.BORDER_THIN);\r
-        style.setBottomBorderColor((short)IndexedColors.BLACK);\r
+        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());\r
         style.setBorderLeft(CellStyle.BORDER_THIN);\r
-        style.setLeftBorderColor((short)IndexedColors.GREEN);\r
+        style.setLeftBorderColor(IndexedColors.GREEN.getIndex());\r
         style.setBorderRight(CellStyle.BORDER_THIN);\r
-        style.setRightBorderColor((short)IndexedColors.BLUE);\r
+        style.setRightBorderColor(IndexedColors.BLUE.getIndex());\r
         style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);\r
-        style.setTopBorderColor((short)IndexedColors.BLACK);\r
+        style.setTopBorderColor(IndexedColors.BLACK.getIndex());\r
         cell.setCellStyle(style);\r
 \r
         // Write the output to a file\r
index e86e56eceda6d2860aaf7313bdf43d7923335e3f..c2276ca8550623423fd3a78e5d0ca1ca508a78cd 100755 (executable)
@@ -21,8 +21,8 @@ import org.apache.poi.hssf.util.HSSFColor;
 import org.apache.poi.ss.usermodel.*;\r
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
 import org.apache.poi.xssf.usermodel.XSSFRichTextString;\r
+import org.apache.poi.xssf.usermodel.IndexedColors;\r
 import org.apache.poi.xssf.usermodel.extensions.XSSFColor;\r
-import org.apache.poi.xssf.util.IndexedColors;\r
 \r
 import java.io.FileOutputStream;\r
 \r
@@ -42,7 +42,7 @@ public class WorkingWithFonts {
         font.setFontHeightInPoints((short)24);\r
         font.setFontName("Courier New");\r
 \r
-        font.setColor((short)IndexedColors.RED);\r
+        font.setColor(IndexedColors.RED.getIndex());\r
 \r
         font.setItalic(true);\r
         font.setStrikeout(true);\r
diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java
new file mode 100755 (executable)
index 0000000..c8bb2f0
--- /dev/null
@@ -0,0 +1,62 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.xssf.usermodel.examples;\r
+\r
+import org.apache.poi.xssf.usermodel.*;\r
+import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.hssf.util.HSSFColor;\r
+import org.apache.xmlbeans.XmlOptions;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;\r
+\r
+import java.io.FileOutputStream;\r
+import java.io.Writer;\r
+import java.io.StringWriter;\r
+\r
+/**\r
+ *  Demonstrates how to work with rich text\r
+ */\r
+public class WorkingWithRichText {\r
+\r
+    public static void main(String[] args)\r
+                throws Exception\r
+        {\r
+            XSSFWorkbook wb = new XSSFWorkbook();\r
+\r
+            XSSFSheet sheet = wb.createSheet();\r
+            XSSFRow row = sheet.createRow((short) 2);\r
+\r
+            XSSFCell cell = row.createCell(1);\r
+            XSSFRichTextString rt = new XSSFRichTextString("The quick");\r
+\r
+            XSSFFont font1 = wb.createFont();\r
+            font1.setBold(true);\r
+            rt.append(" brown fox", font1);\r
+\r
+            XSSFFont font2 = wb.createFont();\r
+            font2.setItalic(true);\r
+            font2.setColor(IndexedColors.RED.getIndex());\r
+            rt.applyFont((short)0);\r
+            cell.setCellValue(rt);\r
+\r
+            // Write the output to a file\r
+            FileOutputStream fileOut = new FileOutputStream("rich_text.xlsx");\r
+            wb.write(fileOut);\r
+            fileOut.close();\r
+\r
+        }\r
+\r
+ }\r
index 1959f9ad5642d8415df41c24eef002a3dda0bdfa..c85f79bfd319ced7662b6a3c8f29369dc7166953 100644 (file)
@@ -18,6 +18,6 @@
 package org.apache.poi.ss.usermodel;
 
 public interface Comment {
-       public void setRow(short row);
+       public void setRow(int row);
        public void setColumn(short row);
 }
index 29e6bdd8f824a7a2417c8e7b09b6af646c0592e8..331dd1e78b0dafa2ff0f4f2fc6ee820b5573471a 100644 (file)
@@ -173,6 +173,8 @@ public interface Cell {
 
     void setCellValue(RichTextString value);
 
+    void setCellValue(String value);
+
     void setCellFormula(String formula);
 
     String getCellFormula();
index ecd23691e6beb031b337f58a4f105035abcbb008..2bbcaf16e3375918722684f10a4ae1ff549c3fa7 100644 (file)
@@ -19,22 +19,5 @@ package org.apache.poi.ss.usermodel;
 
 public interface Color {
 
-    /**
-     * @return index to the standard palette
-     */
-
-    short getIndex();
-
-    /**
-     * @return  triplet representation like that in Excel
-     */
-
-    short[] getTriplet();
-
-    /**
-     * @return a hex string exactly like a gnumeric triplet
-     */
-
-    String getHexString();
 
 }
\ No newline at end of file
index 5c070cdd1c91d35b25ad153755a3ee277a6deba0..a737b26a4154f9123446e4f7427fe3df4755dbe5 100644 (file)
@@ -129,14 +129,6 @@ public interface Font {
 
     String getFontName();
 
-    /**
-     * get the index within the HSSFWorkbook (sequence within the collection of Font objects)
-     * @return unique index number of the underlying record this Font represents (probably you don't care
-     *  unless you're comparing which one is which)
-     */
-
-    short getIndex();
-
     /**
      * set the font height in unit's of 1/20th of a point.  Maybe you might want to
      * use the setFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
@@ -217,24 +209,6 @@ public interface Font {
      */
     short getColor();
 
-    /**
-     * set the boldness to use
-     * @param boldweight
-     * @see #BOLDWEIGHT_NORMAL
-     * @see #BOLDWEIGHT_BOLD
-     */
-
-    void setBoldweight(short boldweight);
-
-    /**
-     * get the boldness to use
-     * @return boldweight
-     * @see #BOLDWEIGHT_NORMAL
-     * @see #BOLDWEIGHT_BOLD
-     */
-
-    short getBoldweight();
-
     /**
      * set normal,super or subscript.
      * @param offset type to use (none,super,sub)
index df0ba9ed0c4fdf7ff1c0da18a0f18607249f4b53..551fede9731f3703ec387eb62b9245490eda49cd 100644 (file)
@@ -25,8 +25,6 @@ package org.apache.poi.ss.usermodel;
  * @author Jason Height (jheight at apache.org)
  */
 public interface RichTextString {
-    /** Place holder for indicating that NO_FONT has been applied here */
-    public static final short NO_FONT = 0;
     
     /**
      * Applies a font to the specified characters of a string.
@@ -68,20 +66,8 @@ public interface RichTextString {
     int length();
 
     /**
-     * Returns the font in use at a particular index.
+     * @return  The number of formatting runs used.
      *
-     * @param index         The index.
-     * @return              The font that's currently being applied at that
-     *                      index or null if no font is being applied or the
-     *                      index is out of range.
-     */
-    short getFontAtIndex(int index);
-
-    /**
-     * @return  The number of formatting runs used. There will always be at
-     *          least one of font NO_FONT.
-     *
-     * @see #NO_FONT
      */
     int numFormattingRuns();
 
@@ -92,14 +78,6 @@ public interface RichTextString {
      */
     int getIndexOfFormattingRun(int index);
 
-    /**
-     * Gets the font used in a particular formatting run.
-     *
-     * @param index     the index of the formatting run
-     * @return  the font number used.
-     */
-    short getFontOfFormattingRun(int index);
-
     /**
      * Applies the specified font to the entire string.
      *
index 500d09a8292b97fe75acafe1e0b330e2008df80a..c268dd4c8c1faffaf1da944daeda7c7db4cffdde 100644 (file)
@@ -100,7 +100,14 @@ public abstract class POIXMLDocument {
             throw new IOException(e.toString());
         }
     }
-    
+    public static Package openPackage(InputStream is) throws IOException {
+        try {
+            return Package.open(is);
+        } catch (InvalidFormatException e) {
+            throw new IOException(e.toString());
+        }
+    }
+
     protected Package getPackage() {
         return this.pkg;
     }
diff --git a/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java b/src/ooxml/java/org/apache/poi/xssf/dev/XSSFSave.java
new file mode 100755 (executable)
index 0000000..148a02f
--- /dev/null
@@ -0,0 +1,42 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.xssf.dev;\r
+\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+\r
+import java.io.FileOutputStream;\r
+\r
+/**\r
+ * Utility which loads a SpreadsheetML file and saves it back.\r
+ * This is a handy tool to investigate read-write round trip safety.\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class XSSFSave {\r
+    public static void main(String[] args) throws Exception {\r
+        for (int i = 0; i < args.length; i++) {\r
+            XSSFWorkbook wb = new XSSFWorkbook(args[i]);\r
+\r
+            int sep = args[i].lastIndexOf('.');\r
+            String outfile = args[i].substring(0, sep) + "-save.xlsx";\r
+            FileOutputStream out = new FileOutputStream(outfile);\r
+            wb.write(out);\r
+            out.close();\r
+        }\r
+    }\r
+\r
+}\r
index 515706c772d0c4b31c5f013dd83f693eb6c20a4c..b526d30828324c248df0fafbb9630c5cd4d7f91f 100644 (file)
@@ -195,7 +195,7 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel {
         options.setUseDefaultNamespace();
 
         //re-create the sst table every time saving a workbook
-        SstDocument doc = SstDocument.Factory.newInstance(options);
+        SstDocument doc = SstDocument.Factory.newInstance();
         CTSst sst = doc.addNewSst();
         sst.setCount(count);
         sst.setUniqueCount(uniqueCount);
index b7a6560a6747e8b613eee9290b64a94aabb85ac5..73269437849777b49211124cfdfccef2f2593ce1 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.poi.xssf.model;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.LinkedList;
@@ -59,7 +60,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
  */
 public class StylesTable implements StylesSource, XSSFModel {
     private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
-    private final LinkedList<CTFont> fonts = new LinkedList<CTFont>();
+    private final ArrayList<CTFont> fonts = new ArrayList<CTFont>();
     private final LinkedList<CTFill> fills = new LinkedList<CTFill>();
     private final LinkedList<CTBorder> borders = new LinkedList<CTBorder>();
     private final LinkedList<CTXf> styleXfs = new LinkedList<CTXf>();
@@ -230,7 +231,12 @@ public class StylesTable implements StylesSource, XSSFModel {
     public int getNumCellStyles(){
         return styleXfs.size();
     }
-      
+    /**
+     * get the size of fonts
+     */
+    public int getNumberOfFonts(){
+        return this.fonts.size();
+    } 
     /**
      * For unit testing only
      */
@@ -367,7 +373,7 @@ public class StylesTable implements StylesSource, XSSFModel {
                return fill.putFill(fills);
        }
 
-    private long putFont(XSSFFont font, LinkedList<CTFont> fonts) {
+    private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
        return font.putFont(fonts);
        }
        private void initialize() {
@@ -423,8 +429,8 @@ public class StylesTable implements StylesSource, XSSFModel {
        XSSFFont font=new XSSFFont(ctFont);
                return font;
                */
-               
-               XSSFFont xssfFont=new XSSFFont();
+               CTFont ctFont = CTFont.Factory.newInstance();           
+               XSSFFont xssfFont=new XSSFFont(ctFont);
                xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
                xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme 
                xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/IndexedColors.java
new file mode 100755 (executable)
index 0000000..fbce6fa
--- /dev/null
@@ -0,0 +1,79 @@
+package org.apache.poi.xssf.usermodel;
+
+/**
+ * A deprecated indexing scheme for colours that is still required for some records, and for backwards
+ *  compatibility with OLE2 formats.
+ *
+ * <p>
+ * Each element corresponds to a color index (zero-based). When using the default indexed color palette,
+ * the values are not written out, but instead are implied. When the color palette has been modified from default,
+ * then the entire color palette is used.
+ * </p>
+ *
+ * @author Yegor Kozlov
+ */
+public enum IndexedColors {
+
+    BLACK(8),
+    WHITE(9),
+    RED(10),
+    BRIGHT_GREEN(11),
+    BLUE(12),
+    YELLOW(13),
+    PINK(14),
+    TURQUOISE(15),
+    DARK_RED(16),
+    GREEN(17),
+    DARK_BLUE(18),
+    DARK_YELLOW(19),
+    VIOLET(20),
+    TEAL(21),
+    GREY_25_PERCENT(22),
+    GREY_50_PERCENT(23),
+    CORNFLOWER_BLUE(24),
+    MAROON(25),
+    LEMON_CHIFFON(26),
+    ORCHID(28),
+    CORAL(29),
+    ROYAL_BLUE(30),
+    LIGHT_CORNFLOWER_BLUE(31),
+    SKY_BLUE(40),
+    LIGHT_TURQUOISE(41),
+    LIGHT_GREEN(42),
+    LIGHT_YELLOW(43),
+    PALE_BLUE(44),
+    ROSE(45),
+    LAVENDER(46),
+    TAN(47),
+    LIGHT_BLUE(48),
+    AQUA(49),
+    LIME(50),
+    GOLD(51),
+    LIGHT_ORANGE(52),
+    ORANGE(53),
+    BLUE_GREY(54),
+    GREY_40_PERCENT(55),
+    DARK_TEAL(56),
+    SEA_GREEN(57),
+    DARK_GREEN(58),
+    OLIVE_GREEN(59),
+    BROWN(60),
+    PLUM(61),
+    INDIGO(62),
+    GREY_80_PERCENT(63);
+
+    private short index;
+
+    IndexedColors(int idx){
+        index = (short)idx;
+    }
+
+    /**
+     * Returns index of this color
+     *
+     * @return index of this color
+     */
+    public short getIndex(){
+        return index;
+    }
+}
index 4b77bd10f0a8b6302ed336bcdedb949374b92e76..29e2f0876af544ed1d7ea6bebf145ef799a0a804 100644 (file)
@@ -392,6 +392,10 @@ public final class XSSFCell implements Cell {
 
     }
 
+    public void setCellValue(String str) {
+        this.setCellValue(new XSSFRichTextString(str));
+    }
+    
     public void setCellValue(RichTextString value) {
         if(this.cell.getT() == STCellType.INLINE_STR) {
             this.cell.setV(value.getString());
@@ -401,6 +405,7 @@ public final class XSSFCell implements Cell {
             this.cell.setT(STCellType.S);
         }
         XSSFRichTextString rt = (XSSFRichTextString)value;
+        rt.setStylesTableReference(stylesSource);
         int sRef = sharedStringSource.addEntry(rt.getCTRst());
         this.cell.setV(Integer.toString(sRef));
     }
@@ -437,13 +442,6 @@ public final class XSSFCell implements Cell {
       }
     }
 
-    /**
-     * Creates an XSSFRichTextString for you.
-     */
-       public RichTextString createRichTextString(String text) {
-               return new XSSFRichTextString(text);
-       }
-       
        public Hyperlink getHyperlink() {
                return row.getSheet().getHyperlink(row.getRowNum(), cellNum);
        }
index cf1efaa99043aa7e73af479b799c3bf04fbc3580..544db15623f795bac0f06f95023c45c46cd1c59e 100644 (file)
@@ -417,7 +417,7 @@ public class XSSFCellStyle implements CellStyle {
         return (short) getBorderColor(side).getIndexed();
     }
 
-    private void setBorderColorIndexed(BorderSide side, long color) {
+    private void setBorderColorIndexed(BorderSide side, int color) {
         getBorderColor(side).setIndexed(color);
     }
 
index fb0e0cbb6839df6e957ea6aefff236d04c219455..e052519451982a39b618807af32d77798b72fc73 100644 (file)
-/* ====================================================================\r
-   Licensed to the Apache Software Foundation (ASF) under one or more\r
-   contributor license agreements.  See the NOTICE file distributed with\r
-   this work for additional information regarding copyright ownership.\r
-   The ASF licenses this file to You under the Apache License, Version 2.0\r
-   (the "License"); you may not use this file except in compliance with\r
-   the License.  You may obtain a copy of the License at\r
-\r
-       http://www.apache.org/licenses/LICENSE-2.0\r
-\r
-   Unless required by applicable law or agreed to in writing, software\r
-   distributed under the License is distributed on an "AS IS" BASIS,\r
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-   See the License for the specific language governing permissions and\r
-   limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.xssf.usermodel;\r
-\r
-import java.util.LinkedList;\r
-\r
-import org.apache.poi.ss.usermodel.Font;\r
-\r
-import org.apache.poi.xssf.util.CTFontWrapper;\r
-import org.apache.poi.xssf.util.Charset;\r
-import org.apache.poi.xssf.util.IndexedColors;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;\r
-\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;\r
-\r
-public class XSSFFont implements Font {\r
-\r
-\r
-       public static final int SCHEME_MAJOR=2;\r
-       public static final int SCHEME_MINOR=3;\r
-       public static final int SCHEME_NONE=0;\r
-\r
-       public static final int FONT_FAMILY_NOT_APPLICABLE=0;\r
-       public static final int FONT_FAMILY_ROMAN=1;\r
-       public static final int FONT_FAMILY_SWISS=2;\r
-       public static final int FONT_FAMILY_MODERN=3;\r
-       public static final int FONT_FAMILY_SCRIPT=4;\r
-       public static final int FONT_FAMILY_DECORATIVE=5;\r
-\r
-\r
-       public static final String DEFAULT_FONT_NAME="Calibri";\r
-       public static final short DEFAULT_FONT_SIZE=11;\r
-       public static final short DEFAULT_FONT_COLOR=(short)IndexedColors.BLACK;\r
-\r
-       private int index=0;\r
-\r
-\r
-       private CTFontWrapper fontWrapper;\r
-\r
-\r
-\r
-       public XSSFFont(CTFont font) {\r
-               this.fontWrapper=new CTFontWrapper(font);\r
-       }\r
-\r
-       /*      \r
-       public XSSFFont(int index) {\r
-               this.fontWrapper=new CTFontWrapper(font);\r
-               this.index=index;\r
-       }\r
-        */\r
-\r
-       public XSSFFont() {\r
-               this.fontWrapper = new CTFontWrapper(CTFont.Factory.newInstance());\r
-       }\r
-\r
-\r
-       public CTFont getCTFont(){\r
-               return fontWrapper.getCTFont();\r
-       }\r
-\r
-\r
-       public short getBoldweight() {\r
-               CTBooleanProperty bold=fontWrapper.getB();\r
-               if(bold!=null && bold.getVal())\r
-                       return Font.BOLDWEIGHT_BOLD;\r
-               else\r
-                       return Font.BOLDWEIGHT_NORMAL;\r
-       } \r
-\r
-\r
-\r
-       public byte getCharSet() {\r
-               CTIntProperty charset= fontWrapper.getCharset();\r
-               if(charset!=null){\r
-                       //this value must be set -- can't be null\r
-                       switch (charset.getVal()) {\r
-                       case Charset.ANSI_CHARSET:\r
-                               return Font.ANSI_CHARSET;\r
-\r
-                       case Charset.DEFAULT_CHARSET:\r
-                               return Font.DEFAULT_CHARSET;\r
-\r
-                       case Charset.SYMBOL_CHARSET:\r
-                               return Font.SYMBOL_CHARSET;\r
-\r
-                       default://maight be correct to return this byte value???\r
-                               return Byte.parseByte(Integer.toString(charset.getVal()));\r
-                       }\r
-               }\r
-               else\r
-                       return Font.ANSI_CHARSET;\r
-       }\r
-\r
-       public short getColor() {\r
-               CTColor color=fontWrapper.getColor();\r
-               long index=color.getIndexed();\r
-               if (index==XSSFFont.DEFAULT_FONT_COLOR){\r
-                       return Font.COLOR_NORMAL;\r
-               }\r
-               else if(index==IndexedColors.RED){\r
-                       return Font.COLOR_RED;\r
-               }\r
-               else{\r
-                       return Short.parseShort(new Long(index).toString());\r
-               }\r
-       }\r
-\r
-       public short getFontHeight() {\r
-               if(fontWrapper.getSz()!=null){\r
-                       double fontHeight= fontWrapper.getSz().getVal()/20;\r
-                       return (short)fontHeight;\r
-               }\r
-               else\r
-                       return DEFAULT_FONT_SIZE/20;\r
-       }\r
-\r
-       public short getFontHeightInPoints() {\r
-               if(fontWrapper.getSz()!=null){\r
-                       double fontHeight= fontWrapper.getSz().getVal();// /72;\r
-                       return (short)fontHeight;//new Double(fontHeight).shortValue();\r
-               }\r
-               else\r
-                       return DEFAULT_FONT_SIZE;\r
-       }\r
-\r
-       //AGGIUNGERE CONTROLLO NULL\r
-       public String getFontName() {   \r
-               if(fontWrapper.getName()!=null)\r
-                       return fontWrapper.getName().getVal();\r
-               else\r
-                       return DEFAULT_FONT_NAME;\r
-       }\r
-\r
-\r
-       public short getIndex() {\r
-               // TODO Auto-generated method stub\r
-               return 0;\r
-       }\r
-\r
-       public boolean getItalic() {\r
-               if(fontWrapper.getI()!=null)\r
-                       return fontWrapper.getI().getVal();\r
-               else\r
-                       return false;\r
-       }\r
-\r
-       public boolean getStrikeout() {\r
-               if(fontWrapper.getStrike()!=null)\r
-                       return fontWrapper.getStrike().getVal();\r
-               else\r
-                       return false;\r
-       }\r
-\r
-       public short getTypeOffset() {\r
-               if(fontWrapper.getVertAlign()!=null){\r
-                       int val=fontWrapper.getVertAlign().getVal().intValue();\r
-                       switch (val) {\r
-                       case STVerticalAlignRun.INT_BASELINE:\r
-                               return Font.SS_NONE;\r
-                       case STVerticalAlignRun.INT_SUBSCRIPT:\r
-                               return Font.SS_SUB;\r
-                       case STVerticalAlignRun.INT_SUPERSCRIPT:\r
-                               return Font.SS_SUPER;\r
-                       default: throw new RuntimeException("Wrong offset value "+val);\r
-                       }\r
-               }\r
-               else\r
-                       return Font.SS_NONE;\r
-       }\r
-\r
-       public byte getUnderline() {            \r
-               if(fontWrapper.getU()!=null){\r
-                       //attenzione : -- get val pu\98 tornare null----\r
-                       switch (fontWrapper.getU().getVal().intValue()) {\r
-                       case STUnderlineValues.INT_DOUBLE:\r
-                               return Font.U_DOUBLE;   \r
-                       case STUnderlineValues.INT_DOUBLE_ACCOUNTING:\r
-                               return Font.U_DOUBLE_ACCOUNTING;        \r
-\r
-                       case STUnderlineValues.INT_SINGLE_ACCOUNTING:\r
-                               return Font.U_SINGLE_ACCOUNTING;        \r
-\r
-                       case STUnderlineValues.INT_NONE:\r
-                               return Font.U_NONE;\r
-\r
-                       case STUnderlineValues.INT_SINGLE:      \r
-                       default:\r
-                               return Font.U_SINGLE;\r
-                       }\r
-               }\r
-               return Font.U_NONE;\r
-       }\r
-\r
-       public void setBoldweight(short boldweight) {           \r
-               if(boldweight==Font.BOLDWEIGHT_BOLD){\r
-\r
-                       CTBooleanProperty bold;\r
-                       if(fontWrapper.getCTFont().getBArray().length==0){\r
-                               bold=fontWrapper.getCTFont().addNewB();\r
-                       }\r
-                       else{\r
-                               bold=CTBooleanProperty.Factory.newInstance();\r
-                       }\r
-                       bold.setVal(true);\r
-                       fontWrapper.setB(bold);\r
-               }\r
-       }\r
-\r
-       public void setCharSet(byte charset) {\r
-               CTIntProperty charsetProperty;\r
-               if(fontWrapper.getCTFont().getCharsetArray().length==0){\r
-                       charsetProperty=fontWrapper.getCTFont().addNewCharset();\r
-               }\r
-               else{\r
-                       charsetProperty=CTIntProperty.Factory.newInstance();\r
-               }\r
-               switch (charset) {\r
-               case Font.ANSI_CHARSET:\r
-                       charsetProperty.setVal(Charset.ANSI_CHARSET);   \r
-                       break;\r
-               case Font.SYMBOL_CHARSET:\r
-                       charsetProperty.setVal(Charset.SYMBOL_CHARSET); \r
-                       break;\r
-               case Font.DEFAULT_CHARSET:\r
-                       charsetProperty.setVal(Charset.DEFAULT_CHARSET);        \r
-                       break;\r
-               default:\r
-                       throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset");\r
-               }\r
-\r
-               fontWrapper.setCharset(charsetProperty);\r
-       }\r
-\r
-\r
-       public void setColor(short color) {\r
-               CTColor ctColor;\r
-               if(fontWrapper.getCTFont().getColorArray().length==0){\r
-                       ctColor=fontWrapper.getCTFont().addNewColor();\r
-               }\r
-               else{\r
-                       ctColor=CTColor.Factory.newInstance();\r
-               }\r
-               \r
-               switch (color) {\r
-               case Font.COLOR_NORMAL:{\r
-                       ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);\r
-                       break;\r
-               }\r
-               case Font.COLOR_RED:{\r
-                       ctColor.setIndexed(IndexedColors.RED);\r
-                       break;\r
-               }\r
-               default:\r
-                       ctColor.setIndexed(color);\r
-               }\r
-\r
-               fontWrapper.setColor(ctColor);\r
-       }\r
-\r
-\r
-\r
-       public void setFontHeight(short height) {\r
-               CTFontSize fontSize;\r
-               if(fontWrapper.getCTFont().getSzArray().length==0){\r
-                       fontSize=fontWrapper.getCTFont().addNewSz();\r
-               }\r
-               else{\r
-                       fontSize=CTFontSize.Factory.newInstance();\r
-               }\r
-               fontSize.setVal(height*20);\r
-               fontWrapper.setSz(fontSize);\r
-       }\r
-\r
-\r
-       public void setFontHeightInPoints(short height) {\r
-               CTFontSize fontSize;\r
-               if(fontWrapper.getCTFont().getSzArray().length==0){\r
-                       fontSize=fontWrapper.getCTFont().addNewSz();\r
-               }\r
-               else{\r
-                       fontSize=CTFontSize.Factory.newInstance();\r
-               }\r
-\r
-               fontSize.setVal(height);\r
-               fontWrapper.setSz(fontSize);\r
-       }\r
-\r
-\r
-\r
-       public void setFontName(String name) {\r
-               CTFontName fontName;\r
-               if(fontWrapper.getCTFont().getNameArray().length==0){\r
-                       fontName=fontWrapper.getCTFont().addNewName();\r
-               }\r
-               else{\r
-                       fontName=CTFontName.Factory.newInstance();\r
-               }\r
-\r
-               fontName.setVal(name);\r
-               fontWrapper.setName(fontName);  \r
-       }\r
-       \r
-\r
-       public void setItalic(boolean italic) {\r
-               CTBooleanProperty bool;\r
-               if(fontWrapper.getCTFont().getIArray().length==0){\r
-                       bool=fontWrapper.getCTFont().addNewI();                 \r
-               }\r
-               else{\r
-                       bool=CTBooleanProperty.Factory.newInstance();\r
-               }\r
-\r
-               bool.setVal(italic);\r
-               fontWrapper.setI(bool);\r
-       }\r
-\r
-       public void setStrikeout(boolean strikeout) {\r
-               CTBooleanProperty strike;\r
-               if(fontWrapper.getCTFont().getStrikeArray().length==0){\r
-                       strike=fontWrapper.getCTFont().addNewStrike();\r
-               }\r
-               else{\r
-                       strike=CTBooleanProperty.Factory.newInstance();\r
-               }\r
-               strike.setVal(strikeout);\r
-               fontWrapper.setStrike(strike);\r
-       }\r
-\r
-       public void setTypeOffset(short offset) {\r
-               CTVerticalAlignFontProperty offsetProperty;\r
-               if(fontWrapper.getCTFont().getVertAlignArray().length==0){\r
-                       offsetProperty=fontWrapper.getCTFont().addNewVertAlign();                       \r
-               }\r
-               else{\r
-                       offsetProperty=CTVerticalAlignFontProperty.Factory.newInstance();\r
-               }\r
-               switch (offset) {\r
-               case Font.SS_NONE:\r
-                       offsetProperty.setVal(STVerticalAlignRun.BASELINE);\r
-                       break;\r
-               case Font.SS_SUB:\r
-                       offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT);\r
-                       break;\r
-               case Font.SS_SUPER:\r
-                       offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT);\r
-                       break;\r
-               }\r
-               fontWrapper.setVertAlign(offsetProperty);\r
-       }\r
-\r
-       public void setUnderline(byte underline) {\r
-               CTUnderlineProperty ctUnderline;\r
-               if(fontWrapper.getCTFont().getUArray().length==0){\r
-                       ctUnderline=fontWrapper.getCTFont().addNewU();\r
-               }\r
-               else{\r
-                       ctUnderline=CTUnderlineProperty.Factory.newInstance();\r
-               }\r
-               switch (underline) {\r
-               case Font.U_DOUBLE:\r
-                       ctUnderline.setVal(STUnderlineValues.DOUBLE);   \r
-                       break;\r
-               case Font.U_DOUBLE_ACCOUNTING:\r
-                       ctUnderline.setVal(STUnderlineValues.DOUBLE_ACCOUNTING);        \r
-                       break;\r
-               case Font.U_SINGLE_ACCOUNTING:\r
-                       ctUnderline.setVal(STUnderlineValues.SINGLE_ACCOUNTING);        \r
-                       break;\r
-               case Font.U_NONE:\r
-                       ctUnderline.setVal(STUnderlineValues.NONE);     \r
-                       break;\r
-\r
-               case Font.U_SINGLE:\r
-               default:\r
-                       ctUnderline.setVal(STUnderlineValues.SINGLE);\r
-               break;\r
-               }\r
-\r
-               fontWrapper.setU(ctUnderline);\r
-       }\r
-\r
-\r
-       public long putFont(LinkedList<CTFont> fonts) {\r
-               //TODO\r
-               /*\r
-                * we need to implement a method equals to check that 2 instances of CTFont\r
-                * are different by comparison of all font attributes.\r
-                * NB: take a look to findFont method in XSSFWorkbook\r
-                */\r
-               CTFont font=fontWrapper.getCTFont();\r
-               if(fonts.contains(font)) {\r
-                       return fonts.indexOf(font);\r
-               }\r
-               fonts.add(font);\r
-               return fonts.size() - 1;\r
-       }\r
-\r
-       // solo di xssfFont - non di Font-\r
-       //sono utilizzati nel metodo createDefaultFont in StylesTable insta.\r
-\r
-       public int getScheme(){\r
-               int fontScheme = fontWrapper.getFontScheme().getVal().intValue();\r
-               switch (fontScheme) {\r
-               case STFontScheme.INT_MAJOR:\r
-                       return XSSFFont.SCHEME_MAJOR; \r
-               case STFontScheme.INT_MINOR:\r
-                       return XSSFFont.SCHEME_MINOR;\r
-               case STFontScheme.INT_NONE:\r
-                       return XSSFFont.SCHEME_NONE;\r
-\r
-               default:\r
-                       return fontScheme;\r
-               } \r
-       }\r
-\r
-\r
-       public void setScheme(int scheme){\r
-               CTFontScheme ctFontScheme;\r
-               if(fontWrapper.getCTFont().getSchemeArray().length==0){\r
-                       ctFontScheme=fontWrapper.getCTFont().addNewScheme();\r
-               }\r
-               else{\r
-                       ctFontScheme=CTFontScheme.Factory.newInstance();\r
-               }\r
-               switch (scheme) {\r
-               case XSSFFont.SCHEME_MAJOR:\r
-                       ctFontScheme.setVal(STFontScheme.MAJOR);\r
-                       break;\r
-               case XSSFFont.SCHEME_MINOR:\r
-                       ctFontScheme.setVal(STFontScheme.MINOR);\r
-                       break;\r
-               case XSSFFont.SCHEME_NONE:\r
-                       ctFontScheme.setVal(STFontScheme.NONE);\r
-                       break;\r
-               default:\r
-                       throw new RuntimeException("Schema value ["+ scheme +"] not supported in XSSFFont");\r
-               }\r
-\r
-               fontWrapper.setFontScheme(ctFontScheme);\r
-       }       \r
-\r
-\r
-\r
-       public int getFamily(){\r
-               if(fontWrapper.getFamily()!=null)\r
-                       return fontWrapper.getFamily().getVal();\r
-               else\r
-                       return XSSFFont.FONT_FAMILY_SWISS;\r
-       }\r
-\r
-       public void setFamily(int value){\r
-               //TODO\r
-               CTIntProperty family;\r
-               if(fontWrapper.getCTFont().getSchemeArray().length==0){\r
-                       family=fontWrapper.getCTFont().addNewFamily();\r
-               }\r
-               else{\r
-                       family=CTIntProperty.Factory.newInstance();\r
-               }\r
-               family.setVal(value);\r
-               //fontWrapper.setFamily\r
-       }       \r
-\r
-\r
-\r
-}\r
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.
+==================================================================== */
+package org.apache.poi.xssf.usermodel;
+
+import org.apache.poi.ss.usermodel.Font;
+
+import org.apache.poi.xssf.util.Charset;
+import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
+
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
+
+import java.util.ArrayList;
+
+public class XSSFFont implements Font {
+
+
+    public static final int SCHEME_MAJOR=2;
+    public static final int SCHEME_MINOR=3;
+    public static final int SCHEME_NONE=0;
+
+    public static final int FONT_FAMILY_NOT_APPLICABLE=0;
+    public static final int FONT_FAMILY_ROMAN=1;
+    public static final int FONT_FAMILY_SWISS=2;
+    public static final int FONT_FAMILY_MODERN=3;
+    public static final int FONT_FAMILY_SCRIPT=4;
+    public static final int FONT_FAMILY_DECORATIVE=5;
+
+
+    public static final String DEFAULT_FONT_NAME="Calibri";
+    public static final short DEFAULT_FONT_SIZE=11;
+    public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex();
+
+     private CTFont ctFont;
+
+    public XSSFFont(CTFont font) {
+         this.ctFont=font;
+    }
+
+     protected XSSFFont() {
+         this.ctFont = CTFont.Factory.newInstance();
+    }
+
+
+    public CTFont getCTFont(){
+         return ctFont;
+    }
+
+     /**
+      *
+      */
+     public boolean getBold() {
+         CTBooleanProperty bold=ctFont.sizeOfBArray() == 0 ? null : ctFont.getBArray(0);
+         return (bold!=null && bold.getVal());
+    }
+
+
+
+    public byte getCharSet() {
+         CTIntProperty charset= ctFont.sizeOfCharsetArray() == 0?null:ctFont.getCharsetArray(0);
+        if(charset!=null){
+            //this value must be set -- can't be null
+            switch (charset.getVal()) {
+            case Charset.ANSI_CHARSET:
+                return Font.ANSI_CHARSET;
+
+            case Charset.DEFAULT_CHARSET:
+                return Font.DEFAULT_CHARSET;
+
+            case Charset.SYMBOL_CHARSET:
+                return Font.SYMBOL_CHARSET;
+
+            default://maight be correct to return this byte value???
+                return Byte.parseByte(Integer.toString(charset.getVal()));
+            }
+        }
+        else
+            return Font.ANSI_CHARSET;
+    }
+
+    public short getColor() {
+        CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0);
+        if(color == null) return Font.COLOR_NORMAL;
+        
+        long index=color.getIndexed();
+        if (index==XSSFFont.DEFAULT_FONT_COLOR){
+            return Font.COLOR_NORMAL;
+        }
+        else if(index == IndexedColors.RED.getIndex()){
+            return Font.COLOR_RED;
+        }
+        else{
+            return Short.parseShort(new Long(index).toString());
+        }
+    }
+
+
+     public byte[] getRgbColor() {
+         CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0);
+         return color.getRgb();
+     }
+
+     public short getThemeColor() {
+         CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0);
+         long index=color.getTheme();
+         return (short)index;
+     }
+
+
+    public short getFontHeight() {
+         CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0);
+         if(size!=null){
+             double fontHeight= size.getVal()/20;
+            return (short)fontHeight;
+        }
+        else
+            return DEFAULT_FONT_SIZE/20;
+    }
+
+    public short getFontHeightInPoints() {
+         CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0);
+         if(size!=null){
+             double fontHeight= size.getVal();
+             return (short)fontHeight;
+          }
+        else
+            return DEFAULT_FONT_SIZE;
+    }
+
+
+     public String getFontName() {
+         CTFontName name=ctFont.sizeOfNameArray()==0?null:ctFont.getNameArray(0);
+         return name==null? null:name.getVal();
+    }
+
+
+      public boolean getItalic() {
+         CTBooleanProperty italic=ctFont.sizeOfIArray()==0?null:ctFont.getIArray(0);
+         return italic!=null && italic.getVal();
+      }
+
+      public boolean getStrikeout() {
+         CTBooleanProperty strike=ctFont.sizeOfStrikeArray()==0?null:ctFont.getStrikeArray(0);
+         return strike!=null && strike.getVal();
+      }
+
+    public short getTypeOffset() {
+        CTVerticalAlignFontProperty vAlign=ctFont.sizeOfVertAlignArray()==0?null:ctFont.getVertAlignArray(0);
+        if(vAlign!=null){
+            int val=vAlign.getVal().intValue();
+            switch (val) {
+            case STVerticalAlignRun.INT_BASELINE:
+                return Font.SS_NONE;
+            case STVerticalAlignRun.INT_SUBSCRIPT:
+                return Font.SS_SUB;
+            case STVerticalAlignRun.INT_SUPERSCRIPT:
+                return Font.SS_SUPER;
+            default: throw new RuntimeException("Wrong offset value "+val);
+            }
+        }
+        else
+            return Font.SS_NONE;
+    }
+
+     public byte getUnderline() {
+         CTUnderlineProperty underline=ctFont.sizeOfUArray()==0?null:ctFont.getUArray(0);
+        if(underline!=null){
+             switch (underline.getVal().intValue()) {
+            case STUnderlineValues.INT_DOUBLE:
+                return Font.U_DOUBLE;
+            case STUnderlineValues.INT_DOUBLE_ACCOUNTING:
+                return Font.U_DOUBLE_ACCOUNTING;
+
+            case STUnderlineValues.INT_SINGLE_ACCOUNTING:
+                return Font.U_SINGLE_ACCOUNTING;
+
+            case STUnderlineValues.INT_NONE:
+                return Font.U_NONE;
+
+            case STUnderlineValues.INT_SINGLE:
+            default:
+                return Font.U_SINGLE;
+            }
+        }
+        return Font.U_NONE;
+    }
+
+     /**
+      * Set characters in bold face font style.
+      * If omitted, the default value is true.
+      */
+     public void setBold(boolean bold) {
+         CTBooleanProperty ctBold=ctFont.sizeOfBArray()==0?ctFont.addNewB():ctFont.getBArray(0);
+         ctBold.setVal(true);
+      }
+
+     /**
+      *
+      */
+    public void setCharSet(byte charset) {
+         CTIntProperty charsetProperty=ctFont.sizeOfCharsetArray()==0?ctFont.addNewCharset():ctFont.getCharsetArray(0);
+        switch (charset) {
+        case Font.ANSI_CHARSET:
+            charsetProperty.setVal(Charset.ANSI_CHARSET);
+            break;
+        case Font.SYMBOL_CHARSET:
+            charsetProperty.setVal(Charset.SYMBOL_CHARSET);
+            break;
+        case Font.DEFAULT_CHARSET:
+            charsetProperty.setVal(Charset.DEFAULT_CHARSET);
+            break;
+        default:
+            throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset");
+        }
+    }
+
+
+    public void setColor(short color) {
+         CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0);
+
+        switch (color) {
+        case Font.COLOR_NORMAL:{
+            ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
+            break;
+        }
+        case Font.COLOR_RED:{
+            ctColor.setIndexed(IndexedColors.RED.getIndex());
+            break;
+        }
+        default:
+            ctColor.setIndexed(color);
+        }
+    }
+
+
+
+    public void setFontHeight(short height) {
+        CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0);
+          fontSize.setVal(height*20);
+    }
+
+
+    public void setFontHeightInPoints(short height) {
+          CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0);
+          fontSize.setVal(height);
+    }
+
+
+
+     public void setRgbColor(XSSFColor color) {
+         CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0);
+         ctColor.setRgb(color.getRgb());
+        }
+
+     public void setThemeColor(short theme) {
+         CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0);
+         ctColor.setTheme(theme);
+     }
+
+    public void setFontName(String name) {
+               CTFontName fontName=ctFont.sizeOfNameArray()==0?ctFont.addNewName():ctFont.getNameArray(0);
+               fontName.setVal(name);
+    }
+
+    public void setItalic(boolean italic) {
+         CTBooleanProperty bool=ctFont.sizeOfIArray()==0?ctFont.addNewI():ctFont.getIArray(0);
+          bool.setVal(italic);
+    }
+
+    public void setStrikeout(boolean strikeout) {
+         CTBooleanProperty strike=ctFont.sizeOfStrikeArray()==0?ctFont.addNewStrike():ctFont.getStrikeArray(0);
+          strike.setVal(strikeout);
+    }
+
+    public void setTypeOffset(short offset) {
+         CTVerticalAlignFontProperty offsetProperty=ctFont.sizeOfVertAlignArray()==0?ctFont.addNewVertAlign(): ctFont.getVertAlignArray(0);
+        switch (offset) {
+        case Font.SS_NONE:
+            offsetProperty.setVal(STVerticalAlignRun.BASELINE);
+            break;
+        case Font.SS_SUB:
+            offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT);
+            break;
+        case Font.SS_SUPER:
+            offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT);
+            break;
+        }
+    }
+
+    public void setUnderline(byte underline) {
+         CTUnderlineProperty ctUnderline=ctFont.sizeOfUArray()==0?ctFont.addNewU():ctFont.getUArray(0);
+        switch (underline) {
+        case Font.U_DOUBLE:
+            ctUnderline.setVal(STUnderlineValues.DOUBLE);
+            break;
+        case Font.U_DOUBLE_ACCOUNTING:
+            ctUnderline.setVal(STUnderlineValues.DOUBLE_ACCOUNTING);
+            break;
+        case Font.U_SINGLE_ACCOUNTING:
+            ctUnderline.setVal(STUnderlineValues.SINGLE_ACCOUNTING);
+            break;
+        case Font.U_NONE:
+            ctUnderline.setVal(STUnderlineValues.NONE);
+            break;
+
+        case Font.U_SINGLE:
+        default:
+            ctUnderline.setVal(STUnderlineValues.SINGLE);
+        break;
+        }
+    }
+
+
+     public long putFont(ArrayList<CTFont> fonts) {
+        //TODO
+        /*
+           * we need to implement a method equals to check that 2 instances of CTFont
+           * are different by comparison of all font attributes.
+           * NB: take a look to findFont method in XSSFWorkbook
+           */
+         if(fonts.contains(ctFont)) {
+             return fonts.indexOf(ctFont);
+        }
+         fonts.add(ctFont);
+        return fonts.size() - 1;
+    }
+
+    // solo di xssfFont - non di Font-
+    //sono utilizzati nel metodo createDefaultFont in StylesTable insta.
+
+    public int getScheme(){
+        CTFontScheme scheme=ctFont.sizeOfSchemeArray()==0?null:ctFont.getSchemeArray(0);
+        if(scheme!=null){
+            int fontScheme = scheme.getVal().intValue();
+            switch (fontScheme) {
+            case STFontScheme.INT_MAJOR:
+                return XSSFFont.SCHEME_MAJOR;
+            case STFontScheme.INT_MINOR:
+                return XSSFFont.SCHEME_MINOR;
+            case STFontScheme.INT_NONE:
+                return XSSFFont.SCHEME_NONE;
+
+            default:
+                return fontScheme;
+            }
+        }
+        return 0;
+    }
+
+
+      public void setScheme(int scheme){
+         CTFontScheme ctFontScheme=ctFont.sizeOfSchemeArray()==0?ctFont.addNewScheme():ctFont.getSchemeArray(0);
+        switch (scheme) {
+        case XSSFFont.SCHEME_MAJOR:
+            ctFontScheme.setVal(STFontScheme.MAJOR);
+            break;
+        case XSSFFont.SCHEME_MINOR:
+            ctFontScheme.setVal(STFontScheme.MINOR);
+            break;
+        case XSSFFont.SCHEME_NONE:
+            ctFontScheme.setVal(STFontScheme.NONE);
+            break;
+        default:
+            throw new RuntimeException("Schema value ["+ scheme +"] not supported in XSSFFont");
+        }
+    }
+
+
+      public int getFamily(){
+         CTIntProperty family=ctFont.sizeOfFamilyArray()==0?ctFont.addNewFamily():ctFont.getFamilyArray(0);
+         if(family!=null)
+             return family.getVal();
+          else
+              return XSSFFont.FONT_FAMILY_SWISS;
+      }
+
+      public void setFamily(int value){
+         CTIntProperty family=ctFont.sizeOfFamilyArray()==0?ctFont.addNewFamily():ctFont.getFamilyArray(0);
+          family.setVal(value);
+     }
+
+}
index 83f44da842d6a2f89751b4ffaf3704c31eb78ec4..799479dc98b7db2e8fde4c830ff33c4d453c97a0 100644 (file)
@@ -20,8 +20,9 @@ package org.apache.poi.xssf.usermodel;
 import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.xssf.model.StylesTable;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
+
+import java.util.ArrayList;
 
 
 /**
@@ -30,23 +31,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
  * <p>
  * Most strings in a workbook have formatting applied at the cell level, that is, the entire string in the cell has the
  * same formatting applied. In these cases, the formatting for the cell is stored in the styles part,
- * and the string for the cell can be shared across the workbook. The following xml and code snippet illustrate the example.
+ * and the string for the cell can be shared across the workbook. The following code illustrates the example.
  * </p>
  *
  * <blockquote>
  * <pre>
- * &lt;sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main
- * count="1" uniqueCount="1">
- * &lt;si&gt;
- * &lt;t&gt;Apache POI&lt;/t&gt;
- * &lt;/si&gt;
- * &lt;/sst&gt;
- * </pre>
- * </blockquote>
- *
- * The code to produce xml above:
- * <blockquote>
- * <pre>
  *     cell1.setCellValue(new XSSFRichTextString("Apache POI"));
  *     cell2.setCellValue(new XSSFRichTextString("Apache POI"));
  *     cell3.setCellValue(new XSSFRichTextString("Apache POI"));
@@ -73,56 +62,32 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
  * </pre>
  * </blockquote>
  *
- * The code above will produce the following xml:
- * <blockquote>
- * <pre>
- * &lt;sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main count="2" uniqueCount="2"&gt;
- *  &lt;si&gt;
- *    &lt;r&gt;
- *      &lt;rPr&gt;
- *        &lt;b/&gt;
- *        &lt;sz val="11"/&gt;
- *        &lt;color theme="1"/&gt;
- *        &lt;rFont val="Arial"/&gt;
- *        &lt;family val="2"/&gt;
- *        &lt;scheme val="minor"/&gt;
- *      &lt;/rPr&gt;
- *      &lt;t&gt;Apache POI&lt;/t&gt;
- *    &lt;/r&gt;
- *  &lt;/si&gt;
- *  &lt;si&gt;
- *    &lt;r&gt;
- *      &lt;rPr&gt;
- *       &lt;i/&gt;
- *       &lt;sz val="11"/&gt;
- *        &lt;color theme="1"/&gt;
- *        &lt;rFont val="Courier"/&gt;
- *        &lt;family val="1"/&gt;
- *        &lt;scheme val="minor"/&gt;
- *      &lt;/rPr&gt;
- *      &lt;t&gt;Apache POI&lt;/t&gt;
- *    &lt;/r&gt;
- *  &lt;/si&gt;
- *&lt;/sst&gt;
- *
- * </pre>
- * </blockquote>
  *
  * @author Yegor Kozlov
  */
 public class XSSFRichTextString implements RichTextString {
     private CTRst st;
     private StylesTable styles;
+    private ArrayList<CTRPrElt> fontIdRuns;
 
+    /**
+     * Create a rich text string and initialize it with empty string
+     */
     public XSSFRichTextString(String str) {
         st = CTRst.Factory.newInstance();
         st.setT(str);
     }
 
+    /**
+     * Create empty rich text string
+     */
     public XSSFRichTextString() {
         st = CTRst.Factory.newInstance();
     }
 
+    /**
+     * Create a rich text string from the supplied XML bean
+     */
     public XSSFRichTextString(CTRst st) {
         this.st = st;
     }
@@ -135,8 +100,17 @@ public class XSSFRichTextString implements RichTextString {
      * @param fontIndex     The font to use.
      */
     public void applyFont(int startIndex, int endIndex, short fontIndex) {
-        // TODO Auto-generated method stub
-
+        XSSFFont font;
+        if(styles == null) {
+            //style table is not set, remember fontIndex and set the run properties later,
+            //when setStylesTableReference is called
+            font = new XSSFFont();
+            font.setFontName("#" + fontIndex);
+            fontIdRuns = new ArrayList<CTRPrElt>();
+        } else {
+            font = (XSSFFont)styles.getFontAt(fontIndex);
+        }
+        applyFont(startIndex, endIndex, font);
     }
 
     /**
@@ -147,7 +121,69 @@ public class XSSFRichTextString implements RichTextString {
      * @param font          The index of the font to use.
      */
     public void applyFont(int startIndex, int endIndex, Font font) {
-        applyFont(0, length(), font.getIndex());
+        if (startIndex > endIndex)
+            throw new IllegalArgumentException("Start index must be less than end index.");
+        if (startIndex < 0 || endIndex > length())
+            throw new IllegalArgumentException("Start and end index not in range.");
+        if (startIndex == endIndex)
+            return;
+
+        if(st.sizeOfRArray() == 0 && st.isSetT()) {
+            //convert <t>string</t> into a text run: <r><t>string</t></r>
+            st.addNewR().setT(st.getT());
+            st.unsetT();
+        }
+
+        String text = getString();
+
+        XSSFFont xssfFont = (XSSFFont)font;
+        ArrayList<CTRElt> runs = new ArrayList<CTRElt>();
+
+        int pos = 0;
+        int i;
+        for (i = 0; i < st.sizeOfRArray(); i++) {
+            CTRElt r = st.getRArray(i);
+
+            int len = r.getT().length();
+            int p1 = pos;
+            int p2 = pos + len;
+            if(startIndex > p2) {
+                runs.add(r);
+            } else if (startIndex >= p1 && startIndex < p2){
+                String t = r.getT();
+                r.setT(t.substring(0, startIndex-p1));
+                runs.add(r);
+            } else {
+                break;
+            }
+            pos = p2;
+        }
+        CTRElt r = CTRElt.Factory.newInstance();
+        r.setT(text.substring(startIndex, endIndex));
+        CTRPrElt pr = r.addNewRPr();
+        setRunAttributes(xssfFont.getCTFont(), pr);
+        if(fontIdRuns != null) fontIdRuns.add(pr);
+        runs.add(r);
+
+        for (; i < st.sizeOfRArray(); i++) {
+            r = st.getRArray(i);
+
+            int len = r.getT().length();
+            int p1 = pos;
+            int p2 = pos + len;
+            if(endIndex > p2) {
+                ;
+            } else if (endIndex >= p1 && endIndex < p2){
+                String t = r.getT();
+                r.setT(t.substring(endIndex-p1, len));
+                runs.add(r);
+            } else {
+                runs.add(r);
+            }
+            pos = p2;
+        }
+
+        st.setRArray(runs.toArray(new CTRElt[runs.size()]));
     }
 
     /**
@@ -155,7 +191,20 @@ public class XSSFRichTextString implements RichTextString {
      * @param font          The font to use.
      */
     public void applyFont(Font font) {
-        applyFont(0, length(), font);
+        if(st.sizeOfRArray() == 0 && st.isSetT()) {
+            CTRElt r = st.addNewR();
+            r.setT(st.getT());
+            setRunAttributes(((XSSFFont)font).getCTFont(), r.addNewRPr());
+            st.unsetT();
+        } else {
+            CTRElt r = CTRElt.Factory.newInstance();
+            r.setT(getString());
+            setRunAttributes(((XSSFFont)font).getCTFont(), r.addNewRPr());
+            st.setRArray(new CTRElt[]{r});
+        }
+
+        if(fontIdRuns != null) fontIdRuns.add(st.getRArray(0).getRPr());
+
     }
 
     /**
@@ -164,50 +213,118 @@ public class XSSFRichTextString implements RichTextString {
      * @param fontIndex  the font to apply.
      */
     public void applyFont(short fontIndex) {
-        applyFont(0, length(), fontIndex);
+        XSSFFont font;
+        if(styles == null) {
+            font = new XSSFFont();
+            font.setFontName("#" + fontIndex);
+            fontIdRuns = new ArrayList<CTRPrElt>();
+        } else {
+            font = (XSSFFont)styles.getFontAt(fontIndex);
+        }
+        applyFont(font);
     }
 
     /**
-     * Removes any formatting that may have been applied to the string.
+     * Append new text to this text run and apply the specify font to it
+     *
+     * @param text  the text to append
+     * @param font  the font to apply to the appended text or <code>null</code> if no formatting is required
      */
-    public void clearFormatting() {
-        for (int i = 0; i < st.sizeOfRArray(); i++) {
-            st.removeR(i);
+    public void append(String text, XSSFFont font){
+        if(st.sizeOfRArray() == 0 && st.isSetT()) {
+            //convert <t>string</t> into a text run: <r><t>string</t></r>
+            st.addNewR().setT(st.getT());
+            st.unsetT();
         }
+        CTRElt lt = st.addNewR();
+        lt.setT(text);
+        CTRPrElt pr = lt.addNewRPr();
+        if(font != null) setRunAttributes(font.getCTFont(), pr);
+
+        if(fontIdRuns != null) fontIdRuns.add(pr);
     }
 
     /**
-     * Returns the font in use at a particular index.
+     * Append new text to this text run
      *
-     * @param index         The index.
-     * @return              The font that's currently being applied at that
-     *                      index or null if no font is being applied or the
-     *                      index is out of range.
+     * @param text  the text to append
      */
-    public short getFontAtIndex(int index) {
-        // TODO Auto-generated method stub
-        return 0;
+    public void append(String text){
+        append(text, null);
     }
 
     /**
-     * Gets the font used in a particular formatting run.
-     *
-     * @param index     the index of the formatting run
-     * @return  the font number used.
+     * Copy font attributes from CTFont bean into CTRPrElt bean
      */
-    public short getFontOfFormattingRun(int index) {
-        // TODO Auto-generated method stub
-        return 0;
+    private void setRunAttributes(CTFont ctFont, CTRPrElt pr){
+        if(ctFont.sizeOfBArray() > 0) pr.addNewB().setVal(ctFont.getBArray(0).getVal());
+        if(ctFont.sizeOfUArray() > 0) pr.addNewU().setVal(ctFont.getUArray(0).getVal());
+        if(ctFont.sizeOfIArray() > 0) pr.addNewI().setVal(ctFont.getIArray(0).getVal());
+        if(ctFont.sizeOfColorArray() > 0) {
+            CTColor c1 = ctFont.getColorArray(0);
+            CTColor c2 = pr.addNewColor();
+            if(c1.isSetAuto()) c2.setAuto(c1.getAuto());
+            if(c1.isSetIndexed()) c2.setIndexed(c1.getIndexed());
+            if(c1.isSetRgb()) c2.setRgb(c1.getRgb());
+            if(c1.isSetTheme()) c2.setTheme(c1.getTheme());
+            if(c1.isSetTint()) c2.setTint(c1.getTint());
+        }
+        if(ctFont.sizeOfNameArray() > 0) pr.addNewRFont().setVal(ctFont.getNameArray(0).getVal());
+        if(ctFont.sizeOfFamilyArray() > 0) pr.addNewFamily().setVal(ctFont.getFamilyArray(0).getVal());
+        if(ctFont.sizeOfSchemeArray() > 0) pr.addNewScheme().setVal(ctFont.getSchemeArray(0).getVal());
+        if(ctFont.sizeOfCharsetArray() > 0) pr.addNewCharset().setVal(ctFont.getCharsetArray(0).getVal());
+        if(ctFont.sizeOfCondenseArray() > 0) pr.addNewCondense().setVal(ctFont.getCondenseArray(0).getVal());
+        if(ctFont.sizeOfExtendArray() > 0) pr.addNewExtend().setVal(ctFont.getExtendArray(0).getVal());
+        if(ctFont.sizeOfVertAlignArray() > 0) pr.addNewVertAlign().setVal(ctFont.getVertAlignArray(0).getVal());
+        if(ctFont.sizeOfOutlineArray() > 0) pr.addNewOutline().setVal(ctFont.getOutlineArray(0).getVal());
+        if(ctFont.sizeOfShadowArray() > 0) pr.addNewShadow().setVal(ctFont.getShadowArray(0).getVal());
+        if(ctFont.sizeOfStrikeArray() > 0) pr.addNewStrike().setVal(ctFont.getStrikeArray(0).getVal());
+    }
+
+    /**
+     * Removes any formatting that may have been applied to the string.
+     */
+    public void clearFormatting() {
+        String text = getString();
+        while (st.sizeOfRArray() > 0) {
+            st.removeR(st.sizeOfRArray()-1);
+        }
+        st.setT(text);
     }
 
     /**
      * The index within the string to which the specified formatting run applies.
+     *
      * @param index     the index of the formatting run
      * @return  the index within the string.
      */
     public int getIndexOfFormattingRun(int index) {
-        // TODO Auto-generated method stub
-        return 0;
+        if(st.sizeOfRArray() == 0) return 0;
+
+        int pos = 0;
+        for(int i = 0; i < st.sizeOfRArray(); i++){
+            CTRElt r = st.getRArray(i);
+            if(i == index) return pos;
+
+            pos += r.getT().length();
+        }
+        return -1;
+    }
+
+    /**
+     * Returns the number of characters this format run covers.
+     *
+     * @param index     the index of the formatting run
+     * @return  the number of characters this format run covers
+     */
+    public int getLengthOfFormattingRun(int index) {
+        if(st.sizeOfRArray() == 0) return length();
+
+        for(int i = 0; i < st.sizeOfRArray(); i++){
+            CTRElt r = st.getRArray(i);
+            if(i == index) return r.getT().length();
+        }
+        return -1;
     }
 
     /**
@@ -255,6 +372,44 @@ public class XSSFRichTextString implements RichTextString {
         return st.sizeOfRArray();
     }
 
+    /**
+     * Gets a copy of the font used in a particular formatting run.
+     *
+     * @param index     the index of the formatting run
+     * @return  A copy of the  font used or null if no formatting is applied to the specified text run.
+     */
+    public XSSFFont getFontOfFormattingRun(int index) {
+        if(st.sizeOfRArray() == 0) return null;
+
+        for(int i = 0; i < st.sizeOfRArray(); i++){
+            CTRElt r = st.getRArray(i);
+            if(i == index) return new XSSFFont(toCTFont(r.getRPr()));
+        }
+        return null;
+    }
+
+    /**
+     * Return a copy of the font in use at a particular index.
+     *
+     * @param index         The index.
+     * @return              A copy of the  font that's currently being applied at that
+     *                      index or null if no font is being applied or the
+     *                      index is out of range.
+     */
+    public XSSFFont getFontAtIndex( int index ) {
+        if(st.sizeOfRArray() == 0) return null;
+
+        int pos = 0;
+        for(int i = 0; i < st.sizeOfRArray(); i++){
+            CTRElt r = st.getRArray(i);
+            if(index >= pos && index < pos + r.getT().length()) return new XSSFFont(toCTFont(r.getRPr()));
+
+            pos += r.getT().length();
+        }
+        return null;
+
+    }
+
     /**
      * Return the underlying xml bean
      */
@@ -264,5 +419,51 @@ public class XSSFRichTextString implements RichTextString {
 
     protected void setStylesTableReference(StylesTable tbl){
         styles = tbl;
+        if(fontIdRuns != null){
+            for (CTRPrElt pr : fontIdRuns) {
+                if(pr.sizeOfRFontArray() > 0 ) {
+                    String fontName = pr.getRFontArray(0).getVal();
+                    if(fontName.startsWith("#")){
+                        int idx = Integer.parseInt(fontName.substring(1));
+                        XSSFFont font = (XSSFFont)styles.getFontAt(idx);
+                        pr.removeRFont(0);
+                        setRunAttributes(font.getCTFont(), pr);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     *
+     * CTRPrElt --> CTFont adapter
+     */
+    protected static CTFont toCTFont(CTRPrElt pr){
+        CTFont ctFont =  CTFont.Factory.newInstance();
+
+        if(pr.sizeOfBArray() > 0) ctFont.addNewB().setVal(pr.getBArray(0).getVal());
+        if(pr.sizeOfUArray() > 0) ctFont.addNewU().setVal(pr.getUArray(0).getVal());
+        if(pr.sizeOfIArray() > 0) ctFont.addNewI().setVal(pr.getIArray(0).getVal());
+        if(pr.sizeOfColorArray() > 0) {
+            CTColor c1 = pr.getColorArray(0);
+            CTColor c2 = ctFont.addNewColor();
+            if(c1.isSetAuto()) c2.setAuto(c1.getAuto());
+            if(c1.isSetIndexed()) c2.setIndexed(c1.getIndexed());
+            if(c1.isSetRgb()) c2.setRgb(c1.getRgb());
+            if(c1.isSetTheme()) c2.setTheme(c1.getTheme());
+            if(c1.isSetTint()) c2.setTint(c1.getTint());
+        }
+        if(pr.sizeOfRFontArray() > 0) ctFont.addNewName().setVal(pr.getRFontArray(0).getVal());
+        if(pr.sizeOfFamilyArray() > 0) ctFont.addNewFamily().setVal(pr.getFamilyArray(0).getVal());
+        if(pr.sizeOfSchemeArray() > 0) ctFont.addNewScheme().setVal(pr.getSchemeArray(0).getVal());
+        if(pr.sizeOfCharsetArray() > 0) ctFont.addNewCharset().setVal(pr.getCharsetArray(0).getVal());
+        if(pr.sizeOfCondenseArray() > 0) ctFont.addNewCondense().setVal(pr.getCondenseArray(0).getVal());
+        if(pr.sizeOfExtendArray() > 0) ctFont.addNewExtend().setVal(pr.getExtendArray(0).getVal());
+        if(pr.sizeOfVertAlignArray() > 0) ctFont.addNewVertAlign().setVal(pr.getVertAlignArray(0).getVal());
+        if(pr.sizeOfOutlineArray() > 0) ctFont.addNewOutline().setVal(pr.getOutlineArray(0).getVal());
+        if(pr.sizeOfShadowArray() > 0) ctFont.addNewShadow().setVal(pr.getShadowArray(0).getVal());
+        if(pr.sizeOfStrikeArray() > 0) ctFont.addNewStrike().setVal(pr.getStrikeArray(0).getVal());
+
+        return ctFont;
     }
 }
index 0e89964b3baf6287700775aa058ac946c8cfa9c1..5165a4d6308c11f7ffc28704230f93c73fcf50ff 100644 (file)
@@ -134,6 +134,8 @@ public class XSSFSheet implements Sheet {
         if (this.worksheet.getSheetData() == null) {
                this.worksheet.addNewSheetData();
         }
+        //CTSheetView sheetView = getSheetTypeSheetView();
+        //sheetView.setWorkbookViewId(0);
         initRows(this.worksheet);
         initColumns(this.worksheet);
         
@@ -984,8 +986,17 @@ public class XSSFSheet implements Sheet {
     }
 
     // HSSFSheet compatibility methods. See also the following zoom related methods
+    /**
+     * Sets the zoom magnication for the sheet.  The zoom is expressed as a
+     * fraction.  For example to express a zoom of 75% use 3 for the numerator
+     * and 4 for the denominator.
+     *
+     * @param numerator     The numerator for the zoom magnification.
+     * @param denominator   The denominator for the zoom magnification.
+     */    
     public void setZoom(int numerator, int denominator) {
-       setZoom((numerator/denominator) * 100);
+        Float result = new Float(numerator)/new Float(denominator)*100;
+       setZoom(result.intValue());
     }
 
     public void setZoom(long scale) {
index 53b13ce3023fd88b71ad61d854cbbff3eaba5376..5bc8c14e5dbe69579fd8e2910364a4564a41e8d3 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
@@ -102,6 +103,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
     public XSSFWorkbook(String path) throws IOException {
        this(openPackage(path));
     }
+    public XSSFWorkbook(InputStream  is) throws IOException {
+       this(openPackage(is));
+    }
+
     public XSSFWorkbook(Package pkg) throws IOException {
         super(pkg);
         try {
@@ -299,11 +304,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
 
     }
 
-    public Font findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
+    public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
        short fontNum=getNumberOfFonts();
-        for (short i = 0; i <= fontNum; i++) {
-            XSSFFont xssfFont = (XSSFFont)getFontAt(i);
-            if (xssfFont.getBoldweight() == boldWeight
+        for (short i = 0; i < fontNum; i++) {
+            XSSFFont xssfFont = getFontAt(i);
+            if (    xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD)
                     && xssfFont.getColor() == color
                     && xssfFont.getFontHeightInPoints() == fontHeight
                     && xssfFont.getFontName().equals(name)
@@ -384,8 +389,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
         return (short) getFirstVisibleTab();
     }
 
-    public Font getFontAt(short idx) {
-        return stylesSource.getFontAt(idx);
+    public XSSFFont getFontAt(short idx) {
+        return (XSSFFont)stylesSource.getFontAt(idx);
     }
 
     public XSSFName getNameAt(int index) {
@@ -418,7 +423,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
 
     public short getNumberOfFonts() {
         // TODO Auto-generated method stub
-        return 0;
+        return (short)((StylesTable)stylesSource).getNumberOfFonts();
     }
 
     public int getNumberOfNames() {
index 57d476b0d2a76f22dba3c11c730556b099387484..d57b82446791a9834932ed5940188513347093f9 100644 (file)
@@ -70,7 +70,7 @@ public class XSSFCellBorder {
        }
 
        public void setBorderColor(BorderSide side, XSSFColor color) {
-               color.setToBorder(getBorder(side));
+               getBorder(side).setColor(color.getCTColor());
        }
        
        private CTBorderPr getBorder(BorderSide side) {
index 1d7835a3c44758ce5cb9ba046d8048fe7320093f..8b2d0e70626d3384fb0cade22d60ea75a6ea1da4 100644 (file)
 ==================================================================== */
 package org.apache.poi.xssf.usermodel.extensions;
 
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
 
+/**
+ * Represents a color in SpreadsheetML
+ */
 public class XSSFColor {
        
-       private CTColor color;
-       
-       public XSSFColor(CTColor color) {
-               this.color = color;
-               if (this.color == null) {
-                       this.color = CTColor.Factory.newInstance();
-               }
+       private CTColor ctColor;
+
+    /**
+     * Create an instance of XSSFColor from the supplied XML bean
+     */
+    public XSSFColor(CTColor color) {
+               this.ctColor = color;
        }
        
-       public boolean isAuto() {
-               return color.getAuto();
+    /**
+     * Create an new instance of XSSFColor
+     */
+    public XSSFColor() {
+        this.ctColor = CTColor.Factory.newInstance();
+    }
+
+    /**
+     * A boolean value indicating the ctColor is automatic and system ctColor dependent.
+     */
+    public boolean isAuto() {
+               return ctColor.getAuto();
        }
        
+    /**
+     * A boolean value indicating the ctColor is automatic and system ctColor dependent.
+     */
        public void setAuto(boolean auto) {
-               color.setAuto(auto);
+               ctColor.setAuto(auto);
        }
-       
-       public long getIndexed() {
-               return color.getIndexed();
+
+    /**
+     * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
+     */
+    public int getIndexed() {
+               return (int)ctColor.getIndexed();
        }
        
-       public void setIndexed(long indexed) {
-               color.setIndexed(indexed);
+    /**
+     * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
+     */
+       public void setIndexed(int indexed) {
+               ctColor.setIndexed(indexed);
        }
-       
-       public byte[] getRgb() {
-               return color.getRgb();
+
+    /**
+     * Standard Alpha Red Green Blue ctColor value (ARGB).
+     */
+    public byte[] getRgb() {
+               return ctColor.getRgb();
        }
        
+    /**
+     * Standard Alpha Red Green Blue ctColor value (ARGB).
+     */
        public void setRgb(byte[] rgb) {
-               color.setRgb(rgb);
+               ctColor.setRgb(rgb);
        }
-       
-       public long getTheme() {
-               return color.getTheme();
+
+    /**
+     * Index into the <clrScheme> collection, referencing a particular <sysClr> or
+     *  <srgbClr> value expressed in the Theme part.
+     */
+    public int getTheme() {
+               return (int)ctColor.getTheme();
        }
        
-       public void setTheme(long theme) {
-               color.setTheme(theme);
+    /**
+     * Index into the <clrScheme> collection, referencing a particular <sysClr> or
+     *  <srgbClr> value expressed in the Theme part.
+     */
+       public void setTheme(int theme) {
+               ctColor.setTheme(theme);
        }
-       
-       public double getTint() {
-               return color.getTint();
+
+    /**
+     * Specifies the tint value applied to the ctColor.
+     *
+     * <p>
+     * If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final
+     * ctColor applied.
+     * </p>
+     * <p>
+     * The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and
+     * 1.0 means 100% lighten. Also, 0.0 means no change.
+     * </p>
+     * <p>
+     * In loading the RGB value, it is converted to HLS where HLS values are (0..HLSMAX), where
+     * HLSMAX is currently 255.
+     * </p>
+     * Here are some examples of how to apply tint to ctColor:
+     * <blockquote>
+     * <pre>
+     * If (tint &lt; 0)
+     * Lum\92 = Lum * (1.0 + tint)
+     *
+     * For example: Lum = 200; tint = -0.5; Darken 50%
+     * Lum\91 = 200 * (0.5) =&gt; 100
+     * For example: Lum = 200; tint = -1.0; Darken 100% (make black)
+     * Lum\91 = 200 * (1.0-1.0) =&gt; 0
+     * If (tint &gt; 0)
+     * Lum\91 = Lum * (1.0-tint) + (HLSMAX \96 HLSMAX * (1.0-tint))
+     * For example: Lum = 100; tint = 0.75; Lighten 75%
+     *
+     * Lum\91 = 100 * (1-.75) + (HLSMAX \96 HLSMAX*(1-.75))
+     * = 100 * .25 + (255 \96 255 * .25)
+     * = 25 + (255 \96 63) = 25 + 192 = 217
+     * For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
+     * Lum\91 = 100 * (1-1) + (HLSMAX \96 HLSMAX*(1-1))
+     * = 100 * 0 + (255 \96 255 * 0)
+     * = 0 + (255 \96 0) = 255
+     * </pre>
+     * </blockquote>
+     *
+     * @return the tint value
+     */
+    public double getTint() {
+               return ctColor.getTint();
        }
        
+    /**
+     * Specifies the tint value applied to the ctColor.
+     *
+     * <p>
+     * If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final
+     * ctColor applied.
+     * </p>
+     * <p>
+     * The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and
+     * 1.0 means 100% lighten. Also, 0.0 means no change.
+     * </p>
+     * <p>
+     * In loading the RGB value, it is converted to HLS where HLS values are (0..HLSMAX), where
+     * HLSMAX is currently 255.
+     * </p>
+     * Here are some examples of how to apply tint to ctColor:
+     * <blockquote>
+     * <pre>
+     * If (tint &lt; 0)
+     * Lum\92 = Lum * (1.0 + tint)
+     *
+     * For example: Lum = 200; tint = -0.5; Darken 50%
+     * Lum\91 = 200 * (0.5) =&gt; 100
+     * For example: Lum = 200; tint = -1.0; Darken 100% (make black)
+     * Lum\91 = 200 * (1.0-1.0) =&gt; 0
+     * If (tint &gt; 0)
+     * Lum\91 = Lum * (1.0-tint) + (HLSMAX \96 HLSMAX * (1.0-tint))
+     * For example: Lum = 100; tint = 0.75; Lighten 75%
+     *
+     * Lum\91 = 100 * (1-.75) + (HLSMAX \96 HLSMAX*(1-.75))
+     * = 100 * .25 + (255 \96 255 * .25)
+     * = 25 + (255 \96 63) = 25 + 192 = 217
+     * For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
+     * Lum\91 = 100 * (1-1) + (HLSMAX \96 HLSMAX*(1-1))
+     * = 100 * 0 + (255 \96 255 * 0)
+     * = 0 + (255 \96 0) = 255
+     * </pre>
+     * </blockquote>
+     *
+     * @param tint the tint value
+     */
        public void setTint(double tint) {
-               color.setTint(tint);
+               ctColor.setTint(tint);
        }
 
-       public void setToBorder(CTBorderPr border) {
-               border.setColor(this.color);
-       }
+    /**
+     * Returns the underlying XML bean
+     *
+     * @return the underlying XML bean
+     */
+    public CTColor getCTColor(){
+        return ctColor;
+    }
 }
index c772a7566e7d6dd5ec57c8b3038db417d154b929..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100755 (executable)
@@ -1,176 +0,0 @@
-package org.apache.poi.xssf.util;
-
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
-
-/*
- * The font element in xml is definited like <choice maxOccurs="unbounded">.
- * So in the java object CTFont all methods get and set returns an array of elements also if there is always defined
- * only one type of attribute per type.
- * This class is made to make simple using method get and set instead of getArray() or set(index,object).
- * We consider always the index 0 like the only one index to refer of CT_Font attribute.
- * 
- */
-
-
-public class CTFontWrapper{
-
-       private CTFont font;
-               
-       
-       public CTFontWrapper(CTFont font){
-               this.font=font;         
-       }
-       
-       public CTFont getCTFont(){
-               return font;
-       }
-       
-       
-       public CTBooleanProperty getB(){
-               if( font.getBArray().length>0) 
-                       return font.getBArray(0);
-               else 
-                       return null;
-       }
-       
-       
-       public CTIntProperty getCharset(){
-               if(font.getCharsetArray().length>0)
-                       return font.getCharsetArray(0);
-               else
-                       return null;
-       }
-       
-       public CTColor getColor(){
-               if(font.getColorArray().length>0)
-                       return font.getColorArray(0);
-               else
-                       return null;
-       }
-       
-       public CTBooleanProperty getStrike(){
-               if(font.getStrikeArray().length>0)
-                       return font.getStrikeArray(0);
-               else
-                       return null;
-       }
-       
-       public CTVerticalAlignFontProperty getVertAlign() {
-               if(font.getVertAlignArray().length>0)
-                       return font.getVertAlignArray(0);
-               else
-                       return null;
-       }
-       
-       public CTFontName setName(){
-               if(font.getNameArray().length>0)
-                       return font.getNameArray(0);
-               else
-                       return null;
-       }
-       
-       public CTFontSize getSz(){
-               if(font.getSzArray().length>0)
-                       return font.getSzArray(0);
-               else
-                       return null;
-       }
-       
-       public CTBooleanProperty getI(){
-               if(font.getIArray().length>0)
-                       return font.getIArray(0);
-               else
-                       return null;
-       }
-       
-       
-       
-       public CTUnderlineProperty getU(){
-               if(font.getUArray().length>0)
-                       return font.getUArray(0);
-               else
-                       return null;
-       }
-       
-       public void setB(CTBooleanProperty value){
-               font.setBArray(0,value);
-       }
-       
-       public void setCharset(CTIntProperty value){
-               font.setCharsetArray(0, value);
-       }
-       
-       public void setColor(CTColor value){
-               font.setColorArray(0,value);
-               }
-
-       public void setFontName(CTFontName value){
-               font.setNameArray(0,value);
-       }
-       
-       public void setSz(CTFontSize value){
-               font.setSzArray(0,value);
-       }
-       public void setI(CTBooleanProperty value){
-               font.setIArray(0,value);
-       }
-       
-       public void setU(CTUnderlineProperty value){
-               font.setUArray(0,value);        
-       }
-       
-       
-       public void setStrike(CTBooleanProperty value){
-               font.setStrikeArray(0,value);
-       }
-       
-       
-       public void setVertAlign(CTVerticalAlignFontProperty value){
-               font.setVertAlignArray(0,value);
-       }
-
-       
-       public void setName(CTFontName fontName) {
-               font.setNameArray(0,fontName);
-       }
-
-       public CTFontName getName() {
-               return font.getNameArray(0);
-       }
-
-       public CTIntProperty getFamily() {
-               return font.getFamilyArray(0);          
-       }
-
-       public void setFamily(CTIntProperty family) {
-                font.setFamilyArray(0,family);         
-       }
-
-       
-       public void setFontScheme(CTFontScheme ctFontScheme) {
-               font.setSchemeArray(0,ctFontScheme);
-       }
-
-       public CTFontScheme getFontScheme() {
-               return font.getSchemeArray(0);
-       }
-       
-       // methods used in FontFormatting
-       
-       public CTBooleanProperty getOutline(){
-               return font.getOutlineArray(0);
-       }
-       
-       
-       
-}
-
-       
\ No newline at end of file
diff --git a/src/ooxml/java/org/apache/poi/xssf/util/IndexedColors.java b/src/ooxml/java/org/apache/poi/xssf/util/IndexedColors.java
deleted file mode 100755 (executable)
index b54ee9b..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-package org.apache.poi.xssf.util;
-
-public class IndexedColors {
-       
-       public static int BLACK=0;
-       public static int WHITE=1;
-       public static int RED=2;
-       public static int GREEN=3;
-       public static int BLUE=4;
-       public static int YELLOW=5;
-       public static int PINK=6;
-       
-       public static int LIGHT_GREY=22;
-       public static int DARK_GREY=23;
-       
-}
index 1f170d27908d9491e0d741faf053adf66caa0108..c389e63fa8063cdf0e21d68f2137902b14e2d361 100644 (file)
@@ -132,8 +132,6 @@ public final class TestXSSFExcelExtractor extends TestCase {
                        POITextExtractor extractor = extractors[i];
                        
                        String text = extractor.getText().replaceAll("[\r\t]", "");
-                       //System.out.println(text.length());
-                       //System.out.println(text);
                        assertTrue(text.startsWith("First Sheet\nTest spreadsheet\n2nd row2nd row 2nd column\n"));
                        Pattern pattern = Pattern.compile(".*13(\\.0+)?\\s+Sheet3.*", Pattern.DOTALL);
                        Matcher m = pattern.matcher(text);
index 58b8ee730f73b414185e8bc3ab0f6db08f2fde10..a83d4a0355662f4b235e5ffc89b9374086dd5f79 100644 (file)
@@ -32,6 +32,7 @@ import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.CommentsTable;
+import org.apache.poi.xssf.model.SharedStringsTable;
 import org.apache.poi.xssf.model.SharedStringSource;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
@@ -336,7 +337,7 @@ public final class TestXSSFCell extends TestCase {
 
     private static XSSFRow createParentObjects() {
         XSSFWorkbook wb = new XSSFWorkbook();
-        wb.setSharedStringSource(new DummySharedStringSource());
+        wb.setSharedStringSource(new SharedStringsTable());
         XSSFSheet sheet = new XSSFSheet(wb);
         XSSFRow row = new XSSFRow(sheet);
         return row;
index 18557639e03dc202c971fa52ba3c707de70c5329..ced39c2b713f98eba3cb1d1b8b6d87930740d803 100755 (executable)
@@ -1,5 +1,7 @@
 package org.apache.poi.xssf.usermodel;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -11,9 +13,8 @@ import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.xssf.util.CTFontWrapper;
 import org.apache.poi.xssf.util.Charset;
-import org.apache.poi.xssf.util.IndexedColors;
+import org.openxml4j.opc.Package;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
@@ -38,60 +39,51 @@ public class TestXSSFFont extends TestCase{
 
        public void testBoldweight(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
-
-               CTBooleanProperty bool=wrapper.getCTFont().addNewB();
+               CTBooleanProperty bool=ctFont.addNewB();
                bool.setVal(false);
-               wrapper.setB(bool);     
-
+               ctFont.setBArray(0,bool);
                XSSFFont xssfFont=new XSSFFont(ctFont);
-               assertEquals(Font.BOLDWEIGHT_NORMAL, xssfFont.getBoldweight());
+               assertEquals(false, xssfFont.getBold());
 
 
-               xssfFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
+               xssfFont.setBold(true);
                assertEquals(ctFont.getBArray().length,1);
                assertEquals(true, ctFont.getBArray(0).getVal());
-               assertEquals(true,wrapper.getB().getVal());
-
        }
 
        public void testCharSet(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
                CTIntProperty prop=ctFont.addNewCharset();
                prop.setVal(Charset.ANSI_CHARSET);
-               
-               wrapper.setCharset(prop);
+
+               ctFont.setCharsetArray(0,prop);
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet());
-               
+
                xssfFont.setCharSet(Font.DEFAULT_CHARSET);
-               assertEquals(Charset.DEFAULT_CHARSET, wrapper.getCharset().getVal());
+               assertEquals(Charset.DEFAULT_CHARSET,ctFont.getCharsetArray(0).getVal());
        }
 
 
        public void testFontName(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
                CTFontName fname=ctFont.addNewName();
                fname.setVal("Arial");
-               wrapper.setFontName(fname);
+               ctFont.setNameArray(0,fname);
 
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals("Arial", xssfFont.getFontName());
 
                xssfFont.setFontName("Courier");
-               assertEquals("Courier",wrapper.getName().getVal());
+               assertEquals("Courier",ctFont.getNameArray(0).getVal());
        }
 
 
        public void testItalic(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
-
-               CTBooleanProperty bool=wrapper.getCTFont().addNewI();
+               CTBooleanProperty bool=ctFont.addNewI();
                bool.setVal(false);
-               wrapper.setI(bool);     
+               ctFont.setIArray(0,bool);
 
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals(false, xssfFont.getItalic());
@@ -99,17 +91,15 @@ public class TestXSSFFont extends TestCase{
                xssfFont.setItalic(true);
                assertEquals(ctFont.getIArray().length,1);
                assertEquals(true, ctFont.getIArray(0).getVal());
-               assertEquals(true,wrapper.getI().getVal());
+               assertEquals(true,ctFont.getIArray(0).getVal());
        }
 
 
        public void testStrikeout(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
-
-               CTBooleanProperty bool=wrapper.getCTFont().addNewStrike();
+               CTBooleanProperty bool=ctFont.addNewStrike();
                bool.setVal(false);
-               wrapper.setStrike(bool);        
+               ctFont.setStrikeArray(0,bool);
 
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals(false, xssfFont.getStrikeout());
@@ -117,139 +107,218 @@ public class TestXSSFFont extends TestCase{
                xssfFont.setStrikeout(true);
                assertEquals(ctFont.getStrikeArray().length,1);
                assertEquals(true, ctFont.getStrikeArray(0).getVal());
-               assertEquals(true,wrapper.getStrike().getVal());
+               assertEquals(true,ctFont.getStrikeArray(0).getVal());
        }
 
 
        public void testFontHeight(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
                CTFontSize size=ctFont.addNewSz();
                size.setVal(11);
-               wrapper.setSz(size);
-               
+               ctFont.setSzArray(0,size);
+
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals(11/20,xssfFont.getFontHeight());
-               
+
                xssfFont.setFontHeight((short)20);
-               assertEquals(new Double(20*20).doubleValue(),wrapper.getSz().getVal()); }
+               assertEquals(new Double(20*20).doubleValue(),ctFont.getSzArray(0).getVal());
+       }
 
 
        public void testFontHeightInPoint(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
                CTFontSize size=ctFont.addNewSz();
                size.setVal(14);
-               wrapper.setSz(size);
-               
+               ctFont.setSzArray(0,size);
+
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals(14,xssfFont.getFontHeightInPoints());
-               
+
                xssfFont.setFontHeightInPoints((short)20);
-               assertEquals(new Double(20).doubleValue(),wrapper.getSz().getVal());
+               assertEquals(new Double(20).doubleValue(),ctFont.getSzArray(0).getVal());
        }
 
 
        public void testUnderline(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
-
-               CTUnderlineProperty underlinePropr=wrapper.getCTFont().addNewU();
+               CTUnderlineProperty underlinePropr=ctFont.addNewU();
                underlinePropr.setVal(STUnderlineValues.SINGLE);
-               wrapper.setU(underlinePropr);   
+               ctFont.setUArray(0,underlinePropr);
 
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals(Font.U_SINGLE, xssfFont.getUnderline());
 
                xssfFont.setUnderline(Font.U_DOUBLE);
                assertEquals(ctFont.getUArray().length,1);
-               assertEquals(STUnderlineValues.DOUBLE,wrapper.getU().getVal());
-               }                       
+               assertEquals(STUnderlineValues.DOUBLE,ctFont.getUArray(0).getVal());
+               }
 
        public void testColor(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
                CTColor color=ctFont.addNewColor();
-               //color.setIndexed(IndexedColors.DEFAULT_COLOR);
                color.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
-               wrapper.setColor(color);
-               
+               ctFont.setColorArray(0,color);
+
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals(Font.COLOR_NORMAL,xssfFont.getColor());
-               
-               xssfFont.setColor(Font.COLOR_RED);
-               assertEquals(IndexedColors.RED,wrapper.getColor().getIndexed());
+
+               xssfFont.setColor(IndexedColors.RED.getIndex());
+               assertEquals(IndexedColors.RED.getIndex(), ctFont.getColorArray(0).getIndexed());
+       }
+/*
+       public void testRgbColor(){
+               CTFont ctFont=CTFont.Factory.newInstance();
+               CTColor color=ctFont.addNewColor();
+               color.setRgb(new byte[]{});
+               ctFont.setColorArray(0,color);
+
+               XSSFFont xssfFont=new XSSFFont(ctFont);
+               assertEquals(,xssfFont.getRgbColor());
+
+               xssfFont.setRgbColor(new XSSFColor(new java.awt.Color(10,19,10)));
+               //assertEquals(,ctFont.getColorArray(0).getRgb());
        }
 
+       public void testThemeColor(){
+               CTFont ctFont=CTFont.Factory.newInstance();
+               CTColor color=ctFont.addNewColor();
+               color.setTheme();
+               ctFont.setColorArray(0,color);
+
+               XSSFFont xssfFont=new XSSFFont(ctFont);
+               assertEquals(,xssfFont.getThemeColor());
 
+               xssfFont.setThemeColor(Font.COLOR_RED);
+               assertEquals(,ctFont.getColorArray(0).getTheme());
+               assertEquals(,ctFont.getColorArray(0).getTint());
+       }
+*/
        public void testFamily(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
                CTIntProperty family=ctFont.addNewFamily();
                family.setVal(XSSFFont.FONT_FAMILY_MODERN);
-               wrapper.setFamily(family);
-               
+               ctFont.setFamilyArray(0,family);
+
                XSSFFont xssfFont=new XSSFFont(ctFont);
                assertEquals(XSSFFont.FONT_FAMILY_MODERN,xssfFont.getFamily());
-
        }
 
-       
+
        public void testScheme(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
                CTFontScheme scheme=ctFont.addNewScheme();
                scheme.setVal(STFontScheme.MAJOR);
-               wrapper.setFontScheme(scheme);
-               
+               ctFont.setSchemeArray(0,scheme);
+
                XSSFFont font=new XSSFFont(ctFont);
                assertEquals(XSSFFont.SCHEME_MAJOR,font.getScheme());
-               
+
                font.setScheme(XSSFFont.SCHEME_NONE);
-               assertEquals(STFontScheme.NONE,wrapper.getFontScheme().getVal());               
+               assertEquals(STFontScheme.NONE,ctFont.getSchemeArray(0).getVal());
        }
 
        public void testTypeOffset(){
                CTFont ctFont=CTFont.Factory.newInstance();
-               CTFontWrapper wrapper=new CTFontWrapper(ctFont);
                CTVerticalAlignFontProperty valign=ctFont.addNewVertAlign();
                valign.setVal(STVerticalAlignRun.BASELINE);
-               wrapper.setVertAlign(valign);
-               
+               ctFont.setVertAlignArray(0,valign);
+
                XSSFFont font=new XSSFFont(ctFont);
                assertEquals(Font.SS_NONE,font.getTypeOffset());
-               
+
                font.setTypeOffset(XSSFFont.SS_SUPER);
-               assertEquals(STVerticalAlignRun.SUPERSCRIPT,wrapper.getVertAlign().getVal());   
+               assertEquals(STVerticalAlignRun.SUPERSCRIPT,ctFont.getVertAlignArray(0).getVal());
        }
-       
+
+       /**
+        * Tests that we can define fonts to a new
+        *  file, save, load, and still see them
+        * @throws Exception
+        */
+       public void testCreateSave() throws Exception {
+               XSSFWorkbook wb = new XSSFWorkbook();
+               XSSFSheet s1 = (XSSFSheet)wb.createSheet();
+               Row r1 = s1.createRow(0);
+               Cell r1c1 = r1.createCell(0);
+               r1c1.setCellValue(2.2);
+
+               assertEquals(1, wb.getNumberOfFonts());
+
+               XSSFFont font=wb.createFont();
+               font.setBold(true);
+               font.setStrikeout(true);
+               font.setColor(IndexedColors.YELLOW.getIndex());
+               font.setFontName("Courier");
+        wb.createCellStyle().setFont(font);
+        assertEquals(2, wb.getNumberOfFonts());
+
+               CellStyle cellStyleTitle=wb.createCellStyle();
+               cellStyleTitle.setFont(font);
+               r1c1.setCellStyle(cellStyleTitle);
+
+               // Save and re-load
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               wb.write(baos);
+               ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+
+               wb = new XSSFWorkbook(Package.open(bais));
+               s1 = (XSSFSheet)wb.getSheetAt(0);
+
+               assertEquals(2, wb.getNumberOfFonts());
+               assertNotNull(s1.getRow(0).getCell(0).getCellStyle().getFont(wb));
+               assertEquals(IndexedColors.YELLOW.getIndex(), s1.getRow(0).getCell(0).getCellStyle().getFont(wb).getColor());
+               assertEquals("Courier", s1.getRow(0).getCell(0).getCellStyle().getFont(wb).getFontName());
+
+               // Now add an orphaned one
+               XSSFFont font2 = wb.createFont();
+               font2.setItalic(true);
+               font2.setFontHeightInPoints((short)15);
+               wb.createCellStyle().setFont(font2);
+        assertEquals(3, wb.getNumberOfFonts());
+
+               // Save and re-load
+               baos = new ByteArrayOutputStream();
+               wb.write(baos);
+               bais = new ByteArrayInputStream(baos.toByteArray());
+
+               wb = new XSSFWorkbook(Package.open(bais));
+               s1 = (XSSFSheet)wb.getSheetAt(0);
+
+               assertEquals(3, wb.getNumberOfFonts());
+               assertNotNull(wb.getFontAt((short)1));
+               assertNotNull(wb.getFontAt((short)2));
+
+               assertEquals(15, wb.getFontAt((short)2).getFontHeightInPoints());
+               assertEquals(true, wb.getFontAt((short)2).getItalic());
+       }
+
 
        public void testXSSFFont() throws IOException{
                XSSFWorkbook workbook=new XSSFWorkbook();
                //Font font1=workbook.createFont();
-               
+
                Sheet sheet=workbook.createSheet("sheet 1 - test font");
-               
-               
+
+
                Row row=sheet.createRow(0);
                Cell cell=row.createCell(0);
                cell.setCellValue(new XSSFRichTextString("XSSFFont test example file"));
-               Font font=new XSSFFont();
-               font.setBoldweight(Font.BOLDWEIGHT_BOLD);
+               XSSFFont font=new XSSFFont();
+               font.setBold(true);
                font.setFontHeightInPoints((short)22);
-               font.setColor((short)IndexedColors.BLUE);
+               font.setColor(IndexedColors.BLUE.getIndex());
                font.setFontName("Verdana");
                CellStyle cellStyleTitle=workbook.createCellStyle();
                cellStyleTitle.setFont(font);
                cell.setCellStyle(cellStyleTitle);
 
-               
+
                row=sheet.createRow(3);
-               Font font1=new XSSFFont();
-               font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
+               XSSFFont font1=new XSSFFont();
+               font1.setBold(true);
                font1.setItalic(true);
                font1.setFontHeightInPoints((short)18);
-               font1.setColor(Font.COLOR_RED);
+               font1.setColor(IndexedColors.RED.getIndex());
                font1.setFontName("Arial");
                CellStyle cellStyle1=workbook.createCellStyle();
                cellStyle1.setFont(font1);
@@ -279,7 +348,7 @@ public class TestXSSFFont extends TestCase{
                font3.setFontHeightInPoints((short)9);
                font3.setFontName("Times");
                font3.setStrikeout(true);
-               font3.setColor((short)IndexedColors.PINK);
+               font3.setColor(IndexedColors.PINK.getIndex());
                CellStyle cellStyle3=workbook.createCellStyle();
                cellStyle3.setFont(font3);
 
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java
new file mode 100755 (executable)
index 0000000..a716d81
--- /dev/null
@@ -0,0 +1,135 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.xssf.usermodel;\r
+\r
+import java.io.*;\r
+import java.util.ArrayList;\r
+import java.util.Calendar;\r
+import java.util.Date;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import org.apache.poi.ss.usermodel.Cell;\r
+import org.apache.poi.ss.usermodel.CellStyle;\r
+import org.apache.poi.ss.usermodel.Comment;\r
+import org.apache.poi.ss.usermodel.CreationHelper;\r
+import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.ss.usermodel.SharedStringSource;\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.xssf.model.CommentsTable;\r
+import org.apache.xmlbeans.XmlOptions;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;\r
+\r
+/**\r
+ * Tests functionality of the XSSFRichTextRun object\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestXSSFRichTextString extends TestCase {\r
+\r
+    public void testCreate() throws Exception {\r
+\r
+        XSSFRichTextString rt = new XSSFRichTextString("Apache POI");\r
+        assertEquals("Apache POI", rt.getString());\r
+\r
+        CTRst st = rt.getCTRst();\r
+        assertTrue(st.isSetT());\r
+        assertEquals("Apache POI", st.getT());\r
+\r
+        rt.append(" is cool stuff");\r
+        assertEquals(2, st.sizeOfRArray());\r
+        assertFalse(st.isSetT());\r
+\r
+        assertEquals("Apache POI is cool stuff", rt.getString());\r
+    }\r
+\r
+\r
+    public void testApplyFont() throws Exception {\r
+\r
+        XSSFRichTextString rt = new XSSFRichTextString();\r
+        rt.append("123");\r
+        rt.append("4567");\r
+        rt.append("89");\r
+\r
+        XSSFFont font1 = new XSSFFont();\r
+        font1.setBold(true);\r
+\r
+        rt.applyFont(2, 5, font1);\r
+\r
+        assertEquals(4, rt.numFormattingRuns());\r
+        assertEquals(0, rt.getIndexOfFormattingRun(0));\r
+        assertEquals(2, rt.getLengthOfFormattingRun(0));\r
+\r
+        assertEquals(2, rt.getIndexOfFormattingRun(1));\r
+        assertEquals(3, rt.getLengthOfFormattingRun(1));\r
+\r
+        assertEquals(5, rt.getIndexOfFormattingRun(2));\r
+        assertEquals(2, rt.getLengthOfFormattingRun(2));\r
+\r
+        assertEquals(7, rt.getIndexOfFormattingRun(3));\r
+        assertEquals(2, rt.getLengthOfFormattingRun(3));\r
+    }\r
+\r
+    public void testClearFormatting() throws Exception {\r
+\r
+        XSSFRichTextString rt = new XSSFRichTextString("Apache POI");\r
+        assertEquals("Apache POI", rt.getString());\r
+\r
+        rt.clearFormatting();\r
+\r
+        CTRst st = rt.getCTRst();\r
+        assertTrue(st.isSetT());\r
+        assertEquals("Apache POI", rt.getString());\r
+        assertEquals(0, rt.numFormattingRuns());\r
+\r
+        XSSFFont font = new XSSFFont();\r
+        font.setBold(true);\r
+\r
+        rt.applyFont(7, 10, font);\r
+        assertEquals(2, rt.numFormattingRuns());\r
+        rt.clearFormatting();\r
+        assertEquals("Apache POI", rt.getString());\r
+        assertEquals(0, rt.numFormattingRuns());\r
+\r
+    }\r
+\r
+    public void testGetFonts() throws Exception {\r
+\r
+        XSSFRichTextString rt = new XSSFRichTextString();\r
+\r
+        XSSFFont font1 = new XSSFFont();\r
+        font1.setFontName("Arial");\r
+        font1.setItalic(true);\r
+        rt.append("The quick", font1);\r
+\r
+        XSSFFont font1$ = rt.getFontOfFormattingRun(0);\r
+        assertEquals(font1.getItalic(), font1$.getItalic());\r
+        assertEquals(font1.getFontName(), font1$.getFontName());\r
+\r
+        XSSFFont font2 = new XSSFFont();\r
+        font2.setFontName("Courier");\r
+        font2.setBold(true);\r
+        rt.append(" brown fox", font2);\r
+\r
+        XSSFFont font2$ = rt.getFontOfFormattingRun(1);\r
+        assertEquals(font2.getBold(), font2$.getBold());\r
+        assertEquals(font2.getFontName(), font2$.getFontName());\r
+\r
+    }\r
+}\r
index a682bab1c64e44bd032aa82f22a17e37ba4897c6..2fbec02863ba72e0cf51e8ab4aa7f88abee7845a 100644 (file)
@@ -24,6 +24,7 @@ import junit.framework.TestCase;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.xssf.usermodel.TestXSSFCell.DummySharedStringSource;
+import org.apache.poi.xssf.model.SharedStringsTable;
 
 /**
  * Tests for XSSFRow
@@ -167,7 +168,7 @@ public final class TestXSSFRow extends TestCase {
         row.setZeroHeight(true);
         assertTrue(row.getZeroHeight());
     }
-        
+
     /**
      * Tests for the missing/blank cell policy stuff
      */
@@ -184,7 +185,7 @@ public final class TestXSSFRow extends TestCase {
         row.createCell((short)1).setCellValue(3.2);
         row.createCell((short)4, Cell.CELL_TYPE_BLANK);
         row.createCell((short)5).setCellValue(4);
-        
+
         // First up, no policy
         assertEquals(Cell.CELL_TYPE_STRING,  row.getCell(0).getCellType());
         assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
@@ -192,7 +193,7 @@ public final class TestXSSFRow extends TestCase {
         assertEquals(null, row.getCell(3));
         assertEquals(Cell.CELL_TYPE_BLANK,   row.getCell(4).getCellType());
         assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5).getCellType());
-        
+
         // RETURN_NULL_AND_BLANK - same as default
         assertEquals(Cell.CELL_TYPE_STRING,  row.getCell(0, Row.RETURN_NULL_AND_BLANK).getCellType());
         assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, Row.RETURN_NULL_AND_BLANK).getCellType());
@@ -200,7 +201,7 @@ public final class TestXSSFRow extends TestCase {
         assertEquals(null, row.getCell(3, Row.RETURN_NULL_AND_BLANK));
         assertEquals(Cell.CELL_TYPE_BLANK,   row.getCell(4, Row.RETURN_NULL_AND_BLANK).getCellType());
         assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, Row.RETURN_NULL_AND_BLANK).getCellType());
-        
+
         // RETURN_BLANK_AS_NULL - nearly the same
         assertEquals(Cell.CELL_TYPE_STRING,  row.getCell(0, XSSFRow.RETURN_BLANK_AS_NULL).getCellType());
         assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, XSSFRow.RETURN_BLANK_AS_NULL).getCellType());
@@ -208,7 +209,7 @@ public final class TestXSSFRow extends TestCase {
         assertEquals(null, row.getCell(3, XSSFRow.RETURN_BLANK_AS_NULL));
         assertEquals(null, row.getCell(4, XSSFRow.RETURN_BLANK_AS_NULL));
         assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, XSSFRow.RETURN_BLANK_AS_NULL).getCellType());
-        
+
         // CREATE_NULL_AS_BLANK - creates as needed
         assertEquals(Cell.CELL_TYPE_STRING,  row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
         assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
@@ -216,7 +217,7 @@ public final class TestXSSFRow extends TestCase {
         assertEquals(Cell.CELL_TYPE_BLANK,   row.getCell(3, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
         assertEquals(Cell.CELL_TYPE_BLANK,   row.getCell(4, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
         assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
-        
+
         // Check created ones get the right column
         assertEquals((short)0, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
         assertEquals((short)1, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
@@ -244,7 +245,7 @@ public final class TestXSSFRow extends TestCase {
 
     private static XSSFSheet createParentObjects() {
         XSSFWorkbook wb = new XSSFWorkbook();
-        wb.setSharedStringSource(new DummySharedStringSource());
+        wb.setSharedStringSource(new SharedStringsTable());
         return new XSSFSheet(wb);
     }
 }
index d5c892c584170ce569abd3dc0fea37eebb64021b..e06340db05aee12703cafa3273561e255d8582ae 100644 (file)
@@ -639,12 +639,10 @@ public class TestXSSFSheet extends TestCase {
     }
     
     public void testGetActiveCell() {
-       Workbook workbook = new XSSFWorkbook();
-       CTSheet ctSheet = CTSheet.Factory.newInstance();
-       CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
-       XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
-       ctWorksheet.addNewSheetViews().addNewSheetView().addNewSelection().setActiveCell("R5");
-       
+       XSSFWorkbook workbook = new XSSFWorkbook();
+       XSSFSheet sheet = workbook.createSheet();
+       sheet.setActiveCell("R5");
+
        assertEquals("R5", sheet.getActiveCell());
        
     }
@@ -733,7 +731,6 @@ public class TestXSSFSheet extends TestCase {
        assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle());
        XSSFRow row = (XSSFRow) sheet.createRow(0);
        XSSFCell cell = (XSSFCell) sheet.getRow(0).createCell(3);
-       //System.out.println(cell.getCellStyle());
        
     }
     
@@ -772,7 +769,6 @@ public class TestXSSFSheet extends TestCase {
                XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
 
                //one level
-               System.out.println("livello 1");
                sheet.groupColumn((short)2,(short)7);
                sheet.groupColumn((short)10,(short)11);
                CTCols cols=sheet.getWorksheet().getColsArray(0);
@@ -784,7 +780,6 @@ public class TestXSSFSheet extends TestCase {
                assertEquals(1, colArray[0].getOutlineLevel());
 
                //two level  
-               System.out.println("\n livello 2");
                sheet.groupColumn((short)1,(short)2);
                cols=sheet.getWorksheet().getColsArray(0);
                assertEquals(4,cols.sizeOfColArray());
@@ -792,7 +787,6 @@ public class TestXSSFSheet extends TestCase {
                assertEquals(2, colArray[1].getOutlineLevel());
 
                //three level
-               System.out.println("\n livello 3");
                sheet.groupColumn((short)6,(short)8);
                sheet.groupColumn((short)2,(short)3);
                cols=sheet.getWorksheet().getColsArray(0);
@@ -855,5 +849,13 @@ public class TestXSSFSheet extends TestCase {
 
                assertEquals(1,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow());
            }
+            
+            public void testSetZoom() {
+                Workbook workBook = new XSSFWorkbook();
+                XSSFSheet sheet1 = (XSSFSheet) workBook.createSheet("new sheet");
+                sheet1.setZoom(3,4);   // 75 percent magnification
+                long zoom = sheet1.getSheetTypeSheetView().getZoomScale();
+                assertEquals(zoom, 75);
+            }
 
 }
index 87cd37035e5749ff0922e09b7134900950f8220d..65a934304bc6529935f9c3ff243815915d22a4c1 100644 (file)
@@ -247,7 +247,7 @@ public class TestXSSFWorkbook extends TestCase {
        
        //get default font, then change 2 values and check against different values (height changes)
        Font font=workbook.createFont();
-       font.setBoldweight(Font.BOLDWEIGHT_BOLD);
+       ((XSSFFont)font).setBold(true);
        font.setUnderline(Font.U_DOUBLE);
        StylesSource styleSource=new StylesTable();
        long index=styleSource.putFont(font);
@@ -291,6 +291,25 @@ public class TestXSSFWorkbook extends TestCase {
         assertNotNull(fontAt);
     }
     
+    public void testGetNumberOfFonts(){
+       XSSFWorkbook wb = new XSSFWorkbook();
+
+        XSSFFont f1=wb.createFont();
+       f1.setBold(true);
+       wb.createCellStyle().setFont(f1);
+
+        XSSFFont f2=wb.createFont();
+       f2.setUnderline(Font.U_DOUBLE);
+        wb.createCellStyle().setFont(f2);
+
+        XSSFFont f3=wb.createFont();
+       f3.setFontHeightInPoints((short)23);
+        wb.createCellStyle().setFont(f3);
+
+        assertEquals(4,wb.getNumberOfFonts());
+       assertEquals(Font.U_DOUBLE,wb.getFontAt((short)2).getUnderline());
+    }
+    
     public void testGetNumCellStyles(){
        XSSFWorkbook workbook = new XSSFWorkbook();
         short i = workbook.getNumCellStyles();