📄 compositetag.java
字号:
// HTMLParser Library $Name: v1_6 $ - A java-based parser for HTML// http://sourceforge.org/projects/htmlparser// Copyright (C) 2004 Somik Raha//// Revision Control Information//// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/CompositeTag.java,v $// $Author: derrickoswald $// $Date: 2006/05/31 02:10:15 $// $Revision: 1.82 $//// 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.tags;import java.util.Locale;import org.htmlparser.Node;import org.htmlparser.NodeFilter;import org.htmlparser.Text;import org.htmlparser.Tag;import org.htmlparser.filters.NodeClassFilter;import org.htmlparser.nodes.AbstractNode;import org.htmlparser.nodes.TagNode;import org.htmlparser.scanners.CompositeTagScanner;import org.htmlparser.util.NodeList;import org.htmlparser.util.SimpleNodeIterator;import org.htmlparser.visitors.NodeVisitor;/** * The base class for tags that have an end tag. * Provided extra accessors for the children above and beyond what the basic * {@link Tag} provides. Also handles the conversion of it's children for * the {@link #toHtml toHtml} method. */public class CompositeTag extends TagNode{ /** * The tag that causes this tag to finish. * May be a virtual tag generated by the scanning logic. */ protected Tag mEndTag; /** * The default scanner for non-composite tags. */ protected final static CompositeTagScanner mDefaultCompositeScanner = new CompositeTagScanner (); /** * Create a composite tag. */ public CompositeTag () { setThisScanner (mDefaultCompositeScanner); } /** * Get an iterator over the children of this node. * @return Am iterator over the children of this node. */ public SimpleNodeIterator children () { SimpleNodeIterator ret; if (null != getChildren ()) ret = getChildren ().elements (); else ret = (new NodeList ()).elements (); return (ret); } /** * Get the child of this node at the given position. * @param index The in the node list of the child. * @return The child at that index. */ public Node getChild (int index) { return ( (null == getChildren ()) ? null : getChildren ().elementAt (index)); } /** * Get the children as an array of <code>Node</code> objects. * @return The children in an array. */ public Node [] getChildrenAsNodeArray () { return ( (null == getChildren ()) ? new Node[0] : getChildren ().toNodeArray ()); } /** * Remove the child at the position given. * @param i The index of the child to remove. */ public void removeChild (int i) { if (null != getChildren ()) getChildren ().remove (i); } /** * Return the child tags as an iterator. * Equivalent to calling getChildren ().elements (). * @return An iterator over the children. */ public SimpleNodeIterator elements() { return ( (null == getChildren ()) ? new NodeList ().elements () : getChildren ().elements ()); } /** * Return the textual contents of this tag and it's children. * @return The 'browser' text contents of this tag. */ public String toPlainTextString() { StringBuffer stringRepresentation = new StringBuffer(); for (SimpleNodeIterator e=children();e.hasMoreNodes();) { stringRepresentation.append(e.nextNode().toPlainTextString()); } return stringRepresentation.toString(); } /** * Add the textual contents of the children of this node to the buffer. * @param verbatim If <code>true</code> return as close to the original * page text as possible. * @param sb The buffer to append to. */ protected void putChildrenInto (StringBuffer sb, boolean verbatim) { Node node; for (SimpleNodeIterator e = children (); e.hasMoreNodes ();) { node = e.nextNode (); // eliminate virtual tags if (!verbatim || !(node.getStartPosition () == node.getEndPosition ())) sb.append (node.toHtml ()); } } /** * Add the textual contents of the end tag of this node to the buffer. * @param verbatim If <code>true</code> return as close to the original * page text as possible. * @param sb The buffer to append to. */ protected void putEndTagInto (StringBuffer sb, boolean verbatim) { // eliminate virtual tags if (!verbatim || !(mEndTag.getStartPosition () == mEndTag.getEndPosition ())) sb.append (getEndTag ().toHtml()); } /** * Return this tag as HTML code. * @param verbatim If <code>true</code> return as close to the original * page text as possible. * @return This tag and it's contents (children) and the end tag * as HTML code. */ public String toHtml (boolean verbatim) { StringBuffer ret; ret = new StringBuffer (); ret.append (super.toHtml (verbatim)); if (!isEmptyXmlTag ()) { putChildrenInto (ret, verbatim); if (null != getEndTag ()) putEndTagInto (ret, verbatim); } return (ret.toString ()); } /** * Searches all children who for a name attribute. Returns first match. * @param name Attribute to match in tag * @return Tag Tag matching the name attribute */ public Tag searchByName(String name) { Node node; Tag tag = null; boolean found = false; for (SimpleNodeIterator e = children();e.hasMoreNodes() && !found;) { node = e.nextNode(); if (node instanceof Tag) { tag = (Tag)node; String nameAttribute = tag.getAttribute("NAME"); if (nameAttribute!=null && nameAttribute.equals(name)) found=true; } } if (found) return tag; else return null; } /** * Searches for all nodes whose text representation contains the search string. * Collects all nodes containing the search string into a NodeList. * This search is <b>case-insensitive</b> and the search string and the * node text are converted to uppercase using an English locale. * For example, if you wish to find any textareas in a form tag containing * "hello world", the code would be: * <code> * NodeList nodeList = formTag.searchFor("Hello World"); * </code> * @param searchString Search criterion. * @return A collection of nodes whose string contents or * representation have the <code>searchString</code> in them. */ public NodeList searchFor (String searchString) { return (searchFor (searchString, false)); } /** * Searches for all nodes whose text representation contains the search string. * Collects all nodes containing the search string into a NodeList. * For example, if you wish to find any textareas in a form tag containing * "hello world", the code would be: * <code> * NodeList nodeList = formTag.searchFor("Hello World"); * </code> * @param searchString Search criterion. * @param caseSensitive If <code>true</code> this search should be case * sensitive. Otherwise, the search string and the node text are converted * to uppercase using an English locale. * @return A collection of nodes whose string contents or * representation have the <code>searchString</code> in them. */ public NodeList searchFor (String searchString, boolean caseSensitive) { return (searchFor (searchString, caseSensitive, Locale.ENGLISH)); } /** * Searches for all nodes whose text representation contains the search string. * Collects all nodes containing the search string into a NodeList. * For example, if you wish to find any textareas in a form tag containing * "hello world", the code would be: * <code> * NodeList nodeList = formTag.searchFor("Hello World"); * </code> * @param searchString Search criterion. * @param caseSensitive If <code>true</code> this search should be case * sensitive. Otherwise, the search string and the node text are converted * to uppercase using the locale provided. * @param locale The locale for uppercase conversion. * @return A collection of nodes whose string contents or * representation have the <code>searchString</code> in them. */ public NodeList searchFor (String searchString, boolean caseSensitive, Locale locale) { Node node; String text; NodeList ret; ret = new NodeList (); if (!caseSensitive) searchString = searchString.toUpperCase (locale); for (SimpleNodeIterator e = children (); e.hasMoreNodes (); ) { node = e.nextNode (); text = node.toPlainTextString (); if (!caseSensitive) text = text.toUpperCase (locale); if (-1 != text.indexOf (searchString)) ret.add (node); } return (ret); } /** * Collect all objects that are of a certain type * Note that this will not check for parent types, and will not * recurse through child tags * @param classType The class to search for. * @param recursive If true, recursively search through the children. * @return A list of children found. */ public NodeList searchFor (Class classType, boolean recursive) { NodeList children; NodeList ret; children = getChildren (); if (null == children) ret = new NodeList (); else ret = children.extractAllNodesThatMatch ( new NodeClassFilter (classType), recursive);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -