You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2012/01/08 08:15:30 UTC

[2/2] git commit: new tag iterator util

new 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/28c1fe4a
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/28c1fe4a
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/28c1fe4a

Branch: refs/heads/sandbox/queueing
Commit: 28c1fe4ac6171397332f80814154b0fb2edfe885
Parents: 30162c6
Author: Igor Vaynberg <ig...@gmail.com>
Authored: Sat Jan 7 23:13:15 2012 -0800
Committer: Igor Vaynberg <ig...@gmail.com>
Committed: Sat Jan 7 23:13:15 2012 -0800

----------------------------------------------------------------------
 .../apache/wicket/markup/ComponentTagIterator.java |  110 +++++++++++++++
 .../wicket/markup/ComponentTagIteratorTest.java    |   95 +++++++++++++
 2 files changed, 205 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/28c1fe4a/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
new file mode 100644
index 0000000..4416452
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/ComponentTagIterator.java
@@ -0,0 +1,110 @@
+/*
+ * 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 regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup;
+
+import java.util.NoSuchElementException;
+import java.util.Stack;
+
+import org.apache.wicket.util.collections.ReadOnlyIterator;
+
+public class ComponentTagIterator extends ReadOnlyIterator<ComponentTag>
+{
+	private final MarkupStream stream;
+	private final Stack<ComponentTag> stack;
+	private ComponentTag next;
+	private boolean end;
+
+	public ComponentTagIterator(MarkupStream stream)
+	{
+		this.stream = stream;
+		stack = new Stack<ComponentTag>();
+	}
+
+	public Stack<ComponentTag> stack()
+	{
+		return stack;
+	}
+
+	@Override
+	public boolean hasNext()
+	{
+		if (end == true)
+		{
+			return false;
+		}
+
+		if (next != null)
+		{
+			return true;
+		}
+
+		findNext();
+
+		return next != null;
+	}
+
+	private void findNext()
+	{
+		next = null;
+		MarkupElement el = stream.get();
+		while (el != null && !(el instanceof ComponentTag))
+		{
+			el = stream.next();
+		}
+		next = (ComponentTag)el;
+	}
+
+	@Override
+	public ComponentTag next()
+	{
+		if (end)
+		{
+			throw new NoSuchElementException();
+		}
+
+		if (next == null)
+		{
+			findNext();
+		}
+
+		if (stack.size() > 0 && stack.peek().isOpenClose())
+		{
+			stack.pop();
+		}
+
+		if (next.isOpen() || next.isOpenClose())
+		{
+			stack.push(next);
+		}
+		else
+		{
+			stack.pop();
+		}
+
+		ComponentTag result = next;
+		next = null;
+
+		if (stream.next() == null)
+		{
+			end = true;
+		}
+
+		return result;
+	}
+
+
+}

http://git-wip-us.apache.org/repos/asf/wicket/blob/28c1fe4a/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
new file mode 100644
index 0000000..c2e2c75
--- /dev/null
+++ b/wicket-core/src/test/java/org/apache/wicket/markup/ComponentTagIteratorTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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 regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup;
+
+import static org.hamcrest.Matchers.*;
+
+import java.util.NoSuchElementException;
+
+import org.apache.wicket.WicketTestCase;
+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 = new ComponentTagIterator(stream);
+
+		// <body wicket:id='body'>
+
+		assertThat(it.hasNext(), is(true));
+		assertThat(it.hasNext(), is(true));
+
+		ComponentTag body = it.next();
+		assertThat(body, is(not(nullValue())));
+		assertThat(body.getId(), is("body"));
+
+		assertThat(it.stack().size(), is(1));
+		assertThat(it.stack(), contains(body));
+
+		// <div wicket:id='label'>
+
+		assertThat(it.hasNext(), is(true));
+		assertThat(it.hasNext(), is(true));
+
+		ComponentTag label = it.next();
+		assertThat(label, is(not(nullValue())));
+		assertThat(label.getId(), is("label"));
+
+		assertThat(it.stack().size(), is(2));
+		assertThat(it.stack(), contains(body, label));
+
+		// </div>
+
+		assertThat(it.hasNext(), is(true));
+
+		ComponentTag label2 = it.next();
+		assertThat(label2, is(not(nullValue())));
+
+		assertThat(it.stack().size(), is(1));
+		assertThat(it.stack(), contains(body));
+
+		// </body>
+
+		assertThat(it.hasNext(), is(true));
+
+		ComponentTag body2 = it.next();
+		assertThat(body2, is(not(nullValue())));
+
+		assertThat(it.stack().size(), is(0));
+
+		// end
+
+		assertThat(it.hasNext(), is(false));
+
+		try
+		{
+			it.next();
+			fail();
+		}
+		catch (NoSuchElementException e)
+		{
+			// expected
+		}
+
+	}
+}