incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robw...@apache.org
Subject svn commit: r1375358 - in /incubator/odf/trunk/simple/src: main/java/org/odftoolkit/simple/text/list/AbstractListContainer.java test/java/org/odftoolkit/simple/text/list/ListItemTest.java test/resources/testList2.odt
Date Tue, 21 Aug 2012 00:52:38 GMT
Author: robweir
Date: Tue Aug 21 00:52:37 2012
New Revision: 1375358

URL: http://svn.apache.org/viewvc?rev=1375358&view=rev
Log:
Reapply patches from ODFTOOLKIT-331, since they were wiped out by the recent revert

Added:
    incubator/odf/trunk/simple/src/test/resources/testList2.odt   (with props)
Modified:
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/list/AbstractListContainer.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/list/ListItemTest.java

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/list/AbstractListContainer.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/list/AbstractListContainer.java?rev=1375358&r1=1375357&r2=1375358&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/list/AbstractListContainer.java
(original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/list/AbstractListContainer.java
Tue Aug 21 00:52:37 2012
@@ -20,12 +20,15 @@ under the License.
 package org.odftoolkit.simple.text.list;
 
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.NoSuchElementException;
 
 import org.odftoolkit.odfdom.dom.element.text.TextListElement;
 import org.odftoolkit.odfdom.pkg.OdfElement;
 import org.odftoolkit.odfdom.pkg.OdfFileDom;
 import org.odftoolkit.simple.Document;
 import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /**
  * AbstractListContainer is an abstract implementation of the ListContainer
@@ -86,57 +89,62 @@ public abstract class AbstractListContai
 	}
 
 	// default iterator to iterate list item.
-	private class SimpleListIterator implements Iterator<List> {
-
-		private OdfElement containerElement;
-		private TextListElement nextListElement;
-		private TextListElement tempListElement;
-
-		private SimpleListIterator(ListContainer container) {
-			containerElement = container.getListContainerElement();
+	private static class SimpleListIterator implements Iterator<List> {
+		
+		private java.util.List<List> allLists;
+		private int index;
+		private List currentList;
+		
+		public SimpleListIterator(ListContainer container) {
+			this.allLists = getLists(container.getListContainerElement().getChildNodes());
 		}
 
 		public boolean hasNext() {
-			tempListElement = findNext(nextListElement);
-			return (tempListElement != null);
+			return this.index < this.allLists.size();
 		}
 
 		public List next() {
-			if (tempListElement != null) {
-				nextListElement = tempListElement;
-				tempListElement = null;
+			if (hasNext()) {
+				this.currentList = allLists.get(index);
+				this.index++;
+				return this.currentList;
 			} else {
-				nextListElement = findNext(nextListElement);
-			}
-			if (nextListElement == null) {
-				return null;
-			} else {
-				return new List(nextListElement);
+				throw new NoSuchElementException();
 			}
 		}
 
 		public void remove() {
-			if (nextListElement == null) {
-				throw new IllegalStateException("please call next() first.");
+			if (this.currentList == null) {
+				throw new IllegalStateException();
+			} else {
+				this.allLists.remove(this.currentList);
+				this.currentList = null;
 			}
-			containerElement.removeChild(nextListElement);
 		}
-
-		private TextListElement findNext(TextListElement nextListElement) {
-			Node child = null;
-			if (nextListElement == null) {
-				child = containerElement.getFirstChild();
+		
+		private java.util.List<List> getLists(NodeList nodes) {
+			java.util.List<List> lists = new LinkedList<List>();
+			int numberOfNodes = nodes.getLength();
+			for (int i = 0; i < numberOfNodes; i++) {
+				Node node = nodes.item(i);
+				lists.addAll(getLists(node));
+			}
+			return lists;
+		}
+		
+		private java.util.List<List> getLists(Node node) {
+			java.util.List<List> lists = new LinkedList<List>();
+			if (node instanceof TextListElement) {
+				TextListElement textListElement = (TextListElement) node;
+				List list = new List(textListElement);
+				lists.add(list);
 			} else {
-				child = nextListElement.getNextSibling();
-			}
-
-			while (child != null) {
-				if (child instanceof TextListElement) {
-					return (TextListElement) child;
-				}
-				child = child.getNextSibling();
+				NodeList childNodes = node.getChildNodes();
+				lists.addAll(getLists(childNodes));
 			}
-			return null;
+			return lists;
 		}
+		
 	}
+	
 }

Modified: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/list/ListItemTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/list/ListItemTest.java?rev=1375358&r1=1375357&r2=1375358&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/list/ListItemTest.java
(original)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/list/ListItemTest.java
Tue Aug 21 00:52:37 2012
@@ -32,6 +32,7 @@ import org.odftoolkit.simple.utils.Resou
 public class ListItemTest {
 
 	private final static String SAMPLE_LIST_DOCUMENT = "testList.odt";
+    private static final String SAMPLE_LIST_DOCUMENT_2 = "testList2.odt";
 	private String[] subItemContents = { "sub list item 1", "sub list item 2", "sub list item
3" };
 
 	@Test
@@ -193,4 +194,27 @@ public class ListItemTest {
 			Assert.fail(e.getMessage());
 		}
 	}
+    
+    @Test
+    public void testListIterator()  {
+        try {
+            // load test list container, which contains 2 lists.
+			TextDocument odtdoc = TextDocument.loadDocument(ResourceUtilities
+					.getTestResourceAsStream(SAMPLE_LIST_DOCUMENT_2));
+			Iterator<List> listIterator = odtdoc.getListIterator();
+            
+            final List bodyList = listIterator.next();
+            Assert.assertEquals("body list should have 2 items", 2, bodyList.size());
+            
+            Assert.assertTrue("iterator should have 1 more list (in the Section)", listIterator.hasNext());
+                        
+            final List sectionList = listIterator.next();
+            Assert.assertEquals("section list should have 3 items", 3, sectionList.size());
+            
+            Assert.assertFalse("iterator should have no more lists", listIterator.hasNext());
+        } catch (Exception e) {
+            Logger.getLogger(ListItemTest.class.getName()).log(Level.SEVERE, null, e);
+			Assert.fail(e.getMessage());
+}
+    }
 }

Added: incubator/odf/trunk/simple/src/test/resources/testList2.odt
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/resources/testList2.odt?rev=1375358&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/odf/trunk/simple/src/test/resources/testList2.odt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message