org.htmlparser.scanners.ScriptScanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bboss-htmlparser Show documentation
Show all versions of bboss-htmlparser Show documentation
bboss is a j2ee framework include aop/ioc,mvc,persistent,taglib,rpc,event ,bean-xml serializable and so on.http://www.bbossgroups.com
The newest version!
// HTMLParser Library $Name: v1_5 $ - A java-based parser for HTML
// http://sourceforge.org/projects/htmlparser
// Copyright (C) 2003 Somik Raha
//
// Revision Control Information
//
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/scanners/ScriptScanner.java,v $
// $Author: derrickoswald $
// $Date: 2005/03/12 17:53:10 $
// $Revision: 1.63 $
//
// 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.scanners;
import java.util.ArrayList;
import java.util.List;
import org.htmlparser.Attribute;
import org.htmlparser.Node;
import org.htmlparser.Tag;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.tags.ScriptTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
/**
* The ScriptScanner handles script CDATA.
*/
public class ScriptScanner
extends
CompositeTagScanner
{
/**
* Strict parsing of CDATA flag.
* If this flag is set true, the parsing of script is performed without
* regard to quotes. This means that erroneous script such as:
*
* document.write("</script>");
*
* will be parsed in strict accordance with appendix
*
* B.3.2 Specifying non-HTML data of the
* HTML 4.01 Specification and
* hence will be split into two or more nodes. Correct javascript would
* escape the ETAGO:
*
* document.write("<\/script>");
*
* If true, CDATA parsing will stop at the first ETAGO ("</") no matter
* whether it is quoted or not. If false, balanced quotes (either single or
* double) will shield an ETAGO. Beacuse of the possibility of quotes within
* single or multiline comments, these are also parsed. In most cases,
* users prefer non-strict handling since there is so much broken script
* out in the wild.
*/
public static boolean STRICT = false;
/**
* Create a script scanner.
*/
public ScriptScanner()
{
}
/**
* Scan for script.
* Accumulates text from the page, until </[a-zA-Z] is encountered.
* @param tag The tag this scanner is responsible for.
* @param lexer The source of CDATA.
* @param stack The parse stack, not used.
*/
public Tag scan (Tag tag, Lexer lexer, NodeList stack)
throws ParserException
{
String language;
String code;
Node content;
int position;
Node node;
Attribute attribute;
List vector;
if (tag instanceof ScriptTag)
{
language = ((ScriptTag)tag).getLanguage ();
if ((null != language) &&
(language.equalsIgnoreCase ("JScript.Encode") ||
language.equalsIgnoreCase ("VBScript.Encode")))
{
code = ScriptDecoder.Decode (lexer.getPage (), lexer.getCursor ());
((ScriptTag)tag).setScriptCode (code);
}
}
content = lexer.parseScriptCDATA(!STRICT);
position = lexer.getPosition ();
node = lexer.nextNode (false);
if (null != node)
if (!(node instanceof Tag) || !( ((Tag)node).isEndTag ()
&& ((Tag)node).getTagName ().equals (tag.getIds ()[0])))
{
lexer.setPosition (position);
node = null;
}
// build new end tag if required
if (null == node)
{
attribute = new Attribute ("/script", null);
vector = new ArrayList ();
vector.add (attribute);
node = lexer.getNodeFactory ().createTagNode (
lexer.getPage (), position, position, vector);
}
tag.setEndTag ((Tag)node);
if (null != content)
{
tag.setChildren (new NodeList (content));
content.setParent (tag);
}
node.setParent (tag);
tag.doSemanticAction ();
return (tag);
}
}