#ODFTOOLKIT-418# Patch by Georg Fuechsle - Adding missing methods to find an embedded section, plus regression test

git-svn-id: https://svn.apache.org/repos/asf/incubator/odf/trunk@1732615 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/simple/src/main/java/org/odftoolkit/simple/text/Section.java b/simple/src/main/java/org/odftoolkit/simple/text/Section.java
index 0e553b4..c9b7b3e 100644
--- a/simple/src/main/java/org/odftoolkit/simple/text/Section.java
+++ b/simple/src/main/java/org/odftoolkit/simple/text/Section.java
@@ -1,4 +1,4 @@
-/* 
+/*
 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
@@ -25,7 +25,6 @@
 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;
@@ -34,19 +33,19 @@
 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.table.TableContainer;
 import org.odftoolkit.simple.text.list.AbstractListContainer;
 import org.odftoolkit.simple.text.list.ListContainer;
 import org.odftoolkit.simple.text.list.ListDecorator;
-
+import org.w3c.dom.Node;
 import sun.misc.BASE64Encoder;
 
 /**
  * This class represents section definition in text document. It provides
  * methods to manipulate section in text document, such as getting/setting
  * section name, moving section and so on.
- * 
+ *
  * @since 0.4
  */
 public class Section extends Component implements ParagraphContainer,
@@ -67,7 +66,7 @@
 
 	/**
 	 * Get a section instance by an object of <code>TextSectionElement</code>.
-	 * 
+	 *
 	 * @param element
 	 *            - an object of <code>TextSectionElement</code>
 	 * @return an instance of <code>Section</code> that can represent
@@ -79,7 +78,7 @@
 
 	/**
 	 * Return the ODF document which this section belongs to.
-	 * 
+	 *
 	 * @return - the ODF document which this section belongs to.
 	 */
 	public Document getOwnerDocument() {
@@ -88,7 +87,7 @@
 
 	/**
 	 * Return the name of this section
-	 * 
+	 *
 	 * @return - the name of this section
 	 */
 	public String getName() {
@@ -97,7 +96,7 @@
 
 	/**
 	 * Set the value of this section name
-	 * 
+	 *
 	 * @param name
 	 *            - the value of name to be set
 	 */
@@ -110,7 +109,7 @@
 	 * <p>
 	 * All the linked resources which are only linked to this section will be
 	 * removed too.
-	 * 
+	 *
 	 */
 	public void remove() {
 		mDocument.removeElementLinkedResource(mSectionElement);
@@ -123,7 +122,7 @@
 	/**
 	 * Return an instance of <code>TextSectionElement</code> which represents
 	 * this section.
-	 * 
+	 *
 	 * @return - an instance of <code>TextSectionElement</code> which represents
 	 *         this section
 	 */
@@ -133,7 +132,7 @@
 
 	/**
 	 * Return whether this section is contained in footer or header.
-	 * 
+	 *
 	 * @return - true if this section is contained in footer or header. false if
 	 *         this section is not contained in footer or header.
 	 */
@@ -152,7 +151,7 @@
 	 * <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
@@ -167,7 +166,7 @@
 	/**
 	 * Return the value of section which specifies whether the section is
 	 * protected.
-	 * 
+	 *
 	 * @return whether the section is protected.
 	 */
 	public boolean isProtected() {
@@ -186,10 +185,10 @@
 	 * {@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) {
@@ -207,7 +206,7 @@
 
 	/**
 	 * Get the protection key of this section.
-	 * 
+	 *
 	 * @return the protection key of this section
 	 */
 	public String getProtectedPassword() {
@@ -219,7 +218,7 @@
 	 * <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.
 	 */
@@ -271,7 +270,7 @@
 	/**
 	 * Set the provider which provides corresponding protection key digest
 	 * algorithm.
-	 * 
+	 *
 	 * @param provider
 	 *            - an instance of a protection key digest algorithm provider
 	 */
@@ -283,7 +282,7 @@
 	/**
 	 * Get current used provider which provides corresponding protection key
 	 * digest algorithm.
-	 * 
+	 *
 	 * @return the current used provider.
 	 */
 	public ProtectionKeyDigestProvider getProtectionKeyDigestProvier() {
@@ -296,7 +295,7 @@
 	/**
 	 * 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() {
@@ -323,7 +322,7 @@
 	}
 
 	//****************Paragraph support******************//
-	
+
 	public Paragraph addParagraph(String textContent) {
 		return getParagraphContainerImpl().addParagraph(textContent);
 	}
@@ -451,4 +450,52 @@
 		return getListContainerImpl().removeList(list);
 	}
 
+    /**
+     * Looks for an embedded section with a given name.
+     *
+     * @param chdSecName Section name looked for
+     * @return TextSectionElement with the given name or null
+     */
+    public Section getEmbeddedSectionByName(String chdSecName) {
+        OdfElement sectEle = getOdfElement();
+        TextSectionElement sectChdEle = findChildSectionByName(sectEle, chdSecName);
+        if (sectChdEle != null) {
+            return new Section((Document) ((OdfFileDom) (sectEle.getOwnerDocument())).getDocument(), sectChdEle);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Recursive function. Looks for an TextSectionElement inside the father
+     * Element 'ele' with the name sectionName
+     *
+     * @param ele
+     * @param sectionName
+     * @return TextSectionElement with the given name or null
+     */
+    private static TextSectionElement findChildSectionByName(OdfElement ele, String sectionName) {
+        if (ele instanceof TextSectionElement) {
+            // is ele the wanted Element?
+            String name = ((TextSectionElement) ele).getTextNameAttribute();
+            if (name != null) {
+                if (name.equals(sectionName)) {
+                    return (TextSectionElement) ele;
+                }
+            }
+        }
+
+        // this Element is not the wanted one? then examine all children
+        Node chdNode = ele.getFirstChild();
+        while (chdNode != null) {
+            if (chdNode instanceof OdfElement) {
+                TextSectionElement result = findChildSectionByName((OdfElement) chdNode, sectionName);
+                if (result != null) {
+                    return result;
+                }
+            }
+            chdNode = chdNode.getNextSibling();
+        }
+        return null;
+    }
 }
diff --git a/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java b/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java
index 2159455..ad41198 100644
--- a/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java
+++ b/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java
@@ -1,4 +1,4 @@
-/* 
+/*
 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
@@ -24,12 +24,9 @@
 import java.util.Iterator;
 import java.util.logging.Level;
 import java.util.logging.Logger;
-
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
-
 import junit.framework.Assert;
-
 import org.junit.Test;
 import org.odftoolkit.odfdom.dom.element.draw.DrawImageElement;
 import org.odftoolkit.odfdom.dom.element.table.TableTableElement;
@@ -44,7 +41,6 @@
 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 {
@@ -393,43 +389,43 @@
 			Assert.fail();
 		}
 	}
-	
+
 	@Test
 	public void testAddParagraph() {
 		try {
 			TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
 			Section theSec = doc.getSectionByName("ImageSection");
-			
+
 			Paragraph para = theSec.addParagraph("paragraph");
 			String paracontent = para.getTextContent();
 			Assert.assertEquals("paragraph", paracontent);
-			
+
 			OdfElement odfEle = theSec.getParagraphContainerElement();
 			Assert.assertEquals("paragraph", odfEle.getLastChild().getTextContent());
-			
+
 			boolean flag = theSec.removeParagraph(para);
 			if(flag){
 				OdfElement odfEle1 = theSec.getParagraphContainerElement();
 				Assert.assertTrue(odfEle1.getLastChild().getTextContent() != "paragraph");
 			}
-			
+
 		} catch (Exception e) {
 			e.printStackTrace();
 			Assert.fail();
 		}
 	}
-	
-	
+
+
 	@Test
 	public void testGetParagraphIterator() {
 		try {
 			TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
 			Section theSec = doc.getSectionByName("ImageSection");
-			
+
 			Paragraph para = theSec.addParagraph("paragraph");
 			String paracontent = para.getTextContent();
 			Assert.assertEquals("paragraph", paracontent);
-			
+
 			boolean flag = false;
 			Iterator<Paragraph> iter = theSec.getParagraphIterator();
 			while(iter.hasNext()){
@@ -437,59 +433,59 @@
 				if("paragraph".equals(parai.getTextContent()))
 						flag = true;
 			}
-			
+
 			Assert.assertTrue(flag);
-			
+
 		} catch (Exception e) {
 			e.printStackTrace();
 			Assert.fail();
 		}
 	}
-	
-	
+
+
 	@Test
 	public void testGetParagraphByIndex() {
 		try {
 			TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
 			Section theSec = doc.getSectionByName("ImageSection");
-			
+
 			Paragraph para = theSec.addParagraph("paragraph");
 			String paracontent = para.getTextContent();
 			Assert.assertEquals("paragraph", paracontent);
-			
+
 			Paragraph para1 = theSec.getParagraphByIndex(2, true);
 			Assert.assertEquals("paragraph", para1.getTextContent());
-			
-			
+
+
 		} catch (Exception e) {
 			e.printStackTrace();
 			Assert.fail();
 		}
 	}
-	
-	
+
+
 	@Test
 	public void testGetParagraphByReverseIndex() {
 		try {
 			TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
 			Section theSec = doc.getSectionByName("ImageSection");
 			Section theSec2 = doc.getSectionByName("ImageSection");
-			
+
 			Paragraph para = theSec.addParagraph("paragraph");
 			String paracontent = para.getTextContent();
 			Assert.assertEquals("paragraph", paracontent);
-			
+
 			Paragraph para1 = theSec.getParagraphByReverseIndex(0, true);
 			Assert.assertEquals("paragraph", para1.getTextContent());
-			
+
 			Paragraph para2 = theSec.getParagraphByIndex(2, true);
 			Assert.assertEquals("paragraph", para2.getTextContent());
-			
+
 			boolean flag = theSec.equals(para);
 			Assert.assertTrue(!flag);
 			flag = theSec.equals(theSec2);
 			Assert.assertTrue(flag);
-			
+
 		} catch (Exception e) {
 			e.printStackTrace();
 			Assert.fail();
@@ -650,4 +646,17 @@
 			Assert.fail(e.getMessage());
 		}
 	}
+
+    @Test
+    public void testGetEmbeddedSectionByName() {
+        try {
+            TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("Sections.odt"));
+            Section sectOut = doc.getSectionByName("InnerSection");
+            Section sectEmbedded = sectOut.getEmbeddedSectionByName("EmbedSection");
+            Assert.assertEquals(true, sectEmbedded != null);
+        } catch (Exception e) {
+            Logger.getLogger(ListTest.class.getName()).log(Level.SEVERE, "Problem with section test:", e);
+            Assert.fail();
+        }
+    }
 }