KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tags > ScriptTag


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/ScriptTag.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/02/29 01:38:36 $
10
// $Revision: 1.37 $
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.tags;
28
29 import org.htmlparser.Node;
30 import org.htmlparser.scanners.ScriptScanner;
31 import org.htmlparser.util.SimpleNodeIterator;
32
33 /**
34  * A script tag.
35  */

36 public class ScriptTag extends CompositeTag
37 {
38     /**
39      * The set of names handled by this tag.
40      */

41     private static final String JavaDoc[] mIds = new String JavaDoc[] {"SCRIPT"};
42
43     /**
44      * The set of end tag names that indicate the end of this tag.
45      */

46     private static final String JavaDoc[] mEndTagEnders = new String JavaDoc[] {"BODY", "HTML"};
47
48     /**
49      * Script code if different from the page contents.
50      */

51     protected String JavaDoc mCode;
52
53     /**
54      * Create a new script tag.
55      */

56     public ScriptTag ()
57     {
58         setThisScanner (new ScriptScanner ());
59     }
60
61     /**
62      * Return the set of names handled by this tag.
63      * @return The names to be matched that create tags of this type.
64      */

65     public String JavaDoc[] getIds ()
66     {
67         return (mIds);
68     }
69
70     /**
71      * Return the set of end tag names that cause this tag to finish.
72      * @return The names of following end tags that stop further scanning.
73      */

74     public String JavaDoc[] getEndTagEnders ()
75     {
76         return (mEndTagEnders);
77     }
78
79     /**
80      * Get the language attribute value.
81      */

82     public String JavaDoc getLanguage()
83     {
84         return (getAttribute("LANGUAGE"));
85     }
86
87     /**
88      * Get the script code.
89      * Normally this is the contents of the children, but in the rare case that
90      * the script is encoded, this is the plaintext decrypted code.
91      * @return The plaintext or overridden code contents of the tag.
92      */

93     public String JavaDoc getScriptCode ()
94     {
95         String JavaDoc ret;
96         
97         if (null != mCode)
98             ret = mCode;
99         else
100             ret = getChildrenHTML ();
101
102         return (ret);
103     }
104
105     /**
106      * Set the code contents.
107      * @param code The new code contents of this tag.
108      */

109     public void setScriptCode (String JavaDoc code)
110     {
111         mCode = code;
112     }
113
114     /**
115      * Get the type attribute value.
116      */

117     public String JavaDoc getType()
118     {
119         return (getAttribute("TYPE"));
120     }
121
122     /**
123      * Set the language of the script tag.
124      * @param language The new language value.
125      */

126     public void setLanguage (String JavaDoc language)
127     {
128         setAttribute ("LANGUAGE", language);
129     }
130
131     /**
132      * Set the type of the script tag.
133      * @param type The new type value.
134      */

135     public void setType (String JavaDoc type)
136     {
137         setAttribute ("TYPE", type);
138     }
139
140     protected void putChildrenInto(StringBuffer JavaDoc sb)
141     {
142         Node node;
143
144         if (null != getScriptCode ())
145             sb.append (getScriptCode ());
146         else
147             for (SimpleNodeIterator e = children (); e.hasMoreNodes ();)
148             {
149                 node = e.nextNode ();
150                 // eliminate virtual tags
151
// if (!(node.getStartPosition () == node.getEndPosition ()))
152
sb.append (node.toHtml ());
153             }
154     }
155
156     /**
157      * Print the contents of the script tag.
158      */

159     public String JavaDoc toString()
160     {
161         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
162         sb.append("Script Node : \n");
163         if (getLanguage () != null || getType () != null)
164         {
165             sb.append("Properties -->\n");
166             if (getLanguage () != null && getLanguage ().length () !=0)
167                 sb.append("[Language : "+ getLanguage ()+"]\n");
168             if (getType () != null && getType ().length () != 0)
169                 sb.append("[Type : "+ getType ()+"]\n");
170         }
171         sb.append("\n");
172         sb.append("Code\n");
173         sb.append("****\n");
174         sb.append(getScriptCode()+"\n");
175         return sb.toString();
176     }
177 }
178
Popular Tags