Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
None
-
None
Description
To handle embedded Sections we miss a method that retrieves an embedded section by name.
In
http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Section.java
we suggest the following code:
/**
- recurisve function. looks for an TextSectionElement inside the father Element 'ele' with the name sectionName
- @param ele
- @param sectionName
- @return null or the wanted TextSectionElement
*/
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;
}
in
http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/SectionTest.java
we propose the appropriate test:
@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)
{
e.printStackTrace();
Assert.fail();
}
}
see the attached file.