]> source.dussan.org Git - poi.git/commitdiff
Allow creating of an empty Themes Table on request
authorNick Burch <nick@apache.org>
Wed, 22 Jul 2015 00:04:59 +0000 (00:04 +0000)
committerNick Burch <nick@apache.org>
Wed, 22 Jul 2015 00:04:59 +0000 (00:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1692211 13f79535-47bb-0310-9956-ffa450edef68

.settings/org.eclipse.jdt.ui.prefs
src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java
src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestThemesTable.java

index 4ad8c933ca83d1653c15d76c4424831295fb5ff3..5227bd2854048bf2a3d7f690c4770f8ca81900b5 100644 (file)
@@ -67,7 +67,7 @@ org.eclipse.jdt.ui.ignorelowercasenames=true
 org.eclipse.jdt.ui.importorder=java;javax;org;com;
 org.eclipse.jdt.ui.javadoc=false
 org.eclipse.jdt.ui.keywordthis=false
-org.eclipse.jdt.ui.ondemandthreshold=10
+org.eclipse.jdt.ui.ondemandthreshold=20
 org.eclipse.jdt.ui.overrideannotation=false
 org.eclipse.jdt.ui.staticondemandthreshold=10
 org.eclipse.jdt.ui.text.custom_code_templates=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?><templates><template autoinsert\="true" context\="gettercomment_context" deleted\="false" description\="Comment for getter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.gettercomment" name\="gettercomment">/**\n * @return the ${bare_field_name}\n */</template><template autoinsert\="true" context\="settercomment_context" deleted\="false" description\="Comment for setter method" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.settercomment" name\="settercomment">/**\n * @param ${param} the ${bare_field_name} to set\n */</template><template autoinsert\="false" context\="constructorcomment_context" deleted\="false" description\="Comment for created constructors" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorcomment" name\="constructorcomment">/**\n *\n * ${tags}\n */</template><template autoinsert\="false" context\="filecomment_context" deleted\="false" description\="Comment for created Java files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.filecomment" name\="filecomment">/* \=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\n   Licensed to the Apache Software Foundation (ASF) under one or more\n   contributor license agreements.  See the NOTICE file distributed with\n   this work for additional information regarding copyright ownership.\n   The ASF licenses this file to You under the Apache License, Version 2.0\n   (the "License"); you may not use this file except in compliance with\n   the License.  You may obtain a copy of the License at\n\n       http\://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an "AS IS" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\=\= */</template><template autoinsert\="false" context\="typecomment_context" deleted\="false" description\="Comment for created types" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.typecomment" name\="typecomment">/**\n *\n * ${tags}\n */</template><template autoinsert\="true" context\="fieldcomment_context" deleted\="false" description\="Comment for fields" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name\="fieldcomment">/**\n * \n */</template><template autoinsert\="false" context\="methodcomment_context" deleted\="false" description\="Comment for non-overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodcomment" name\="methodcomment">/**\n *\n * ${tags}\n */</template><template autoinsert\="true" context\="overridecomment_context" deleted\="false" description\="Comment for overriding methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.overridecomment" name\="overridecomment">/* (non-Javadoc)\n * ${see_to_overridden}\n */</template><template autoinsert\="true" context\="delegatecomment_context" deleted\="false" description\="Comment for delegate methods" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.delegatecomment" name\="delegatecomment">/**\n * ${tags}\n * ${see_to_target}\n */</template><template autoinsert\="true" context\="newtype_context" deleted\="false" description\="Newly created files" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.newtype" name\="newtype">${filecomment}\n${package_declaration}\n\n${typecomment}\n${type_declaration}</template><template autoinsert\="true" context\="classbody_context" deleted\="false" description\="Code in new class type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.classbody" name\="classbody">\n</template><template autoinsert\="true" context\="interfacebody_context" deleted\="false" description\="Code in new interface type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.interfacebody" name\="interfacebody">\n</template><template autoinsert\="true" context\="enumbody_context" deleted\="false" description\="Code in new enum type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.enumbody" name\="enumbody">\n</template><template autoinsert\="true" context\="annotationbody_context" deleted\="false" description\="Code in new annotation type bodies" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.annotationbody" name\="annotationbody">\n</template><template autoinsert\="true" context\="catchblock_context" deleted\="false" description\="Code in new catch blocks" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.catchblock" name\="catchblock">// ${todo} Auto-generated catch block\n${exception_var}.printStackTrace();</template><template autoinsert\="true" context\="methodbody_context" deleted\="false" description\="Code in created method stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.methodbody" name\="methodbody">// ${todo} Auto-generated method stub\n${body_statement}</template><template autoinsert\="true" context\="constructorbody_context" deleted\="false" description\="Code in created constructor stubs" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.constructorbody" name\="constructorbody">${body_statement}\n// ${todo} Auto-generated constructor stub</template><template autoinsert\="true" context\="getterbody_context" deleted\="false" description\="Code in created getters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.getterbody" name\="getterbody">return ${field};</template><template autoinsert\="true" context\="setterbody_context" deleted\="false" description\="Code in created setters" enabled\="true" id\="org.eclipse.jdt.ui.text.codetemplates.setterbody" name\="setterbody">${field} \= ${param};</template></templates>
index 3937e3eeabdec449350b7a817d2854defd05a885..c89150927449af8aa196d19e3b2c392ddcad6ec6 100644 (file)
@@ -35,7 +35,10 @@ import org.apache.poi.ss.usermodel.BuiltinFormats;
 import org.apache.poi.ss.usermodel.FontFamily;
 import org.apache.poi.ss.usermodel.FontScheme;
 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
+import org.apache.poi.xssf.usermodel.XSSFFactory;
 import org.apache.poi.xssf.usermodel.XSSFFont;
+import org.apache.poi.xssf.usermodel.XSSFRelation;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
 import org.apache.xmlbeans.XmlException;
@@ -57,7 +60,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
 
-
 /**
  * Table of styles shared across all sheets in a workbook.
  */
@@ -79,6 +81,7 @@ public class StylesTable extends POIXMLDocumentPart {
     private static final int MAXIMUM_STYLE_ID = SpreadsheetVersion.EXCEL2007.getMaxCellStyles();
 
     private StyleSheetDocument doc;
+    private XSSFWorkbook workbook;
     private ThemesTable theme;
 
     /**
@@ -96,7 +99,17 @@ public class StylesTable extends POIXMLDocumentPart {
         super(part, rel);
         readFrom(part.getInputStream());
     }
+    
+    public void setWorkbook(XSSFWorkbook wb) {
+        this.workbook = wb;
+    }
 
+    /**
+     * Get the current Workbook's theme table, or null if the
+     *  Workbook lacks any themes.
+     * <p>Use {@link #ensureThemesTable()} to have a themes table
+     *  created if needed
+     */
     public ThemesTable getTheme() {
         return theme;
     }
@@ -113,6 +126,17 @@ public class StylesTable extends POIXMLDocumentPart {
             border.setThemesTable(theme);
         }
     }
+    
+    /**
+     * If there isn't currently a {@link ThemesTable} for the
+     *  current Workbook, then creates one and sets it up.
+     * After this, calls to {@link #getTheme()} won't give null
+     */
+    public void ensureThemesTable() {
+        if (theme != null) return;
+
+        theme = (ThemesTable)workbook.createRelationship(XSSFRelation.THEME, XSSFFactory.getInstance());
+    }
 
     /**
      * Read this shared styles table from an XML file.
index bf58989ccbdaeb2539698b8a7de998894064697b..c710e3576f4bc0950dc6f2cec49f80dedbefa2ef 100644 (file)
 package org.apache.poi.xssf.model;
 
 import java.io.IOException;
+import java.io.OutputStream;
 
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.xssf.usermodel.XSSFColor;
 import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlOptions;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
 import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
@@ -34,6 +36,15 @@ import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
 public class ThemesTable extends POIXMLDocumentPart {
     private ThemeDocument theme;
 
+    /**
+     * Create a new, empty ThemesTable
+     */
+    public ThemesTable() {
+        super();
+        theme = ThemeDocument.Factory.newInstance();
+        theme.addNewTheme().addNewThemeElements();
+    }
+    
     /**
      * Construct a ThemesTable.
      * @param part A PackagePart.
@@ -120,4 +131,24 @@ public class ThemesTable extends POIXMLDocumentPart {
 
        // All done
     }
+    
+    /**
+     * Write this table out as XML.
+     * 
+     * @param out The stream to write to.
+     * @throws IOException if an error occurs while writing.
+     */
+    public void writeTo(OutputStream out) throws IOException {
+        XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
+
+        theme.save(out, options);
+    }
+
+    @Override
+    protected void commit() throws IOException {
+        PackagePart part = getPackagePart();
+        OutputStream out = part.getOutputStream();
+        writeTo(out);
+        out.close();
+    }
 }
index f90b48dfa17369bbbb2d332f68068e47cec7769a..0d62135d6e3f8023393fbe7ef1c01faa52caad18 100644 (file)
@@ -153,8 +153,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
      */
     private StylesTable stylesSource;
 
-    private ThemesTable theme;
-
     /**
      * The locator of user-defined functions.
      * By default includes functions from the Excel Analysis Toolpack
@@ -337,6 +335,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
             WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream());
             this.workbook = doc.getWorkbook();
 
+            ThemesTable theme = null;
             Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
             Map<String, ExternalLinksTable> elIdMap = new HashMap<String, ExternalLinksTable>();
             for(POIXMLDocumentPart p : getRelations()){
@@ -362,6 +361,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
                     stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
                 }
             }
+            stylesSource.setWorkbook(this);
             stylesSource.setTheme(theme);
 
             if (sharedStringSource == null) {
@@ -428,6 +428,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
 
         sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
         stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
+        stylesSource.setWorkbook(this);
 
         namedRanges = new ArrayList<XSSFName>();
         sheets = new ArrayList<XSSFSheet>();
@@ -1578,7 +1579,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
      * Returns the Theme of current workbook.
      */
     public ThemesTable getTheme() {
-        return theme;
+        if (stylesSource == null) return null;
+        return stylesSource.getTheme();
     }
 
     /**
index 40ab5b46e5bacf37c7a3dedca1919255763df2b5..6be48768edd8090c534d65837b19f82df7337287 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.xssf.model;\r
 \r
 import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
 \r
 import java.io.FileOutputStream;\r
 \r
@@ -29,6 +30,7 @@ import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 import org.apache.poi.xssf.usermodel.XSSFColor;\r
 import org.apache.poi.xssf.usermodel.XSSFFont;\r
 import org.apache.poi.xssf.usermodel.XSSFRow;\r
+import org.apache.poi.xssf.usermodel.XSSFSheet;\r
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
 import org.junit.Test;\r
 \r
@@ -75,4 +77,24 @@ public class TestThemesTable {
             fos.close();\r
         }\r
     }\r
+    \r
+    @Test\r
+    public void testAddNew() throws Exception {\r
+        XSSFWorkbook wb = new XSSFWorkbook();\r
+        XSSFSheet s = wb.createSheet();\r
+        assertEquals(null, wb.getTheme());\r
+        \r
+        StylesTable styles = wb.getStylesSource();\r
+        assertEquals(null, styles.getTheme());\r
+        \r
+        styles.ensureThemesTable();\r
+        \r
+        assertNotNull(styles.getTheme());\r
+        assertNotNull(wb.getTheme());\r
+        \r
+        wb = XSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        styles = wb.getStylesSource();\r
+        assertNotNull(styles.getTheme());\r
+        assertNotNull(wb.getTheme());\r
+    }\r
 }
\ No newline at end of file