⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remarknodeparsertest.java

📁 html 解析处理代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// HTMLParser Library $Name: v1_6 $ - A java-based parser for HTML// http://sourceforge.org/projects/htmlparser// Copyright (C) 2004 Somik Raha//// Revision Control Information//// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/parserHelperTests/RemarkNodeParserTest.java,v $// $Author: derrickoswald $// $Date: 2006/05/27 14:02:28 $// $Revision: 1.49 $//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//package org.htmlparser.tests.parserHelperTests;import org.htmlparser.PrototypicalNodeFactory;import org.htmlparser.Remark;import org.htmlparser.Tag;import org.htmlparser.Text;import org.htmlparser.lexer.Lexer;import org.htmlparser.tests.ParserTestCase;import org.htmlparser.util.ParserException;public class RemarkNodeParserTest extends ParserTestCase{    static    {        System.setProperty ("org.htmlparser.tests.parserHelperTests.RemarkParserTest", "RemarkParserTest");    }    public RemarkNodeParserTest (String name) {        super(name);    }    /**     * Test unparsed remark node.     * The bug being reproduced is this : <BR>     * &lt;!-- saved from url=(0022)http://internet.e-mail --&gt;     * &lt;HTML&gt;     * &lt;HEAD&gt;&lt;META name="title" content="Training Introduction"&gt;     * &lt;META name="subject" content=""&gt;     * &lt;!--         Whats gonna happen now ?     * --&gt;     * &lt;TEST&gt;     * &lt;/TEST&gt;     *     * The above line is incorrectly parsed - the remark is not correctly identified.     * This bug was reported by Serge Kruppa (2002-Feb-08).     */    public void testRemarkBug() throws ParserException    {        createParser(            "<!-- saved from url=(0022)http://internet.e-mail -->\n"+            "<HTML>\n"+            "<HEAD><META name=\"title\" content=\"Training Introduction\">\n"+            "<META name=\"subject\" content=\"\">\n"+            "<!--\n"+            "   Whats gonna happen now ?\n"+            "-->\n"+            "<TEST>\n"+            "</TEST>\n");        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount(15);        // The first node should be a Remark        assertTrue("First node should be a Remark",node[0] instanceof Remark);        Remark Remark = (Remark)node[0];        assertEquals("Text of the Remark #1"," saved from url=(0022)http://internet.e-mail ",Remark.getText());        // The tenth node should be a Remark        assertTrue("Tenth node should be a Remark",node[9] instanceof Remark);        Remark = (Remark)node[9];        assertEquals("Text of the Remark #10","\n   Whats gonna happen now ?\n",Remark.getText());    }    public void testGetText () throws ParserException {        createParser(            "<!-- saved from url=(0022)http://internet.e-mail -->\n"+            "<HTML>\n"+            "<HEAD><META name=\"title\" content=\"Training Introduction\">\n"+            "<META name=\"subject\" content=\"\">\n"+            "<!--\n"+            "   Whats gonna happen now ?\n"+            "-->\n"+            "<TEST>\n"+            "</TEST>\n");        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount(15);        // The first node should be a Remark        assertTrue("First node should be a Remark",node[0] instanceof Remark);        Remark Remark = (Remark)node[0];        assertEquals("Plain Text of the Remark #1"," saved from url=(0022)http://internet.e-mail ",Remark.getText ());        // The tenth node should be a Remark        assertTrue("Tenth node should be a Remark",node[9] instanceof Remark);        Remark = (Remark)node[9];        assertEquals("Plain Text of the Remark #10","\n   Whats gonna happen now ?\n",Remark.getText());    }    public void testToRawString()  throws ParserException {        createParser(            "<!-- saved from url=(0022)http://internet.e-mail -->\n"+            "<HTML>\n"+            "<HEAD><META name=\"title\" content=\"Training Introduction\">\n"+            "<META name=\"subject\" content=\"\">\n"+            "<!--\n"+            "   Whats gonna happen now ?\n"+            "-->\n"+            "<TEST>\n"+            "</TEST>\n");        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount(15);        // The first node should be a Remark        assertTrue("First node should be a Remark",node[0] instanceof Remark);        Remark Remark = (Remark)node[0];        assertStringEquals("Raw String of the Remark #1","<!-- saved from url=(0022)http://internet.e-mail -->",Remark.toHtml());        // The tenth node should be a Remark        assertTrue("Tenth node should be a Remark",node[9] instanceof Remark);        Remark = (Remark)node[9];        assertStringEquals("Raw String of the Remark #6","<!--\n   Whats gonna happen now ?\n-->",Remark.toHtml());    }    public void testNonRemark() throws ParserException {        createParser("&nbsp;<![endif]>");        parseAndAssertNodeCount(2);        // The first node should be a Remark        assertTrue("First node should be a string node",node[0] instanceof Text);        assertTrue("Second node should be a Tag",node[1] instanceof Tag);        Text stringNode = (Text)node[0];        Tag tag = (Tag)node[1];        assertEquals("Text contents","&nbsp;",stringNode.getText());        assertEquals("Tag Contents","![endif]",tag.getText());    }    /**     * This is the simulation of bug report 586756, submitted     * by John Zook.     * If all the comment contains is a blank line, it breaks     * the state     */    public void testRemarkWithBlankLine() throws ParserException {        createParser("<!--\n"+        "\n"+        "-->");        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount(1);        assertTrue("Node should be a Remark",node[0] instanceof Remark);        Remark Remark = (Remark)node[0];        assertEquals("Expected contents","\n\n",Remark.getText());    }    /**     * This is the simulation of a bug report submitted     * by Claude Duguay.     * If it is a comment with nothing in it, parser crashes     */    public void testRemarkWithNothing() throws ParserException {        createParser("<!-->");        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount(1);        assertTrue("Node should be a Remark",node[0] instanceof Remark);        Remark Remark = (Remark)node[0];        assertEquals("Expected contents","",Remark.getText());    }    /**     * Test tag within remark.     * Reproduction of bug reported by John Zook [594301]     * When we have tags like :     * &lt;!-- &lt;A&gt; --&gt;     * it doesent get parsed correctly     */    public void testTagWithinRemark() throws ParserException {        createParser("<!-- \n"+        "<A>\n"+        "bcd -->");        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount(1);        assertTrue("Node should be a Remark",node[0] instanceof Remark);        Remark Remark = (Remark)node[0];        assertStringEquals("Expected contents"," \n<A>\nbcd ",Remark.getText());    }    /**     * Bug reported by John Zook [594301], invalid remark nodes are accepted as remark nodes.     * &lt;<br>     * -<br>     * -<br>     * ssd --&gt;<br>     * This is not supposed to be a Remark     */    public void testInvalidTag() throws ParserException {        createParser("<!\n"+        "-\n"+        "-\n"+        "ssd -->");        parser.setNodeFactory (new PrototypicalNodeFactory (true));        parseAndAssertNodeCount(1);        assertTrue("Node should be a Tag but was "+node[0],node[0] instanceof Tag);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -