📄 compositetagscannertest.java
字号:
new Tag[] { new CustomTag (), new AnotherTag (true), })); parseAndAssertNodeCount(2); assertType("node",CustomTag.class,node[0]); CustomTag customTag = (CustomTag)node[0]; assertEquals("child count",1,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",0,customTag.getEndingLineNumber()); AnotherTag anotherTag = (AnotherTag)customTag.childAt(0); assertEquals("anotherTag child count",1,anotherTag.getChildCount()); Text stringNode = (Text)anotherTag.childAt(0); assertStringEquals("anotherTag child text","something",stringNode.toPlainTextString()); assertStringEquals( "first custom tag html", "<custom><another>something</another></custom>", customTag.toHtml() ); customTag = (CustomTag)node[1]; assertStringEquals( "second custom tag html", "<custom><another>else</another></custom>", customTag.toHtml() ); } public void testCompositeTagCorrectionWithSplitLines() throws ParserException { createParser( "<custom>" + "<another><abcdefg>\n" + "</custom>" ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (), new AnotherTag (true), })); parseAndAssertNodeCount(1); assertType("node",CustomTag.class,node[0]); CustomTag customTag = (CustomTag)node[0]; assertEquals("child count",1,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); AnotherTag anotherTag = (AnotherTag)customTag.childAt(0); assertEquals("anotherTag child count",2,anotherTag.getChildCount()); assertEquals("anotherTag end loc",27,anotherTag.getEndTag ().getEndPosition ()); assertEquals("custom end tag begin loc",27,customTag.getEndTag().getStartPosition ()); assertEquals("custom end tag end loc",36,customTag.getEndTag().getEndPosition ()); } public void testCompositeTagWithSelfChildren() throws ParserException { String tag1 = "<custom>"; String tag2 = "<custom>something</custom>"; String tag3 = "</custom>"; createParser(tag1 + tag2 + tag3); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (false), new AnotherTag (false), })); parseAndAssertNodeCount(3); CustomTag customTag = (CustomTag)node[0]; assertEquals("child count",0,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertStringEquals( "first custom tag html", tag1 + "</custom>", customTag.toHtml() ); customTag = (CustomTag)node[1]; assertStringEquals( "second custom tag html", tag2, customTag.toHtml() ); Tag endTag = (Tag)node[2]; assertStringEquals( "third custom tag html", tag3, endTag.toHtml() ); } public void testParentConnections() throws ParserException { String tag1 = "<custom>"; String tag2 = "<custom>something</custom>"; String tag3 = "</custom>"; createParser(tag1 + tag2 + tag3); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (false), new AnotherTag (false), })); parseAndAssertNodeCount(3); CustomTag customTag = (CustomTag)node[0]; assertStringEquals( "first custom tag html", tag1 + "</custom>", customTag.toHtml() ); assertNull( "first custom tag should have no parent", customTag.getParent() ); customTag = (CustomTag)node[1]; assertStringEquals( "second custom tag html", tag2, customTag.toHtml() ); assertNull( "second custom tag should have no parent", customTag.getParent() ); Node firstChild = customTag.childAt(0); assertType("firstChild",Text.class,firstChild); Node parent = firstChild.getParent(); assertNotNull("first child parent should not be null",parent); assertSame("parent and custom tag should be the same",customTag,parent); Tag endTag = (Tag)node[2]; assertStringEquals( "third custom tag html", tag3, endTag.toHtml() ); assertNull( "end tag should have no parent", endTag.getParent() ); } public void testUrlBeingProvidedToCreateTag() throws ParserException { createParser("<Custom/>","http://www.yahoo.com"); parser.setNodeFactory (new PrototypicalNodeFactory (new CustomTag ())); parseAndAssertNodeCount(1); assertStringEquals("url","http://www.yahoo.com",((AbstractNode)node[0]).getPage ().getUrl ()); } public void testComplexNesting() throws ParserException { createParser( "<custom>" + "<custom>" + "<another>" + "</custom>" + "<custom>" + "<another>" + "</custom>" + "</custom>" ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (), new AnotherTag (false), })); parseAndAssertNodeCount(1); assertType("root node",CustomTag.class, node[0]); CustomTag root = (CustomTag)node[0]; assertNodeCount("child count",2,root.getChildrenAsNodeArray()); Node child = root.childAt(0); assertType("child",CustomTag.class,child); CustomTag customChild = (CustomTag)child; assertNodeCount("grand child count",1,customChild.getChildrenAsNodeArray()); Node grandchild = customChild.childAt(0); assertType("grandchild",AnotherTag.class,grandchild); } public void testDisallowedChildren() throws ParserException { createParser( "<custom>\n" + "Hello" + "<custom>\n" + "World" + "<custom>\n" + "Hey\n" + "</custom>" ); parser.setNodeFactory (new PrototypicalNodeFactory (new CustomTag (false))); parseAndAssertNodeCount(3); for (int i=0;i<nodeCount;i++) { assertType("node "+i,CustomTag.class,node[i]); } } public static class CustomScanner extends CompositeTagScanner { private static final String MATCH_NAME [] = { "CUSTOM" }; public CustomScanner() { } public String[] getID() { return MATCH_NAME; } } public static class AnotherScanner extends CompositeTagScanner { private static final String MATCH_NAME [] = { "ANOTHER" }; public AnotherScanner() { } public String[] getID() { return MATCH_NAME; } protected boolean isBrokenTag() { return false; } } public static class CustomTag extends CompositeTag { /** * The set of names handled by this tag. */ private static final String[] mIds = new String[] {"CUSTOM"}; protected String[] mEnders; /** * The default scanner for custom tags. */ protected final static CustomScanner mCustomScanner = new CustomScanner (); public CustomTag () { this (true); } public CustomTag (boolean selfChildrenAllowed) { if (selfChildrenAllowed) mEnders = new String[0]; else mEnders = mIds; setThisScanner (mCustomScanner); } /** * Return the set of names handled by this tag. * @return The names to be matched that create tags of this type. */ public String[] getIds () { return (mIds); } /** * Return the set of tag names that cause this tag to finish. * @return The names of following tags that stop further scanning. */ public String[] getEnders () { return (mEnders); } } public static class AnotherTag extends CompositeTag { /** * The set of names handled by this tag. */ private static final String[] mIds = new String[] {"ANOTHER"}; /** * The set of tag names that indicate the end of this tag. */ private final String[] mEnders; /** * The set of end tag names that indicate the end of this tag. */ private final String[] mEndTagEnders; /** * The default scanner for custom tags. */ protected final static AnotherScanner mAnotherScanner = new AnotherScanner (); public AnotherTag (boolean acceptCustomTagsButDontAcceptCustomEndTags) { if (acceptCustomTagsButDontAcceptCustomEndTags) { mEnders = new String[0]; mEndTagEnders = new String[] {"CUSTOM"}; } else { mEnders = new String[] {"CUSTOM"}; mEndTagEnders = new String[] {"CUSTOM"}; } setThisScanner (mAnotherScanner); } /** * Return the set of names handled by this tag. * @return The names to be matched that create tags of this type. */ public String[] getIds () { return (mIds); } /** * Return the set of tag names that cause this tag to finish. * @return The names of following tags that stop further scanning. */ public String[] getEnders () { return (mEnders); } /** * Return the set of end tag names that cause this tag to finish. * @return The names of following end tags that stop further scanning. */ public String[] getEndTagEnders () { return (mEndTagEnders); } } /** * Extracted from "http://scores.nba.com/games/20031029/scoreboard.html" * which has a lot of table columns with unclosed DIV tags because the * closing DIV doesn't have a slash. * This caused java.lang.StackOverflowError on Windows. * Tests the new non-recursive CompositeTagScanner with the walk back * through the parse stack. * See also Bug #750117 StackOverFlow while Node-Iteration and * others. */ public void testInvalidNesting () throws ParserException { String html = "<table cellspacing=\"2\" cellpadding=\"0\" border=\"0\" width=\"600\">\n" + "<tr>\n" + "<td><div class=\"ScoreBoardSec\"> <a target=\"_parent\" class=\"ScoreBoardSec\" href=\"http://www.nba.com/heat/\">Heat</a><div></td>\n" + "</tr>\n" + "</table>"; createParser (html); parseAndAssertNodeCount (1); assertType ("table", TableTag.class, node[0]); TableTag table = (TableTag)node[0]; assertTrue ("table should have 3 nodes", 3 == table.getChildCount ()); assertType ("row", TableRow.class, table.childAt (1)); TableRow row = (TableRow)table.childAt (1); assertTrue ("row should have 3 nodes", 3 == row.getChildCount ()); assertType ("column", TableColumn.class, row.childAt (1)); TableColumn column = (TableColumn)row.childAt (1); assertTrue ("column should have 1 node", 1 == column.getChildCount ()); assertType ("element", Div.class, column.childAt (0)); Div div = (Div)column.childAt (0); assertTrue ("div should have 3 nodes", 3 == div.getChildCount ()); assertType ("link", LinkTag.class, div.childAt (1)); LinkTag link = (LinkTag)div.childAt (1); assertTrue ("link contents", link.getLink ().equals ("http://www.nba.com/heat/")); assertType ("bogus div", Div.class, div.childAt (2)); assertTrue ("bogus div should have no children", 0 == ((Div)div.childAt (2)).getChildCount ()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -