]> source.dussan.org Git - poi.git/commitdiff
XSSF implementation of header and footer. Contributed by Paolo Mottadelli <p.mottadel...
authorUgo Cei <ugo@apache.org>
Thu, 7 Feb 2008 09:24:49 +0000 (09:24 +0000)
committerUgo Cei <ugo@apache.org>
Thu, 7 Feb 2008 09:24:49 +0000 (09:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@619316 13f79535-47bb-0310-9956-ffa450edef68

14 files changed:
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/HeaderFooterHelper.java [new file with mode: 0644]
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooter.java [new file with mode: 0644]
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestHeaderFooterHelper.java [new file with mode: 0644]

diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenFooter.java
new file mode 100644 (file)
index 0000000..a215285
--- /dev/null
@@ -0,0 +1,38 @@
+/* ====================================================================
+   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.Footer;
+import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+public class XSSFEvenFooter extends XSSFHeaderFooter implements Footer{
+
+    public XSSFEvenFooter(CTHeaderFooter headerFooter) {
+        super(headerFooter);
+    }
+    
+    public String getText() {
+        return getHeaderFooter().getEvenFooter();
+    }
+    
+    public void setText(String text) {
+        getHeaderFooter().setEvenFooter(text);
+    }
+
+}
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvenHeader.java
new file mode 100644 (file)
index 0000000..670b498
--- /dev/null
@@ -0,0 +1,38 @@
+/* ====================================================================
+   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.Header;
+import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+public class XSSFEvenHeader extends XSSFHeaderFooter implements Header{
+
+    public XSSFEvenHeader(CTHeaderFooter headerFooter) {
+        super(headerFooter);
+    }
+    
+    public String getText() {
+        return getHeaderFooter().getEvenHeader();
+    }
+    
+    public void setText(String text) {
+        getHeaderFooter().setEvenHeader(text);
+    }
+
+}
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstFooter.java
new file mode 100644 (file)
index 0000000..e9a1f37
--- /dev/null
@@ -0,0 +1,38 @@
+/* ====================================================================
+   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.Footer;
+import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+public class XSSFFirstFooter extends XSSFHeaderFooter implements Footer{
+
+    public XSSFFirstFooter(CTHeaderFooter headerFooter) {
+        super(headerFooter);
+    }
+    
+    public String getText() {
+        return getHeaderFooter().getFirstFooter();
+    }
+    
+    public void setText(String text) {
+        getHeaderFooter().setFirstFooter(text);
+    }
+
+}
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFirstHeader.java
new file mode 100644 (file)
index 0000000..17e5a70
--- /dev/null
@@ -0,0 +1,38 @@
+/* ====================================================================
+   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.Header;
+import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+public class XSSFFirstHeader extends XSSFHeaderFooter implements Header{
+
+    public XSSFFirstHeader(CTHeaderFooter headerFooter) {
+        super(headerFooter);
+    }
+    
+    public String getText() {
+        return getHeaderFooter().getFirstHeader();
+    }
+    
+    public void setText(String text) {
+        getHeaderFooter().setFirstHeader(text);
+    }
+
+}
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddFooter.java
new file mode 100644 (file)
index 0000000..709212e
--- /dev/null
@@ -0,0 +1,38 @@
+/* ====================================================================
+   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.Footer;
+import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+public class XSSFOddFooter extends XSSFHeaderFooter implements Footer{
+
+    public XSSFOddFooter(CTHeaderFooter headerFooter) {
+        super(headerFooter);
+    }
+    
+    public String getText() {
+        return getHeaderFooter().getOddFooter();
+    }
+    
+    public void setText(String text) {
+        getHeaderFooter().setOddFooter(text);
+    }
+
+}
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFOddHeader.java
new file mode 100644 (file)
index 0000000..e03a649
--- /dev/null
@@ -0,0 +1,38 @@
+/* ====================================================================
+   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.Header;
+import org.apache.poi.xssf.usermodel.extensions.XSSFHeaderFooter;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+public class XSSFOddHeader extends XSSFHeaderFooter implements Header{
+
+    public XSSFOddHeader(CTHeaderFooter headerFooter) {
+        super(headerFooter);
+    }
+    
+    public String getText() {
+        return getHeaderFooter().getOddHeader();
+    }
+    
+    public void setText(String text) {
+        getHeaderFooter().setOddHeader(text);
+    }
+
+}
index 02a192716aa059e695611fff67781097b48eba3a..2c63a9afc9afb6a22204ad9f7794785517e0441b 100644 (file)
@@ -96,8 +96,6 @@ public class XSSFSheet implements Sheet {
             col.setCustomWidth(true);
         }
         CTHeaderFooter hf = this.worksheet.addNewHeaderFooter();
-        hf.setOddHeader("&amp;C&amp;A");
-        hf.setOddFooter("&amp;C&amp;\"Arial\"&amp;10Page &amp;P");
         CTPageBreak rowBreaks = worksheet.addNewRowBreaks();
         CTPageBreak columnBreak = worksheet.addNewColBreaks();
         CTSheetPr sheetPr = worksheet.addNewSheetPr();
@@ -251,13 +249,36 @@ public class XSSFSheet implements Sheet {
     }
 
     public Footer getFooter() {
-        // TODO Auto-generated method stub
-        return null;
+        return getOddFooter();
+    }
+    
+    public Footer getOddFooter() {
+        return new XSSFOddFooter(worksheet.getHeaderFooter());
+    }
+    
+    public Footer getEvenFooter() {
+        return new XSSFEvenFooter(worksheet.getHeaderFooter());
+    }
+    
+    public Footer getFirstFooter() {
+        return new XSSFFirstFooter(worksheet.getHeaderFooter());
     }
 
     public Header getHeader() {
-        // TODO Auto-generated method stub
-        return null;
+        return getOddHeader();
+    }
+    
+    public Header getOddHeader() {
+        return new XSSFOddHeader(worksheet.getHeaderFooter());
+    }
+    
+    public Header getEvenHeader() {
+        return new XSSFEvenHeader(worksheet.getHeaderFooter()
+);
+    }
+    
+    public Header getFirstHeader() {
+        return new XSSFFirstHeader(worksheet.getHeaderFooter());
     }
 
     public boolean getHorizontallyCenter() {
@@ -282,23 +303,23 @@ public class XSSFSheet implements Sheet {
     }
 
     public double getMargin(short margin) {
-       CTPageMargins pageMargins = worksheet.getPageMargins();
-       switch (margin) {
-       case LeftMargin:
-               return pageMargins.getLeft();
-       case RightMargin:
-               return pageMargins.getRight();
-       case TopMargin:
-               return pageMargins.getTop();
-       case BottomMargin:
-               return pageMargins.getBottom();
-       case HeaderMargin:
-               return pageMargins.getHeader();
-       case FooterMargin:
-               return pageMargins.getFooter();
-       default :
-               throw new RuntimeException( "Unknown margin constant:  " + margin );
-       }
+        CTPageMargins pageMargins = worksheet.getPageMargins();
+        switch (margin) {
+        case LeftMargin:
+            return pageMargins.getLeft();
+        case RightMargin:
+            return pageMargins.getRight();
+        case TopMargin:
+            return pageMargins.getTop();
+        case BottomMargin:
+            return pageMargins.getBottom();
+        case HeaderMargin:
+            return pageMargins.getHeader();
+        case FooterMargin:
+            return pageMargins.getFooter();
+        default :
+            throw new RuntimeException( "Unknown margin constant:  " + margin );
+        }
     }
 
     public Region getMergedRegionAt(int index) {
@@ -537,19 +558,11 @@ public class XSSFSheet implements Sheet {
     }
 
     public void setColumnHidden(short column, boolean hidden) {
-        CTCol col = columnHelper.getColumn(column);
-        if (col == null) {
-            col = columnHelper.createColumn(column);
-        }
-        col.setHidden(hidden);
+        columnHelper.getColumn(column).setHidden(hidden);
     }
 
     public void setColumnWidth(short column, short width) {
-        CTCol col = columnHelper.getColumn(column);
-        if (col == null) {
-            col = columnHelper.createColumn(column);
-        }
-        col.setWidth(width);
+        columnHelper.getColumn(column).setWidth(width);
     }
 
     public void setDefaultColumnStyle(short column, CellStyle style) {
@@ -611,21 +624,21 @@ public class XSSFSheet implements Sheet {
     }
 
     public void setMargin(short margin, double size) {
-           CTPageMargins pageMargins = worksheet.getPageMargins();
-           switch (margin) {
-           case LeftMargin:
-               pageMargins.setLeft(size);
-           case RightMargin:
-               pageMargins.setRight(size);
-           case TopMargin:
-               pageMargins.setTop(size);
-           case BottomMargin:
-               pageMargins.setBottom(size);
-           case HeaderMargin:
-               pageMargins.setHeader(size);
-           case FooterMargin:
-               pageMargins.setFooter(size);
-           }
+        CTPageMargins pageMargins = worksheet.getPageMargins();
+        switch (margin) {
+        case LeftMargin:
+            pageMargins.setLeft(size);
+        case RightMargin:
+            pageMargins.setRight(size);
+        case TopMargin:
+            pageMargins.setTop(size);
+        case BottomMargin:
+            pageMargins.setBottom(size);
+        case HeaderMargin:
+            pageMargins.setHeader(size);
+        case FooterMargin:
+            pageMargins.setFooter(size);
+        }
     }
 
     public void setPrintGridlines(boolean newPrintGridlines) {
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFHeaderFooter.java
new file mode 100644 (file)
index 0000000..57728a6
--- /dev/null
@@ -0,0 +1,73 @@
+/* ====================================================================
+   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.extensions;
+
+import org.apache.poi.ss.usermodel.Footer;
+import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+public abstract class XSSFHeaderFooter implements Footer{
+    
+    private HeaderFooterHelper helper;
+    private CTHeaderFooter headerFooter;
+    private String value;
+
+    public XSSFHeaderFooter(CTHeaderFooter headerFooter) {
+       this.headerFooter = headerFooter;
+       this.value = getText();
+       this.value = this.value != null ? this.value : "";
+       this.helper = new HeaderFooterHelper();
+    }
+    
+    public CTHeaderFooter getHeaderFooter() {
+        return this.headerFooter;
+    }
+    
+    public String getValue() {
+        return this.value;
+    }
+    
+    public abstract String getText();
+    
+    protected abstract void setText(String text);
+
+    public String getCenter() {
+        return helper.getCenterSection(getText());
+    }
+
+    public String getLeft() {
+        return helper.getLeftSection(getText());
+    }
+
+    public String getRight() {
+        return helper.getRightSection(getText());
+    }
+
+    public void setCenter(String newCenter) {
+        setText(helper.setCenterSection(getText(), newCenter));
+    }
+
+    public void setLeft(String newLeft) {
+        setText(helper.setLeftSection(getText(), newLeft));
+    }
+
+    public void setRight(String newRight) {
+        setText(helper.setRightSection(getText(), newRight));
+    }
+
+}
index a05a3e6db8f606908180a1dfb213b6109c6b1a4f..74e37e8285d24c57306ba3f98d233f8d8af3101d 100644 (file)
@@ -1,99 +1,90 @@
+/* ====================================================================
+   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.helpers;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
 
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
 
+
 public class ColumnHelper {
-       
-       private List<CTCol> columns;
-       
-    public ColumnHelper(CTWorksheet worksheet) {
-               super();
-               setColumns(worksheet);
-       }
+    
+    private CTWorksheet worksheet;
+    
 
-       public List<CTCol> getColumns() {
-       return columns;
+    public ColumnHelper(CTWorksheet worksheet) {
+        super();
+        this.worksheet = worksheet;
+        cleanColumns();
     }
-       
-       public void setColumns(CTWorksheet worksheet) {
-       columns = new ArrayList<CTCol>();
-       CTCols[] colsArray = worksheet.getColsArray();
-       for (int i = 0 ; i < colsArray.length ; i++) {
-               CTCols cols = colsArray[i];
-               CTCol[] colArray = cols.getColArray();
-               for (int y = 0 ; y < colArray.length ; y++) {
-                       CTCol col = colArray[y];
-                       for (long k = col.getMin() ; k <= col.getMax() ; k++) {
-                               setColumn(columns, col, k);
-                       }
-               }
-       }
-       }
-
-       private void setColumn(List<CTCol> columns, CTCol col, long k) {
-               CTCol column = getColumn(columns, k);
-               if (column == null) {
-                       column = CTCol.Factory.newInstance();
-                       column.setMin(k);
-                       column.setMax(k);
-                       setColumnAttributes(col, column);
-                       columns.add(column);
-               }
-               else {
-                       setColumnAttributes(col, column);
-               }
-       }
 
-       private void setColumnAttributes(CTCol col, CTCol column) {
-               if (col.getWidth() > 0) {
-                       column.setWidth(col.getWidth());
-               }
-               // TODO set all col attributes
-       }
-    
-    public CTCol getColumn(List<CTCol> columns, long k) {
-       for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) {
-               CTCol column = it.next();
-               if (column.getMin() == k) {
-                       return column;
-               }
-       }
-       return null;
+    public void cleanColumns() {
+        CTCols newCols = CTCols.Factory.newInstance();
+        CTCols[] colsArray = worksheet.getColsArray();
+        int i = 0;
+        for (i = 0 ; i < colsArray.length ; i++) {
+            CTCols cols = colsArray[i];
+            CTCol[] colArray = cols.getColArray();
+            for (int y = 0 ; y < colArray.length ; y++) {
+                 CTCol col = colArray[y];
+                 for (long k = col.getMin() ; k <= col.getMax() ; k++) {
+                     if (!columnExists(newCols, k)) {
+                         CTCol newCol = newCols.addNewCol();
+                         newCol.setMin(k);
+                         newCol.setMax(k);
+                         setColumnAttributes(col, newCol);
+                     }
+                 }
+            }
+        }
+        for (int y = i-1 ; y >= 0 ; y--) {
+            worksheet.removeCols(y);
+        }
+        worksheet.addNewCols();
+        worksheet.setColsArray(0, newCols);
     }
     
     public CTCol getColumn(long index) {
-       for (Iterator<CTCol> it = columns.iterator() ; it.hasNext() ; ) {
-               CTCol column = it.next();
-               if (getColumnIndex(column) == index) {
-                       return column;
-               }
-       }
-       return null;
+        for (int i = 0 ; i < worksheet.getColsArray(0).sizeOfColArray() ; i++) {
+            if (worksheet.getColsArray(0).getColArray(i).getMin() == index) {
+                return worksheet.getColsArray(0).getColArray(i);
+            }
+        }
+        return  null;
     }
     
-    public long getColumnIndex(CTCol column) {
-       if (column.getMin() == column.getMax()) {
-               return column.getMin();
-       }
-       return -1;
+    public boolean columnExists(CTCols cols, long index) {
+        for (int i = 0 ; i < cols.sizeOfColArray() ; i++) {
+            if (cols.getColArray(i).getMin() == index) {
+                return true;
+            }
+        }
+        return false;
     }
     
-    public CTCol createColumn(long index) {
-       CTCol column = CTCol.Factory.newInstance();
-       setIndex(column, index);
-       columns.add(column);
-       return column;
+    public void setColumnAttributes(CTCol col, CTCol newCol) {
+        if (col.getWidth() != 0) {
+            newCol.setWidth(col.getWidth());
+        }
+        if (col.getHidden()) {
+            newCol.setHidden(true);
+        }
     }
-
-       private void setIndex(CTCol column, long index) {
-               column.setMin(index);
-               column.setMax(index);
-       }
-
+    
 }
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/HeaderFooterHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/HeaderFooterHelper.java
new file mode 100644 (file)
index 0000000..18fddf5
--- /dev/null
@@ -0,0 +1,73 @@
+/* ====================================================================
+   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.helpers;
+
+
+public class HeaderFooterHelper {
+    
+    private static final String HeaderFooterEntity = "&amp;";
+    private static final String HeaderFooterEntity_R = "&amp;R";
+    private static final String HeaderFooterEntity_L = "&amp;L";
+    private static final String HeaderFooterEntity_C = "&amp;C";
+
+    public String getCenterSection(String string) {
+        return getSection(string, HeaderFooterEntity_C);
+    }
+    public String getLeftSection(String string) {
+        return getSection(string, HeaderFooterEntity_L);
+    }
+    public String getRightSection(String string) {
+        return getSection(string, HeaderFooterEntity_R);
+    }
+    
+    public String setCenterSection(String string, String newCenter) {
+        return setSection(string, newCenter, HeaderFooterEntity_C);
+    }
+    public String setLeftSection(String string, String newLeft) {
+        return setSection(string, newLeft, HeaderFooterEntity_L);
+    }
+    public String setRightSection(String string, String newRight) {
+        return setSection(string, newRight, HeaderFooterEntity_R);
+    }
+    
+    public String setSection(String string, String newSection, String entity) {
+        string = string != null ? string : "";
+        String oldSection = getSection(string, entity);
+        if (oldSection.equals("")) {
+            return string.concat(entity + newSection);
+        }
+        return string.replaceAll(entity + oldSection, entity + newSection);
+    }
+
+    private String getSection(String string, String entity) {
+        if (string == null) {
+            return "";
+        }
+        String stringAfterEntity = "";
+        if (string.indexOf(entity) >= 0) {
+            stringAfterEntity = string.substring(string.indexOf(entity) + entity.length());
+        }
+        String nextEntity = "";
+        if (stringAfterEntity.indexOf(HeaderFooterEntity) > 0) {
+            nextEntity = stringAfterEntity.substring(stringAfterEntity.indexOf(HeaderFooterEntity), stringAfterEntity.indexOf(HeaderFooterEntity) + (HeaderFooterEntity.length()));
+            stringAfterEntity = stringAfterEntity.substring(0, stringAfterEntity.indexOf(nextEntity));
+        }
+        return stringAfterEntity;
+    }
+
+}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooter.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHeaderFooter.java
new file mode 100644 (file)
index 0000000..9b1c145
--- /dev/null
@@ -0,0 +1,50 @@
+/* ====================================================================
+   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.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+import junit.framework.TestCase;
+
+
+public class TestXSSFHeaderFooter extends TestCase {
+    
+    public void testGetSetCenterLeftRight() {
+        
+        XSSFOddFooter footer = new XSSFOddFooter(CTHeaderFooter.Factory.newInstance());
+        assertEquals("", footer.getCenter());
+        footer.setCenter("My first center section");
+        assertEquals("My first center section", footer.getCenter());
+        footer.setCenter("No, let's update the center section");
+        assertEquals("No, let's update the center section", footer.getCenter());
+        footer.setLeft("And add a left one");
+        footer.setRight("Finally the right section is added");
+        assertEquals("And add a left one", footer.getLeft());
+        assertEquals("Finally the right section is added", footer.getRight());
+        
+        // Test changing the three sections value
+        footer.setCenter("Second center version");
+        footer.setLeft("Second left version");
+        footer.setRight("Second right version");
+        assertEquals("Second center version", footer.getCenter());
+        assertEquals("Second left version", footer.getLeft());
+        assertEquals("Second right version", footer.getRight());
+        
+    }
+    
+}
index be61dfdc07d73f1513a26772980b762bb945002f..e96384e3e3009fb5e424f33b084fe9804b223794 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.poi.xssf.usermodel;
 import java.util.Iterator;
 
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Footer;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 
@@ -125,14 +126,6 @@ public class TestXSSFSheet extends TestCase {
         assertEquals((short) 14, sheet.getDefaultColumnWidth());
     }
     
-    public void testGetSetColumnWidth() {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet = workbook.createSheet("Sheet 1");
-        // Test setting a column width and getting that value
-        sheet.setColumnWidth((short) 0, (short) 16);
-        assertEquals(16, sheet.getColumnWidth((short) 0));
-    }
-    
     public void testGetFirstLastRowNum() {
         XSSFWorkbook workbook = new XSSFWorkbook();
         Sheet sheet = workbook.createSheet("Sheet 1");
@@ -220,17 +213,6 @@ public class TestXSSFSheet extends TestCase {
         assertFalse(sheet.getAutobreaks());
     }
     
-    public void testIsSetColumnHidden() {
-        XSSFWorkbook workbook = new XSSFWorkbook();
-        Sheet sheet = workbook.createSheet("Sheet 1");
-        sheet.setColumnWidth((short) 0, (short) 13);
-        sheet.setColumnHidden((short) 1, false);
-        sheet.setColumnHidden((short) 2, true);
-        assertFalse(sheet.isColumnHidden((short) 0));
-        assertFalse(sheet.isColumnHidden((short) 1));
-        assertTrue(sheet.isColumnHidden((short) 2));
-    }
-    
     public void testIsSetFitToPage() {
         XSSFWorkbook workbook = new XSSFWorkbook();
         Sheet sheet = workbook.createSheet("Sheet 1");
@@ -246,16 +228,31 @@ public class TestXSSFSheet extends TestCase {
         Sheet sheet = workbook.createSheet("Sheet 1");
         assertEquals((double) 0, sheet.getMargin((short) 0));
         sheet.setMargin((short) 0, 10);
-        sheet.setMargin((short) 1, 11);
-        sheet.setMargin((short) 2, 12);
-        sheet.setMargin((short) 3, 13);
-        sheet.setMargin((short) 4, 14);
-        sheet.setMargin((short) 5, 15);
         assertEquals((double) 10, sheet.getMargin((short) 0));
+        assertEquals((double) 10, sheet.getMargin((short) 1));
+        assertEquals((double) 10, sheet.getMargin((short) 2));
+        assertEquals((double) 10, sheet.getMargin((short) 3));
+        assertEquals((double) 10, sheet.getMargin((short) 4));
+        assertEquals((double) 10, sheet.getMargin((short) 5));
+        sheet.setMargin((short) 1, 11);
         assertEquals((double) 11, sheet.getMargin((short) 1));
+        assertEquals((double) 11, sheet.getMargin((short) 2));
+        assertEquals((double) 11, sheet.getMargin((short) 3));
+        assertEquals((double) 11, sheet.getMargin((short) 4));
+        assertEquals((double) 11, sheet.getMargin((short) 5));
+        sheet.setMargin((short) 2, 12);
         assertEquals((double) 12, sheet.getMargin((short) 2));
+        assertEquals((double) 12, sheet.getMargin((short) 3));
+        assertEquals((double) 12, sheet.getMargin((short) 4));
+        assertEquals((double) 12, sheet.getMargin((short) 5));
+        sheet.setMargin((short) 3, 13);
         assertEquals((double) 13, sheet.getMargin((short) 3));
+        assertEquals((double) 13, sheet.getMargin((short) 4));
+        assertEquals((double) 13, sheet.getMargin((short) 5));
+        sheet.setMargin((short) 4, 14);
         assertEquals((double) 14, sheet.getMargin((short) 4));
+        assertEquals((double) 14, sheet.getMargin((short) 5));
+        sheet.setMargin((short) 5, 15);
         assertEquals((double) 15, sheet.getMargin((short) 5));
         
         // Test that nothing happens if another margin constant is given (E.G. 65)
@@ -267,4 +264,62 @@ public class TestXSSFSheet extends TestCase {
         assertEquals((double) 14, sheet.getMargin((short) 4));
         assertEquals((double) 15, sheet.getMargin((short) 5));
     }
+    
+    public void testGetFooter() {
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        Sheet sheet = workbook.createSheet("Sheet 1");
+        assertNotNull(sheet.getFooter());
+        sheet.getFooter().setCenter("test center footer");
+        assertEquals("test center footer", sheet.getFooter().getCenter());
+    }
+    
+    public void testGetAllHeadersFooters() {
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1");
+        assertNotNull(sheet.getOddFooter());
+        assertNotNull(sheet.getEvenFooter());
+        assertNotNull(sheet.getFirstFooter());
+        assertNotNull(sheet.getOddHeader());
+        assertNotNull(sheet.getEvenHeader());
+        assertNotNull(sheet.getFirstHeader());
+        
+        assertEquals("", sheet.getOddFooter().getLeft());
+        sheet.getOddFooter().setLeft("odd footer left");
+        assertEquals("odd footer left", sheet.getOddFooter().getLeft());
+        
+        assertEquals("", sheet.getEvenFooter().getLeft());
+        sheet.getEvenFooter().setLeft("even footer left");
+        assertEquals("even footer left", sheet.getEvenFooter().getLeft());
+        
+        assertEquals("", sheet.getFirstFooter().getLeft());
+        sheet.getFirstFooter().setLeft("first footer left");
+        assertEquals("first footer left", sheet.getFirstFooter().getLeft());
+        
+        assertEquals("", sheet.getOddHeader().getLeft());
+        sheet.getOddHeader().setLeft("odd header left");
+        assertEquals("odd header left", sheet.getOddHeader().getLeft());
+        
+        assertEquals("", sheet.getOddHeader().getRight());
+        sheet.getOddHeader().setRight("odd header right");
+        assertEquals("odd header right", sheet.getOddHeader().getRight());
+        
+        assertEquals("", sheet.getOddHeader().getCenter());
+        sheet.getOddHeader().setCenter("odd header center");
+        assertEquals("odd header center", sheet.getOddHeader().getCenter());
+
+    }
+    
+    public void testGetSetColumnWidth() {
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        Sheet sheet = workbook.createSheet("Sheet 1");
+        sheet.setColumnWidth((short) 1,(short)  22);
+        assertEquals(22, sheet.getColumnWidth((short) 1));
+    }
+    
+    public void testGetSetColumnHidden() {
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        Sheet sheet = workbook.createSheet("Sheet 1");
+        sheet.setColumnHidden((short) 2, true);
+        assertTrue(sheet.isColumnHidden((short) 2));
+    }
 }
index 8c799a542e4f5ad4028cc6b327bb823169b4d9ef..db3d52f9fbfd308884c7efdfccfc1ff7c4d40cda 100644 (file)
 
 package org.apache.poi.xssf.usermodel.helpers;
 
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-
 import junit.framework.TestCase;
 
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.SharedStringSource;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
 
 
 public class TestColumnHelper extends TestCase {
-       
-       public void testGetColumnList() {
-               CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
-               ColumnHelper columnHelper = new ColumnHelper(worksheet);
-               
-               CTCols cols1 = worksheet.addNewCols();
-               CTCols cols2 = worksheet.addNewCols();
-               
-               CTCol col1_1 = cols1.addNewCol();
-               col1_1.setMin(1);
-               col1_1.setMax(10);
-               col1_1.setWidth(13);
-               CTCol col1_2 = cols1.addNewCol();
-               col1_2.setMin(15);
-               col1_2.setMax(15);
-               col1_2.setWidth(14);
+    
+    public void testCleanColumns() {
+        CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
+        
+        CTCols cols1 = worksheet.addNewCols();
+        CTCol col1 = cols1.addNewCol();
+        col1.setMin(1);
+        col1.setMax(1);
+        col1.setWidth(88);
+        col1.setHidden(true);
+        CTCol col2 = cols1.addNewCol();
+        col2.setMin(2);
+        col2.setMax(3);
+        CTCols cols2 = worksheet.addNewCols();
+        CTCol col4 = cols2.addNewCol();
+        col4.setMin(3);
+        col4.setMax(6);
+        
+        // Test cleaning cols
+        assertEquals(2, worksheet.sizeOfColsArray());
+        int count = countColumns(worksheet);
+        assertEquals(7, count);
+        // Clean columns and test a clean worksheet
+        ColumnHelper helper = new ColumnHelper(worksheet);
+        assertEquals(1, worksheet.sizeOfColsArray());
+        count = countColumns(worksheet);
+        assertEquals(6, count);
+        assertEquals((double) 88, helper.getColumn(1).getWidth());
+        assertTrue(helper.getColumn(1).getHidden());
+        
+    }
+    
+    public void testGetColumn() {
+        CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
+        
+        CTCols cols1 = worksheet.addNewCols();
+        CTCol col1 = cols1.addNewCol();
+        col1.setMin(1);
+        col1.setMax(1);
+        col1.setWidth(88);
+        col1.setHidden(true);
+        CTCol col2 = cols1.addNewCol();
+        col2.setMin(2);
+        col2.setMax(3);
+        CTCols cols2 = worksheet.addNewCols();
+        CTCol col4 = cols2.addNewCol();
+        col4.setMin(3);
+        col4.setMax(6);
+        
+        ColumnHelper helper = new ColumnHelper(worksheet);
+        assertNotNull(helper.getColumn(1));
+        assertEquals((double) 88, helper.getColumn(1).getWidth());
+        assertTrue(helper.getColumn(1).getHidden());
+        assertFalse(helper.getColumn(2).getHidden());
+        assertNull(helper.getColumn(99));
+    }
+    
+    public void testSetColumnAttributes() {
+        CTCol col = CTCol.Factory.newInstance();
+        col.setWidth(12);
+        col.setHidden(true);
+        CTCol newCol = CTCol.Factory.newInstance();
+        assertEquals((double) 0, newCol.getWidth());
+        assertFalse(newCol.getHidden());
+        ColumnHelper helper = new ColumnHelper(CTWorksheet.Factory.newInstance());
+        helper.setColumnAttributes(col, newCol);
+        assertEquals((double) 12, newCol.getWidth());
+        assertTrue(newCol.getHidden());
+    }
 
-               CTCol col2_1 = cols2.addNewCol();
-               col2_1.setMin(6);
-               col2_1.setMax(10);
-               CTCol col2_2 = cols2.addNewCol();
-               col2_2.setMin(20);
-               col2_2.setMax(20);
-               
-               columnHelper.setColumns(worksheet);
-               List<CTCol> columns = columnHelper.getColumns();
-               
-               assertEquals(12, columns.size());
-               assertEquals((double) 14, columnHelper.getColumn(15).getWidth());
-       }
-       
+    private int countColumns(CTWorksheet worksheet) {
+        int count;
+        count = 0;
+        for (int i = 0 ; i < worksheet.sizeOfColsArray() ; i++) {
+            for (int y = 0 ; y < worksheet.getColsArray(i).sizeOfColArray() ; y++) {
+                for (long k = worksheet.getColsArray(i).getColArray(y).getMin() ; k <= worksheet.getColsArray(i).getColArray(y).getMax() ; k++) {
+                    count++;
+                }
+            }
+        }
+        return count;
+    }
+    
 }
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestHeaderFooterHelper.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestHeaderFooterHelper.java
new file mode 100644 (file)
index 0000000..8c0b612
--- /dev/null
@@ -0,0 +1,59 @@
+/* ====================================================================
+   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.helpers;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.xssf.usermodel.helpers.HeaderFooterHelper;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
+
+
+public class TestHeaderFooterHelper extends TestCase {
+    
+    public void testGetCenterLeftRightSection() {
+        HeaderFooterHelper helper = new HeaderFooterHelper();
+        String headerFooter = "&amp;CTest the center section";
+        assertEquals("Test the center section", helper.getCenterSection(headerFooter));
+        headerFooter = "&amp;CTest the center section&amp;LThe left one&amp;RAnd the right one";
+        assertEquals("Test the center section", helper.getCenterSection(headerFooter));
+        assertEquals("The left one", helper.getLeftSection(headerFooter));
+        assertEquals("And the right one", helper.getRightSection(headerFooter));
+    }
+    
+    public void testSetCenterLeftRightSection() {
+        HeaderFooterHelper helper = new HeaderFooterHelper();
+        String headerFooter = "";
+        headerFooter = helper.setCenterSection(headerFooter, "First added center section");
+        assertEquals("First added center section", helper.getCenterSection(headerFooter));
+        headerFooter = helper.setLeftSection(headerFooter, "First left");
+        assertEquals("First left", helper.getLeftSection(headerFooter));
+
+        headerFooter = helper.setRightSection(headerFooter, "First right");
+        assertEquals("First right", helper.getRightSection(headerFooter));
+        assertEquals("&amp;CFirst added center section&amp;LFirst left&amp;RFirst right", headerFooter);
+
+        headerFooter = helper.setRightSection(headerFooter, "First right&amp");
+        assertEquals("First right&amp", helper.getRightSection(headerFooter));
+        assertEquals("&amp;CFirst added center section&amp;LFirst left&amp;RFirst right&amp", headerFooter);
+        
+        headerFooter = helper.setRightSection(headerFooter, "First right&amp;");
+        assertEquals("First right", helper.getRightSection(headerFooter));
+        assertEquals("&amp;CFirst added center section&amp;LFirst left&amp;RFirst right&amp;", headerFooter);
+    }
+    
+}