ScriptTag.java
1 // HTMLParser Library $Name: v1_6_20060319 $ - 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: 2005/04/10 23:20:45 $ 10 // $Revision: 1.38 $ 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[] mIds = new String[] {"SCRIPT"}; 42 43 /** 44 * The set of end tag names that indicate the end of this tag. 45 */ 46 private static final String[] mEndTagEnders = new String[] {"BODY", "HTML"}; 47 48 /** 49 * Script code if different from the page contents. 50 */ 51 protected String 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[] 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[] getEndTagEnders () 75 { 76 return (mEndTagEnders); 77 } 78 79 /** 80 * Get the <code>LANGUAGE</code> attribute, if any. 81 * @return The scripting language. 82 */ 83 public String getLanguage() 84 { 85 return (getAttribute ("LANGUAGE")); 86 } 87 88 /** 89 * Get the script code. 90 * Normally this is the contents of the children, but in the rare case that 91 * the script is encoded, this is the plaintext decrypted code. 92 * @return The plaintext or overridden code contents of the tag. 93 */ 94 public String getScriptCode () 95 { 96 String ret; 97 98 if (null != mCode) 99 ret = mCode; 100 else 101 ret = getChildrenHTML (); 102 103 return (ret); 104 } 105 106 /** 107 * Set the code contents. 108 * @param code The new code contents of this tag. 109 */ 110 public void setScriptCode (String code) 111 { 112 mCode = code; 113 } 114 115 /** 116 * Get the <code>TYPE</code> attribute, if any. 117 * @return The script mime type. 118 */ 119 public String getType() 120 { 121 return (getAttribute ("TYPE")); 122 } 123 124 /** 125 * Set the language of the script tag. 126 * @param language The new language value. 127 */ 128 public void setLanguage (String language) 129 { 130 setAttribute ("LANGUAGE", language); 131 } 132 133 /** 134 * Set the mime type of the script tag. 135 * @param type The new mime type. 136 */ 137 public void setType (String type) 138 { 139 setAttribute ("TYPE", type); 140 } 141 142 /** 143 * Places the script contents into the provided buffer. 144 * @param sb The buffer to add the script to. 145 */ 146 protected void putChildrenInto (StringBuffer sb) 147 { 148 Node node; 149 150 if (null != getScriptCode ()) 151 sb.append (getScriptCode ()); 152 else 153 for (SimpleNodeIterator e = children (); e.hasMoreNodes ();) 154 { 155 node = e.nextNode (); 156 // eliminate virtual tags 157 // if (!(node.getStartPosition () == node.getEndPosition ())) 158 sb.append (node.toHtml ()); 159 } 160 } 161 162 /** 163 * Print the contents of the script tag suitable for debugging display. 164 * @return The script language or type and code as a string. 165 */ 166 public String toString() 167 { 168 StringBuffer sb = new StringBuffer(); 169 sb.append("Script Node : \n"); 170 if (getLanguage () != null || getType () != null) 171 { 172 sb.append("Properties -->\n"); 173 if (getLanguage () != null && getLanguage ().length () !=0) 174 sb.append("[Language : "+ getLanguage ()+"]\n"); 175 if (getType () != null && getType ().length () != 0) 176 sb.append("[Type : "+ getType ()+"]\n"); 177 } 178 sb.append("\n"); 179 sb.append("Code\n"); 180 sb.append("****\n"); 181 sb.append(getScriptCode()+"\n"); 182 return sb.toString(); 183 } 184 }