]> source.dussan.org Git - poi.git/commitdiff
Rename/Move xssf.model.Table to xssf.usermodel.XSSFTable as it now has usermodel...
authorNick Burch <nick@apache.org>
Thu, 14 Apr 2011 14:32:49 +0000 (14:32 +0000)
committerNick Burch <nick@apache.org>
Thu, 14 Apr 2011 14:32:49 +0000 (14:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1092286 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java
src/ooxml/java/org/apache/poi/xssf/extractor/XSSFImportFromXML.java
src/ooxml/java/org/apache/poi/xssf/model/Table.java [deleted file]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFMap.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFXmlColumnPr.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java

index b01bf5e9832faacd1bff0d9931a15918ef22b9a6..1c398d0b9606f323f49a4fc5e5a520e09fdfa4be 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta3" date="2011-??-??">
+           <action dev="poi-developers" type="fix">Rename/Move xssf.model.Table to xssf.usermodel.XSSFTable as it now has usermodel-like features</action>
            <action dev="poi-developers" type="fix">51061 - Correct target URI for new XSSF Tables</action>
            <action dev="poi-developers" type="add">Initial support for XSSF Charts. Provides easy access to the underlying CTChart object via the Sheet Drawing, but no high level interface onto the chart contents as yet.</action>
            <action dev="poi-developers" type="fix">50884 - XSSF and HSSF freeze panes now behave the same</action>
index b5b4a6ba6eb022e688bbfb0e4e3ca75493162d18..2e6b90242a21d10c96fae896e7c9886a91e297ee 100644 (file)
@@ -41,7 +41,7 @@ import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
 
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
-import org.apache.poi.xssf.model.Table;
+import org.apache.poi.xssf.usermodel.XSSFTable;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFMap;
 import org.apache.poi.xssf.usermodel.XSSFRow;
@@ -123,7 +123,7 @@ public class XSSFExportToXml implements Comparator<String>{
      */
     public void exportToXML(OutputStream os, String encoding, boolean validate) throws SAXException, ParserConfigurationException, TransformerException{
         List<XSSFSingleXmlCell> singleXMLCells = map.getRelatedSingleXMLCell();
-        List<Table> tables = map.getRelatedTables();
+        List<XSSFTable> tables = map.getRelatedTables();
 
         String rootElement = map.getCtMap().getRootElement();
 
@@ -141,13 +141,13 @@ public class XSSFExportToXml implements Comparator<String>{
 
         List<String> xpaths = new Vector<String>();
         Map<String,XSSFSingleXmlCell> singleXmlCellsMappings = new HashMap<String,XSSFSingleXmlCell>();
-        Map<String,Table> tableMappings = new HashMap<String,Table>();
+        Map<String,XSSFTable> tableMappings = new HashMap<String,XSSFTable>();
 
         for(XSSFSingleXmlCell simpleXmlCell : singleXMLCells) {
             xpaths.add(simpleXmlCell.getXpath());
             singleXmlCellsMappings.put(simpleXmlCell.getXpath(), simpleXmlCell);
         }
-        for(Table table : tables) {
+        for(XSSFTable table : tables) {
             String commonXPath = table.getCommonXpath();
             xpaths.add(commonXPath);
             tableMappings.put(commonXPath, table);
@@ -159,7 +159,7 @@ public class XSSFExportToXml implements Comparator<String>{
         for(String xpath : xpaths) {
 
             XSSFSingleXmlCell simpleXmlCell = singleXmlCellsMappings.get(xpath);
-            Table table = tableMappings.get(xpath);
+            XSSFTable table = tableMappings.get(xpath);
 
             if (!xpath.matches(".*\\[.*")) {
 
index 854dc305bbde8beacec7a72c372a237f00bad040..e87a0333b372b2438a55f6d12aecd227e43afe99 100644 (file)
@@ -33,7 +33,7 @@ import javax.xml.xpath.XPathFactory;
 
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
-import org.apache.poi.xssf.model.Table;
+import org.apache.poi.xssf.usermodel.XSSFTable;
 import org.apache.poi.xssf.usermodel.XSSFCell;
 import org.apache.poi.xssf.usermodel.XSSFMap;
 import org.apache.poi.xssf.usermodel.XSSFRow;
@@ -85,7 +85,7 @@ public class XSSFImportFromXML {
 
         List<XSSFSingleXmlCell> singleXmlCells = _map.getRelatedSingleXMLCell();
 
-        List<Table> tables = _map.getRelatedTables();
+        List<XSSFTable> tables = _map.getRelatedTables();
 
         XPathFactory xpathFactory = XPathFactory.newInstance();
         XPath xpath = xpathFactory.newXPath();
@@ -107,7 +107,7 @@ public class XSSFImportFromXML {
             cell.setCellValue(textContent);
         }
 
-        for (Table table : tables) {
+        for (XSSFTable table : tables) {
 
             String commonXPath = table.getCommonXpath();
             NodeList result = (NodeList) xpath.evaluate(commonXPath, doc, XPathConstants.NODESET);
diff --git a/src/ooxml/java/org/apache/poi/xssf/model/Table.java b/src/ooxml/java/org/apache/poi/xssf/model/Table.java
deleted file mode 100644 (file)
index ee88721..0000000
+++ /dev/null
@@ -1,279 +0,0 @@
-/* ====================================================================
-   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.model;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Vector;
-
-import org.apache.poi.POIXMLDocumentPart;
-import org.apache.poi.openxml4j.opc.PackagePart;
-import org.apache.poi.openxml4j.opc.PackageRelationship;
-import org.apache.poi.ss.util.CellReference;
-import org.apache.poi.xssf.usermodel.XSSFSheet;
-import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
-import org.apache.xmlbeans.XmlException;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.TableDocument;
-
-/**
- * 
- * This class implements the Table Part (Open Office XML Part 4:
- * chapter 3.5.1)
- * 
- * This implementation works under the assumption that a table contains mappings to a subtree of an XML.
- * The root element of this subtree an occur multiple times (one for each row of the table). The child nodes
- * of the root element can be only attributes or element with maxOccurs=1 property set
- * 
- *
- * @author Roberto Manicardi
- */
-public class Table extends POIXMLDocumentPart {
-       
-       private CTTable ctTable;
-       private List<XSSFXmlColumnPr> xmlColumnPr;
-       private CellReference startCellReference;
-       private CellReference endCellReference; 
-       private String commonXPath; 
-       
-       
-       public Table() {
-               super();
-               ctTable = CTTable.Factory.newInstance();
-
-       }
-
-       public Table(PackagePart part, PackageRelationship rel)
-                       throws IOException {
-               super(part, rel);
-               readFrom(part.getInputStream());
-       }
-
-       public void readFrom(InputStream is) throws IOException {
-               try {
-                       TableDocument doc = TableDocument.Factory.parse(is);
-                       ctTable = doc.getTable();
-               } catch (XmlException e) {
-                       throw new IOException(e.getLocalizedMessage());
-               }
-       }
-       
-       public XSSFSheet getXSSFSheet(){
-               return (XSSFSheet) getParent();
-       }
-
-       public void writeTo(OutputStream out) throws IOException {
-               TableDocument doc = TableDocument.Factory.newInstance();
-               doc.setTable(ctTable);
-               doc.save(out, DEFAULT_XML_OPTIONS);
-       }
-
-       @Override
-       protected void commit() throws IOException {
-               PackagePart part = getPackagePart();
-               OutputStream out = part.getOutputStream();
-               writeTo(out);
-               out.close();
-       }
-       
-       public CTTable getCTTable(){
-               return ctTable;
-       }
-       
-       /**
-        * Checks if this Table element contains even a single mapping to the map identified by id
-        * @param id the XSSFMap ID
-        * @return true if the Table element contain mappings
-        */
-       public boolean mapsTo(long id){
-               boolean maps =false;
-               
-               List<XSSFXmlColumnPr> pointers = getXmlColumnPrs();
-               
-               for(XSSFXmlColumnPr pointer: pointers){
-                       if(pointer.getMapId()==id){
-                               maps=true;
-                               break;
-                       }
-               }
-               
-               return maps;
-       }
-
-       
-       /**
-        * 
-        * Calculates the xpath of the root element for the table. This will be the common part
-        * of all the mapping's xpaths
-        * 
-        * @return the xpath of the table's root element
-        */
-       public String getCommonXpath() {
-               
-               if(commonXPath == null){
-               
-               String[] commonTokens ={};
-               
-               for(CTTableColumn column :ctTable.getTableColumns().getTableColumnList()){
-                       if(column.getXmlColumnPr()!=null){
-                               String xpath = column.getXmlColumnPr().getXpath();
-                               String[] tokens =  xpath.split("/");
-                               if(commonTokens.length==0){
-                                       commonTokens = tokens;
-                                       
-                               }else{
-                                       int maxLenght = commonTokens.length>tokens.length? tokens.length:commonTokens.length;
-                                       for(int i =0; i<maxLenght;i++){
-                                               if(!commonTokens[i].equals(tokens[i])){
-                                                List<String> subCommonTokens = Arrays.asList(commonTokens).subList(0, i);
-                                                
-                                                String[] container = {};
-                                                
-                                                commonTokens = subCommonTokens.toArray(container);
-                                                break;
-                                                
-                                                
-                                               }
-                                       }
-                               }
-                               
-                       }
-               }
-               
-               
-               commonXPath ="";
-               
-               for(int i = 1 ; i< commonTokens.length;i++){
-                       commonXPath +="/"+commonTokens[i];
-               
-               }
-               }
-               
-               return commonXPath;
-       }
-
-       
-       public List<XSSFXmlColumnPr> getXmlColumnPrs() {
-               
-               if(xmlColumnPr==null){
-                       xmlColumnPr = new Vector<XSSFXmlColumnPr>();
-                       for(CTTableColumn column:ctTable.getTableColumns().getTableColumnList()){
-                               if(column.getXmlColumnPr()!=null){
-                                       XSSFXmlColumnPr columnPr = new XSSFXmlColumnPr(this,column,column.getXmlColumnPr());
-                                       xmlColumnPr.add(columnPr);
-                               }
-                       }
-               }
-               return xmlColumnPr;
-       }
-       
-       /**
-        * @return the name of the Table, if set
-        */
-       public String getName() {
-          return ctTable.getName();
-       }
-       
-       /**
-        * Changes the name of the Table
-        */
-       public void setName(String name) {
-          if(name == null) {
-             ctTable.unsetName();
-             return;
-          }
-          ctTable.setName(name);
-       }
-
-   /**
-    * @return the display name of the Table, if set
-    */
-   public String getDisplayName() {
-      return ctTable.getDisplayName();
-   }
-
-   /**
-    * Changes the display name of the Table
-    */
-   public void setDisplayName(String name) {
-      ctTable.setDisplayName(name);
-   }
-
-       /**
-        * @return  the number of mapped table columns (see Open Office XML Part 4: chapter 3.5.1.4)
-        */
-       public long getNumerOfMappedColumns(){
-               return ctTable.getTableColumns().getCount();
-       }
-       
-       
-       /**
-        * @return The reference for the cell in the top-left part of the table
-        * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref) 
-        *
-        */
-       public CellReference getStartCellReference() {
-               
-               if(startCellReference==null){                   
-                               String ref = ctTable.getRef();
-                               String[] boundaries = ref.split(":");
-                               String from = boundaries[0];
-                               startCellReference = new CellReference(from);
-               }
-               return startCellReference;
-       }
-       
-       /**
-        * @return The reference for the cell in the bottom-right part of the table
-        * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref)
-        *
-        */
-       public CellReference getEndCellReference() {
-               
-               if(endCellReference==null){
-                       
-                               String ref = ctTable.getRef();
-                               String[] boundaries = ref.split(":");
-                               String from = boundaries[1];
-                               endCellReference = new CellReference(from);
-               }
-               return endCellReference;
-       }
-       
-       
-       /**
-        *  @return the total number of rows in the selection. (Note: in this version autofiltering is ignored)
-        *
-        */
-       public int getRowCount(){
-               
-               
-               CellReference from = getStartCellReference();
-               CellReference to = getEndCellReference();
-               
-               int rowCount = -1;
-               if (from!=null && to!=null){
-                rowCount = to.getRow()-from.getRow();
-               }
-               return rowCount;
-       }
-}
index 6d40b686ad15867d87cef264e333a7ca495da1be..6ddda437d1dfae5e6079151baf715b745dd2a6a5 100644 (file)
@@ -24,7 +24,6 @@ import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.util.Internal;
 import org.apache.poi.xssf.model.MapInfo;
 import org.apache.poi.xssf.model.SingleXmlCells;
-import org.apache.poi.xssf.model.Table;
 import org.apache.poi.xssf.usermodel.helpers.XSSFSingleXmlCell;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMap;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSchema;
@@ -101,16 +100,16 @@ public class XSSFMap {
     /**
      * @return the list of all Tables that provide a map rule to this mapping
      */
-    public List<Table> getRelatedTables() {
+    public List<XSSFTable> getRelatedTables() {
 
-        List<Table> tables = new Vector<Table>();
+        List<XSSFTable> tables = new Vector<XSSFTable>();
         int sheetNumber = mapInfo.getWorkbook().getNumberOfSheets();
 
         for (int i = 0; i < sheetNumber; i++) {
             XSSFSheet sheet = mapInfo.getWorkbook().getSheetAt(i);
             for (POIXMLDocumentPart p : sheet.getRelations()) {
                 if (p.getPackageRelationship().getRelationshipType().equals(XSSFRelation.TABLE.getRelation())) {
-                    Table table = (Table) p;
+                    XSSFTable table = (XSSFTable) p;
                     if (table.mapsTo(ctMap.getID())) {
                         tables.add(table);
                     }
index b49d6c2d3b7ec733d95e8a33e8811c20185d6bea..f0b9a9b71522e700dea5ae5691819c9ff905a427 100644 (file)
@@ -39,7 +39,6 @@ import org.apache.poi.xssf.model.MapInfo;
 import org.apache.poi.xssf.model.SharedStringsTable;
 import org.apache.poi.xssf.model.SingleXmlCells;
 import org.apache.poi.xssf.model.StylesTable;
-import org.apache.poi.xssf.model.Table;
 import org.apache.poi.xssf.model.ThemesTable;
 
 /**
@@ -146,7 +145,7 @@ public final class XSSFRelation extends POIXMLRelation {
                        "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml",
                        "http://schemas.openxmlformats.org/officeDocument/2006/relationships/table",
                        "/xl/tables/table#.xml",
-                       Table.class
+                       XSSFTable.class
        );
 
     public static final XSSFRelation IMAGES = new XSSFRelation(
index 163da4d262ef155dc905147873501822c94a8485..3d72c82f8fd68d7f9d57197713c88a7bbbbd9760 100644 (file)
@@ -59,7 +59,6 @@ import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.xssf.model.CommentsTable;
-import org.apache.poi.xssf.model.Table;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
 import org.apache.poi.xssf.usermodel.helpers.XSSFRowShifter;
 import org.apache.xmlbeans.XmlException;
@@ -130,7 +129,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
      * Master shared formula is the first formula in a group of shared formulas is saved in the f element.
      */
     private Map<Integer, CTCellFormula> sharedFormulas;
-    private TreeMap<String,Table> tables;
+    private TreeMap<String,XSSFTable> tables;
     private List<CellRangeAddress> arrayFormulas;
     private XSSFDataValidationHelper dataValidationHelper;    
 
@@ -194,8 +193,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
                sheetComments = (CommentsTable)p;
                break;
             }
-            if(p instanceof Table) {
-               tables.put( p.getPackageRelationship().getId(), (Table)p );
+            if(p instanceof XSSFTable) {
+               tables.put( p.getPackageRelationship().getId(), (XSSFTable)p );
             }
         }
         
@@ -217,7 +216,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
     @SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
     private void initRows(CTWorksheet worksheet) {
         _rows = new TreeMap<Integer, XSSFRow>();
-        tables = new TreeMap<String, Table>();
+        tables = new TreeMap<String, XSSFTable>();
         sharedFormulas = new HashMap<Integer, CTCellFormula>();
         arrayFormulas = new ArrayList<CellRangeAddress>();
         for (CTRow row : worksheet.getSheetData().getRowArray()) {
@@ -3022,7 +3021,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
     /**
      * Creates a new Table, and associates it with this Sheet
      */
-    public Table createTable() {
+    public XSSFTable createTable() {
        if(! worksheet.isSetTableParts()) {
           worksheet.addNewTableParts();
        }
@@ -3033,7 +3032,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
        // Table numbers need to be unique in the file, not just
        //  unique within the sheet. Find the next one
        int tableNumber = getPackagePart().getPackage().getPartsByContentType(XSSFRelation.TABLE.getContentType()).size() + 1;
-       Table table = (Table)createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber);
+       XSSFTable table = (XSSFTable)createRelationship(XSSFRelation.TABLE, XSSFFactory.getInstance(), tableNumber);
        tbl.setId(table.getPackageRelationship().getId());
        
        tables.put(tbl.getId(), table);
@@ -3044,8 +3043,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
     /**
      * Returns any tables associated with this Sheet
      */
-    public List<Table> getTables() {
-       List<Table> tableList = new ArrayList<Table>(
+    public List<XSSFTable> getTables() {
+       List<XSSFTable> tableList = new ArrayList<XSSFTable>(
              tables.values()
        );
        return tableList;
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
new file mode 100644 (file)
index 0000000..851aeaa
--- /dev/null
@@ -0,0 +1,278 @@
+/* ====================================================================
+   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 java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Vector;
+
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr;
+import org.apache.xmlbeans.XmlException;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.TableDocument;
+
+/**
+ * 
+ * This class implements the Table Part (Open Office XML Part 4:
+ * chapter 3.5.1)
+ * 
+ * This implementation works under the assumption that a table contains mappings to a subtree of an XML.
+ * The root element of this subtree an occur multiple times (one for each row of the table). The child nodes
+ * of the root element can be only attributes or element with maxOccurs=1 property set
+ * 
+ *
+ * @author Roberto Manicardi
+ */
+public class XSSFTable extends POIXMLDocumentPart {
+       
+       private CTTable ctTable;
+       private List<XSSFXmlColumnPr> xmlColumnPr;
+       private CellReference startCellReference;
+       private CellReference endCellReference; 
+       private String commonXPath; 
+       
+       
+       public XSSFTable() {
+               super();
+               ctTable = CTTable.Factory.newInstance();
+
+       }
+
+       public XSSFTable(PackagePart part, PackageRelationship rel)
+                       throws IOException {
+               super(part, rel);
+               readFrom(part.getInputStream());
+       }
+
+       public void readFrom(InputStream is) throws IOException {
+               try {
+                       TableDocument doc = TableDocument.Factory.parse(is);
+                       ctTable = doc.getTable();
+               } catch (XmlException e) {
+                       throw new IOException(e.getLocalizedMessage());
+               }
+       }
+       
+       public XSSFSheet getXSSFSheet(){
+               return (XSSFSheet) getParent();
+       }
+
+       public void writeTo(OutputStream out) throws IOException {
+               TableDocument doc = TableDocument.Factory.newInstance();
+               doc.setTable(ctTable);
+               doc.save(out, DEFAULT_XML_OPTIONS);
+       }
+
+       @Override
+       protected void commit() throws IOException {
+               PackagePart part = getPackagePart();
+               OutputStream out = part.getOutputStream();
+               writeTo(out);
+               out.close();
+       }
+       
+       public CTTable getCTTable(){
+               return ctTable;
+       }
+       
+       /**
+        * Checks if this Table element contains even a single mapping to the map identified by id
+        * @param id the XSSFMap ID
+        * @return true if the Table element contain mappings
+        */
+       public boolean mapsTo(long id){
+               boolean maps =false;
+               
+               List<XSSFXmlColumnPr> pointers = getXmlColumnPrs();
+               
+               for(XSSFXmlColumnPr pointer: pointers){
+                       if(pointer.getMapId()==id){
+                               maps=true;
+                               break;
+                       }
+               }
+               
+               return maps;
+       }
+
+       
+       /**
+        * 
+        * Calculates the xpath of the root element for the table. This will be the common part
+        * of all the mapping's xpaths
+        * 
+        * @return the xpath of the table's root element
+        */
+       public String getCommonXpath() {
+               
+               if(commonXPath == null){
+               
+               String[] commonTokens ={};
+               
+               for(CTTableColumn column :ctTable.getTableColumns().getTableColumnList()){
+                       if(column.getXmlColumnPr()!=null){
+                               String xpath = column.getXmlColumnPr().getXpath();
+                               String[] tokens =  xpath.split("/");
+                               if(commonTokens.length==0){
+                                       commonTokens = tokens;
+                                       
+                               }else{
+                                       int maxLenght = commonTokens.length>tokens.length? tokens.length:commonTokens.length;
+                                       for(int i =0; i<maxLenght;i++){
+                                               if(!commonTokens[i].equals(tokens[i])){
+                                                List<String> subCommonTokens = Arrays.asList(commonTokens).subList(0, i);
+                                                
+                                                String[] container = {};
+                                                
+                                                commonTokens = subCommonTokens.toArray(container);
+                                                break;
+                                                
+                                                
+                                               }
+                                       }
+                               }
+                               
+                       }
+               }
+               
+               
+               commonXPath ="";
+               
+               for(int i = 1 ; i< commonTokens.length;i++){
+                       commonXPath +="/"+commonTokens[i];
+               
+               }
+               }
+               
+               return commonXPath;
+       }
+
+       
+       public List<XSSFXmlColumnPr> getXmlColumnPrs() {
+               
+               if(xmlColumnPr==null){
+                       xmlColumnPr = new Vector<XSSFXmlColumnPr>();
+                       for(CTTableColumn column:ctTable.getTableColumns().getTableColumnList()){
+                               if(column.getXmlColumnPr()!=null){
+                                       XSSFXmlColumnPr columnPr = new XSSFXmlColumnPr(this,column,column.getXmlColumnPr());
+                                       xmlColumnPr.add(columnPr);
+                               }
+                       }
+               }
+               return xmlColumnPr;
+       }
+       
+       /**
+        * @return the name of the Table, if set
+        */
+       public String getName() {
+          return ctTable.getName();
+       }
+       
+       /**
+        * Changes the name of the Table
+        */
+       public void setName(String name) {
+          if(name == null) {
+             ctTable.unsetName();
+             return;
+          }
+          ctTable.setName(name);
+       }
+
+   /**
+    * @return the display name of the Table, if set
+    */
+   public String getDisplayName() {
+      return ctTable.getDisplayName();
+   }
+
+   /**
+    * Changes the display name of the Table
+    */
+   public void setDisplayName(String name) {
+      ctTable.setDisplayName(name);
+   }
+
+       /**
+        * @return  the number of mapped table columns (see Open Office XML Part 4: chapter 3.5.1.4)
+        */
+       public long getNumerOfMappedColumns(){
+               return ctTable.getTableColumns().getCount();
+       }
+       
+       
+       /**
+        * @return The reference for the cell in the top-left part of the table
+        * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref) 
+        *
+        */
+       public CellReference getStartCellReference() {
+               
+               if(startCellReference==null){                   
+                               String ref = ctTable.getRef();
+                               String[] boundaries = ref.split(":");
+                               String from = boundaries[0];
+                               startCellReference = new CellReference(from);
+               }
+               return startCellReference;
+       }
+       
+       /**
+        * @return The reference for the cell in the bottom-right part of the table
+        * (see Open Office XML Part 4: chapter 3.5.1.2, attribute ref)
+        *
+        */
+       public CellReference getEndCellReference() {
+               
+               if(endCellReference==null){
+                       
+                               String ref = ctTable.getRef();
+                               String[] boundaries = ref.split(":");
+                               String from = boundaries[1];
+                               endCellReference = new CellReference(from);
+               }
+               return endCellReference;
+       }
+       
+       
+       /**
+        *  @return the total number of rows in the selection. (Note: in this version autofiltering is ignored)
+        *
+        */
+       public int getRowCount(){
+               
+               
+               CellReference from = getStartCellReference();
+               CellReference to = getEndCellReference();
+               
+               int rowCount = -1;
+               if (from!=null && to!=null){
+                rowCount = to.getRow()-from.getRow();
+               }
+               return rowCount;
+       }
+}
index 42b67471b716b45e72a2fba5d795d435d8954944..f5d29d3729ad5e51518ac668209a501132963183 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.xssf.usermodel.helpers;
 
-import org.apache.poi.xssf.model.Table;
+import org.apache.poi.xssf.usermodel.XSSFTable;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXmlColumnPr;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXmlDataType.Enum;
@@ -33,11 +33,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXmlDataType.Enum;
  */
 public class XSSFXmlColumnPr {
        
-       private Table table;
+       private XSSFTable table;
        private CTTableColumn ctTableColumn;
        private CTXmlColumnPr ctXmlColumnPr;
        
-       public XSSFXmlColumnPr(Table table ,CTTableColumn ctTableColum,CTXmlColumnPr ctXmlColumnPr){
+       public XSSFXmlColumnPr(XSSFTable table ,CTTableColumn ctTableColum,CTXmlColumnPr ctXmlColumnPr){
                this.table = table;
                this.ctTableColumn = ctTableColum;
                this.ctXmlColumnPr = ctXmlColumnPr;
index d716cea47175f4e6a9e418b6b30cb260898170a8..54c5b1632e5b961b4d2cd61055a7ea2f3f115640 100644 (file)
@@ -43,7 +43,6 @@ import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.CalculationChain;
-import org.apache.poi.xssf.model.Table;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
 
@@ -852,7 +851,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
        assertEquals(0, s2.getTables().size());
        assertEquals(0, s3.getTables().size());
        
-       Table t = s1.getTables().get(0);
+       XSSFTable t = s1.getTables().get(0);
        assertEquals("Tabella1", t.getName());
        assertEquals("Tabella1", t.getDisplayName());
        assertEquals("A1:C3", t.getCTTable().getRef());
index 410ab61eb91e26a7acbc005d3ab6b05d22f80b78..1f5edc57e7340882ff214197cb2c70e3d111dc10 100644 (file)
@@ -26,7 +26,6 @@ import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.model.CalculationChain;
-import org.apache.poi.xssf.model.Table;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
 import org.apache.poi.util.HexDump;
 import org.apache.poi.hssf.record.PasswordRecord;
@@ -1050,11 +1049,11 @@ public final class TestXSSFSheet extends BaseTestSheet {
        assertEquals("a", s1.getRow(0).getCell(0).getRichStringCellValue().toString());
        assertEquals(1.0, s1.getRow(1).getCell(0).getNumericCellValue());
        
-       List<Table> tables = s1.getTables();
+       List<XSSFTable> tables = s1.getTables();
        assertNotNull(tables);
        assertEquals(1, tables.size());
        
-       Table table = tables.get(0);
+       XSSFTable table = tables.get(0);
        assertEquals("Tabella1", table.getName());
        assertEquals("Tabella1", table.getDisplayName());