From: <tri...@us...> - 2007-08-22 14:38:04
|
Revision: 15 http://staticwiki.svn.sourceforge.net/staticwiki/?rev=15&view=rev Author: triathlon98 Date: 2007-08-22 07:38:00 -0700 (Wed, 22 Aug 2007) Log Message: ----------- fix problems in MarkupSplitter Modified Paths: -------------- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/MarkupSplitter.java trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/MarkupSplitterTest.java Modified: trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/MarkupSplitter.java =================================================================== --- trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/MarkupSplitter.java 2007-08-22 10:34:20 UTC (rev 14) +++ trunk/staticwiki-renderer/src/main/java/org/staticwiki/wiki/MarkupSplitter.java 2007-08-22 14:38:00 UTC (rev 15) @@ -19,16 +19,18 @@ * Splits a string, providing 2 characters like '!' and '!', '[' and ']' as separators * * @author Florin + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> */ public class MarkupSplitter implements Iterator<CharSequence>, Iterable<CharSequence> { - CharSequence data; - CharSequence markup; - int curPos; - int nextPos; - char markupBegin, markupEnd; - boolean inside; + protected CharSequence data; + protected char markupBegin, markupEnd; + protected int nextPos; + protected int foundPart, nextPart; + protected int inBegin, inEnd; + protected int outBegin, outEnd; + protected boolean hasNextCalled; public MarkupSplitter( CharSequence input, char markupBegin, char markupEnd ) { @@ -39,43 +41,74 @@ public boolean hasNext() { - markup = null; + if ( data == null ) return false; - nextPos = curPos; - char terminator=(inside)?markupEnd:markupBegin; + if ( foundPart > nextPart ) return true; // we already know the answer - while ( nextPos < data.length() ) + if ( ( foundPart & 1 ) == 1 ) { - char c = data.charAt( nextPos ); - if ( c == terminator ) + // check whether there is an "inside" part + if ( inEnd == 0 || inBegin > data.length() ) return false; + foundPart++; + } + else + { + if ( nextPos >= data.length() ) return false; + outBegin = nextPos; + while ( nextPos < data.length() ) { - break; + char c = data.charAt( nextPos ); + if ( c == markupBegin ) break; + nextPos++; } + outEnd = nextPos; nextPos++; + inBegin = nextPos; + while ( nextPos < data.length() ) + { + char c = data.charAt( nextPos ); + if ( c == markupEnd ) break; + nextPos++; + } + if ( nextPos == data.length() ) + { + outEnd = nextPos; + inBegin = inEnd = 0; + } + else + { + inEnd = nextPos; + nextPos++; + } + foundPart++; } - if ( nextPos<data.length() || !inside ) - { - markup = data.subSequence( curPos, nextPos ); - nextPos++; - return true; - } - return false; + return true; } public boolean isNextInsideMarkup() { - return inside; + return ( ( nextPart & 1 ) == 1 ); } public CharSequence next() { - if ( markup != null ) + if ( nextPart == foundPart ) { - curPos = nextPos; - inside=!inside; - return markup; + if ( !hasNext() ) throw new NoSuchElementException(); } - throw new NoSuchElementException(); + CharSequence res; + if ( ( nextPart & 1 ) == 1 ) + { + // inside part + res = data.subSequence( inBegin, inEnd ); + } + else + { + // outside part + res = data.subSequence( outBegin, outEnd ); + } + nextPart++; + return res; } public void remove() Modified: trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/MarkupSplitterTest.java =================================================================== --- trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/MarkupSplitterTest.java 2007-08-22 10:34:20 UTC (rev 14) +++ trunk/staticwiki-renderer/src/test/java/org/staticwiki/wiki/MarkupSplitterTest.java 2007-08-22 14:38:00 UTC (rev 15) @@ -16,29 +16,72 @@ import java.util.List; import java.util.ArrayList; +import java.util.NoSuchElementException; /** * Test for MarkupSplitter * * @author Florin + * @author <a href="mailto:jo...@pr...">Joachim Van der Auwera</a> */ public class MarkupSplitterTest extends TestCase { + public void testNullHasNext() + { + MarkupSplitter splitter = new MarkupSplitter( null, 'x', 'y' ); + assertFalse( splitter.hasNext() ); + } + + public void testNullNext() + { + MarkupSplitter splitter = new MarkupSplitter( null, 'x', 'y' ); + try + { + splitter.next(); + fail( "should have failed" ); + } + catch ( NoSuchElementException nse ) + {/* correct */} + } + + public void testMultipleHasNext() + { + MarkupSplitter splitter = new MarkupSplitter( "aaa", 'x', 'y' ); + assertTrue( splitter.hasNext() ); + assertTrue( splitter.hasNext() ); + assertTrue( splitter.hasNext() ); + assertEquals( "aaa", splitter.next() ); + assertFalse( splitter.hasNext() ); + } + + public void testNextOnly() + { + MarkupSplitter splitter = new MarkupSplitter( "here is no image", '!', '!' ); + assertEquals( "here is no image", splitter.next() ); + try + { + splitter.next(); + fail( "should have failed" ); + } + catch ( NoSuchElementException nse ) + {/* correct */} + } + public void testDifferentBeginEnd() { MarkupSplitter splitter = - new MarkupSplitter( "here is a link [the link] and another one: [other link]", '[', ']' ); + new MarkupSplitter( "here is a link [the link] and another [other link] one", '[', ']' ); List<String> itemsToRender = new ArrayList<String>(); List<String> itemsToCopy = new ArrayList<String>(); getItems( splitter, itemsToRender, itemsToCopy ); - assertEquals( itemsToRender.size(), 2 ); - assertEquals( itemsToCopy.size(), 3 ); - assertEquals( itemsToRender.get( 0 ), "the link" ); - assertEquals( itemsToRender.get( 1 ), "other link" ); - assertEquals( itemsToCopy.get( 0 ), "here is a link " ); - assertEquals( itemsToCopy.get( 1 ), " and another one: " ); - assertEquals( itemsToCopy.get( 2 ), "" ); + assertEquals( 2, itemsToRender.size() ); + assertEquals( 3, itemsToCopy.size() ); + assertEquals( "the link", itemsToRender.get( 0 ) ); + assertEquals( "other link", itemsToRender.get( 1 ) ); + assertEquals( "here is a link ", itemsToCopy.get( 0 ) ); + assertEquals( " and another ", itemsToCopy.get( 1 ) ); + assertEquals( " one", itemsToCopy.get( 2 ) ); } public void testSameBeginEnd() @@ -48,57 +91,54 @@ List<String> itemsToRender = new ArrayList<String>(); List<String> itemsToCopy = new ArrayList<String>(); getItems( splitter, itemsToRender, itemsToCopy ); - assertEquals( itemsToRender.size(), 2 ); - assertEquals( itemsToCopy.size(), 3 ); - assertEquals( itemsToRender.get( 0 ), "theimage.jpg" ); - assertEquals( itemsToRender.get( 1 ), "otherimage.jpg" ); - assertEquals( itemsToCopy.get( 0 ), "here is an image " ); - assertEquals( itemsToCopy.get( 1 ), " and another one: " ); - assertEquals( itemsToCopy.get( 2 ), "" ); + assertEquals( 2, itemsToRender.size() ); + assertEquals( 2, itemsToCopy.size() ); + assertEquals( "theimage.jpg", itemsToRender.get( 0 ) ); + assertEquals( "otherimage.jpg", itemsToRender.get( 1 ) ); + assertEquals( "here is an image ", itemsToCopy.get( 0 ) ); + assertEquals( " and another one: ", itemsToCopy.get( 1 ) ); } public void testDifferentBeginEndMessedUp() { MarkupSplitter splitter = - new MarkupSplitter( - "[]here is a link [the [link] and another one: [other link[] and another one, not closed [aaa", '[', - ']' ); + new MarkupSplitter( "[]here is a link [the [link] another [other link[] and unclosed [aaa", '[', ']' ); List<String> itemsToRender = new ArrayList<String>(); List<String> itemsToCopy = new ArrayList<String>(); getItems( splitter, itemsToRender, itemsToCopy ); - assertEquals( itemsToRender.size(), 3 ); - assertEquals( itemsToCopy.size(), 4 ); + assertEquals( 3, itemsToRender.size() ); + assertEquals( 4, itemsToCopy.size() ); - assertEquals( itemsToRender.get( 0 ), "" ); - assertEquals( itemsToRender.get( 1 ), "the [link" ); - assertEquals( itemsToRender.get( 2 ), "other link[" ); + assertEquals( "", itemsToRender.get( 0 ), "" ); + assertEquals( "the [link", itemsToRender.get( 1 ) ); + assertEquals( "other link[", itemsToRender.get( 2 ) ); - assertEquals( itemsToCopy.get( 0 ), "" ); - assertEquals( itemsToCopy.get( 1 ), "here is a link " ); - assertEquals( itemsToCopy.get( 2 ), " and another one: " ); - assertEquals( itemsToCopy.get( 3 ), " and another one, not closed " ); + assertEquals( "", itemsToCopy.get( 0 ) ); + assertEquals( "here is a link ", itemsToCopy.get( 1 ) ); + assertEquals( " another ", itemsToCopy.get( 2 ) ); + assertEquals( " and unclosed [aaa", itemsToCopy.get( 3 ) ); } public void testSameBeginEndMessedUp() { MarkupSplitter splitter = - new MarkupSplitter( "!!here is an image !theim!age.jpg! and !another one, not closed: !otherimage.jpg", '!', '!' ); + new MarkupSplitter( "!!here is an image !theim!age.jpg! and !another unclosed: !otherimage.jpg", '!', '!' ); List<String> itemsToRender = new ArrayList<String>(); List<String> itemsToCopy = new ArrayList<String>(); getItems( splitter, itemsToRender, itemsToCopy ); - assertEquals( itemsToRender.size(), 3 ); - assertEquals( itemsToCopy.size(), 4 ); + assertEquals( 3, itemsToRender.size() ); + assertEquals( 4, itemsToCopy.size() ); - assertEquals( itemsToRender.get( 0 ), "" ); - assertEquals( itemsToRender.get( 1 ), "theim" ); - assertEquals( itemsToRender.get( 2 ), " and " ); + assertEquals( "", itemsToRender.get( 0 ) ); + assertEquals( "theim", itemsToRender.get( 1 ) ); + assertEquals( " and ", itemsToRender.get( 2 ) ); - assertEquals( itemsToCopy.get(0), ""); - assertEquals( itemsToCopy.get(1), "here is an image "); - assertEquals( itemsToCopy.get(2), "age.jpg"); - assertEquals( itemsToCopy.get(3), "another one, not closed: "); + assertEquals( "", itemsToCopy.get( 0 ) ); + assertEquals( "here is an image ", itemsToCopy.get( 1 ) ); + assertEquals( "age.jpg", itemsToCopy.get( 2 ) ); + assertEquals( "another unclosed: !otherimage.jpg", itemsToCopy.get( 3 ) ); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |