incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liud...@apache.org
Subject svn commit: r1411086 - in /incubator/odf/trunk/simple/src: main/java/org/odftoolkit/simple/ main/java/org/odftoolkit/simple/text/ test/java/org/odftoolkit/simple/text/
Date Mon, 19 Nov 2012 07:11:01 GMT
Author: liudali
Date: Mon Nov 19 07:11:00 2012
New Revision: 1411086

URL: http://svn.apache.org/viewvc?rev=1411086&view=rev
Log:
fix for ODFTOOLKIT-346, patched by KeJiaYe.

Added:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/ProtectionKeyDigestProvider.java
Modified:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Footer.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Header.java
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Section.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/FooterTest.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/HeaderTest.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java?rev=1411086&r1=1411085&r2=1411086&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.java Mon Nov
19 07:11:00 2012
@@ -47,6 +47,7 @@ import org.odftoolkit.odfdom.dom.element
 import org.odftoolkit.odfdom.dom.element.style.StyleHeaderElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleMasterPageElement;
 import org.odftoolkit.odfdom.dom.element.style.StylePageLayoutPropertiesElement;
+import org.odftoolkit.odfdom.dom.element.style.StyleSectionPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
 import org.odftoolkit.odfdom.dom.element.text.TextIndexBodyElement;
 import org.odftoolkit.odfdom.dom.element.text.TextIndexEntryLinkStartElement;
@@ -442,6 +443,42 @@ public class TextDocument extends Docume
 	}
 
 	/**
+	 * Create an empty section and append it at the end of the text document.
+	 * 
+	 * @param name
+	 *            - specify the section name
+	 * @return an instance of the section
+	 * @throws RuntimeException
+	 *             if content DOM could not be initialized
+	 */
+	public Section appendSection(String name) {
+		TextSectionElement newSectionEle = null;
+		try {
+			OdfContentDom contentDocument = getContentDom();
+			OdfOfficeAutomaticStyles styles = contentDocument
+					.getAutomaticStyles();
+			OdfStyle style = styles.newStyle(OdfStyleFamily.Section);
+			StyleSectionPropertiesElement sProperties = style
+					.newStyleSectionPropertiesElement();
+			sProperties.setTextDontBalanceTextColumnsAttribute(false);
+			sProperties.setStyleEditableAttribute(false);
+			StyleColumnsElement columnEle = sProperties
+					.newStyleColumnsElement(1);
+			columnEle.setFoColumnGapAttribute("0in");
+
+			newSectionEle = getContentRoot()
+					.newTextSectionElement("true", name);
+			newSectionEle.setStyleName(style.getStyleNameAttribute());
+			return Section.getInstance(newSectionEle);
+
+		} catch (Exception e) {
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE,
+					null, e);
+			throw new RuntimeException(name + "Section appends failed.", e);
+		}
+	}
+
+	/**
 	 * Get the Standard Page header of this text document.
 	 * 
 	 * @return the Standard Page header of this text document.

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Footer.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Footer.java?rev=1411086&r1=1411085&r2=1411086&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Footer.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Footer.java Mon Nov
19 07:11:00 2012
@@ -25,19 +25,24 @@ import java.util.logging.Logger;
 import org.odftoolkit.odfdom.dom.OdfContentDom;
 import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
 import org.odftoolkit.odfdom.dom.OdfStylesDom;
+import org.odftoolkit.odfdom.dom.element.style.StyleColumnsElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleFooterElement;
+import org.odftoolkit.odfdom.dom.element.style.StyleSectionPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleTableCellPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleTablePropertiesElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleTextPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableCellElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
 import org.odftoolkit.odfdom.dom.element.text.TextPElement;
+import org.odftoolkit.odfdom.dom.element.text.TextSectionElement;
 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.odfdom.pkg.OdfElement;
 import org.odftoolkit.odfdom.pkg.OdfFileDom;
 import org.odftoolkit.simple.Component;
+import org.odftoolkit.simple.Document;
+import org.odftoolkit.simple.TextDocument;
 import org.odftoolkit.simple.common.field.AbstractVariableContainer;
 import org.odftoolkit.simple.common.field.VariableContainer;
 import org.odftoolkit.simple.common.field.VariableField;
@@ -234,4 +239,42 @@ public class Footer extends Component im
 			}
 		}
 	}
+
+	/**
+	 * Create an empty section and append it at the end of the footer.
+	 * 
+	 * @param name
+	 *            - specify the section name
+	 * @return an instance of the section
+	 * @throws RuntimeException
+	 *             if content DOM could not be initialized
+	 */
+	public Section appendSection(String name) {
+		TextSectionElement newSectionEle = null;
+		try {
+			Document doc = (Document) ((OdfFileDom) footerEle
+					.getOwnerDocument()).getDocument();
+			OdfContentDom contentDocument = doc.getContentDom();
+			OdfOfficeAutomaticStyles styles = contentDocument
+					.getAutomaticStyles();
+			OdfStyle style = styles.newStyle(OdfStyleFamily.Section);
+			StyleSectionPropertiesElement sProperties = style
+					.newStyleSectionPropertiesElement();
+			sProperties.setTextDontBalanceTextColumnsAttribute(false);
+			sProperties.setStyleEditableAttribute(false);
+			StyleColumnsElement columnEle = sProperties
+					.newStyleColumnsElement(1);
+			columnEle.setFoColumnGapAttribute("0in");
+
+			newSectionEle = footerEle.newTextSectionElement("true", name);
+			newSectionEle.setStyleName(style.getStyleNameAttribute());
+			return Section.getInstance(newSectionEle);
+
+		} catch (Exception e) {
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE,
+					null, e);
+			throw new RuntimeException(name + "Section appends failed.", e);
+		}
+	}
+
 }

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Header.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Header.java?rev=1411086&r1=1411085&r2=1411086&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Header.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Header.java Mon Nov
19 07:11:00 2012
@@ -25,19 +25,24 @@ import java.util.logging.Logger;
 import org.odftoolkit.odfdom.dom.OdfContentDom;
 import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
 import org.odftoolkit.odfdom.dom.OdfStylesDom;
+import org.odftoolkit.odfdom.dom.element.style.StyleColumnsElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleHeaderElement;
+import org.odftoolkit.odfdom.dom.element.style.StyleSectionPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleTableCellPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleTablePropertiesElement;
 import org.odftoolkit.odfdom.dom.element.style.StyleTextPropertiesElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableCellElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
 import org.odftoolkit.odfdom.dom.element.text.TextPElement;
+import org.odftoolkit.odfdom.dom.element.text.TextSectionElement;
 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.odfdom.pkg.OdfElement;
 import org.odftoolkit.odfdom.pkg.OdfFileDom;
 import org.odftoolkit.simple.Component;
+import org.odftoolkit.simple.Document;
+import org.odftoolkit.simple.TextDocument;
 import org.odftoolkit.simple.common.field.AbstractVariableContainer;
 import org.odftoolkit.simple.common.field.VariableContainer;
 import org.odftoolkit.simple.common.field.VariableField;
@@ -235,4 +240,41 @@ public class Header extends Component im
 			}
 		}
 	}
+
+	/**
+	 * Create an empty section and append it at the end of the footer.
+	 * 
+	 * @param name
+	 *            - specify the section name
+	 * @return an instance of the section
+	 * @throws RuntimeException
+	 *             if content DOM could not be initialized
+	 */
+	public Section appendSection(String name) {
+		TextSectionElement newSectionEle = null;
+		try {
+			Document doc = (Document) ((OdfFileDom) headerEle
+					.getOwnerDocument()).getDocument();
+			OdfContentDom contentDocument = doc.getContentDom();
+			OdfOfficeAutomaticStyles styles = contentDocument
+					.getAutomaticStyles();
+			OdfStyle style = styles.newStyle(OdfStyleFamily.Section);
+			StyleSectionPropertiesElement sProperties = style
+					.newStyleSectionPropertiesElement();
+			sProperties.setTextDontBalanceTextColumnsAttribute(false);
+			sProperties.setStyleEditableAttribute(false);
+			StyleColumnsElement columnEle = sProperties
+					.newStyleColumnsElement(1);
+			columnEle.setFoColumnGapAttribute("0in");
+
+			newSectionEle = headerEle.newTextSectionElement("true", name);
+			newSectionEle.setStyleName(style.getStyleNameAttribute());
+			return Section.getInstance(newSectionEle);
+
+		} catch (Exception e) {
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE,
+					null, e);
+			throw new RuntimeException(name + "Section appends failed.", e);
+		}
+	}
 }

Added: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/ProtectionKeyDigestProvider.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/ProtectionKeyDigestProvider.java?rev=1411086&view=auto
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/ProtectionKeyDigestProvider.java
(added)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/ProtectionKeyDigestProvider.java
Mon Nov 19 07:11:00 2012
@@ -0,0 +1,16 @@
+package org.odftoolkit.simple.text;
+
+public interface ProtectionKeyDigestProvider {
+
+	/**
+	 * Generate a digest value of the input key
+	 * 
+	 * @param key
+	 *            -an key required to be digest
+	 * @return the digest result
+	 */
+	public String generateHashKey(String key);
+
+	public String getProtectionKeyDigestAlgorithm();
+
+}

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Section.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Section.java?rev=1411086&r1=1411085&r2=1411086&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Section.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Section.java Mon Nov
19 07:11:00 2012
@@ -19,15 +19,28 @@ under the License.
 
 package org.odftoolkit.simple.text;
 
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Iterator;
+import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import org.odftoolkit.odfdom.dom.OdfDocumentNamespace;
 import org.odftoolkit.odfdom.dom.element.text.TextSectionElement;
 import org.odftoolkit.odfdom.pkg.OdfElement;
 import org.odftoolkit.odfdom.pkg.OdfFileDom;
 import org.odftoolkit.simple.Component;
 import org.odftoolkit.simple.Document;
+import org.odftoolkit.simple.table.AbstractTableContainer;
+import org.odftoolkit.simple.table.Table;
+import org.odftoolkit.simple.table.TableContainer;
+import org.odftoolkit.simple.table.Table.TableBuilder;
+import org.odftoolkit.simple.text.list.AbstractListContainer;
+import org.odftoolkit.simple.text.list.ListContainer;
+import org.odftoolkit.simple.text.list.ListDecorator;
+
+import sun.misc.BASE64Encoder;
 
 /**
  * This class represents section definition in text document. It provides
@@ -36,11 +49,16 @@ import org.odftoolkit.simple.Document;
  * 
  * @since 0.4
  */
-public class Section extends Component implements ParagraphContainer {
+public class Section extends Component implements ParagraphContainer,
+		TableContainer, ListContainer {
 
 	private ParagraphContainerImpl paragraphContainerImpl;
+	private TableContainerImpl tableContainerImpl;
+	private ListContainerImpl listContainerImpl;
+
 	private TextSectionElement mSectionElement;
 	private Document mDocument;
+	private ProtectionKeyDigestProvider protectionKeyDigestProvider;
 
 	private Section(Document doc, TextSectionElement element) {
 		mSectionElement = element;
@@ -129,6 +147,171 @@ public class Section extends Component i
 		return false;
 	}
 
+	/**
+	 * Set the value to specify whether the section is protected.
+	 * <p>
+	 * If this value is set to false, the existing password will be removed at
+	 * the same.
+	 * 
+	 * @param isProtected
+	 *            - "true" represents the section cannot be edited through a
+	 *            user interface. "false" represents the section is allowed to
+	 *            be edited.
+	 */
+	public void setProtected(boolean isProtected) {
+		mSectionElement.setTextProtectedAttribute(isProtected);
+		if (!isProtected && getProtectedPassword() != null)
+			setProtectedWithPassword(null);
+	}
+
+	/**
+	 * Return the value of section which specifies whether the section is
+	 * protected.
+	 * 
+	 * @return whether the section is protected.
+	 */
+	public boolean isProtected() {
+		return mSectionElement.getTextProtectedAttribute();
+	}
+
+	/**
+	 * Set the password which specifies that an authorization is required for
+	 * removing the protection of this section.
+	 * <p>
+	 * If key is empty or null, the attribute of
+	 * <code>text:protection-key</code> and
+	 * <code>text:protection-key-digest-algorithm</code> will be removed.
+	 * <p>
+	 * The authentication procedure can be customized by
+	 * {@link Section#setProtectionKeyDigestProvider(ProtectionKeyDigestProvider)}
+	 * . The default digest algorighom of the protection key is SHA-1:
+	 * {@link http://www.w3.org/2000/09/xmldsig#sha1.}
+	 * 
+	 * @param key
+	 *            -the value of the password.
+	 * 
+	 */
+	public void setProtectedWithPassword(String key) {
+		if (key != null && key.length() > 0) {
+			mSectionElement.setTextProtectionKeyAttribute(generateHashKey(key));
+			mSectionElement
+					.setTextProtectionKeyDigestAlgorithmAttribute(getDigestAlgorithm());
+			setProtected(true);
+		} else {
+			mSectionElement.removeAttributeNS(OdfDocumentNamespace.TEXT
+					.getUri(), "protection-key");
+			mSectionElement.removeAttributeNS(OdfDocumentNamespace.TEXT
+					.getUri(), "protection-key-digest-algorithm");
+		}
+	}
+
+	/**
+	 * Get the protection key of this section.
+	 * 
+	 * @return the protection key of this section
+	 */
+	public String getProtectedPassword() {
+		return mSectionElement.getTextProtectionKeyAttribute();
+	}
+
+	/**
+	 * Get the protection key digest algorithm.
+	 * <p>
+	 * The default value is http://www.w3.org/2000/09/xmldsig#sha1, if no value
+	 * specified.
+	 * 
+	 * @return an IRI that identifies an authentication procedure for removing a
+	 *         protection.
+	 */
+	public String getProtectionKeyDigestAlgorithm() {
+		return mSectionElement.getTextProtectionKeyDigestAlgorithmAttribute();
+	}
+
+	private static class SHA1KeyDigest implements ProtectionKeyDigestProvider {
+
+		private static final String KEY_DIGEST_ALGORITHM = "http://www.w3.org/2000/09/xmldsig#sha1";
+		private static SHA1KeyDigest provider;
+
+		static SHA1KeyDigest getInstance() {
+			if (provider == null)
+				provider = new SHA1KeyDigest();
+			return provider;
+		}
+
+//		@Override
+		public String generateHashKey(String passwd) {
+			String hashKey = null;
+			if (passwd != null && passwd.length() > 0) {
+				MessageDigest md;
+				try {
+					byte[] pwd = new byte[passwd.length() * 2];
+					for (int i = 0; i < passwd.length(); i++) {
+						pwd[2 * i] = (byte) (passwd.charAt(i) & 0xFF);
+						pwd[2 * i + 1] = (byte) (passwd.charAt(i) >> 8);
+					}
+					md = MessageDigest.getInstance("SHA-1");
+					byte[] byteCode = md.digest(pwd);
+					BASE64Encoder encoder = new BASE64Encoder();
+					hashKey = encoder.encode(byteCode);
+				} catch (NoSuchAlgorithmException e) {
+					Logger.getLogger(Section.class.getName(),
+							"Fail to initiate the digest method.");
+				}
+			}
+			return hashKey;
+		}
+
+//		@Override
+		public String getProtectionKeyDigestAlgorithm() {
+			return KEY_DIGEST_ALGORITHM;
+		}
+
+	}
+
+	/**
+	 * Set the provider which provides corresponding protection key digest
+	 * algorithm.
+	 * 
+	 * @param provider
+	 *            - an instance of a protection key digest algorithm provider
+	 */
+	public void setProtectionKeyDigestProvider(
+			ProtectionKeyDigestProvider provider) {
+		protectionKeyDigestProvider = provider;
+	}
+
+	/**
+	 * Get current used provider which provides corresponding protection key
+	 * digest algorithm.
+	 * 
+	 * @return the current used provider.
+	 */
+	public ProtectionKeyDigestProvider getProtectionKeyDigestProvier() {
+		if (protectionKeyDigestProvider == null) {
+			protectionKeyDigestProvider = getDefaultProtectionKeyDigestProvider();
+		}
+		return protectionKeyDigestProvider;
+	}
+
+	/**
+	 * Get the default provider which use SHA-1 standard as the protection key
+	 * digest algorithm.
+	 * 
+	 * @return the default protection key digest algorithm.
+	 */
+	public ProtectionKeyDigestProvider getDefaultProtectionKeyDigestProvider() {
+		return SHA1KeyDigest.getInstance();
+	}
+
+	private String generateHashKey(String passwd) {
+		return getProtectionKeyDigestProvier().generateHashKey(passwd);
+	}
+
+	private String getDigestAlgorithm() {
+		return getProtectionKeyDigestProvier()
+				.getProtectionKeyDigestAlgorithm();
+	}
+
 	@Override
 	public boolean equals(Object obj) {
 		if (!(obj instanceof Section))
@@ -176,4 +359,96 @@ public class Section extends Component i
 	public Iterator<Paragraph> getParagraphIterator() {
 		return getParagraphContainerImpl().getParagraphIterator();
 	}
+
+	// ****************Table support******************//
+	protected TableContainer getTableContainerImpl() {
+		if (tableContainerImpl == null) {
+			tableContainerImpl = new TableContainerImpl();
+		}
+		return tableContainerImpl;
+	}
+
+	private class TableContainerImpl extends AbstractTableContainer {
+
+		public OdfElement getTableContainerElement() {
+			return mSectionElement;
+		}
+	}
+
+//	@Override
+	public Table addTable() {
+		return getTableContainerImpl().addTable();
+	}
+
+//	@Override
+	public Table addTable(int numRows, int numCols) {
+		return getTableContainerImpl().addTable(numRows, numCols);
+	}
+
+//	@Override
+	public TableBuilder getTableBuilder() {
+		return getTableContainerImpl().getTableBuilder();
+	}
+
+//	@Override
+	public Table getTableByName(String name) {
+		return getTableContainerImpl().getTableByName(name);
+	}
+
+//	@Override
+	public OdfElement getTableContainerElement() {
+		return getTableContainerImpl().getTableContainerElement();
+	}
+
+//	@Override
+	public List<Table> getTableList() {
+		return getTableContainerImpl().getTableList();
+	}
+
+	// ****************List support******************//
+
+	private ListContainerImpl getListContainerImpl() {
+		if (listContainerImpl == null) {
+			listContainerImpl = new ListContainerImpl();
+		}
+		return listContainerImpl;
+	}
+
+	private class ListContainerImpl extends AbstractListContainer {
+
+		public OdfElement getListContainerElement() {
+			return mSectionElement;
+		}
+	}
+
+//	@Override
+	public org.odftoolkit.simple.text.list.List addList() {
+		return getListContainerImpl().addList();
+	}
+
+//	@Override
+	public org.odftoolkit.simple.text.list.List addList(ListDecorator decorator) {
+		return getListContainerImpl().addList(decorator);
+	}
+
+//	@Override
+	public void clearList() {
+		getListContainerImpl().clearList();
+	}
+
+//	@Override
+	public OdfElement getListContainerElement() {
+		return getListContainerImpl().getListContainerElement();
+	}
+
+//	@Override
+	public Iterator<org.odftoolkit.simple.text.list.List> getListIterator() {
+		return getListContainerImpl().getListIterator();
+	}
+
+//	@Override
+	public boolean removeList(org.odftoolkit.simple.text.list.List list) {
+		return getListContainerImpl().removeList(list);
+	}
+
 }

Modified: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/FooterTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/FooterTest.java?rev=1411086&r1=1411085&r2=1411086&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/FooterTest.java (original)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/FooterTest.java Mon
Nov 19 07:11:00 2012
@@ -260,4 +260,26 @@ public class FooterTest {
 			Assert.fail(e.getMessage());
 		}
 	}
+
+	@Test
+	public void testAppendNewSection() {
+		try {
+			TextDocument doc = TextDocument.newTextDocument();
+			Footer footer = doc.getFooter();
+			Section sect = footer.appendSection("Section1");
+			Assert.assertNotNull(sect);
+
+			StyleFooterElement styleFoot = footer.getOdfElement();
+			Node nod = styleFoot.getFirstChild();
+			NamedNodeMap nameMap = nod.getAttributes();
+			Node nodtext = nameMap.getNamedItem("text:name");
+			Assert.assertEquals("Section1", nodtext.getNodeValue());
+
+		} catch (Exception e) {
+			Logger.getLogger(HeaderTest.class.getName()).log(Level.SEVERE,
+					null, e);
+			Assert.fail(e.getMessage());
+		}
+
+	}
 }

Modified: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/HeaderTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/HeaderTest.java?rev=1411086&r1=1411085&r2=1411086&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/HeaderTest.java (original)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/HeaderTest.java Mon
Nov 19 07:11:00 2012
@@ -242,4 +242,26 @@ public class HeaderTest {
 			Assert.fail(e.getMessage());
 		}
 	}
+
+	@Test
+	public void testAppendNewSection() {
+		try {
+			TextDocument doc = TextDocument.newTextDocument();
+			Header header = doc.getHeader();
+			Section sect = header.appendSection("Section1");
+			Assert.assertNotNull(sect);
+
+			StyleHeaderElement styleHead = header.getOdfElement();
+			Node nod = styleHead.getFirstChild();
+			NamedNodeMap nameMap = nod.getAttributes();
+			Node nodtext = nameMap.getNamedItem("text:name");
+			Assert.assertEquals("Section1", nodtext.getNodeValue());
+
+		} catch (Exception e) {
+			Logger.getLogger(HeaderTest.class.getName()).log(Level.SEVERE,
+					null, e);
+			Assert.fail(e.getMessage());
+		}
+
+	}
 }

Modified: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java?rev=1411086&r1=1411085&r2=1411086&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java (original)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java Mon
Nov 19 07:11:00 2012
@@ -19,7 +19,11 @@ under the License.
 
 package org.odftoolkit.simple.text;
 
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.Iterator;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
@@ -28,11 +32,21 @@ import junit.framework.Assert;
 
 import org.junit.Test;
 import org.odftoolkit.odfdom.dom.element.draw.DrawImageElement;
+import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
 import org.odftoolkit.odfdom.pkg.OdfElement;
 import org.odftoolkit.odfdom.pkg.OdfPackage;
 import org.odftoolkit.simple.TextDocument;
+import org.odftoolkit.simple.table.Cell;
+import org.odftoolkit.simple.table.Table;
+import org.odftoolkit.simple.text.list.List;
+import org.odftoolkit.simple.text.list.ListDecorator;
+import org.odftoolkit.simple.text.list.ListTest;
+import org.odftoolkit.simple.text.list.NumberDecorator;
+import org.odftoolkit.simple.text.list.OutLineDecorator;
 import org.odftoolkit.simple.utils.ResourceUtilities;
 
+import sun.misc.BASE64Encoder;
+
 public class SectionTest {
 
 	@Test
@@ -185,6 +199,137 @@ public class SectionTest {
 	}
 
 	@Test
+	public void testAppendNewSection() {
+		try {
+			TextDocument doc = TextDocument.newTextDocument();
+			doc.addParagraph("Paragraph1");
+			Section section = doc.appendSection("Section1");
+			section.addParagraph("Here's a section.");
+			Assert.assertNotNull(section);
+			Assert.assertEquals(section.getName(), "Section1");
+			Assert.assertEquals(section.getParagraphByIndex(0, true)
+					.getTextContent(), "Here's a section.");
+
+		} catch (Exception e) {
+			e.printStackTrace();
+			Assert.fail();
+		}
+	}
+
+	private class MyDigestGenerator implements ProtectionKeyDigestProvider {
+
+//		@Override
+		public String generateHashKey(String passwd) {
+
+			String hashKey = null;
+			if (passwd != null && passwd.length() > 0) {
+				MessageDigest md;
+				try {
+					byte[] pwd = passwd.getBytes();
+					md = MessageDigest.getInstance("MD5");
+					byte[] byteCode = md.digest(pwd);
+					BASE64Encoder encoder = new BASE64Encoder();
+					hashKey = encoder.encode(byteCode);
+				} catch (NoSuchAlgorithmException e) {
+					Logger.getLogger(Section.class.getName(),
+							"Fail to initiate the digest method.");
+				}
+			}
+			return hashKey;
+
+		}
+
+//		@Override
+		public String getProtectionKeyDigestAlgorithm() {
+			return "http://www.w3.org/2000/09/#md5";
+		}
+
+	}
+
+	@Test
+	public void testSetProtectionKeyDigestProvider() {
+		try {
+			TextDocument doc = TextDocument.newTextDocument();
+			Section section = doc.appendSection("Section1");
+			section.addParagraph("Section 1");
+			Assert.assertNotNull(section);
+
+			section.setProtectionKeyDigestProvider(new MyDigestGenerator());
+			section.setProtectedWithPassword("12345");
+			Assert.assertEquals(true, section.isProtected());
+			Assert.assertEquals("gnzLDuqKcGxMNKFokfhOew==", section
+					.getProtectedPassword());
+			Assert.assertEquals("http://www.w3.org/2000/09/#md5", section
+					.getProtectionKeyDigestAlgorithm());
+
+			section.setProtectionKeyDigestProvider(null);
+			section.setProtectedWithPassword("12345");
+			Assert.assertEquals(true, section.isProtected());
+			Assert.assertEquals("LyQWujvPXbGDYsrSDKkAiVFavg8=", section
+					.getProtectedPassword());
+			Assert.assertEquals("http://www.w3.org/2000/09/xmldsig#sha1",
+					section.getProtectionKeyDigestAlgorithm());
+
+		} catch (Exception e) {
+			e.printStackTrace();
+			Assert.fail();
+		}
+	}
+
+	@Test
+	public void testSetProtectSection() {
+		try {
+			TextDocument doc = TextDocument.newTextDocument();
+			doc.addParagraph("Paragraph1");
+			String secName = "Section1";
+			Section section = doc.appendSection(secName);
+			Assert.assertNotNull(section);
+
+			section.setProtectedWithPassword("12345");
+			Assert.assertEquals(true, section.isProtected());
+			Assert.assertEquals("LyQWujvPXbGDYsrSDKkAiVFavg8=", section
+					.getProtectedPassword());
+			Assert.assertEquals("http://www.w3.org/2000/09/xmldsig#sha1",
+					section.getProtectionKeyDigestAlgorithm());
+
+			section.setProtectedWithPassword("");
+			Assert.assertEquals(true, section.isProtected());
+			Assert.assertNull(section.getProtectedPassword());
+			Assert.assertEquals("http://www.w3.org/2000/09/xmldsig#sha1",
+					section.getProtectionKeyDigestAlgorithm());
+
+			section.setProtected(false);
+			Assert.assertEquals(false, section.isProtected());
+			Assert.assertNull(section.getProtectedPassword());
+			Assert.assertEquals("http://www.w3.org/2000/09/xmldsig#sha1",
+					section.getProtectionKeyDigestAlgorithm());
+
+			section.setProtectedWithPassword(null);
+			Assert.assertEquals(false, section.isProtected());
+			Assert.assertEquals("http://www.w3.org/2000/09/xmldsig#sha1",
+					section.getProtectionKeyDigestAlgorithm());
+			Assert.assertNull(section.getProtectedPassword());
+
+			section.setProtected(true);
+			Assert.assertEquals(true, section.isProtected());
+			Assert.assertEquals("http://www.w3.org/2000/09/xmldsig#sha1",
+					section.getProtectionKeyDigestAlgorithm());
+			Assert.assertNull(section.getProtectedPassword());
+
+			section.setProtectedWithPassword("12345");
+			Assert.assertEquals(true, section.isProtected());
+			Assert.assertEquals("LyQWujvPXbGDYsrSDKkAiVFavg8=", section
+					.getProtectedPassword());
+			Assert.assertEquals("http://www.w3.org/2000/09/xmldsig#sha1",
+					section.getProtectionKeyDigestAlgorithm());
+
+		} catch (Exception e) {
+			e.printStackTrace();
+			Assert.fail();
+		}
+	}
+
+	@Test
 	public void testRemoveSection() {
 		try {
 			TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
@@ -350,4 +495,159 @@ public class SectionTest {
 			Assert.fail();
 		}
 	}
+
+	@Test
+	public void testAddTable() {
+		try {
+			TextDocument doc = TextDocument.newTextDocument();
+			Section theSec = doc.appendSection("TableSection");
+
+			Table table1 = theSec.addTable();
+			table1.getCellByPosition("A1").addParagraph("A1");
+			OdfElement odfEle = theSec.getTableContainerElement();
+			Assert.assertEquals("A1", Table.getInstance(
+					(TableTableElement) odfEle.getLastChild())
+					.getCellByPosition("A1").getDisplayText());
+
+			Table table2 = theSec.addTable(3, 3);
+			table2.getCellByPosition("C3").addParagraph("C3");
+			odfEle = theSec.getTableContainerElement();
+			Assert.assertEquals("C3", Table.getInstance(
+					(TableTableElement) odfEle.getLastChild())
+					.getCellByPosition("C3").getDisplayText());
+
+			table1.remove();
+			table2.remove();
+			OdfElement odfEle1 = theSec.getTableContainerElement();
+			Assert.assertNull(odfEle1.getLastChild());
+
+		} catch (Exception e) {
+			e.printStackTrace();
+			Assert.fail();
+		}
+	}
+
+	@Test
+	public void testGetTableByName() {
+		try {
+			TextDocument doc = TextDocument.newTextDocument();
+			Section sect = doc.appendSection("TableSeciton");
+			sect.addTable(2, 2);
+			sect.addTable(2, 2);
+			sect.addTable(5, 5).getCellByPosition("E5").setBooleanValue(true);
+			Cell cell = sect.getTableByName("Table3").getCellByPosition(4, 4);
+			Assert.assertNotNull(cell);
+			Assert.assertTrue(cell.getBooleanValue());
+
+		} catch (Exception e) {
+			e.printStackTrace();
+			Assert.fail();
+		}
+	}
+
+	@Test
+	public void testAddRemoveList() {
+
+		try {
+			TextDocument doc = TextDocument.newTextDocument();
+			Section sect = doc.appendSection("List Section");
+
+			ListDecorator numberDecorator = new NumberDecorator(doc);
+			ListDecorator outLineDecorator = new OutLineDecorator(doc);
+			String[] subItemContents = { "sub list item 1", "sub list item 2",
+					"sub list item 3" };
+
+			List list1 = new List(sect);
+			boolean removeResult = sect.removeList(list1);
+			Assert.assertTrue(removeResult);
+
+			List list2 = new List(sect, numberDecorator);
+			list2.addItems(subItemContents);
+			Assert.assertEquals(ListDecorator.ListType.NUMBER, list2.getType());
+			removeResult = sect.removeList(list2);
+			Assert.assertTrue(removeResult);
+
+			List list3 = new List(sect, "Bullet List", null);
+			list3.addItems(subItemContents);
+			Assert.assertEquals(ListDecorator.ListType.BULLET, list3.getType());
+			removeResult = sect.removeList(list3);
+			Assert.assertTrue(removeResult);
+
+			List list4 = sect.addList();
+			removeResult = sect.removeList(list4);
+			Assert.assertTrue(removeResult);
+
+			List list5 = sect.addList(outLineDecorator);
+			list5.addItems(subItemContents);
+			Assert.assertEquals(ListDecorator.ListType.NUMBER, list5.getType());
+			removeResult = sect.removeList(list5);
+			Assert.assertTrue(removeResult);
+		} catch (Exception e) {
+			Logger.getLogger(ListTest.class.getName()).log(Level.SEVERE, null,
+					e);
+			Assert.fail(e.getMessage());
+		}
+	}
+
+	@Test
+	public void testIterateList() {
+		try {
+
+			TextDocument doc = TextDocument.newTextDocument();
+			Section sect = doc.appendSection("List Section");
+
+			ListDecorator numberDecorator = new NumberDecorator(doc);
+			ListDecorator outLineDecorator = new OutLineDecorator(doc);
+			String[] subItemContents = { "sub list item 1", "sub list item 2",
+					"sub list item 3" };
+
+			// create 2 lists
+			new List(sect, numberDecorator);
+			sect.addList().addItems(subItemContents);
+
+			Iterator<List> listIterator = sect.getListIterator();
+			int i = 0;
+			while (listIterator.hasNext()) {
+				listIterator.next();
+				i++;
+			}
+			Assert.assertEquals(2, i);
+
+			// add 2 new lists.
+			sect.addList(outLineDecorator);
+			List list = sect.addList();
+			listIterator = sect.getListIterator();
+			i = 0;
+			while (listIterator.hasNext()) {
+				listIterator.next();
+				i++;
+			}
+			Assert.assertEquals(4, i);
+
+			// remove 1 list.
+			sect.removeList(list);
+			listIterator = sect.getListIterator();
+			i = 0;
+			while (listIterator.hasNext()) {
+				listIterator.next();
+				i++;
+			}
+			Assert.assertEquals(3, i);
+
+			// remove all of the lists.
+			sect.clearList();
+			listIterator = sect.getListIterator();
+			i = 0;
+			while (listIterator.hasNext()) {
+				listIterator.next();
+				i++;
+			}
+			Assert.assertEquals(0, i);
+
+		} catch (Exception e) {
+			Logger.getLogger(ListTest.class.getName()).log(Level.SEVERE, null,
+					e);
+			Assert.fail(e.getMessage());
+		}
+	}
 }



Mime
View raw message