incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liud...@apache.org
Subject svn commit: r1406035 - in /incubator/odf/trunk/simple/src: main/java/org/odftoolkit/simple/ main/java/org/odftoolkit/simple/table/ test/java/org/odftoolkit/simple/table/ test/resources/
Date Tue, 06 Nov 2012 03:33:12 GMT
Author: liudali
Date: Tue Nov  6 03:33:11 2012
New Revision: 1406035

URL: http://svn.apache.org/viewvc?rev=1406035&view=rev
Log:
Fix for ODFTOOLKIT-334"Add new API to support table autoformat",patch by Kejia Ye

Added:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/TableTemplate.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableTemplateTest.java
    incubator/odf/trunk/simple/src/test/resources/TableTemplate.odt   (with props)
    incubator/odf/trunk/simple/src/test/resources/TestTableTemplate.odt   (with props)
Modified:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java?rev=1406035&r1=1406034&r2=1406035&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/Document.java Tue Nov  6 03:33:11 2012
@@ -64,6 +64,8 @@ import org.odftoolkit.odfdom.dom.element
 import org.odftoolkit.odfdom.dom.element.office.OfficeBodyElement;
 import org.odftoolkit.odfdom.dom.element.office.OfficeMasterStylesElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableCellElement;
+import org.odftoolkit.odfdom.dom.element.table.TableTableCellElementBase;
+import org.odftoolkit.odfdom.dom.element.table.TableTableTemplateElement;
 import org.odftoolkit.odfdom.dom.element.text.TextPElement;
 import org.odftoolkit.odfdom.dom.element.text.TextSectionElement;
 import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
@@ -85,10 +87,13 @@ import org.odftoolkit.odfdom.pkg.OdfVali
 import org.odftoolkit.odfdom.pkg.manifest.OdfFileEntry;
 import org.odftoolkit.odfdom.type.Duration;
 import org.odftoolkit.simple.meta.Meta;
+import org.odftoolkit.simple.table.TableTemplate;
 import org.odftoolkit.simple.table.AbstractTableContainer;
+import org.odftoolkit.simple.table.Cell;
 import org.odftoolkit.simple.table.Table;
-import org.odftoolkit.simple.table.TableContainer;
 import org.odftoolkit.simple.table.Table.TableBuilder;
+import org.odftoolkit.simple.table.TableContainer;
+import org.odftoolkit.simple.text.Paragraph;
 import org.odftoolkit.simple.text.Section;
 import org.w3c.dom.Attr;
 import org.w3c.dom.NamedNodeMap;
@@ -2423,4 +2428,169 @@ public abstract class Document extends O
 	protected IdentityHashMap<OdfElement, Component> getComponentMap() {
 		return mComponentRepository;
 	}
+
+	/**
+	 * Construct a
+	 * 
+	 * 
+	 * <code>TableTemplate<code> feature by extracting style template from an pre-defined table in a foreign document. The styles loaded by the template will be copied into the document as well and can be referenced by table directly.
+	 * <p>
+	 * The imported table need to be at least a 5*5 table (e.g. A1E5).  Each type of style in the template will be set according to the style reference in a specific table cell, as following:
+	 * <br>first column - A2
+	 * <br>last column - E2
+	 * <br>first row - A2
+	 * <br>last row - E2
+	 * <br>even rows - B3
+	 * <br>odd rows - B2
+	 * <br>even columns - C2
+	 * <br>odd columns - B2
+	 * <br>body - B2
+	 * <br>first-row-start-column -A1
+	 * <br>first-row-end-column -E1
+	 * <br>last-row-start-column -A5
+	 * <br>last-row-end-column -E5
+	 * 
+	 * @param templateFileInputStream
+	 *            - the InputStream of the ODF document.
+	 * @param tableName
+	 *            - the table name which will be used to load styles as template
+	 * @throws Exception
+	 *             - if content DOM could not be initialized
+	 */
+	public TableTemplate LoadTableTemplateFromForeignTable(
+			InputStream templateFileInputStream, String tableName) throws Exception {
+
+		Document doc = Document.loadDocument(templateFileInputStream);
+
+		if (doc == null)
+			throw new IllegalStateException(
+					"Cannot load specified template file.");
+
+		Table table = doc.getTableByName(tableName);
+		if (table == null)
+			throw new IllegalStateException(
+					"Cannot load table template from specified file.");
+
+		if (table.getRowCount() < 5 || table.getColumnCount() < 5)
+			throw new IllegalStateException(
+					"The template cannot be loaded. It should be at least a 5*5 table.");
+
+		TableTemplate template = new TableTemplate(getStylesDom()
+				.newOdfElement(TableTableTemplateElement.class));
+
+		// first-row-start-column
+		Cell cell = table.getCellByPosition(0, 0);
+		cell.getParagraphIterator().hasNext();
+		cell.getParagraphIterator().next().getStyleName();
+		Paragraph para = cell.getParagraphByIndex(0, false);
+		String paraStyle = (para != null ? para.getStyleName() : null);
+		template.setExtendedStyleByType(
+				TableTemplate.ExtendedStyleType.FIRSTROWSTARTCOLUM, cell
+						.getStyleName(), paraStyle);
+		TableTableCellElementBase oldCellEle = cell.getOdfElement();
+		TableTableCellElementBase newCellEle = (TableTableCellElementBase) oldCellEle
+				.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// first-row-end-column
+		cell = table.getCellByPosition(4, 0);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setExtendedStyleByType(
+				TableTemplate.ExtendedStyleType.FIRSTROWENDCOLUMN, cell
+						.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// last-row-start-column
+		cell = table.getCellByPosition(0, 4);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setExtendedStyleByType(
+				TableTemplate.ExtendedStyleType.LASTROWSTARTCOLUMN, cell
+						.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// last-row-end-column
+		cell = table.getCellByPosition(4, 4);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setExtendedStyleByType(
+				TableTemplate.ExtendedStyleType.LASTROWENDCOLUMN, cell
+						.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// first column
+		cell = table.getCellByPosition(0, 1);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setTableFirstColumnStyle(cell.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// last column
+		cell = table.getCellByPosition(4, 2);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setTableLastColumnStyle(cell.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// first row
+		cell = table.getCellByPosition(1, 0);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setTableFirstRowStyle(cell.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// last row
+		cell = table.getCellByPosition(1, 4);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setTableLastRowStyle(cell.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// body (=odd row/column)
+		cell = table.getCellByPosition(1, 1);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setTableBodyStyle(cell.getStyleName(), paraStyle);
+		template.setTableOddRowsStyle(cell.getStyleName(), paraStyle);
+		template.setTableOddColumnsStyle(cell.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// even row
+		cell = table.getCellByPosition(1, 2);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setTableEvenRowsStyle(cell.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		// even row
+		cell = table.getCellByPosition(2, 1);
+		para = cell.getParagraphByIndex(0, false);
+		paraStyle = (para != null ? para.getStyleName() : null);
+		template.setTableEvenColumnsStyle(cell.getStyleName(), paraStyle);
+		oldCellEle = cell.getOdfElement();
+		newCellEle = (TableTableCellElementBase) oldCellEle.cloneNode(true);
+		copyForeignStyleRef(newCellEle, cell.getOwnerDocument());
+
+		return template;
+	}
+
 }

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java?rev=1406035&r1=1406034&r2=1406035&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/Table.java Tue Nov  6 03:33:11 2012
@@ -73,6 +73,7 @@ import org.odftoolkit.simple.Component;
 import org.odftoolkit.simple.Document;
 import org.odftoolkit.simple.SpreadsheetDocument;
 import org.odftoolkit.simple.TextDocument;
+import org.odftoolkit.simple.text.Paragraph;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -1013,6 +1014,204 @@ public class Table extends Component {
 		return newTEle;
 	}
 
+	/**
+	 * Apply the formatting specified in the template to corresponding table
+	 * cells.
+	 * <p>
+	 * A table can only be formatted as one type of styles: even-odd-rows or
+	 * even-odd-columns. The rule is to check the style of odd rows and even
+	 * rows in the template, only if they have one different properties, table:
+	 * style-name or table:paragraph-style-name, the table template will be
+	 * treated as a even-odd-columns styled table.
+	 * <p>
+	 * If one style in the template is null, the style of corresponding cells
+	 * will be removed. An empty template can be used to remove all the styles
+	 * in a table.
+	 * 
+	 * @param template
+	 * @throws IllegalArgumentException
+	 *             if the given template is null
+	 * @throws Exception
+	 *             if content DOM could not be initialized
+	 */
+	public void applyStyle(TableTemplate template) throws Exception {
+
+		if (template == null)
+			throw new IllegalArgumentException(
+					"The template cannot null to be applied to a table.");
+
+		Document doc = this.getOwnerDocument();
+		OdfOfficeAutomaticStyles styles = doc.getContentDom()
+				.getAutomaticStyles();
+
+		// decide row style or column style
+		boolean isEqualTableStyle = true;
+		boolean isEqualParaStyle = true;
+		OdfStyle evenRowsTableStyle = styles.getStyle(template
+				.getTableEvenRowsTableStyle(), OdfStyleFamily.TableCell);
+		OdfStyle oddRowsTableStyle = styles.getStyle(template
+				.getTableOddRowsTableStyle(), OdfStyleFamily.TableCell);
+		OdfStyle evenRowsParagraphStyle = styles.getStyle(template
+				.getTableEvenRowsParagraphStyle(), OdfStyleFamily.Paragraph);
+		OdfStyle oddRowsParagraphStyle = styles.getStyle(template
+				.getTableOddRowsParagraphStyle(), OdfStyleFamily.Paragraph);
+		if (evenRowsTableStyle != null || oddRowsTableStyle != null)
+			isEqualTableStyle = evenRowsTableStyle.compareTo(oddRowsTableStyle) == 0;
+		if (evenRowsParagraphStyle != null || oddRowsParagraphStyle != null)
+			isEqualParaStyle = evenRowsParagraphStyle
+					.compareTo(oddRowsParagraphStyle) == 0;
+
+		Iterator<Row> rowIterator = this.getRowIterator();
+
+		if (rowIterator.hasNext()) { // first row
+			Row currentRow = rowIterator.next();
+			String firstCellTableStyle = template
+					.getExtendedTableStyleByType(TableTemplate.ExtendedStyleType.FIRSTROWSTARTCOLUM);
+			String firstCellParagraphStyle = template
+					.getExtendedParagraphStyleByType(TableTemplate.ExtendedStyleType.FIRSTROWSTARTCOLUM);
+			String lastCellTableStyle = template
+					.getExtendedTableStyleByType(TableTemplate.ExtendedStyleType.FIRSTROWENDCOLUMN);
+			String lastCellParagraphStyle = template
+					.getExtendedParagraphStyleByType(TableTemplate.ExtendedStyleType.FIRSTROWENDCOLUMN);
+			String evenCellTableStyle = template.getTableFirstRowTableStyle();
+			String evenCellParagraphStyle = template
+					.getTableFirstRowParagraphStyle();
+			String oddCellTableStyle = evenCellTableStyle;
+			String oddCellParagraphStyle = evenCellParagraphStyle;
+
+			applyStyleToRow(template, currentRow, firstCellTableStyle,
+					oddCellTableStyle, evenCellTableStyle, lastCellTableStyle,
+					firstCellParagraphStyle, oddCellParagraphStyle,
+					evenCellParagraphStyle, lastCellParagraphStyle);
+
+			int line = 0;
+			while (rowIterator.hasNext()) {
+				currentRow = rowIterator.next();
+				line++;
+
+				if (!rowIterator.hasNext()) { // last row
+					firstCellTableStyle = template
+							.getExtendedTableStyleByType(TableTemplate.ExtendedStyleType.LASTROWSTARTCOLUMN);
+					firstCellParagraphStyle = template
+							.getExtendedParagraphStyleByType(TableTemplate.ExtendedStyleType.LASTROWSTARTCOLUMN);
+					lastCellTableStyle = template
+							.getExtendedTableStyleByType(TableTemplate.ExtendedStyleType.LASTROWENDCOLUMN);
+					lastCellParagraphStyle = template
+							.getExtendedParagraphStyleByType(TableTemplate.ExtendedStyleType.LASTROWENDCOLUMN);
+					oddCellTableStyle = evenCellTableStyle = template
+							.getTableLastRowTableStyle();
+					oddCellParagraphStyle = evenCellParagraphStyle = template
+							.getTableLastRowParagraphStyle();
+
+					applyStyleToRow(template, currentRow, firstCellTableStyle,
+							oddCellTableStyle, evenCellTableStyle,
+							lastCellTableStyle, firstCellParagraphStyle,
+							oddCellParagraphStyle, evenCellParagraphStyle,
+							lastCellParagraphStyle);
+
+				} else if (!isEqualTableStyle || !isEqualParaStyle) {
+					firstCellTableStyle = template
+							.getTableFirstColumnTableStyle();
+					firstCellParagraphStyle = template
+							.getTableFirstColumnParagraphStyle();
+					lastCellTableStyle = template
+							.getTableLastColumnTableStyle();
+					lastCellParagraphStyle = template
+							.getTableLastColumnParagraphStyle();
+
+					if (line % 2 != 0) { // odd row
+
+						oddCellTableStyle = evenCellTableStyle = template
+								.getTableOddRowsTableStyle();
+						oddCellParagraphStyle = evenCellParagraphStyle = template
+								.getTableOddRowsParagraphStyle();
+						applyStyleToRow(template, currentRow,
+								firstCellTableStyle, oddCellTableStyle,
+								evenCellTableStyle, lastCellTableStyle,
+								firstCellParagraphStyle, oddCellParagraphStyle,
+								evenCellParagraphStyle, lastCellParagraphStyle);
+					} else { // even row
+
+						oddCellTableStyle = evenCellTableStyle = template
+								.getTableEvenRowsTableStyle();
+						oddCellParagraphStyle = evenCellParagraphStyle = template
+								.getTableEvenRowsParagraphStyle();
+
+						applyStyleToRow(template, currentRow,
+								firstCellTableStyle, oddCellTableStyle,
+								evenCellTableStyle, lastCellTableStyle,
+								firstCellParagraphStyle, oddCellParagraphStyle,
+								evenCellParagraphStyle, lastCellParagraphStyle);
+					}
+
+				} else { // even&odd column
+					firstCellTableStyle = template
+							.getTableFirstColumnTableStyle();
+					firstCellParagraphStyle = template
+							.getTableFirstColumnParagraphStyle();
+					lastCellTableStyle = template
+							.getTableLastColumnTableStyle();
+					lastCellParagraphStyle = template
+							.getTableLastColumnParagraphStyle();
+					evenCellTableStyle = template
+							.getTableEvenColumnsTableStyle();
+					evenCellParagraphStyle = template
+							.getTableEvenColumnsParagraphStyle();
+					oddCellTableStyle = template.getTableOddColumnsTableStyle();
+					oddCellParagraphStyle = template
+							.getTableOddColumnsParagraphStyle();
+					applyStyleToRow(template, currentRow, firstCellTableStyle,
+							oddCellTableStyle, evenCellTableStyle,
+							lastCellTableStyle, firstCellParagraphStyle,
+							oddCellParagraphStyle, evenCellParagraphStyle,
+							lastCellParagraphStyle);
+				}
+			}
+
+		}
+
+	}
+
+	private void applyStyleToRow(TableTemplate template, Row row,
+			String firstCellTableStyle, String oddCellTableStyle,
+			String evenCellTableStyle, String lastCellTableStyle,
+			String firstCellParagraphStyle, String oddCellParagraphStyle,
+			String evenCellParagraphStyle, String lastCellParagraphStyle) {
+		int cellIndex = 0;
+		int mnRepeatedIndex = row.getRowsRepeatedNumber();
+		int lastIndex = row.getCellCount() - 1;
+		Cell cell;
+		String tableStyle, paraStyle;
+		for (Node n : new DomNodeList(row.getOdfElement().getChildNodes())) {
+			if (n instanceof TableTableCellElementBase) {
+				cell = this.getCellInstance((TableTableCellElementBase) n, 0,
+						mnRepeatedIndex);
+				if (cell.getColumnsRepeatedNumber() > 1)
+					lastIndex -= cell.getColumnsRepeatedNumber() - 1;
+				if (cellIndex == 0) {
+					tableStyle = firstCellTableStyle;
+					paraStyle = firstCellParagraphStyle;
+				} else if (cellIndex == lastIndex) {
+					tableStyle = lastCellTableStyle;
+					paraStyle = lastCellParagraphStyle;
+				} else if (cellIndex % 2 == 0) {
+					tableStyle = evenCellTableStyle;
+					paraStyle = evenCellParagraphStyle;
+				} else {
+					tableStyle = oddCellTableStyle;
+					paraStyle = oddCellParagraphStyle;
+				}
+				cell.setCellStyleName(tableStyle);
+				Iterator<Paragraph> paraIterator = cell.getParagraphIterator();
+				while (paraIterator.hasNext()) {
+					Paragraph t = paraIterator.next();
+					t.getOdfElement().setStyleName(paraStyle);
+				}
+				cellIndex++;
+			}
+		}
+	}
+
 	private static String getUniqueTableName(TableContainer container) {
 		List<Table> tableList = container.getTableList();
 		boolean notUnique = true;

Added: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/TableTemplate.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/TableTemplate.java?rev=1406035&view=auto
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/TableTemplate.java (added)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/table/TableTemplate.java Tue Nov  6 03:33:11 2012
@@ -0,0 +1,765 @@
+/* 
+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.odftoolkit.simple.table;
+
+import java.util.HashMap;
+
+import org.odftoolkit.odfdom.dom.element.table.TableBodyElement;
+import org.odftoolkit.odfdom.dom.element.table.TableEvenColumnsElement;
+import org.odftoolkit.odfdom.dom.element.table.TableEvenRowsElement;
+import org.odftoolkit.odfdom.dom.element.table.TableFirstColumnElement;
+import org.odftoolkit.odfdom.dom.element.table.TableFirstRowElement;
+import org.odftoolkit.odfdom.dom.element.table.TableLastColumnElement;
+import org.odftoolkit.odfdom.dom.element.table.TableLastRowElement;
+import org.odftoolkit.odfdom.dom.element.table.TableOddColumnsElement;
+import org.odftoolkit.odfdom.dom.element.table.TableOddRowsElement;
+import org.odftoolkit.odfdom.dom.element.table.TableTableTemplateElement;
+import org.w3c.dom.NodeList;
+
+/**
+ * TableTemplate represents the table template feature in ODF documents.
+ * <p>
+ * Besides the seven types of style (first row, first column, last row, last
+ * column, even/odd rows,even/odd columns and body) defined by
+ * <code>table:table-template</code> in ODF 1.2, TableTemplate provide extension
+ * mechnism, which allows user to specify style value to additional cells. For
+ * example, in ODF 1.2 the four coner cells can only inherit styles from the
+ * colum or row they are existing, but through TableTemplate.ExtendedStyleType,
+ * they can reference any style defined in the document.
+ * <p>
+ * TableTemplate provide method to get/set a set of references to table cell
+ * styles that specify the formatting to be used on a table.
+ * 
+ */
+public class TableTemplate {
+
+	private TableTableTemplateElement mElement;
+
+	/**
+	 * Extended style types supported by table template.
+	 * 
+	 */
+	public static enum ExtendedStyleType {
+		FIRSTROWSTARTCOLUM, FIRSTROWENDCOLUMN, LASTROWSTARTCOLUMN, LASTROWENDCOLUMN
+	}
+
+	HashMap<ExtendedStyleType, String> extendedTableStyleMap;
+	HashMap<ExtendedStyleType, String> extendedParagraphStyleMap;
+
+	/**
+	 * Create an instance of TableTemplate
+	 */
+	protected TableTemplate() {
+	}
+
+	/**
+	 * Create an instance of TableTemplate from an element
+	 * <table:table-template>
+	 * 
+	 * @param tableTableTemplate
+	 *            - the element of table:table-template
+	 */
+	public TableTemplate(TableTableTemplateElement tableTableTemplate) {
+		mElement = tableTableTemplate;
+	}
+
+	/**
+	 * Return the name of table template.
+	 * <p>
+	 * Null will be returned if there is no table template name setting.
+	 * 
+	 * @return the table template name.
+	 */
+	public String getTableName() {
+		return mElement.getTableNameAttribute();
+	}
+
+	/**
+	 * Set the name of table template.
+	 * <p>
+	 * If the parameter <code>tableNameValue</code> is null, the table template
+	 * name definition will be removed.
+	 * 
+	 * @param tableNameValue
+	 *            - the table template name
+	 */
+	public void setTableName(String tableNameValue) {
+		mElement.setTableNameAttribute(tableNameValue);
+	}
+
+	/**
+	 * get the value of table style specified by type.
+	 * <p>
+	 * Null will be returned if there is no such extended style type setting.
+	 * 
+	 * @param type
+	 *            - style type
+	 * @return the style name referenced by this style type
+	 */
+	public String getExtendedTableStyleByType(ExtendedStyleType type) {
+		if (extendedTableStyleMap == null)
+			return null;
+		return extendedTableStyleMap.get(type);
+	}
+
+	/**
+	 * get the value of paragraph style specified by type.
+	 * <p>
+	 * Null will be returned if there is no such extended style type setting.
+	 * 
+	 * @param type
+	 *            - extended style type
+	 * @return the style name referenced by this style type
+	 */
+	public String getExtendedParagraphStyleByType(ExtendedStyleType type) {
+		if (extendedParagraphStyleMap == null)
+			return null;
+		return extendedParagraphStyleMap.get(type);
+	}
+
+	/**
+	 * set the value of table style and paragraph style specified by type.
+	 * 
+	 * @param type
+	 *            - extended style type
+	 * @param tableStyle
+	 *            - table style name
+	 * @param paraStyle
+	 *            - paragraph style name
+	 */
+	public void setExtendedStyleByType(ExtendedStyleType type,
+			String tableStyle, String paraStyle) {
+
+		if (extendedTableStyleMap == null) {
+			extendedTableStyleMap = new HashMap<ExtendedStyleType, String>(5);
+		}
+		if (extendedParagraphStyleMap == null) {
+			extendedParagraphStyleMap = new HashMap<ExtendedStyleType, String>(
+					5);
+		}
+		extendedTableStyleMap.put(type, tableStyle);
+		extendedParagraphStyleMap.put(type, paraStyle);
+	}
+
+	/**
+	 * get the value of table style of body.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of body.
+	 */
+	public String getTableBodyTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableBodyElement.ELEMENT_NAME.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableBodyElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of body.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of body.
+	 */
+	public String getTableBodyParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableBodyElement.ELEMENT_NAME.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableBodyElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of table style of even columns.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of even columns.
+	 */
+	public String getTableEvenColumnsTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableEvenColumnsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableEvenColumnsElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of even columns.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of even columns.
+	 */
+	public String getTableEvenColumnsParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableEvenColumnsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableEvenColumnsElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of table style of even rows.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of even rows.
+	 */
+	public String getTableEvenRowsTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableEvenRowsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableEvenRowsElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of even rows.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of even rows.
+	 */
+	public String getTableEvenRowsParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableEvenRowsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableEvenRowsElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of table style of first colum.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of first column.
+	 */
+	public String getTableFirstColumnTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableFirstColumnElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableFirstColumnElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of first column.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of first column.
+	 */
+	public String getTableFirstColumnParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableFirstColumnElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableFirstColumnElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of table style of first row.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of first row.
+	 */
+	public String getTableFirstRowTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableFirstRowElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableFirstRowElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of first row.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of first row.
+	 */
+	public String getTableFirstRowParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableFirstRowElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableFirstRowElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of table style of last column.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of last column.
+	 */
+	public String getTableLastColumnTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableLastColumnElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableLastColumnElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of last column.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of last column.
+	 */
+	public String getTableLastColumnParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableLastColumnElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableLastColumnElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of table style of last row.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of last row.
+	 */
+	public String getTableLastRowTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableLastRowElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableLastRowElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of last row.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of last row.
+	 */
+	public String getTableLastRowParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableLastRowElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableLastRowElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of table style of odd columns.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of odd columns.
+	 */
+	public String getTableOddColumnsTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableOddColumnsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableOddColumnsElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of odd columns.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of odd columns.
+	 */
+	public String getTableOddColumnsParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableOddColumnsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableOddColumnsElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of table style of odd rows.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by table style of odd rows.
+	 */
+	public String getTableOddRowsTableStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableOddRowsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableOddRowsElement) elements.item(0))
+					.getTableStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * get the value of paragraph style of odd rows.
+	 * <p>
+	 * Null will be returned if there is no such style setting.
+	 * 
+	 * @return the style name referenced by paragraph style of odd rows.
+	 */
+	public String getTableOddRowsParagraphStyle() {
+		NodeList elements = mElement
+				.getElementsByTagName(TableOddRowsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			return ((TableOddRowsElement) elements.item(0))
+					.getTableParagraphStyleNameAttribute();
+		}
+		return null;
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of body.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in body will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in body will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by body
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by body
+	 */
+	public void setTableBodyStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableBodyElement.ELEMENT_NAME.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableBodyElement ele = (TableBodyElement) elements.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableBodyElement ele = mElement
+					.newTableBodyElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of even columns.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in even columns will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in even columns will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by even columns
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by even columns
+	 */
+	public void setTableEvenColumnsStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableEvenColumnsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableEvenColumnsElement ele = (TableEvenColumnsElement) elements
+					.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableEvenColumnsElement ele = mElement
+					.newTableEvenColumnsElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of even rows.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in even rows will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in even rows will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by even rows
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by even rows
+	 */
+	public void setTableEvenRowsStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableEvenRowsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableEvenRowsElement ele = (TableEvenRowsElement) elements.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableEvenRowsElement ele = mElement
+					.newTableEvenRowsElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of first column.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in first column will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in first column will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by first column
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by first column
+	 */
+	public void setTableFirstColumnStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableFirstColumnElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableFirstColumnElement ele = (TableFirstColumnElement) elements
+					.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableFirstColumnElement ele = mElement
+					.newTableFirstColumnElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of first row.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in first row will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in first row will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by first row
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by first row
+	 */
+	public void setTableFirstRowStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableFirstRowElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableFirstRowElement ele = (TableFirstRowElement) elements.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableFirstRowElement ele = mElement
+					.newTableFirstRowElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of last column.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in last column will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in last column will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by last column
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by last column
+	 */
+	public void setTableLastColumnStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableLastColumnElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableLastColumnElement ele = (TableLastColumnElement) elements
+					.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableLastColumnElement ele = mElement
+					.newTableLastColumnElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of last row.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in last row will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in last row will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by last row
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by last row
+	 */
+	public void setTableLastRowStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableLastRowElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableLastRowElement ele = (TableLastRowElement) elements.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableLastRowElement ele = mElement
+					.newTableLastRowElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of odd columns.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in odd columns will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in odd columns will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by odd columns
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by odd columns
+	 */
+	public void setTableOddColumnsStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableOddColumnsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableOddColumnsElement ele = (TableOddColumnsElement) elements
+					.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableOddColumnsElement ele = mElement
+					.newTableOddColumnsElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+	/**
+	 * Set the value of table style and paragraph style of odd rows.
+	 * <p>
+	 * If the parameter <code>tableStyleNameValue</code> is null, the table
+	 * style name definition in odd rows will be removed. If the parameter
+	 * <code>tableParagraphStyleNameValue</code> is null, the paragraph style
+	 * name definition in odd rows will be removed.
+	 * 
+	 * @param tableStyleNameValue
+	 *            - table style name referenced by odd rows
+	 * @param tableParagraphStyleNameValue
+	 *            - paragraph style name referenced by odd rows
+	 */
+	public void setTableOddRowsStyle(String tableStyleNameValue,
+			String tableParagraphStyleNameValue) {
+		NodeList elements = mElement
+				.getElementsByTagName(TableOddRowsElement.ELEMENT_NAME
+						.getQName());
+		if (elements != null && elements.getLength() > 0) {
+			TableOddRowsElement ele = (TableOddRowsElement) elements.item(0);
+			ele.setTableStyleNameAttribute(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		} else {
+			TableOddRowsElement ele = mElement
+					.newTableOddRowsElement(tableStyleNameValue);
+			ele
+					.setTableParagraphStyleNameAttribute(tableParagraphStyleNameValue);
+		}
+	}
+
+}

Added: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableTemplateTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableTemplateTest.java?rev=1406035&view=auto
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableTemplateTest.java (added)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/table/TableTemplateTest.java Tue Nov  6 03:33:11 2012
@@ -0,0 +1,514 @@
+/* 
+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.odftoolkit.simple.table;
+
+import java.util.Iterator;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import junit.framework.Assert;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.odftoolkit.odfdom.dom.element.table.TableTableCellElementBase;
+import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
+import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeAutomaticStyles;
+import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
+import org.odftoolkit.simple.Document;
+import org.odftoolkit.simple.TextDocument;
+import org.odftoolkit.simple.text.Paragraph;
+import org.odftoolkit.simple.utils.ResourceUtilities;
+import org.w3c.dom.Node;
+
+public class TableTemplateTest {
+
+	private static final String TEST_TABLE_FILE_NAME = "TestTableTemplate.odt";
+	private static final String TEST_TEMPLATE_FILE_NAME = "TableTemplate.odt";
+
+	Document doc;
+
+	@Before
+	public void loadTestDocument() {
+		try {
+			doc = TextDocument.loadDocument(ResourceUtilities
+					.getTestResourceAsStream(TEST_TABLE_FILE_NAME));
+
+		} catch (Exception e) {
+			Logger.getLogger(CellStyleHandlerTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+		}
+	}
+
+	 @Test
+	public void testLoadTableTemplates() {
+		try {
+
+			TableTemplate template = doc.LoadTableTemplateFromForeignTable(
+					ResourceUtilities
+							.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+					"ColumnStyledTable");
+			checkLoadTemplateResults(template, "ColumnStyledTable");
+
+			template = doc.LoadTableTemplateFromForeignTable(ResourceUtilities
+					.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+					"RowStyledTable");
+			checkLoadTemplateResults(template, "RowStyledTable");
+
+			try {
+				template = doc
+						.LoadTableTemplateFromForeignTable(
+								ResourceUtilities
+										.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+								"TableR4C3");
+				Assert.fail("Fail to load this template.");
+			} catch (IllegalStateException e) {
+				Assert.assertTrue(true);
+			}
+
+		} catch (Exception e) {
+			Logger.getLogger(TableTemplateTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+		}
+	}
+
+	 @Test
+	public void testApplyColumnStyledTableTemplate() {
+		try {
+			TableTemplate template = doc.LoadTableTemplateFromForeignTable(
+					ResourceUtilities
+							.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+					"ColumnStyledTable");
+
+			Table table = doc.getTableByName("DataTable1");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			table = doc.getTableByName("Table_4*4");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			table = doc.getTableByName("Table_4*3");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			table = doc.getTableByName("Table_4*2");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			table = doc.getTableByName("Table_4*1");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			table = doc.getTableByName("Table_1*3");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			table = doc.getTableByName("Table_1*2");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			table = doc.getTableByName("Table_1*1");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			// apply to empty table
+			table = doc.addTable(8, 8);
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, false);
+
+			doc
+					.save(ResourceUtilities
+							.newTestOutputFile("TestApplyColumnStyledTableTemplate.odt"));
+		} catch (Exception e) {
+			Logger.getLogger(TableTemplateTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+		}
+	}
+
+	 @Test
+	public void testApplyRowStyledTableTemplate() {
+		try {
+			TableTemplate template = doc.LoadTableTemplateFromForeignTable(
+					ResourceUtilities
+							.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+					"RowStyledTable");
+
+			Table table = doc.getTableByName("DataTable1");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, true);
+
+			table = doc.getTableByName("Table_3*4");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, true);
+
+			table = doc.getTableByName("Table_2*4");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, true);
+
+			table = doc.getTableByName("Table_1*4");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, true);
+
+			table = doc.getTableByName("Table2_1*1");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, true);
+
+			table = doc.getTableByName("Table2_1*2");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, true);
+
+			table = doc.getTableByName("Table2_1*3");
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, true);
+
+			// apply to empty table
+			table = doc.addTable(8, 8);
+			table.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table, true);
+
+			doc.save(ResourceUtilities
+					.newTestOutputFile("TestApplyRowStyledTableTemplate.odt"));
+		} catch (Exception e) {
+			Logger.getLogger(TableTemplateTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+		}
+	}
+
+	 @Test
+	public void testReApplyTableTemplateAfterChanges() {
+		try {
+			TableTemplate template = doc.LoadTableTemplateFromForeignTable(
+					ResourceUtilities
+							.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+					"RowStyledTable");
+			Table table1 = doc.getTableByName("DataTable1");
+			Table table2 = doc.getTableByName("DataTable2");
+
+			table1.applyStyle(template);
+			table2.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table1, true);
+			checkApplyRowOrColumnStyledTableTemplate(table2, true);
+
+			template = doc.LoadTableTemplateFromForeignTable(ResourceUtilities
+					.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+					"ColumnStyledTable");
+			table1.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table1, false);
+
+			table2.removeRowsByIndex(7, 2);
+			table2.appendColumns(2);
+			table2.applyStyle(template);
+			checkApplyRowOrColumnStyledTableTemplate(table2, false);
+
+			doc.save(ResourceUtilities
+					.newTestOutputFile("TestReApplyTableTemplate.odt"));
+
+		} catch (Exception e) {
+			Logger.getLogger(TableTemplateTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+		}
+	}
+
+	 @Test
+	public void testApplyTemplateWithEmptyCells() {
+		try {
+			TableTemplate template = doc.LoadTableTemplateFromForeignTable(
+					ResourceUtilities
+							.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+					"RowStyledEmptyTable");
+			Table table = doc.getTableByName("DataTable1");
+			table.applyStyle(template);
+			checkApplyTemplateWithEmptyCells(table, true);
+
+			template = doc.LoadTableTemplateFromForeignTable(ResourceUtilities
+					.getTestResourceAsStream(TEST_TEMPLATE_FILE_NAME),
+					"ColumnStyledEmptyTable");
+			table = doc.getTableByName("DataTable2");
+			table.applyStyle(template);
+			checkApplyTemplateWithEmptyCells(table, false);
+
+			doc
+					.save(ResourceUtilities
+							.newTestOutputFile("TestApplyTemplateWithNoParagraphStyle.odt"));
+		} catch (Exception e) {
+			Logger.getLogger(TableTemplateTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+		}
+	}
+
+	@Test
+	public void testApplyEmptyTemplate() {
+		try {
+			TableTemplate template = new TableTemplate(doc.getStylesDom()
+					.getOfficeStyles().newTableTableTemplateElement("", "", "",
+							"", ""));
+			doc.getTableByName("DataTable1").applyStyle(template);
+			doc.save(ResourceUtilities
+					.newTestOutputFile("TestApplyEmptyTemplate.odt"));
+		} catch (Exception e) {
+			Logger.getLogger(TableTemplateTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+		}
+
+	}
+
+	private void checkFormatResultsByRow(Table table, Row row,
+			String firstTableStyle, String oddTableStyle,
+			String evenTableStyle, String lastTableStyle,
+			String firstParaStyle, String oddParaStyle, String evenParaStyle,
+			String lastParaStyle) {
+		int cellIndex = 0;
+		int mnRepeatedIndex = row.getRowsRepeatedNumber();
+		Cell cell;
+		String paraStyle;
+		for (Node n : new DomNodeList(row.getOdfElement().getChildNodes())) {
+			if (n instanceof TableTableCellElementBase) {
+				cell = table.getCellInstance((TableTableCellElementBase) n, 0,
+						mnRepeatedIndex);
+				int lastIndex = row.getCellCount() - 1;
+				if (cell.getColumnsRepeatedNumber() > 1)
+					lastIndex -= cell.getColumnsRepeatedNumber();
+				if (cellIndex == 0) {
+					Assert.assertEquals(cell.getCellStyleName(),
+							firstTableStyle);
+					paraStyle = firstParaStyle;
+				} else if (cellIndex == lastIndex) {
+					Assert
+							.assertEquals(cell.getCellStyleName(),
+									lastTableStyle);
+					paraStyle = lastParaStyle;
+				} else if (cellIndex % 2 == 0) {
+					Assert
+							.assertEquals(cell.getCellStyleName(),
+									evenTableStyle);
+					paraStyle = evenParaStyle;
+				} else {
+					Assert.assertEquals(cell.getCellStyleName(), oddTableStyle);
+					paraStyle = oddParaStyle;
+				}
+				Iterator<Paragraph> paraIterator = cell.getParagraphIterator();
+				while (paraIterator.hasNext()) {
+					Paragraph t = paraIterator.next();
+					if (paraStyle != null && paraStyle != "") {
+						Assert.assertEquals(t.getOdfElement().getStyleName(),
+								paraStyle);
+					}
+				}
+				cellIndex++;
+			}
+		}
+	}
+
+	private void checkApplyRowOrColumnStyledTableTemplate(Table table,
+			boolean isRowStyle) {
+
+		Iterator<Row> rowIterator = table.getRowIterator();
+		if (rowIterator.hasNext()) { // first row
+			Row currentRow = rowIterator.next();
+			if (isRowStyle) {
+				checkFormatResultsByRow(table, currentRow, "RowStyledTable.A1",
+						"RowStyledTable.B1", "RowStyledTable.B1",
+						"RowStyledTable.E1", "P1", "P2", "P2", "P5");
+			} else {
+				checkFormatResultsByRow(table, currentRow,
+						"ColumnStyledTable.A1", "ColumnStyledTable.B1",
+						"ColumnStyledTable.B1", "ColumnStyledTable.E1", "P1",
+						"P2", "P2", "P5");
+			}
+
+			int line = 0;
+			while (rowIterator.hasNext()) {
+				currentRow = rowIterator.next();
+				line++;
+				if (!rowIterator.hasNext()) { // last row
+
+					if (isRowStyle) {
+						checkFormatResultsByRow(table, currentRow,
+								"RowStyledTable.A5", "RowStyledTable.B5",
+								"RowStyledTable.B5", "RowStyledTable.E5", "P3",
+								"P2", "P2", "P4");
+					} else {
+						checkFormatResultsByRow(table, currentRow,
+								"ColumnStyledTable.A5", "ColumnStyledTable.B5",
+								"ColumnStyledTable.B5", "ColumnStyledTable.E5",
+								"P3", "P2", "P2", "P4");
+					}
+
+				} else if (isRowStyle) {
+					if (line % 2 != 0) { // odd row
+						checkFormatResultsByRow(table, currentRow,
+								"RowStyledTable.A2", "RowStyledTable.B2",
+								"RowStyledTable.B2", "RowStyledTable.E2", "P2",
+								"P2", "P2", "P2");
+					} else { // even row
+						checkFormatResultsByRow(table, currentRow,
+								"RowStyledTable.A2", "RowStyledTable.B3",
+								"RowStyledTable.B3", "RowStyledTable.E2", "P2",
+								"P2", "P2", "P2");
+					}
+				} else { // even&odd column
+					checkFormatResultsByRow(table, currentRow,
+							"ColumnStyledTable.A2", "ColumnStyledTable.B2",
+							"ColumnStyledTable.C2", "ColumnStyledTable.E2",
+							"P2", "P2", "P2", "P2");
+				}
+			}
+		}
+
+	}
+
+	private void checkApplyTemplateWithEmptyCells(Table table,
+			boolean isRowStyle) {
+
+		Iterator<Row> rowIterator = table.getRowIterator();
+		if (rowIterator.hasNext()) { // first row
+			Row currentRow = rowIterator.next();
+			if (isRowStyle) {
+				checkFormatResultsByRow(table, currentRow,
+						"RowStyledEmptyTable.A1", "RowStyledEmptyTable.B1",
+						"RowStyledEmptyTable.B1", "RowStyledEmptyTable.E1",
+						"P1", "P2", "P2", "P5");
+			} else {
+				checkFormatResultsByRow(table, currentRow,
+						"ColumnStyledEmptyTable.A1",
+						"ColumnStyledEmptyTable.B1",
+						"ColumnStyledEmptyTable.B1",
+						"ColumnStyledEmptyTable.E1", "P1", "P2", "P2", "P5");
+			}
+
+			int line = 0;
+			while (rowIterator.hasNext()) {
+				currentRow = rowIterator.next();
+				line++;
+				if (!rowIterator.hasNext()) { // last row
+
+					if (isRowStyle) {
+						checkFormatResultsByRow(table, currentRow,
+								"RowStyledEmptyTable.A5",
+								"RowStyledEmptyTable.B5",
+								"RowStyledEmptyTable.B5",
+								"RowStyledEmptyTable.E5", "P3", "P2", "P2",
+								"P4");
+					} else {
+						checkFormatResultsByRow(table, currentRow,
+								"ColumnStyledEmptyTable.A5",
+								"ColumnStyledEmptyTable.B5",
+								"ColumnStyledEmptyTable.B5",
+								"ColumnStyledEmptyTable.E5", "P3", "P2", "P2",
+								"P4");
+					}
+
+				} else if (isRowStyle) {
+					if (line % 2 != 0) { // odd row
+						checkFormatResultsByRow(table, currentRow,
+								"RowStyledEmptyTable.A2",
+								"RowStyledEmptyTable.B2",
+								"RowStyledEmptyTable.B2",
+								"RowStyledEmptyTable.E2", "P2", "P2", "P2",
+								"P2");
+					} else { // even row
+						checkFormatResultsByRow(table, currentRow,
+								"RowStyledEmptyTable.A2",
+								"RowStyledEmptyTable.B3",
+								"RowStyledEmptyTable.B3",
+								"RowStyledEmptyTable.E2", "P2", "P2", "P2",
+								"P2");
+					}
+				} else { // even&odd column
+					checkFormatResultsByRow(table, currentRow,
+							"ColumnStyledEmptyTable.A2",
+							"ColumnStyledEmptyTable.B2",
+							"ColumnStyledEmptyTable.C2",
+							"ColumnStyledEmptyTable.E2", "P2", "P2", "P2", "P2");
+				}
+			}
+		}
+
+	}
+
+	private void checkLoadTemplateResults(TableTemplate template, String name) {
+		OdfStyle style = null;
+		try {
+			OdfOfficeAutomaticStyles styles = doc.getContentDom()
+					.getAutomaticStyles();
+			style = styles.getStyle(name + ".A1", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle(name + ".B1", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle(name + ".E1", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle(name + ".A2", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle(name + ".B2", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle(name + ".E2", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			if (name.contains("Column")) {
+				style = styles.getStyle(name + ".C2", OdfStyleFamily.TableCell);
+				Assert.assertNotNull(style);
+			} else {
+				style = styles.getStyle(name + ".B3", OdfStyleFamily.TableCell);
+				Assert.assertNotNull(style);
+			}
+			style = styles.getStyle(name + ".A5", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle(name + ".B5", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle(name + ".E5", OdfStyleFamily.TableCell);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle("P1", OdfStyleFamily.Paragraph);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle("P2", OdfStyleFamily.Paragraph);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle("P4", OdfStyleFamily.Paragraph);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle("P5", OdfStyleFamily.Paragraph);
+			Assert.assertNotNull(style);
+
+			style = styles.getStyle("P3", OdfStyleFamily.Paragraph);
+			Assert.assertNotNull(style);
+
+		} catch (Exception e) {
+			Logger.getLogger(TableTemplateTest.class.getName()).log(
+					Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+		}
+	}
+
+}

Added: incubator/odf/trunk/simple/src/test/resources/TableTemplate.odt
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/resources/TableTemplate.odt?rev=1406035&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/odf/trunk/simple/src/test/resources/TableTemplate.odt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/odf/trunk/simple/src/test/resources/TestTableTemplate.odt
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/resources/TestTableTemplate.odt?rev=1406035&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/odf/trunk/simple/src/test/resources/TestTableTemplate.odt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message