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

📄 tagtest.java

📁 html to xml convertor
💻 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/tagTests/TagTest.java,v $// $Author: derrickoswald $// $Date: 2004/09/02 02:28:14 $// $Revision: 1.62 $//// 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.tagTests;import org.htmlparser.Attribute;import org.htmlparser.Node;import org.htmlparser.PrototypicalNodeFactory;import org.htmlparser.Tag;import org.htmlparser.Text;import org.htmlparser.tags.BodyTag;import org.htmlparser.tags.Div;import org.htmlparser.tags.Html;import org.htmlparser.tags.LinkTag;import org.htmlparser.tests.ParserTestCase;import org.htmlparser.util.NodeIterator;import org.htmlparser.util.ParserException;public class TagTest extends ParserTestCase{    static    {        System.setProperty ("org.htmlparser.tests.tagTests.TagTest", "TagTest");    }    private static final boolean JSP_TESTS_ENABLED = false;    public TagTest(String name) {        super(name);    }    /**     * The bug being reproduced is this : <BR>     * &lt;BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc onload=setfocus() text=#000000 <BR>     * vLink=#551a8b&gt;     * The above line is incorrectly parsed in that, the BODY tag is not identified.     */    public void testBodyTagBug1() throws ParserException {        String body = "<BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc "            + "onload=setfocus() text=#000000\nvLink=#551a8b>";        createParser(body);        parseAndAssertNodeCount(1);        // The node should be a body Tag        assertTrue("Node should be a BodyTag",node[0] instanceof BodyTag);        BodyTag tag = (BodyTag)node[0];        String text = tag.toHtml ();        assertEquals("Contents of the tag",body + "</BODY>",text);    }    /**     * The following should be identified as a tag : <BR>     *  &lt;MYTAG abcd\n"+     *      "efgh\n"+     *      "ijkl\n"+     *      "mnop&gt;     * Creation date: (6/17/2001 5:27:42 PM)     */    public void testLargeTagBug() throws ParserException {        String mytag = "MYTAG abcd\n"+            "efgh\n"+            "ijkl\n"+            "mnop";        createParser(            "<" + mytag + ">"        );        parseAndAssertNodeCount(1);        // The node should be an Tag        assertTrue("Node should be a Tag",node[0] instanceof Tag);        Tag tag = (Tag)node[0];        assertEquals("Contents of the tag",mytag,tag.getText());    }    /**     * Bug reported by Gordon Deudney 2002-03-15     * Nested JSP Tags were not working     */    public void testNestedTags() throws ParserException    {        if (JSP_TESTS_ENABLED)        {            String s = "input type=\"text\" value=\"<%=\"test\"%>\" name=\"text\"";            String line = "<"+s+">";            createParser(line);            parseAndAssertNodeCount(1);            assertTrue("The node found should have been an Tag",node[0] instanceof Tag);            Tag tag = (Tag) node[0];            assertEquals("Tag Contents",s,tag.getText());        }    }    /**     * Test parseParameter method     * Created by Kaarle Kaila (august 2001)     * the tag name is here G     */    public void testParseParameter3() throws ParserException {        Tag tag;        Node node=null;        String lin1 = "<DIV class=\"userData\" id=\"oLayout\" name=\"oLayout\"></DIV>";        createParser(lin1);        NodeIterator en = parser.elements();        try {            if (en.hasMoreNodes()) {                node = en.nextNode();                tag = (Tag)node;                String classValue= tag.getAttribute ("CLASS");                assertEquals ("The class value should be ","userData",classValue);            }        }        catch (ClassCastException ce) {            fail("Bad class element = " + node.getClass().getName());        }    }    /**     * Test parseParameter method     * Created by Kaarle Kaila (august 2001)     * the tag name is here A (and should be eaten up by linkScanner)     */    public void testParseParameterA() throws ParserException {        Tag tag;        Tag etag;        Text snode;        Node node=null;        String lin1 = "<A href=\"http://www.iki.fi/kaila\" myParameter yourParameter=\"Kaarle Kaaila\">Kaarle's homepage</A><p>Paragraph</p>";        createParser(lin1);        NodeIterator en = parser.elements();        String a,href,myValue,nice;        try {            if (en.hasMoreNodes()) {                node = en.nextNode();                tag = (Tag)node;                a = ((Attribute)(tag.getAttributesEx ().elementAt (0))).getName ();                href = tag.getAttribute ("HREF");                myValue = tag.getAttribute ("MYPARAMETER");                nice = tag.getAttribute ("YOURPARAMETER");                assertEquals ("Link tag (A)","A",a);                assertEquals ("href value","http://www.iki.fi/kaila",href);                assertEquals ("myparameter value",null,myValue);                assertEquals ("yourparameter value","Kaarle Kaaila",nice);            }            if (!(node instanceof LinkTag)) {                // linkscanner has eaten up this piece                if ( en.hasMoreNodes()) {                    node = en.nextNode();                    snode = (Text)node;                    assertEquals("Value of element","Kaarle's homepage",snode.getText());                }                if (en.hasMoreNodes()) {                    node = en.nextNode();                    etag = (Tag)node;                    assertEquals("endtag of link","/A", etag.getText());                }            }            // testing rest            if (en.hasMoreNodes()) {                node = en.nextNode();                tag = (Tag)node;                assertEquals("following paragraph begins",tag.getText(),"p");            }            if (en.hasMoreNodes()) {                node = en.nextNode();                snode = (Text)node;                assertEquals("paragraph contents","Paragraph",snode.getText());            }            if (en.hasMoreNodes()) {                node = en.nextNode();                etag = (Tag)node;                assertEquals("paragrapg endtag","/p",etag.getText());            }        }        catch (ClassCastException ce) {            fail("Bad class element = " + node.getClass().getName());        }    }    /**     * Test parseParameter method     * Created by Kaarle Kaila (august 2001)     * the tag name is here G     */    public void testParseParameterG() throws ParserException{        Tag tag;        Tag etag;        Text snode;        Node node=null;        String lin1 = "<G href=\"http://www.iki.fi/kaila\" myParameter yourParameter=\"Kaila\">Kaarle's homepage</G><p>Paragraph</p>";        createParser(lin1);        NodeIterator en = parser.elements();        String a,href,myValue,nice;        try {            if (en.hasMoreNodes()) {                node = en.nextNode();                tag = (Tag)node;                a = ((Attribute)(tag.getAttributesEx ().elementAt (0))).getName ();                href = tag.getAttribute ("HREF");                myValue = tag.getAttribute ("MYPARAMETER");                nice = tag.getAttribute ("YOURPARAMETER");                assertEquals ("The tagname should be G",a,"G");                assertEquals ("Check the http address",href,"http://www.iki.fi/kaila");                assertEquals ("myValue is not null",myValue,null);                assertEquals ("The second parameter value",nice,"Kaila");            }            if (en.hasMoreNodes()) {                node = en.nextNode();                snode = (Text)node;                assertEquals("The text of the element",snode.getText(),"Kaarle's homepage");            }            if (en.hasMoreNodes()) {                node = en.nextNode();                etag = (Tag)node;                assertEquals("Endtag is G","/G", etag.getText());            }            // testing rest            if (en.hasMoreNodes()) {                node = en.nextNode();                tag = (Tag)node;                assertEquals("Follow up by p-tag","p", tag.getText());            }            if (en.hasMoreNodes()) {                node = en.nextNode();                snode = (Text)node;                assertEquals("Verify the paragraph text","Paragraph", snode.getText());            }            if (en.hasMoreNodes()) {                node = en.nextNode();                etag = (Tag)node;                assertEquals("Still patragraph endtag","/p", etag.getText());            }        } catch (ClassCastException ce) {            fail("Bad class element = " + node.getClass().getName());        }    }   /**    * Test parseParameter method    * Created by Kaarle Kaila (august 2002)    * the tag name is here A (and should be eaten up by linkScanner)    * Tests elements where = sign is surrounded by spaces    */    public void testParseParameterSpace() throws ParserException{        Tag tag;        Tag etag;        Text snode;        Node node=null;        String lin1 = "<A yourParameter = \"Kaarle\">Kaarle's homepage</A>";        createParser(lin1);        NodeIterator en = parser.elements();        String a,nice;        try {            if (en.hasMoreNodes()) {                node = en.nextNode();                tag = (Tag)node;                a = ((Attribute)(tag.getAttributesEx ().elementAt (0))).getName ();                nice = tag.getAttribute ("YOURPARAMETER");                assertEquals ("Link tag (A)",a,"A");                assertEquals ("yourParameter value","Kaarle",nice);            }            if (!(node instanceof LinkTag)) {                // linkscanner has eaten up this piece                if ( en.hasMoreNodes()) {                    node = en.nextNode();                    snode = (Text)node;                    assertEquals("Value of element","Kaarle's homepage",snode.getText());                }                if (en.hasMoreNodes()) {                    node = en.nextNode();                    etag = (Tag)node;                    assertEquals("Still patragraph endtag","/A",etag.getText());                }            }            // testing rest        } catch (ClassCastException ce) {            fail("Bad class element = " + node.getClass().getName());        }    }    /**     * Reproduction of a bug reported by Annette Doyle     * This is actually a pretty good example of dirty html - we are in a fix     * here, bcos the font tag (the first one) has an erroneous inverted comma. In Tag,     * we ignore anything in inverted commas, and dont if its outside. This kind of messes     * up our parsing almost completely.     */    public void testStrictParsing() throws ParserException {        String testHTML =        "<div align=\"center\">" +            "<font face=\"Arial,\"helvetica,\" sans-serif=\"sans-serif\" size=\"2\" color=\"#FFFFFF\">" +                "<a href=\"/index.html\" link=\"#000000\" vlink=\"#000000\"><font color=\"#FFFFFF\">Home</font></a>\n"+                "<a href=\"/cia/notices.html\" link=\"#000000\" vlink=\"#000000\"><font color=\"#FFFFFF\">Notices</font></a>\n"+                "<a href=\"/cia/notices.html#priv\" link=\"#000000\" vlink=\"#000000\"><font color=\"#FFFFFF\">Privacy</font></a>\n"+                "<a href=\"/cia/notices.html#sec\" link=\"#000000\" vlink=\"#000000\"><font color=\"#FFFFFF\">Security</font></a>\n"+                "<a href=\"/cia/contact.htm\" link=\"#000000\" vlink=\"#000000\"><font color=\"#FFFFFF\">Contact Us</font></a>\n"+                "<a href=\"/cia/sitemap.html\" link=\"#000000\" vlink=\"#000000\"><font color=\"#FFFFFF\">Site Map</font></a>\n"+                "<a href=\"/cia/siteindex.html\" link=\"#000000\" vlink=\"#000000\"><font color=\"#FFFFFF\">Index</font></a>\n"+                "<a href=\"/search\" link=\"#000000\" vlink=\"#000000\"><font color=\"#FFFFFF\">Search</font></a>\n"+            "</font>" +

⌨️ 快捷键说明

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