poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gwool...@apache.org
Subject svn commit: r1799733 - in /poi/trunk: src/ooxml/java/org/apache/poi/xssf/usermodel/ src/ooxml/testcases/org/apache/poi/xssf/model/ test-data/spreadsheet/
Date Sat, 24 Jun 2017 05:55:45 GMT
Author: gwoolsey
Date: Sat Jun 24 05:55:45 2017
New Revision: 1799733

URL: http://svn.apache.org/viewvc?rev=1799733&view=rev
Log:
Add file that failed regression tests as a unit test.

Adjust table style processing to allow for AlternateContent wrappers for table element style
definitions.  

Added:
    poi/trunk/test-data/spreadsheet/style-alternate-content.xlsx   (with props)
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFBuiltinTableStyle.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFBuiltinTableStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFBuiltinTableStyle.java?rev=1799733&r1=1799732&r2=1799733&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFBuiltinTableStyle.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFBuiltinTableStyle.java Sat
Jun 24 05:55:45 2017
@@ -388,20 +388,22 @@ public enum XSSFBuiltinTableStyle {
             final InputStream is = XSSFBuiltinTableStyle.class.getResourceAsStream("presetTableStyles.xml");
             try {
                 final Document doc = DocumentHelper.readDocument(is);
+                
                 final NodeList styleNodes = doc.getDocumentElement().getChildNodes();
                 for (int i=0; i < styleNodes.getLength(); i++) {
                     final Node node = styleNodes.item(i);
                     if (node.getNodeType() != Node.ELEMENT_NODE) continue; // only care about
elements
                     final Element tag = (Element) node;
                     String styleName = tag.getTagName();
+                    XSSFBuiltinTableStyle builtIn = XSSFBuiltinTableStyle.valueOf(styleName);
+                    
                     Node dxfsNode = tag.getElementsByTagName("dxfs").item(0);
                     Node tableStyleNode = tag.getElementsByTagName("tableStyles").item(0);
-
+                    
                     // hack because I can't figure out how to get XMLBeans to parse a sub-element
in a standalone manner
                     // - build a fake styles.xml file with just this built-in
                     StylesTable styles = new StylesTable();
                     styles.readFrom(new ByteArrayInputStream(styleXML(dxfsNode, tableStyleNode).getBytes(Charset.forName("UTF-8"))));
-                    XSSFBuiltinTableStyle builtIn = XSSFBuiltinTableStyle.valueOf(styleName);
                     styleMap.put(builtIn, new XSSFBuiltinTypeStyleStyle(builtIn, styles.getExplicitTableStyle(styleName)));
                 }
             } finally {

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java?rev=1799733&r1=1799732&r2=1799733&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTableStyle.java Sat Jun 24
05:55:45 2017
@@ -17,12 +17,18 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import java.util.ArrayList;
 import java.util.EnumMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.poi.ss.usermodel.DifferentialStyleProvider;
 import org.apache.poi.ss.usermodel.TableStyle;
 import org.apache.poi.ss.usermodel.TableStyleType;
+import org.apache.xmlbeans.XmlCursor;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxfs;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyle;
@@ -48,17 +54,41 @@ public class XSSFTableStyle implements T
     public XSSFTableStyle(int index, CTDxfs dxfs, CTTableStyle tableStyle, IndexedColorMap
colorMap) {
         this.name = tableStyle.getName();
         this.index = index;
+        
+        List<CTDxf> dxfList = new ArrayList<CTDxf>();
+
+        // CT* classes don't handle "mc:AlternateContent" elements, so get the Dxf instances
manually
+        XmlCursor cur = dxfs.newCursor();
+        // sometimes there are namespaces sometimes not.
+        String xquery = "declare namespace x='"+XSSFRelation.NS_SPREADSHEETML+"' .//x:dxf
| .//dxf";
+        cur.selectPath(xquery);
+        while (cur.toNextSelection()) {
+            XmlObject obj = cur.getObject();
+            String parentName = obj.getDomNode().getParentNode().getNodeName();
+            // ignore alternate content choices, we won't know anything about their namespaces
+            if (parentName.equals("mc:Fallback") || parentName.equals("x:dxfs") || parentName.contentEquals("dxfs"))
{
+                CTDxf dxf;
+                try {
+                    if (obj instanceof CTDxf) {
+                        dxf = (CTDxf) obj;
+                    } else {
+                        dxf = CTDxf.Factory.parse(obj.newXMLStreamReader(), new XmlOptions().setDocumentType(CTDxf.type));
+                    }
+                    if (dxf != null) dxfList.add(dxf);
+                } catch (XmlException e) {
+                    // ignore
+                    e.printStackTrace();
+                }
+            }
+        }
+
         for (CTTableStyleElement element : tableStyle.getTableStyleElementList()) {
             TableStyleType type = TableStyleType.valueOf(element.getType().toString());
             DifferentialStyleProvider dstyle = null;
             if (element.isSetDxfId()) {
                 int idx = (int) element.getDxfId();
                 CTDxf dxf;
-                if (idx >= 0 && idx < dxfs.getCount()) {
-                    dxf = dxfs.getDxfArray(idx);
-                } else {
-                    dxf = null;
-                }
+                dxf = dxfList.get(idx);
                 int stripeSize = 0;
                 if (element.isSetSize()) stripeSize = (int) element.getSize();
                 if (dxf != null) dstyle = new XSSFDxfStyleProvider(dxf, stripeSize, colorMap);

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java?rev=1799733&r1=1799732&r2=1799733&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/model/TestStylesTable.java Sat Jun 24
05:55:45 2017
@@ -356,4 +356,14 @@ public final class TestStylesTable {
             wb.close();
         }
     }
+
+    @Test
+    public void testLoadWithAlternateContent() {
+        XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("style-alternate-content.xlsx");
+        assertNotNull(workbook.getStylesSource());
+
+        StylesTable st = workbook.getStylesSource();
+
+        assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
+    }
 }

Added: poi/trunk/test-data/spreadsheet/style-alternate-content.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/style-alternate-content.xlsx?rev=1799733&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/style-alternate-content.xlsx
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message