KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > visitorsTests > ScriptCommentTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Jim Arnell
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/visitorsTests/ScriptCommentTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/31 16:42:33 $
10
// $Revision: 1.4 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.tests.visitorsTests;
28
29 import org.htmlparser.tags.CompositeTag;
30 import org.htmlparser.tags.ScriptTag;
31 import org.htmlparser.Tag;
32 import org.htmlparser.tests.ParserTestCase;
33 import org.htmlparser.visitors.NodeVisitor;
34
35 public class ScriptCommentTest extends ParserTestCase {
36
37     static
38     {
39         System.setProperty ("org.htmlparser.tests.visitorsTests.ScriptCommentTest", "ScriptCommentTest");
40     }
41     
42     private String JavaDoc workingScriptTag =
43         "<script language='javascript'>"
44         + "// I cant handle single quotations\n"
45         + "</script>";
46
47     private String JavaDoc workingHtml =
48         this.workingScriptTag
49         + "<HTML>"
50         + "</HTML>";
51
52     private String JavaDoc failingScriptTag =
53         "<script language='javascript'>"
54         + "// I can't handle single quotations.\n"
55         + "</script>";
56
57     private String JavaDoc failingHtml =
58         this.failingScriptTag
59         + "<HTML>"
60         + "</HTML>";
61
62     private String JavaDoc failingHtml2 =
63         "<HTML>"
64         + this.failingScriptTag
65         + "</HTML>";
66
67     private String JavaDoc anotherFailingScriptTag =
68         "<script language='javascript'>"
69         + "/* I can't handle single quotations. */"
70         + "</script>";
71
72     public ScriptCommentTest(String JavaDoc name) {
73         super(name);
74     }
75
76     public void testTagWorking() throws Exception JavaDoc {
77         createParser(this.workingHtml);
78         ScriptVisitor visitor = new ScriptVisitor();
79         this.parser.visitAllNodesWith(visitor);
80         String JavaDoc scriptNodeHtml = visitor.scriptTag.toHtml();
81         assertEquals("Script parsing worked", this.workingScriptTag, scriptNodeHtml);
82     }
83
84     public void testScriptTagNotWorkingOuter() throws Exception JavaDoc {
85         createParser(this.failingHtml);
86         ScriptVisitor visitor = new ScriptVisitor();
87         this.parser.visitAllNodesWith(visitor);
88         String JavaDoc scriptNodeHtml = visitor.scriptTag.toHtml();
89         assertEquals("Script parsing not working", this.failingScriptTag, scriptNodeHtml);
90     }
91
92     public void testScriptTagNotWorkingInner() throws Exception JavaDoc {
93         createParser(this.failingHtml2);
94         ScriptVisitor visitor = new ScriptVisitor();
95         this.parser.visitAllNodesWith(visitor);
96         String JavaDoc scriptNodeHtml = visitor.scriptTag.toHtml();
97         assertEquals("Script parsing not working", this.failingScriptTag, scriptNodeHtml);
98     }
99
100     public void testScriptTagNotWorkingMultiLine() throws Exception JavaDoc {
101         createParser(this.anotherFailingScriptTag);
102         ScriptVisitor visitor = new ScriptVisitor();
103         this.parser.visitAllNodesWith(visitor);
104         String JavaDoc scriptNodeHtml = visitor.scriptTag.toHtml();
105         assertEquals("Script parsing not working", this.anotherFailingScriptTag, scriptNodeHtml);
106     }
107
108     /**
109      * Implement test case NodeVisitor.
110      */

111     public final class ScriptVisitor extends NodeVisitor {
112
113         /** Keps the only script tag. */
114         public ScriptTag scriptTag;
115
116         /**
117          * Creates a new ScriptVisitor object.
118          */

119         public ScriptVisitor() {
120             super(true, true);
121         }
122
123         /**
124          * @see org.htmlparser.visitors.NodeVisitor
125          */

126         public void visitTag(final Tag n) {
127             if ((null != n.getParent())
128                 || ((n instanceof CompositeTag)
129                     && (null == ((CompositeTag) n).getEndTag()))) {
130
131                 if (n instanceof ScriptTag) {
132                     this.scriptTag = (ScriptTag) n;
133                 }
134             } else {
135                 if (n instanceof ScriptTag) {
136                     this.scriptTag = (ScriptTag) n;
137                 }
138             }
139         }
140     }
141 }
142
143
Popular Tags