incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From svanteschub...@apache.org
Subject svn commit: r1732615 - in /incubator/odf/trunk/simple/src: main/java/org/odftoolkit/simple/text/Section.java test/java/org/odftoolkit/simple/text/SectionTest.java
Date Sat, 27 Feb 2016 09:05:05 GMT
Author: svanteschubert
Date: Sat Feb 27 09:05:05 2016
New Revision: 1732615

URL: http://svn.apache.org/viewvc?rev=1732615&view=rev
Log:
#ODFTOOLKIT-418# Patch by Georg Fuechsle - Adding missing methods to find an embedded section,
plus regression test

Modified:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Section.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/text/Section.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Section.java?rev=1732615&r1=1732614&r2=1732615&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 Sat Feb
27 09:05:05 2016
@@ -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.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;
@@ -34,19 +33,19 @@ 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.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 @@ public class Section extends Component i
 
 	/**
 	 * 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 @@ public class Section extends Component i
 
 	/**
 	 * 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 @@ public class Section extends Component i
 
 	/**
 	 * Return the name of this section
-	 * 
+	 *
 	 * @return - the name of this section
 	 */
 	public String getName() {
@@ -97,7 +96,7 @@ public class Section extends Component i
 
 	/**
 	 * Set the value of this section name
-	 * 
+	 *
 	 * @param name
 	 *            - the value of name to be set
 	 */
@@ -110,7 +109,7 @@ public class Section extends Component i
 	 * <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 @@ public class Section extends Component i
 	/**
 	 * 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 @@ public class Section extends Component i
 
 	/**
 	 * 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 @@ public class Section extends Component i
 	 * <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 @@ public class Section extends Component i
 	/**
 	 * 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 @@ public class Section extends Component i
 	 * {@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 @@ public class Section extends Component i
 
 	/**
 	 * Get the protection key of this section.
-	 * 
+	 *
 	 * @return the protection key of this section
 	 */
 	public String getProtectedPassword() {
@@ -219,7 +218,7 @@ public class Section extends Component i
 	 * <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 @@ public class Section extends Component i
 	/**
 	 * 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 @@ public class Section extends Component i
 	/**
 	 * Get current used provider which provides corresponding protection key
 	 * digest algorithm.
-	 * 
+	 *
 	 * @return the current used provider.
 	 */
 	public ProtectionKeyDigestProvider getProtectionKeyDigestProvier() {
@@ -296,7 +295,7 @@ public class Section extends Component i
 	/**
 	 * 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 @@ public class Section extends Component i
 	}
 
 	//****************Paragraph support******************//
-	
+
 	public Paragraph addParagraph(String textContent) {
 		return getParagraphContainerImpl().addParagraph(textContent);
 	}
@@ -451,4 +450,52 @@ public class Section extends Component i
 		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;
+    }
 }

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=1732615&r1=1732614&r2=1732615&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 Sat
Feb 27 09:05:05 2016
@@ -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.security.NoSuchAlgorithmExce
 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.L
 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 @@ public class SectionTest {
 			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 @@ public class SectionTest {
 				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 @@ public class SectionTest {
 			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();
+        }
+    }
 }



Mime
View raw message