incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robw...@apache.org
Subject svn commit: r1375237 - in /incubator/odf/trunk/simple/src: main/java/org/odftoolkit/simple/TextDocument.java main/java/org/odftoolkit/simple/style/TOCStyle.java test/java/org/odftoolkit/simple/text/TableOfContentTest.java
Date Mon, 20 Aug 2012 21:40:24 GMT
Author: robweir
Date: Mon Aug 20 21:40:24 2012
New Revision: 1375237

URL: http://svn.apache.org/viewvc?rev=1375237&view=rev
Log:
Re-apply patch for OFFICE-329, since it was wiped out in the reversion of the earlier OFFICE-319
patch

Added:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/TOCStyle.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/TableOfContentTest.java
Modified:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/TextDocument.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=1375237&r1=1375236&r2=1375237&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 Aug
20 21:40:24 2012
@@ -25,7 +25,11 @@ import java.awt.Rectangle;
 import java.io.File;
 import java.io.InputStream;
 import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -41,11 +45,24 @@ 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.text.TextIndexBodyElement;
+import org.odftoolkit.odfdom.dom.element.text.TextIndexEntryLinkStartElement;
+import org.odftoolkit.odfdom.dom.element.text.TextIndexEntryTabStopElement;
+import org.odftoolkit.odfdom.dom.element.text.TextIndexSourceStylesElement;
+import org.odftoolkit.odfdom.dom.element.text.TextIndexTitleElement;
+import org.odftoolkit.odfdom.dom.element.text.TextIndexTitleTemplateElement;
 import org.odftoolkit.odfdom.dom.element.text.TextPElement;
+import org.odftoolkit.odfdom.dom.element.text.TextParagraphElementBase;
 import org.odftoolkit.odfdom.dom.element.text.TextSectionElement;
+import org.odftoolkit.odfdom.dom.element.text.TextTableOfContentElement;
+import org.odftoolkit.odfdom.dom.element.text.TextTableOfContentEntryTemplateElement;
+import org.odftoolkit.odfdom.dom.element.text.TextTableOfContentSourceElement;
+import org.odftoolkit.odfdom.dom.element.text.TextTocMarkElement;
+import org.odftoolkit.odfdom.dom.element.text.TextTocMarkStartElement;
 import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
 import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeAutomaticStyles;
 import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeMasterStyles;
+import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeStyles;
 import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle;
 import org.odftoolkit.odfdom.incubator.doc.style.OdfStylePageLayout;
 import org.odftoolkit.odfdom.incubator.doc.text.OdfTextParagraph;
@@ -62,6 +79,7 @@ import org.odftoolkit.simple.common.fiel
 import org.odftoolkit.simple.common.field.VariableContainer;
 import org.odftoolkit.simple.common.field.VariableField;
 import org.odftoolkit.simple.common.field.VariableField.VariableType;
+import org.odftoolkit.simple.style.TOCStyle;
 import org.odftoolkit.simple.text.AbstractParagraphContainer;
 import org.odftoolkit.simple.text.Footer;
 import org.odftoolkit.simple.text.Header;
@@ -74,6 +92,7 @@ import org.odftoolkit.simple.text.list.L
 import org.odftoolkit.simple.text.list.ListDecorator;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
 
 /**
  * This class represents an empty ODF text document.
@@ -859,4 +878,342 @@ public class TextDocument extends Docume
 			return drawFrame;
 		}
 	}
+
+	/**
+	 * Create a new Table Of Content to this document before the reference
+	 * paragraph. Because until you do a full layout of the document, taking
+	 * into account font metrics,line breaking algorithms, hyphenation, image
+	 * positioning, "orphan and widow" rules, etc., you don't know what content
+	 * is on which page. So all the page numbers in TOC are be set value "1".
+	 * Please update the page numbers from AOO Menus: Tools->Update->All Indexes
+	 * and Tables
+	 * 
+	 * @param refParagraph
+	 *            the reference paragraph where the TOC be inserted.
+	 * @param before
+	 *            true:insert TOC before the reference paragraph. false:insert
+	 *            TOC after the reference paragraph.
+	 * @since 0.8.6
+	 */
+	public TextTableOfContentElement createDefaultTOC(Paragraph refParagraph, boolean before)
{
+		
+		TextTableOfContentElement textTableOfContent =null;
+		if(refParagraph==null){
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE,
+					"Failed to create Default TOC, The refParagraph where the TOC be inserted is null");
+			throw new RuntimeException("Failed to create Default TOC, The refParagraph where the TOC
be inserted is null");
+		}
+		Node refparagraphNode = refParagraph.getOdfElement();
+		Node rootNode = refparagraphNode.getParentNode();		
+		try {
+			OdfContentDom content = getContentDom();
+			textTableOfContent = content
+					.newOdfElement(TextTableOfContentElement.class);
+			textTableOfContent.setTextNameAttribute("Table of Contents");
+			textTableOfContent.setTextProtectedAttribute(true);
+			TextTableOfContentSourceElement textTableOfContentSource = textTableOfContent
+					.newTextTableOfContentSourceElement();
+			textTableOfContentSource.setTextOutlineLevelAttribute(10);
+			textTableOfContentSource.setTextUseIndexMarksAttribute(true);
+			TextIndexTitleTemplateElement textIndexTitleTemplate = textTableOfContentSource
+					.newTextIndexTitleTemplateElement();
+			textIndexTitleTemplate
+					.setTextStyleNameAttribute("Contents_20_Heading");
+			textIndexTitleTemplate.setTextContent("Table of Contents");
+			for (int i = 1; i <= 10; i++) {
+				TextTableOfContentEntryTemplateElement textTableOfContentEntryTemplate = textTableOfContentSource
+						.newTextTableOfContentEntryTemplateElement(i,
+								"Contents_20_" + i);
+				TextIndexEntryLinkStartElement TextIndexEntryLinkStart = textTableOfContentEntryTemplate
+						.newTextIndexEntryLinkStartElement();
+				TextIndexEntryLinkStart
+						.setTextStyleNameAttribute("Index_20_Link");
+				textTableOfContentEntryTemplate
+						.newTextIndexEntryChapterElement();
+				textTableOfContentEntryTemplate.newTextIndexEntryTextElement();
+				TextIndexEntryTabStopElement TextIndexEntryTabStop = textTableOfContentEntryTemplate
+						.newTextIndexEntryTabStopElement("right");
+				TextIndexEntryTabStop.setStyleLeaderCharAttribute(".");
+				textTableOfContentEntryTemplate
+						.newTextIndexEntryPageNumberElement();
+				textTableOfContentEntryTemplate
+						.newTextIndexEntryLinkEndElement();
+			}
+			TextIndexBodyElement textIndexBody = textTableOfContent
+					.newTextIndexBodyElement();
+			TextIndexTitleElement TextIndexTitle = textIndexBody
+					.newTextIndexTitleElement("Table of Contents_Head");
+			TextPElement texp = TextIndexTitle.newTextPElement();
+			texp.setTextStyleNameAttribute("Contents_20_Heading");
+			texp.setTextContent("Table of Contents");
+			Iterator<Paragraph> paragraphIterator = getParagraphIterator();
+			while (paragraphIterator.hasNext()) {
+				Paragraph paragraph = paragraphIterator.next();
+				String text = paragraph.getTextContent();
+				String stylename = paragraph.getStyleName();
+				// Outline support
+				if (paragraph.isHeading()) {
+					int headingLevel = paragraph.getHeadingLevel();
+					if (stylename.length() <= 0) {
+						stylename = "Contents_20_" + headingLevel;
+					}
+					ceateIndexBodyEntry(textIndexBody, stylename, text);
+				}
+				// end of Outline support
+				// Index Makes support
+				TextParagraphElementBase podf = paragraph.getOdfElement();
+				NodeList cns = podf.getChildNodes();
+				for (int i = 0; i < cns.getLength(); i++) {
+					Node node = cns.item(i);
+					if (node instanceof TextTocMarkElement) {
+						TextTocMarkElement textTocMarkElement = (TextTocMarkElement) node;
+						text = textTocMarkElement.getTextStringValueAttribute();
+						int headingLevel = textTocMarkElement
+								.getTextOutlineLevelAttribute();
+						stylename = "Contents_20_" + headingLevel;
+						ceateIndexBodyEntry(textIndexBody, stylename, text);
+					}
+					if (node instanceof TextTocMarkStartElement) {
+						TextTocMarkStartElement textTocMarkStartElement = (TextTocMarkStartElement) node;
+						Node tmp = node.getNextSibling();
+						while (!(tmp instanceof Text)) {
+							tmp = node.getNextSibling();
+						}
+						text = tmp.getTextContent();
+						int headingLevel = textTocMarkStartElement
+								.getTextOutlineLevelAttribute();
+						stylename = "Contents_20_" + headingLevel;
+						ceateIndexBodyEntry(textIndexBody, stylename, text);
+					}
+				}
+				// end of Index Makes support
+			}
+			if (before) {
+				rootNode.insertBefore(textTableOfContent, refparagraphNode);
+			} else {
+				// Insert TOC after the refParagraph
+				Node refNextNode = refparagraphNode.getNextSibling();
+				if (refNextNode == null) {
+					rootNode.appendChild(textTableOfContent);
+				} else {
+					rootNode.insertBefore(textTableOfContent, refNextNode);
+				}
+			}
+			return textTableOfContent;
+		} catch (Exception e) {
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE,
+					"Failed to create Default TOC", e);
+			throw new RuntimeException("Failed to create Default TOC", e);
+		}
+	}
+
+	private void ceateIndexBodyEntry(TextIndexBodyElement textIndexBody,
+			String stylename, String text) {
+		TextPElement textp = textIndexBody.newTextPElement();
+		textp.setTextStyleNameAttribute(stylename);
+		textp.newTextNode(text);
+		textp.newTextTabElement();
+		textp.newTextNode("1");
+	}
+
+	/**
+	 * Create a new Table Of Content to this document before the reference
+	 * paragraph. The additional paragraph styles list will be included in the
+	 * TOC. Because until you do a full layout of the document, taking into
+	 * account font metrics,line breaking algorithms, hyphenation, image
+	 * positioning, "orphan and widow" rules, etc., you don't know what content
+	 * is on which page. So all the page numbers in TOC are be set value "1".
+	 * Please update the page numbers from AOO Menus: Tools->Update->All Indexes
+	 * and Tables.  If additionalStyle is null then call createDefaultTOC().
+	 * 
+	 * @param refParagraph
+	 *            the reference paragraph before where the TOC inserted.
+	 * @param additionalStyle
+	 *            the additional paragraph styles that you want to include in
+	 *            the TOC
+	 * @param before
+	 *            true:insert TOC before the reference paragraph. 
+	 *            false:insert TOC after the reference paragraph.
+	 * @since 0.8.6
+	 */
+	public TextTableOfContentElement createTOCwithStyle(Paragraph refParagraph,
+			TOCStyle additionalStyle, boolean before) {
+		if(refParagraph==null){
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE,
+					"Failed to create Default TOC, The refParagraph where the TOC be inserted is null");
+			throw new RuntimeException("Failed to create Default TOC, The refParagraph where the TOC
be inserted is null");
+		}
+		if (additionalStyle == null) {
+			TextTableOfContentElement textTableOfContentElement = createDefaultTOC(refParagraph, before);
+			return textTableOfContentElement;
+		}
+		HashMap<Integer, String> tocstyleList = additionalStyle.getStyle();
+		if (tocstyleList.isEmpty()) {
+			TextTableOfContentElement textTableOfContentElement =createDefaultTOC(refParagraph, before);
+			return textTableOfContentElement;
+		}
+
+		Collection<String> tocvalues = tocstyleList.values();
+		OdfOfficeStyles docstyles = getOrCreateDocumentStyles();
+		Iterable<OdfStyle> paragraphStyles = docstyles
+				.getStylesForFamily(OdfStyleFamily.Paragraph);
+		ArrayList<String> pstyle = new ArrayList<String>();
+		Iterator<OdfStyle> iterator = paragraphStyles.iterator();
+		while (iterator.hasNext()) {
+			OdfStyle style = iterator.next();
+			String name = style.getStyleNameAttribute();
+			pstyle.add(name);
+		}
+		if (!pstyle.containsAll(tocvalues)) {
+			Logger.getLogger(TextDocument.class.getName())
+					.log(Level.SEVERE,
+							"Failed to create TOC with Styles. Some of addtional Styles that you want to include
in the TOC can't be found in the document.\n"
+									+ tocvalues.toString());
+			throw new RuntimeException(
+					"Failed to create TOC Styles.Some of addtional Styles that you want to include in the
TOC can't be found in the document.");
+		}
+		TextTableOfContentElement textTableOfContent =null;
+		// Additional Styles support
+		HashMap<String, Integer> tmptocstyleList = new HashMap<String, Integer>();
+		if (!tocstyleList.isEmpty()) {
+			Set<Integer> key = tocstyleList.keySet();
+			Iterator<Integer> it = key.iterator();
+			while (it.hasNext()) {
+				Integer textOutlineLevelValue = it.next();
+				String textStyleNameValue = tocstyleList
+						.get(textOutlineLevelValue);
+				tmptocstyleList.put(textStyleNameValue, textOutlineLevelValue);
+			}
+		}
+		// end of Additional Styles support
+
+		Node refparagraphNode = refParagraph.getOdfElement();
+		Node rootNode = refparagraphNode.getParentNode();
+		try {
+			OdfContentDom content = getContentDom();
+			textTableOfContent = content
+					.newOdfElement(TextTableOfContentElement.class);
+			textTableOfContent.setTextNameAttribute("Table of Contents");
+			textTableOfContent.setTextProtectedAttribute(true);
+
+			TextTableOfContentSourceElement textTableOfContentSource = textTableOfContent
+					.newTextTableOfContentSourceElement();
+			textTableOfContentSource.setTextOutlineLevelAttribute(10);
+			textTableOfContentSource.setTextUseIndexMarksAttribute(true);
+			textTableOfContentSource.setTextUseIndexSourceStylesAttribute(true);
+			TextIndexTitleTemplateElement textIndexTitleTemplate = textTableOfContentSource
+					.newTextIndexTitleTemplateElement();
+			textIndexTitleTemplate
+					.setTextStyleNameAttribute("Contents_20_Heading");
+			textIndexTitleTemplate.setTextContent("Table of Contents");
+			for (int i = 1; i <= 10; i++) {
+				TextTableOfContentEntryTemplateElement textTableOfContentEntryTemplate = textTableOfContentSource
+						.newTextTableOfContentEntryTemplateElement(i,
+								"Contents_20_" + i);
+				TextIndexEntryLinkStartElement TextIndexEntryLinkStart = textTableOfContentEntryTemplate
+						.newTextIndexEntryLinkStartElement();
+				TextIndexEntryLinkStart
+						.setTextStyleNameAttribute("Index_20_Link");
+				textTableOfContentEntryTemplate
+						.newTextIndexEntryChapterElement();
+				textTableOfContentEntryTemplate.newTextIndexEntryTextElement();
+				TextIndexEntryTabStopElement TextIndexEntryTabStop = textTableOfContentEntryTemplate
+						.newTextIndexEntryTabStopElement("right");
+				TextIndexEntryTabStop.setStyleLeaderCharAttribute(".");
+				textTableOfContentEntryTemplate
+						.newTextIndexEntryPageNumberElement();
+				textTableOfContentEntryTemplate
+						.newTextIndexEntryLinkEndElement();
+			}
+
+			if (!tocstyleList.isEmpty()) {
+				Set<Integer> key = tocstyleList.keySet();
+				Iterator<Integer> it = key.iterator();
+				while (it.hasNext()) {
+					Integer textOutlineLevelValue = it.next();
+					String textStyleNameValue = tocstyleList
+							.get(textOutlineLevelValue);
+					TextIndexSourceStylesElement textIndexSourceStyles = textTableOfContentSource
+							.newTextIndexSourceStylesElement(textOutlineLevelValue);
+					textIndexSourceStyles
+							.newTextIndexSourceStyleElement(textStyleNameValue);
+				}
+			}
+
+			TextIndexBodyElement textIndexBody = textTableOfContent
+					.newTextIndexBodyElement();
+			TextIndexTitleElement TextIndexTitle = textIndexBody
+					.newTextIndexTitleElement("Table of Contents_Head");
+			TextPElement texp = TextIndexTitle.newTextPElement();
+			texp.setTextStyleNameAttribute("Contents_20_Heading");
+			texp.setTextContent("Table of Contents");
+			Iterator<Paragraph> paragraphIterator = getParagraphIterator();
+			while (paragraphIterator.hasNext()) {
+				Paragraph paragraph = paragraphIterator.next();
+				String text = paragraph.getTextContent();
+				String stylename = paragraph.getStyleName();
+				// Outline support
+				if (paragraph.isHeading()) {
+					int headingLevel = paragraph.getHeadingLevel();
+					if (stylename.length() <= 0) {
+						stylename = "Contents_20_" + headingLevel;
+					}
+					ceateIndexBodyEntry(textIndexBody, stylename, text);
+				}
+				// end of Outline support
+				// Additional Styles support
+				if (tmptocstyleList.containsKey(stylename)) {
+					int headingLevel = tmptocstyleList.get(stylename);
+					stylename = "Contents_20_" + headingLevel;
+					ceateIndexBodyEntry(textIndexBody, stylename, text);
+				}
+				// end of Additional Styles support
+				// Index Makes support
+				TextParagraphElementBase podf = paragraph.getOdfElement();
+				NodeList cns = podf.getChildNodes();
+				for (int i = 0; i < cns.getLength(); i++) {
+					Node node = cns.item(i);
+					if (node instanceof TextTocMarkElement) {
+						TextTocMarkElement textTocMarkElement = (TextTocMarkElement) node;
+						text = textTocMarkElement.getTextStringValueAttribute();
+						int headingLevel = textTocMarkElement
+								.getTextOutlineLevelAttribute();
+						stylename = "Contents_20_" + headingLevel;
+						ceateIndexBodyEntry(textIndexBody, stylename, text);
+					}
+					if (node instanceof TextTocMarkStartElement) {
+						TextTocMarkStartElement textTocMarkStartElement = (TextTocMarkStartElement) node;
+						Node tmp = node.getNextSibling();
+						while (!(tmp instanceof Text)) {
+							tmp = node.getNextSibling();
+						}
+						text = tmp.getTextContent();
+						int headingLevel = textTocMarkStartElement
+								.getTextOutlineLevelAttribute();
+						stylename = "Contents_20_" + headingLevel;
+						ceateIndexBodyEntry(textIndexBody, stylename, text);
+					}
+				}
+				// end of Index Makes support
+			}
+			if (before) {
+				rootNode.insertBefore(textTableOfContent, refparagraphNode);
+			} else {
+				// Insert TOC after the refParagraph
+				Node refNextNode = refparagraphNode.getNextSibling();
+				if (refNextNode == null) {
+					rootNode.appendChild(textTableOfContent);
+				} else {
+					rootNode.insertBefore(textTableOfContent, refNextNode);
+				}
+			}
+			return textTableOfContent;
+			
+		} catch (Exception e) {
+			Logger.getLogger(TextDocument.class.getName()).log(Level.SEVERE,
+					"Failed to create Default TOC", e);
+			throw new RuntimeException("Failed to create Default TOC", e);
+		}
+	}
+	
 }

Added: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/TOCStyle.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/TOCStyle.java?rev=1375237&view=auto
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/TOCStyle.java (added)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/style/TOCStyle.java Mon
Aug 20 21:40:24 2012
@@ -0,0 +1,84 @@
+/* 
+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.style;
+
+import java.util.HashMap;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+
+/**
+ * This class represents the additional paragraph styles list that you want to
+ * include in the TOC.
+ * 
+ * @since 0.8.6
+ */
+public class TOCStyle {
+	HashMap<Integer, String> stylelist=new HashMap<Integer, String>();
+	
+	/**
+	 * Create an instance of TOCStyle
+	 * @param doc 
+	 */
+	public TOCStyle() {
+		
+	}
+
+	/**
+	 * Add an additional paragraph style to the style list that you want to
+	 * include in the TOC.
+	 * 
+	 * @param styleName
+	 *            - the style name
+	 * @param level
+	 *            - the outline level of this style, level value is: [1,10]
+	 */
+	public void addStyle(String styleName, int level) {
+		if ((level >= 1) && (level <= 10)) {
+			
+			stylelist.put(level, styleName);
+		
+		} else {
+			Logger.getLogger(TOCStyle.class.getName()).log(
+					Level.SEVERE,
+					"Outline level:" + level + " is out of range[1,10] "
+							+ "TOCStyle.addStyle failed.");
+			throw new RuntimeException("Outline level:" + level
+					+ " is out of range[1,10] " + "TOCStyle.addStyle failed.");
+		}
+	}
+	public HashMap<Integer, String> getStyle(){
+		return stylelist;
+	}
+	@Override
+	public String toString() {
+		StringBuilder strBuilder = new StringBuilder();
+		int size=stylelist.size();
+		strBuilder.append("The additional paragraph styles list that you want to include in the
TOC.\n");
+		for(int i=0;i<size;i++){
+			strBuilder.append("Outline Level:"+ (i+1));	
+			String styleName=stylelist.get(i+1);
+			strBuilder.append(" Style Name:"+styleName);
+			strBuilder.append("\n");
+		}
+		return strBuilder.toString();
+	}
+
+}

Added: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/TableOfContentTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/TableOfContentTest.java?rev=1375237&view=auto
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/TableOfContentTest.java
(added)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/TableOfContentTest.java
Mon Aug 20 21:40:24 2012
@@ -0,0 +1,210 @@
+/* 
+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.text;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+import org.odftoolkit.odfdom.dom.element.text.TextHElement;
+import org.odftoolkit.odfdom.dom.element.text.TextTableOfContentElement;
+import org.odftoolkit.odfdom.dom.element.text.TextTocMarkElement;
+import org.odftoolkit.odfdom.dom.element.text.TextTocMarkStartElement;
+import org.odftoolkit.simple.TextDocument;
+import org.odftoolkit.simple.style.TOCStyle;
+import org.odftoolkit.simple.utils.ResourceUtilities;
+import org.w3c.dom.Node;
+
+public class TableOfContentTest {
+	private static final Logger LOG = Logger.getLogger(TableOfContentTest.class
+			.getName());
+
+	@Test
+	public void testCreateDefaultTOC() {
+		try {
+			TextDocument doc = buildSample();
+			Assert.assertNotNull(doc);
+			
+			Paragraph paragraph1 = doc.getParagraphByIndex(0, true);
+			TextTableOfContentElement textTableOfContentElement = doc.createDefaultTOC(paragraph1,false);
+			Assert.assertNotNull(textTableOfContentElement);	
+			
+			Node pnode = paragraph1.getOdfElement().getNextSibling();
+			
+			if (pnode.equals(textTableOfContentElement)) {
+				Assert.assertTrue(true);
+			} else {
+				Assert.fail();
+			}
+			
+			
+			try {
+				// Should throw error!
+				doc.createDefaultTOC(null, true);
+				Assert.fail();
+			} catch (Exception e) {
+//				LOG.log(Level.SEVERE, null, e);
+				Assert.assertTrue(true);
+			}
+			doc.save(ResourceUtilities.newTestOutputFile("DefaultTOC.odt"));
+		} catch (Exception e) {
+			LOG.log(Level.SEVERE, null, e);
+			Assert.fail();
+		}
+	}
+
+	@Test
+	public void testCreateStyleTOC() {
+		try {
+			TextDocument doc = buildSample();
+			Assert.assertNotNull(doc);
+
+			TOCStyle tocstyle = new TOCStyle();
+			tocstyle.addStyle("User_20_Index_20_1", 1);
+			tocstyle.addStyle("User_20_Index_20_2", 2);
+			tocstyle.addStyle("User_20_Index_20_3", 3);
+			tocstyle.addStyle("User_20_Index_20_4", 4);
+			tocstyle.addStyle("User_20_Index_20_5", 5);
+			tocstyle.addStyle("User_20_Index_20_6", 6);
+			tocstyle.addStyle("User_20_Index_20_7", 7);
+			tocstyle.addStyle("User_20_Index_20_8", 8);
+			tocstyle.addStyle("User_20_Index_20_9", 9);
+			tocstyle.addStyle("User_20_Index_20_10", 10);
+			LOG.info(tocstyle.toString());
+
+			Paragraph paragraph1 = doc.getParagraphByIndex(0, true);
+			TextTableOfContentElement textTableOfContentElement = doc
+					.createTOCwithStyle(paragraph1, tocstyle, true);
+			Assert.assertNotNull(textTableOfContentElement);
+			Node pnode = textTableOfContentElement.getNextSibling();
+			if (pnode.equals(paragraph1.getOdfElement())) {
+				Assert.assertTrue(true);
+			} else {
+				Assert.fail();
+			}
+			try {
+				// Should throw error!
+				doc.createTOCwithStyle(null, tocstyle, true);
+				Assert.fail();
+			} catch (Exception e) {
+//				LOG.log(Level.SEVERE, null, e);
+				Assert.assertTrue(true);
+			}
+			doc.save(ResourceUtilities.newTestOutputFile("TOC_Styles.odt"));
+		} catch (Exception e) {
+			LOG.log(Level.SEVERE, null, e);
+			Assert.fail();
+		}
+
+	}
+
+	private TextDocument buildSample() throws Exception {
+		TextDocument doc = TextDocument.newTextDocument();
+		Paragraph p1 = doc
+				.addParagraph("1.This is a test paragraph apply with Heading 1 style!");
+		p1.applyHeading();
+		Paragraph p11 = doc
+				.addParagraph("1.1This is a test paragraph apply with heading 2 style!");
+		p11.applyHeading(true, 2);
+		Paragraph p2 = doc
+				.addParagraph("2.This is a test paragraph apply with Heading 1 style!");
+		p2.applyHeading();
+		Paragraph p21 = doc
+				.addParagraph("2.1This is a test paragraph apply with Heading 2 style!");
+		p21.applyHeading(true, 2);
+		Paragraph p22 = doc
+				.addParagraph("2.2This is a test paragraph apply with Heading 2 style!");
+		p22.applyHeading(true, 2);
+		Paragraph p221 = doc
+				.addParagraph("2.2.1This is a test paragraph apply with Heading 3 style!");
+		p221.applyHeading(true, 3);
+		Paragraph p222 = doc
+				.addParagraph("2.2.2This is a test paragraph apply with Heading 3 style!");
+		p222.applyHeading(true, 3);
+		Paragraph p23 = doc
+				.addParagraph("2.3This is a test paragraph apply with Heading 2 style!");
+		p23.applyHeading(true, 2);
+		Paragraph p3 = doc
+				.addParagraph("3.This is a test paragraph apply with ");
+		p3.applyHeading();
+		TextHElement h3 = (TextHElement) p3.getOdfElement();
+		TextTocMarkElement TextTocMark = h3
+				.newTextTocMarkElement("TextTocMarkElement");
+		TextTocMark.setTextOutlineLevelAttribute(2);
+		h3.newTextNode("Heading ");
+		TextTocMarkStartElement TextTocMarkStart = h3
+				.newTextTocMarkStartElement("IMark159230668");
+		TextTocMarkStart.setTextOutlineLevelAttribute(3);
+		h3.newTextNode("1 style!");
+		h3.newTextTocMarkEndElement("IMark159230668");
+
+		Paragraph p4 = doc
+				.addParagraph("4.This is a test paragraph apply with User Index 1 style!");
+		p4.getOdfElement().setStyleName("User_20_Index_20_1");
+
+		Paragraph p5 = doc
+				.addParagraph("5.This is a test paragraph apply with User Index 1 style!");
+		p5.getOdfElement().setStyleName("User_20_Index_20_1");
+
+		Paragraph p51 = doc
+				.addParagraph("5.1 This is a test paragraph apply with User Index 2 style!");
+		p51.getOdfElement().setStyleName("User_20_Index_20_2");
+
+		Paragraph p52 = doc
+				.addParagraph("5.2 This is a test paragraph apply with User Index 2 style!");
+		p52.getOdfElement().setStyleName("User_20_Index_20_2");
+
+		Paragraph p6 = doc
+				.addParagraph("6.This is a test paragraph apply with User Index 1 style!");
+		p6.getOdfElement().setStyleName("User_20_Index_20_1");
+
+		Paragraph p62 = doc
+				.addParagraph("6.1This is a test paragraph apply with User Index 2 style!");
+		p62.getOdfElement().setStyleName("User_20_Index_20_2");
+		Paragraph p63 = doc
+				.addParagraph("6.1.1This is a test paragraph apply with User Index 3 style!");
+		p63.getOdfElement().setStyleName("User_20_Index_20_3");
+		Paragraph p64 = doc
+				.addParagraph("6.1.1.1This is a test paragraph apply with User Index 4 style!");
+		p64.getOdfElement().setStyleName("User_20_Index_20_4");
+		Paragraph p65 = doc
+				.addParagraph("6.1.1.1.1This is a test paragraph apply with User Index 5 style!");
+		p65.getOdfElement().setStyleName("User_20_Index_20_5");
+		Paragraph p66 = doc
+				.addParagraph("6.1.1.1.1.1This is a test paragraph apply with User Index 6 style!");
+		p66.getOdfElement().setStyleName("User_20_Index_20_6");
+		Paragraph p67 = doc
+				.addParagraph("6.1.1.1.1.1.1.1This is a test paragraph apply with User Index 7 style!");
+		p67.getOdfElement().setStyleName("User_20_Index_20_7");
+		Paragraph p68 = doc
+				.addParagraph("6.1.1.1.1.1.1.1.1This is a test paragraph apply with User Index 8 style!");
+		p68.getOdfElement().setStyleName("User_20_Index_20_8");
+		Paragraph p69 = doc
+				.addParagraph("6.1.1.1.1.1.1.1.1.1This is a test paragraph apply with User Index 9 style!");
+		p69.getOdfElement().setStyleName("User_20_Index_20_9");
+		Paragraph p60 = doc
+				.addParagraph("6.1.1.1.1.1.1.1.1.1.1This is a test paragraph apply with User Index 10
style!");
+		p60.getOdfElement().setStyleName("User_20_Index_20_10");
+		return doc;
+	}
+
+}



Mime
View raw message