📄 htmlparser.java
字号:
package com.ibm.staf.service.http.html;/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2004 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/import java.io.StringReader;import java.io.IOException;import java.util.HashMap;import java.io.InputStream;import com.ibm.staf.service.http.InvalidElementIDException;import org.cyberneko.html.parsers.DOMParser;import org.w3c.dom.html.HTMLDocument;// xercesimport org.xml.sax.SAXException;import org.xml.sax.InputSource;import org.w3c.dom.html.HTMLCollection;import org.w3c.dom.html.HTMLFormElement;import org.w3c.dom.html.HTMLElement;import org.w3c.dom.html.HTMLAnchorElement;import org.w3c.dom.html.HTMLAreaElement;/*****************************************************************************//* *//* Class: HTMLParser *//* Description: This class provides the handle to manipulate a html document.*//* *//*****************************************************************************/public class HTMLParser // XXX may need to implement org.xml.sax Interface ErrorHandler{ protected HTMLDocument document; protected DOMParser parser; public static final String EMPTY_DOC = "<html></html>"; /*****************************************************************************//* *//* Method: Constructor *//* Description: Constructor method *//* Parameter: none *//* *//*****************************************************************************/ public HTMLParser() { parser = new DOMParser(); try { setContent (EMPTY_DOC); }catch (SAXException e) { // EMPTY_DOC should not throw any errors }catch (IOException e) { // EMPTY_DOC should not throw any errors } }/*****************************************************************************//* *//* Method: setContent *//* Description: sets the content to draw elements from *//* Parameters: content - html content to parse and draw elements from *//* Returns: void *//* *//*****************************************************************************/ public void setContent (String content) throws SAXException, IOException { InputSource bodySource = new InputSource( new StringReader(content)); setContent(bodySource); }/*****************************************************************************//* *//* Method: setContent *//* Description: sets the content to draw elements from *//* Parameters: content - html content to parse and draw elements from *//* Returns: void *//* *//*****************************************************************************/ public void setContent (InputStream content) throws SAXException, IOException { setContent(new InputSource(content)); content.close(); }/*****************************************************************************//* *//* Method: setContent *//* Description: sets the content to draw elements from *//* Parameters: content - html content to parse and draw elements from *//* Returns: void *//* *//*****************************************************************************/ public void setContent (InputSource source) throws SAXException, IOException { parser.parse(source); document = (HTMLDocument)parser.getDocument(); }/*****************************************************************************//* *//* Method: getTitle *//* Description: get the title of the current document *//* Parameters: none *//* Returns: the title of the current html document *//* *//*****************************************************************************/ public String getTitle() { return document.getTitle(); } /*****************************************************************************//* *//* Method: getLinkByID *//* Description: get the WebLink identified by with id attribute id. *//* Parameters: id - id attribute to identify the link by *//* Returns: html link *//* *//*****************************************************************************/ public WebLink getLinkByID(String id) throws InvalidElementIDException { HTMLCollection links = document.getLinks(); for(int i = 0; i < links.getLength(); i++) { if (id.equals( ((HTMLElement) links.item(i)).getId() )) { if ( org.apache.html.dom.HTMLAnchorElementImpl.class == links.item(i).getClass()) return new WebLink((HTMLAnchorElement)links.item(i), i + 1); if ( org.apache.html.dom.HTMLAreaElementImpl.class == links.item(i).getClass()) return new WebLink((HTMLAreaElement)links.item(i), i + 1); } } throw new InvalidElementIDException (id, "Link " + id); } /*****************************************************************************//* *//* Method: getLinkByName *//* Description: get the WebLink identified by with name attribute name. *//* Parameters: name - name attribute to identify the form by *//* Returns: html link *//* *//*****************************************************************************/ public WebLink getLinkByName(String name) throws InvalidElementIDException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -