|
What this is
Other links
The source code
// $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tags/CompositeTag.java,v 1.2 2004/02/10 13:41:07 woolfel Exp $
/*
* ====================================================================
* Copyright 2002-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// The developers of JMeter and Apache are greatful to the developers
// of HTMLParser for giving Apache Software Foundation a non-exclusive
// license. The performance benefits of HTMLParser are clear and the
// users of JMeter will benefit from the hard work the HTMLParser
// team. For detailed information about HTMLParser, the project is
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
//
// HTMLParser was originally created by Somik Raha in 2000. Since then
// a healthy community of users has formed and helped refine the
// design so that it is able to tackle the difficult task of parsing
// dirty HTML. Derrick Oswald is the current lead developer and was kind
// enough to assist JMeter.
package org.htmlparser.tags;
import org.htmlparser.*;
import org.htmlparser.Node;
import org.htmlparser.tags.data.CompositeTagData;
import org.htmlparser.tags.data.TagData;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.SimpleNodeIterator;
import org.htmlparser.visitors.NodeVisitor;
public abstract class CompositeTag extends Tag
{
protected Tag startTag, endTag;
protected NodeList childTags;
public CompositeTag(TagData tagData, CompositeTagData compositeTagData)
{
super(tagData);
this.childTags = compositeTagData.getChildren();
this.startTag = compositeTagData.getStartTag();
this.endTag = compositeTagData.getEndTag();
}
public SimpleNodeIterator children()
{
return childTags.elements();
}
public Node getChild(int index)
{
return childTags.elementAt(index);
}
public Node[] getChildrenAsNodeArray()
{
return childTags.toNodeArray();
}
public NodeList getChildren()
{
return childTags;
}
/**
* Return the child tags as an iterator.
* Equivalent to calling getChildren ().elements ().
* @return An iterator over the children.
*/
public SimpleNodeIterator elements()
{
return (getChildren().elements());
}
public String toPlainTextString()
{
StringBuffer stringRepresentation = new StringBuffer();
for (SimpleNodeIterator e = children(); e.hasMoreNodes();)
{
stringRepresentation.append(e.nextNode().toPlainTextString());
}
return stringRepresentation.toString();
}
public void putStartTagInto(StringBuffer sb)
{
sb.append(startTag.toHtml());
}
protected void putChildrenInto(StringBuffer sb)
{
Node node, prevNode = startTag;
for (SimpleNodeIterator e = children(); e.hasMoreNodes();)
{
node = e.nextNode();
if (prevNode != null)
{
if (prevNode.elementEnd() > node.elementBegin())
{
// Its a new line
sb.append(lineSeparator);
}
}
sb.append(node.toHtml());
prevNode = node;
}
if (prevNode.elementEnd() > endTag.elementBegin())
{
sb.append(lineSeparator);
}
}
protected void putEndTagInto(StringBuffer sb)
{
sb.append(endTag.toHtml());
}
public String toHtml()
{
StringBuffer sb = new StringBuffer();
putStartTagInto(sb);
if (!startTag.isEmptyXmlTag())
{
putChildrenInto(sb);
putEndTagInto(sb);
}
return sb.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 = (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 any node whose text representation contains the search
* string. Collects all such nodes in a NodeList.
* e.g. if you wish to find any textareas in a form tag containing "hello
* world", the code would be :
*
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.