Updated Branches:
  refs/heads/sandbox/hierarchy-completion 8e0b265f9 -> 9a7bc7c27


tweaks to component tag iterator util


Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/9a7bc7c2
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/9a7bc7c2
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/9a7bc7c2

Branch: refs/heads/sandbox/hierarchy-completion
Commit: 9a7bc7c2790642f42e9296c7f5bc449659a55cd5
Parents: 7de5083
Author: Igor Vaynberg <[email protected]>
Authored: Sun Jan 8 11:05:06 2012 -0800
Committer: Igor Vaynberg <[email protected]>
Committed: Sun Jan 8 11:06:42 2012 -0800

----------------------------------------------------------------------
 .../apache/wicket/markup/ComponentTagIterator.java |   42 ++++++
 .../wicket/markup/ComponentTagIteratorTest.java    |  103 ++++++++++++++-
 2 files changed, 138 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/9a7bc7c2/wicket-core/src/main/java/org/apache/wicket/markup/ComponentTagIterator.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/markup/ComponentTagIterator.java 
b/wicket-core/src/main/java/org/apache/wicket/markup/ComponentTagIterator.java
index 4416452..527264e 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/markup/ComponentTagIterator.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/markup/ComponentTagIterator.java
@@ -26,6 +26,7 @@ public class ComponentTagIterator extends 
ReadOnlyIterator<ComponentTag>
        private final MarkupStream stream;
        private final Stack<ComponentTag> stack;
        private ComponentTag next;
+       private ComponentTag last;
        private boolean end;
 
        public ComponentTagIterator(MarkupStream stream)
@@ -39,6 +40,33 @@ public class ComponentTagIterator extends 
ReadOnlyIterator<ComponentTag>
                return stack;
        }
 
+       public void skipToCloseTag()
+       {
+               if (last == null)
+               {
+                       // if no next() has been called we skip to the end of 
the markup
+                       end = true;
+               }
+               if (last.isClose() || last.isOpenClose())
+               {
+                       throw new IllegalStateException("Cannot skip to the 
closing tag of a closed tag: " +
+                               last);
+               }
+
+               MarkupElement element = null;
+               while ((element = stream.next()) != null)
+               {
+                       if (element.closes(last))
+                       {
+                               next = (ComponentTag)element;
+                               return;
+                       }
+               }
+
+               end = true;
+               next = null;
+       }
+
        @Override
        public boolean hasNext()
        {
@@ -68,6 +96,19 @@ public class ComponentTagIterator extends 
ReadOnlyIterator<ComponentTag>
                next = (ComponentTag)el;
        }
 
+       public ComponentTag peek()
+       {
+               if (end == true)
+               {
+                       return null;
+               }
+               if (next == null)
+               {
+                       findNext();
+               }
+               return next;
+       }
+
        @Override
        public ComponentTag next()
        {
@@ -103,6 +144,7 @@ public class ComponentTagIterator extends 
ReadOnlyIterator<ComponentTag>
                        end = true;
                }
 
+               last = result;
                return result;
        }
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/9a7bc7c2/wicket-core/src/test/java/org/apache/wicket/markup/ComponentTagIteratorTest.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/test/java/org/apache/wicket/markup/ComponentTagIteratorTest.java
 
b/wicket-core/src/test/java/org/apache/wicket/markup/ComponentTagIteratorTest.java
index c2e2c75..0124d2a 100644
--- 
a/wicket-core/src/test/java/org/apache/wicket/markup/ComponentTagIteratorTest.java
+++ 
b/wicket-core/src/test/java/org/apache/wicket/markup/ComponentTagIteratorTest.java
@@ -21,19 +21,24 @@ import static org.hamcrest.Matchers.*;
 import java.util.NoSuchElementException;
 
 import org.apache.wicket.WicketTestCase;
+import org.junit.Before;
 import org.junit.Test;
 
 public class ComponentTagIteratorTest extends WicketTestCase
 {
-       @Test
-       public void test()
-       {
-               Markup markup = Markup.of("<body wicket:id='body'><div 
wicket:id='label'> text </div></body>");
-               MarkupFragment fragment = new MarkupFragment(markup, 0);
-               MarkupStream stream = new MarkupStream(fragment);
+       ComponentTagIterator it;
 
-               ComponentTagIterator it = new ComponentTagIterator(stream);
+       /** sets up a test iterator */
+       @Before
+       public void setupIterator()
+       {
+               it = newIterator("<body wicket:id='body'><div 
wicket:id='label'> text </div></body> tail");
+       }
 
+       /** tests basic iteration */
+       @Test
+       public void next()
+       {
                // <body wicket:id='body'>
 
                assertThat(it.hasNext(), is(true));
@@ -90,6 +95,90 @@ public class ComponentTagIteratorTest extends WicketTestCase
                {
                        // expected
                }
+       }
+
+       /** tests skipping to close tag */
+       @Test
+       public void skipToCloseTag()
+       {
+               it.next();
+               it.skipToCloseTag();
+
+               assertThat(it.hasNext(), is(true));
+
+               ComponentTag close = it.next();
+               assertThat(close.getName(), is("body"));
+               assertThat(close.isClose(), is(true));
+       }
+
+       /** tests skipping to close tag */
+       @Test
+       public void skipToCloseTag_error()
+       {
+               it.next();
+               it.next();
+
+               ComponentTag tag = it.next();
+               assertThat(tag.isClose(), is(true));
+               try
+               {
+                       it.skipToCloseTag();
+                       fail();
+               }
+               catch (IllegalStateException e)
+               {
+                       // expected
+               }
+       }
+
+       /** tests basic iteration */
+       @Test
+       public void peek()
+       {
+               // peek
+
+               ComponentTag tag = it.peek();
+               assertThat(tag, is(not(nullValue())));
+               assertThat(tag.getName(), is("body"));
+
+               // peek again
+
+               assertThat(it.peek(), is(sameInstance(tag)));
+
+               // hasNext and peek again
+
+               assertThat(it.hasNext(), is(true));
+               assertThat(it.peek(), is(sameInstance(tag)));
 
+               // advance
+
+               assertThat(it.next(), is(sameInstance(tag)));
+
+               // peek again
+
+               tag = it.peek();
+               assertThat(tag, is(not(nullValue())));
+               assertThat(tag.getName(), is("div"));
+
+               // advance
+               assertThat(it.next(), is(sameInstance(tag)));
+
+               // peek a closed tag
+               tag = it.peek();
+               assertThat(tag, is(not(nullValue())));
+               assertThat(tag.getName(), is("div"));
+               assertThat(tag.isClose(), is(true));
+
+               // advance
+
+               assertThat(it.next(), is(sameInstance(tag)));
+       }
+
+       private static ComponentTagIterator newIterator(String markup)
+       {
+               MarkupFragment fragment = new MarkupFragment(Markup.of(markup), 
0);
+               MarkupStream stream = new MarkupStream(fragment);
+               return new ComponentTagIterator(stream);
        }
+
 }

Reply via email to