|
HTML Parser Home Page | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.htmlparser.Parser
public class Parser
The main parser class.
This is the primary class of the HTML Parser library. It provides
constructors that take a String
,
a URLConnection
, or a
Lexer
. In the case of a String,
a check is made to see if the first non-whitespace character is a <, in
which case it is assumed to be HTML. Otherwise an
attempt is made to open it as a URL, and if that fails it assumes it is a
local disk file. If you want to parse a String after using the
no-args
constructor, use
setInputHTML()
, or you can use createParser(java.lang.String, java.lang.String)
.
The Parser provides access to the contents of the
page, via a NodeIterator
, a
NodeList
or a
NodeVisitor
.
Typical usage of the parser is:
Parser parser = new Parser ("http://whatever");
NodeList list = parser.parse (null);
// do something with your list of nodes.
What types of nodes and what can be done with them is dependant on the
setup, but in general a node can be converted back to HTML and it's
children (enclosed nodes) and parent can be obtained, because nodes are
nested. See the Node
interface.
For example, if the URL contains:
<html>
<head>
<title>Mondays -- What a bad idea.</title>
</head>
<body BGCOLOR="#FFFFFF">
Most people have a pathological hatred of Mondays...
</body>
</html>
and the example code above is used, the list contain only one element, the
<html> node. This node is a tag
,
which is an object of class
Html
if the default NodeFactory
(a PrototypicalNodeFactory
) is used.
To get at further content, the children of the top
level nodes must be examined. When digging through a node list one must be
conscious of the possibility of whitespace between nodes, e.g. in the example
above:
would print out 5, not 2, because there are newlines after <html>,
</head> and </body> that are children of the HTML node
besides the <head> and <body> nodes.
Node node = list.elementAt (0);
NodeList sublist = node.getChildren ();
System.out.println (sublist.size ());
Because processing nodes is so common, two interfaces are provided to
ease this task, filters
and visitors
.
Field Summary | |
---|---|
static ParserFeedback |
DEVNULL
A quiet message sink. |
protected ParserFeedback |
mFeedback
Feedback object. |
protected Lexer |
mLexer
The html lexer associated with this parser. |
static ParserFeedback |
STDOUT
A verbose message sink. |
static String |
VERSION_DATE
The date of the version ("Jun 10, 2006"). |
static double |
VERSION_NUMBER
The floating point version number (1.6). |
static String |
VERSION_STRING
The display version ("1.6 (Release Build Jun 10, 2006)"). |
static String |
VERSION_TYPE
The type of version ("Release Build"). |
Constructor Summary | |
---|---|
Parser()
Zero argument constructor. |
|
Parser(Lexer lexer)
Construct a parser using the provided lexer. |
|
Parser(Lexer lexer,
ParserFeedback fb)
Construct a parser using the provided lexer and feedback object. |
|
Parser(String resource)
Creates a Parser object with the location of the resource (URL or file). |
|
Parser(String resource,
ParserFeedback feedback)
Creates a Parser object with the location of the resource (URL or file) You would typically create a DefaultHTMLParserFeedback object and pass it in. |
|
Parser(URLConnection connection)
Construct a parser using the provided URLConnection. |
|
Parser(URLConnection connection,
ParserFeedback fb)
Constructor for custom HTTP access. |
Method Summary | |
---|---|
static Parser |
createParser(String html,
String charset)
Creates the parser on an input string. |
NodeIterator |
elements()
Returns an iterator (enumeration) over the html nodes. |
NodeList |
extractAllNodesThatMatch(NodeFilter filter)
Extract all nodes matching the given filter. |
URLConnection |
getConnection()
Return the current connection. |
static ConnectionManager |
getConnectionManager()
Get the connection manager all Parsers use. |
String |
getEncoding()
Get the encoding for the page this parser is reading from. |
ParserFeedback |
getFeedback()
Returns the current feedback object. |
Lexer |
getLexer()
Returns the lexer associated with the parser. |
NodeFactory |
getNodeFactory()
Get the current node factory. |
String |
getURL()
Return the current URL being parsed. |
static String |
getVersion()
Return the version string of this parser. |
static double |
getVersionNumber()
Return the version number of this parser. |
static void |
main(String[] args)
The main program, which can be executed from the command line. |
NodeList |
parse(NodeFilter filter)
Parse the given resource, using the filter provided. |
void |
postConnect(HttpURLConnection connection)
Called just after calling connect. |
void |
preConnect(HttpURLConnection connection)
Called just prior to calling connect. |
void |
reset()
Reset the parser to start from the beginning again. |
void |
setConnection(URLConnection connection)
Set the connection for this parser. |
static void |
setConnectionManager(ConnectionManager manager)
Set the connection manager all Parsers use. |
void |
setEncoding(String encoding)
Set the encoding for the page this parser is reading from. |
void |
setFeedback(ParserFeedback fb)
Sets the feedback object used in scanning. |
void |
setInputHTML(String inputHTML)
Initializes the parser with the given input HTML String. |
void |
setLexer(Lexer lexer)
Set the lexer for this parser. |
void |
setNodeFactory(NodeFactory factory)
Set the current node factory. |
void |
setResource(String resource)
Set the html, a url, or a file. |
void |
setURL(String url)
Set the URL for this parser. |
void |
visitAllNodesWith(NodeVisitor visitor)
Apply the given visitor to the current page. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final double VERSION_NUMBER
public static final String VERSION_TYPE
public static final String VERSION_DATE
public static final String VERSION_STRING
protected ParserFeedback mFeedback
protected Lexer mLexer
public static final ParserFeedback DEVNULL
public static final ParserFeedback STDOUT
System.out
.
Constructor Detail |
---|
public Parser()
setLexer(org.htmlparser.lexer.Lexer)
or setConnection(java.net.URLConnection)
.
setLexer(Lexer)
,
setConnection(URLConnection)
public Parser(Lexer lexer, ParserFeedback fb)
lexer
- The lexer to draw characters from.fb
- The object to use when information,
warning and error messages are produced. If null no feedback
is provided.public Parser(URLConnection connection, ParserFeedback fb) throws ParserException
ConnectionManager
.
connection
- A fully conditioned connection. The connect()
method will be called so it need not be connected yet.fb
- The object to use for message communication.
ParserException
- If the creation of the underlying Lexer
cannot be performed.public Parser(String resource, ParserFeedback feedback) throws ParserException
resource
- Either a URL, a filename or a string of HTML.
The string is considered HTML if the first non-whitespace character
is a <. The use of a url or file is autodetected by first attempting
to open the resource as a URL, if that fails it is assumed to be a file
name.
A standard HTTP GET is performed to read the content of the URL.feedback
- The HTMLParserFeedback object to use when information,
warning and error messages are produced. If null no feedback
is provided.
ParserException
- If the URL is invalid.Parser(URLConnection,ParserFeedback)
public Parser(String resource) throws ParserException
resource
- Either HTML, a URL or a filename (autodetects).
ParserException
- If the resourceLocn argument does not resolve
to a valid page or file.Parser(String,ParserFeedback)
public Parser(Lexer lexer)
System.out
is used.
This would be used to create a parser for special cases where the
normal creation of a lexer on a URLConnection needs to be customized.
lexer
- The lexer to draw characters from.public Parser(URLConnection connection) throws ParserException
ConnectionManager
.
A feedback object printing to System.out
is used.
connection
- A fully conditioned connection. The connect()
method will be called so it need not be connected yet.
ParserException
- If the creation of the underlying Lexer
cannot be performed.Parser(URLConnection,ParserFeedback)
Method Detail |
---|
public static String getVersion()
"[floating point number] ([build-type] [build-date])"
public static double getVersionNumber()
public static ConnectionManager getConnectionManager()
setConnectionManager(org.htmlparser.http.ConnectionManager)
public static void setConnectionManager(ConnectionManager manager)
manager
- The new connection manager.getConnectionManager()
public static Parser createParser(String html, String charset)
html
- The string containing HTML.charset
- Optional. The character set encoding that will
be reported by getEncoding()
. If charset is null
the default character set is used.
html
string as input.
IllegalArgumentException
- if html
is null
.public void setResource(String resource) throws ParserException
resource
- The resource to use.
IllegalArgumentException
- if resource
is null
.
ParserException
- if a problem occurs in connecting.public void setConnection(URLConnection connection) throws ParserException
Lexer
reading from the connection.
connection
- A fully conditioned connection. The connect()
method will be called so it need not be connected yet.
ParserException
- if the character set specified in the
HTTP header is not supported, or an i/o exception occurs creating the
lexer.
IllegalArgumentException
- if connection
is null
.
ParserException
- if a problem occurs in connecting.setLexer(org.htmlparser.lexer.Lexer)
,
getConnection()
public URLConnection getConnection()
setConnection(java.net.URLConnection)
.setConnection(URLConnection)
public void setURL(String url) throws ParserException
url
- The new URL for the parser.
ParserException
- If the url is invalid or creation of the
underlying Lexer cannot be performed.
ParserException
- if a problem occurs in connecting.getURL()
public String getURL()
Page.getUrl()
,
setURL(java.lang.String)
public void setEncoding(String encoding) throws ParserException
encoding
- The new character set to use.
ParserException
- If the encoding change causes characters that
have already been consumed to differ from the characters that would
have been seen had the new encoding been in force.EncodingChangeException
,
getEncoding()
public String getEncoding()
setEncoding(java.lang.String)
public void setLexer(Lexer lexer)
feedback
object.
lexer
- The lexer object to use.
IllegalArgumentException
- if lexer
is null
.setNodeFactory(org.htmlparser.NodeFactory)
,
getLexer()
public Lexer getLexer()
setLexer(org.htmlparser.lexer.Lexer)
public NodeFactory getNodeFactory()
setNodeFactory(org.htmlparser.NodeFactory)
public void setNodeFactory(NodeFactory factory)
factory
- The new node factory for the current lexer.
IllegalArgumentException
- if factory
is null
.getNodeFactory()
public void setFeedback(ParserFeedback fb)
fb
- The new feedback object to use. If this is null a
silent feedback object
is used.getFeedback()
public ParserFeedback getFeedback()
setFeedback(org.htmlparser.util.ParserFeedback)
public void reset()
Source
object.
This is cheaper (in terms of time) than resetting the URL, i.e.
parser.setURL (parser.getURL ());because the page is not refetched from the internet. Note: the nodes returned on the second parse are new nodes and not the same nodes returned on the first parse. If you want the same nodes for re-use, collect them in a NodeList with
parse(null)
and operate on the NodeList.
public NodeIterator elements() throws ParserException
Nodes
can be of three main types:
In general, when parsing with an iterator or processing a NodeList,
you will need to use recursion. For example:
void processMyNodes (Node node)
{
if (node instanceof TextNode)
{
// downcast to TextNode
TextNode text = (TextNode)node;
// do whatever processing you want with the text
System.out.println (text.getText ());
}
if (node instanceof RemarkNode)
{
// downcast to RemarkNode
RemarkNode remark = (RemarkNode)node;
// do whatever processing you want with the comment
}
else if (node instanceof TagNode)
{
// downcast to TagNode
TagNode tag = (TagNode)node;
// do whatever processing you want with the tag itself
// ...
// process recursively (nodes within nodes) via getChildren()
NodeList nl = tag.getChildren ();
if (null != nl)
for (NodeIterator i = nl.elements (); i.hasMoreElements (); )
processMyNodes (i.nextNode ());
}
}
Parser parser = new Parser ("http://www.yahoo.com");
for (NodeIterator i = parser.elements (); i.hasMoreElements (); )
processMyNodes (i.nextNode ());
ParserException
- If a parsing error occurs.public NodeList parse(NodeFilter filter) throws ParserException
null
filter it returns an
entire page which can then be modified and converted back to HTML
(Note: the synthesis use-case is not handled very well; the parser
is more often used to extract information from a web page).
For example, to replace the entire contents of the HEAD with a single TITLE tag you could do this:
NodeList nl = parser.parse (null); // here is your two node list NodeList heads = nl.extractAllNodesThatMatch (new TagNameFilter ("HEAD")) if (heads.size () > 0) // there may not be a HEAD tag { Head head = heads.elementAt (0); // there should be only one head.removeAll (); // clean out the contents Tag title = new TitleTag (); title.setTagName ("title"); title.setChildren (new NodeList (new TextNode ("The New Title"))); Tag title_end = new TitleTag (); title_end.setTagName ("/title"); title.setEndTag (title_end); head.add (title); } System.out.println (nl.toHtml ()); // output the modified HTML
filter
- The filter to apply to the parsed nodes,
or null
to retrieve all the top level nodes.
null
filter this is all the top level nodes).
ParserException
- If a parsing error occurs.public void visitAllNodesWith(NodeVisitor visitor) throws ParserException
accept()
method of each node
in the page in a depth first traversal. The visitor
beginParsing()
method is called prior to processing the
page and finishedParsing()
is called after the processing.
visitor
- The visitor to visit all nodes with.
ParserException
- If a parse error occurs while traversing
the page with the visitor.public void setInputHTML(String inputHTML) throws ParserException
inputHTML
- the input HTML that is to be parsed.
ParserException
- If a error occurs in setting up the
underlying Lexer.
IllegalArgumentException
- if inputHTML
is null
.public NodeList extractAllNodesThatMatch(NodeFilter filter) throws ParserException
filter
- The filter to be applied to the nodes.
true
.
ParserException
- If a parse error occurs.Node.collectInto(NodeList, NodeFilter)
public void preConnect(HttpURLConnection connection) throws ParserException
preConnect
in interface ConnectionMonitor
connection
- The connection which is about to be connected.
ParserException
- Not usedConnectionMonitor.preConnect(java.net.HttpURLConnection)
public void postConnect(HttpURLConnection connection) throws ParserException
postConnect
in interface ConnectionMonitor
connection
- The connection that was just connected.
ParserException
- Not used.ConnectionMonitor.postConnect(java.net.HttpURLConnection)
public static void main(String[] args)
args
- A URL or file name to parse, and an optional tag name to be
used as a filter.
|
© 2006 Derrick Oswald Sep 17, 2006
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
HTML Parser is an open source library released under Common Public License. |