| Prev Class | Next Class | Frames | No Frames |
| Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Objectau.id.jericho.lib.html.Segmentpublic class Segmentextends java.lang.Objectimplements Comparable, CharSequenceSource document.
Many of the tag search methods are defined in this class.
The span of a segment is defined by the combination of its begin and end character positions.
Constructor Summary | |
Method Summary | |
char |
|
int |
|
boolean | |
boolean |
|
boolean |
|
String |
|
String |
|
List |
|
List |
|
List |
|
List |
|
List |
|
List |
|
List |
|
List |
|
List |
|
List |
|
List |
|
FormFields |
|
int | |
List |
|
String |
|
int | |
Iterator |
|
Renderer |
|
Source | |
TextExtractor |
|
int |
|
void |
|
boolean |
|
static boolean |
|
int |
|
Attributes |
|
CharSequence |
|
String |
|
public final char charAt(int index)
Returns the character at the specified index. This is logically equivalent totoString().charAt(index)for valid argument values0 <= index <32length(). However because this implementation works directly on the underlying document source string, it should not be assumed that anIndexOutOfBoundsExceptionis thrown for an invalid argument value.
- Parameters:
index- the index of the character.
- Returns:
- the character at the specified index.
public int compareTo(Object o)
Compares thisSegmentobject to another object. If the argument is not aSegment, aClassCastExceptionis thrown. A segment is considered to be before another segment if its begin position is earlier, or in the case that both segments begin at the same position, its end position is earlier. Segments that begin and end at the same position are considered equal for the purposes of this comparison, even if they relate to different source documents. Note: this class has a natural ordering that is inconsistent with equals. This means that this method may return zero in some cases where calling theequals(Object)method with the same argument returnsfalse.
- Parameters:
o- the segment to be compared
- Returns:
- a negative integer, zero, or a positive integer as this segment is before, equal to, or after the specified segment.
public final boolean encloses(Segment segment)
Indicates whether thisSegmentencloses the specifiedSegment. This is the case ifgetBegin()<=segment.getBegin()&&getEnd()>=segment.getEnd().
- Parameters:
segment- the segment to be tested for being enclosed by this segment.
- Returns:
trueif thisSegmentencloses the specifiedSegment, otherwisefalse.
public final boolean encloses(int pos)
Indicates whether this segment encloses the specified character position in the source document. This is the case ifgetBegin()<= pos <getEnd().
- Parameters:
pos- the position in theSourcedocument.
- Returns:
trueif this segment encloses the specified character position in the source document, otherwisefalse.
public final boolean equals(Object object)
Compares the specified object with thisSegmentfor equality. Returnstrueif and only if the specified object is also aSegment, and both segments have the sameSource, and the same begin and end positions.
- Parameters:
object- the object to be compared for equality with thisSegment.
- Returns:
trueif the specified object is equal to thisSegment, otherwisefalse.
public String extractText()
Deprecated. Use
getTextExtractor().toString()instead.Extracts the textual content from the HTML markup of this segment. This method has been deprecated as of version 2.4 and replaced with thegetTextExtractor()method.
- Returns:
- the textual content from the HTML markup of this segment.
public String extractText(boolean includeAttributes)
Deprecated. Use
getTextExtractor().setIncludeAttributes(includeAttributes).toString()instead.Extracts the textual content from the HTML markup of this segment. This method has been deprecated as of version 2.4 and replaced with thegetTextExtractor()method.
- Returns:
- the textual content from the HTML markup of this segment.
public List findAllCharacterReferences()
Returns a list of allCharacterReferenceobjects that are enclosed by this segment.
- Returns:
- a list of all
CharacterReferenceobjects that are enclosed by this segment.
public List findAllElements()
Returns a list of allElementobjects that are enclosed by this segment. TheSource.fullSequentialParse()method should be called after construction of theSourceobject if this method is to be used on a large proportion of the source. It is called automatically if this method is called on theSourceobject itself. The elements returned correspond exactly with the start tags returned in thefindAllStartTags()method.
public List findAllElements(String name)
Returns a list of allElementobjects with the specified name that are enclosed by this segment. The elements returned correspond exactly with the start tags returned in thefindAllStartTags(String name)method. Specifying anullargument to thenameparameter is equivalent tofindAllElements(). This method also returns elements consisting of unregistered tags if the specified name is not a valid XML tag name.
- Parameters:
name- the name of the elements to find.
public List findAllElements(String attributeName,
String value,
boolean valueCaseSensitive)Returns a list of allElementobjects with the specified attribute name/value pair that are enclosed by this segment. The elements returned correspond exactly with the start tags returned in thefindAllStartTags(String attributeName, String value, boolean valueCaseSensitive)method.
- Parameters:
attributeName- the attribute name (case insensitive) to search for, must not benull.value- the value of the specified attribute to search for, must not benull.valueCaseSensitive- specifies whether the attribute value matching is case sensitive.
public List findAllElements(StartTagType startTagType)
Returns a list of allElementobjects with start tags of the specified type that are enclosed by this segment. The elements returned correspond exactly with the start tags returned in thefindAllTags(TagType)method.
- Parameters:
startTagType- the type of start tags to find, must not benull.
public List findAllStartTags()
Returns a list of allStartTagobjects that are enclosed by this segment. TheSource.fullSequentialParse()method should be called after construction of theSourceobject if this method is to be used on a large proportion of the source. It is called automatically if this method is called on theSourceobject itself. See theTagclass documentation for more details about the behaviour of this method.
public List findAllStartTags(String name)
Returns a list of all normalStartTagobjects with the specified name that are enclosed by this segment. See theTagclass documentation for more details about the behaviour of this method. Specifying anullargument to thenameparameter is equivalent tofindAllStartTags(). This method also returns unregistered tags if the specified name is not a valid XML tag name.
- Parameters:
name- the name of the start tags to find.
public List findAllStartTags(String attributeName,
String value,
boolean valueCaseSensitive)Returns a list of allStartTagobjects with the specified attribute name/value pair that are enclosed by this segment. See theTagclass documentation for more details about the behaviour of this method.
- Parameters:
attributeName- the attribute name (case insensitive) to search for, must not benull.value- the value of the specified attribute to search for, must not benull.valueCaseSensitive- specifies whether the attribute value matching is case sensitive.
public List findAllTags()
Returns a list of allTagobjects that are enclosed by this segment. TheSource.fullSequentialParse()method should be called after construction of theSourceobject if this method is to be used on a large proportion of the source. It is called automatically if this method is called on theSourceobject itself. See theTagclass documentation for more details about the behaviour of this method.
public List findAllTags(TagType tagType)
Returns a list of allTagobjects of the specified type that are enclosed by this segment. See theTagclass documentation for more details about the behaviour of this method. Specifying anullargument to thetagTypeparameter is equivalent tofindAllTags().
- Parameters:
tagType- the type of tags to find.
public List findFormControls()
Returns a list of theFormControlobjects that are enclosed by this segment.
- Returns:
- a list of the
FormControlobjects that are enclosed by this segment.
public FormFields findFormFields()
Returns theFormFieldsobject representing all form fields that are enclosed by this segment. This is equivalent tonew FormFields(findFormControls()).
- Returns:
- the
FormFieldsobject representing all form fields that are enclosed by this segment.
- See Also:
findFormControls()
public final int getBegin()
Returns the character position in theSourcedocument at which this segment begins, inclusive.
- Returns:
- the character position in the
Sourcedocument at which this segment begins, inclusive.
public List getChildElements()
Returns a list of the immediate children of this segment in the document element hierarchy. The returned list may include an element that extends beyond the end of this segment, as long as it begins within this segment. An element found at the start of this segment is included in the list. Note however that if this segment is anElement, the overridingElement.getChildElements()method is called instead, which only returns the children of the element. CallinggetChildElements()on anElementis usually more efficient than calling it on aSegment. The objects in the list are all of typeElement. TheSource.fullSequentialParse()method should be called after construction of theSourceobject if this method is to be used on a large proportion of the source. It is called automatically if this method is called on theSourceobject itself. See theSource.getChildElements()method for more details.
- Returns:
- the a list of the immediate children of this segment in the document element hierarchy, guaranteed not
null.
- See Also:
Element.getParentElement()
public String getDebugInfo()
Returns a string representation of this object useful for debugging purposes.
- Returns:
- a string representation of this object useful for debugging purposes.
public final int getEnd()
Returns the character position in theSourcedocument immediately after the end of this segment. The character at the position specified by this property is not included in the segment.
- Returns:
- the character position in the
Sourcedocument immediately after the end of this segment.
public Iterator getNodeIterator()
Returns an iterator over every tag and text segment contained within this segment. Every tag found in thefindAllTags()list is included in this iterator, including all server tags. Segments of the document between the tags are also included, resulting in a sequential walk-through of every "node" in this segment, where a node is either a tag or a segment of text. The end position of each segment should correspond with the begin position of the subsequent segment, unless any of the tags are enclosed by other tags, which is common when server tags are present. TheCharacterReference.decodeCollapseWhiteSpace(CharSequence)method can be used to retrieve the text from each text segment.
for (Iterator nodeIterator=segment.getNoteIterator(); nodeIterator.hasNext();) { Segment nodeSegment=(Segment)nodeIterator.next(); if (nodeSegment instanceof Tag) { Tag tag=(Tag)nodeSegment; if (tag.getTagType().isServerTag()) continue; // ignore server tags // Process the tag (just output it in this example): System.out.println(tag.tidy()); } else { // Segment is a text segment. // Process the text segment (just output its text in this example): String text=CharacterReference.decodeCollapseWhiteSpace(nodeSegment); System.out.println(text); } }
- Returns:
- an iterator over every tag and text segment contained within this segment.
public Renderer getRenderer()
Performs a simple rendering of the HTML markup in this segment into text. The output can be configured by setting any number of properties on the returnedRendererinstance before obtaining its output.
- Returns:
- an instance of
Rendererbased on this segment.
- See Also:
getTextExtractor()
public final Source getSource()
Returns theSourcedocument containing this segment.
- Returns:
- the
Sourcedocument containing this segment.
public TextExtractor getTextExtractor()
Extracts the textual content from the HTML markup of this segment. The output can be configured by setting properties on the returnedTextExtractorinstance before obtaining its output.
- Returns:
- an instance of
TextExtractorbased on this segment.
- See Also:
getRenderer()
public int hashCode()
Returns a hash code value for the segment. The current implementation returns the sum of the begin and end positions, although this is not guaranteed in future versions.
- Returns:
- a hash code value for the segment.
public void ignoreWhenParsing()
Causes the this segment to be ignored when parsing. Ignored segments are treated as blank spaces by the parsing mechanism, but are included as normal text in all other functions. This method was originally the only means of preventing server tags located inside normal tags from interfering with the parsing of the tags (such as where an attribute of a normal tag uses a server tag to dynamically set its value), as well as preventing non-server tags from being recognised inside server tags. As of version 2.4 it is no longer necessary to use this method to ignore server tags located inside normal tags, as the attributes parser now automatically ignores any server tags. As of version 2.5 it is no longer necessary to use this method to ignore the contents ofSCRIPTelements, as the parser now does this automatically when performing a full sequential parse. As of version 2.6 is is no longer necessary to use this method to ignore non-server tags inside server tags, as the parser now does this automatically when performing a full sequential parse. This leaves only very few scenarios where calling this method still provides a significant benefit. One such case is where XML-style server tags are used inside normal tags. Here is an example using an XML-style JSP tag:The first double-quote of<a href="<i18n:resource path="/Portal"/>?BACK=TRUE">back</a>"/Portal"will be interpreted as the end quote for thehrefattribute, as there is no way for the parser to recognise theil8n:resourceelement as a server tag. Such use of XML-style server tags inside normal tags is generally seen as bad practice, but it is nevertheless valid JSP. The only way to ensure that this library is able to parse the normal tag surrounding it is to find these server tags first and call theignoreWhenParsingmethod to ignore them before parsing the rest of the document. It is important to understand the difference between ignoring the segment when parsing and removing the segment completely. Any text inside a segment that is ignored when parsing is treated by most functions as content, and as such is included in the output of tools such asTextExtractorandRenderer. To remove segments completely, create anOutputDocumentand call itsremove(Segment)orreplaceWithSpaces(int begin, int end)method for each segment. Then create a new source document usingnew Source(outputDocument.toString())and perform the desired operations on this new source object. Calling this method after theSource.fullSequentialParse()method has been called is not permitted and throws anIllegalStateException. Any tags appearing in this segment that are found before this method is called will remain in the tag cache, and so will continue to be found by the tag search methods. If this is undesirable, theSource.clearCache()method can be called to remove them from the cache. Calling theSource.fullSequentialParse()method after this method clears the cache automatically. For best performance, this method should be called on all segments that need to be ignored without calling any of the tag search methods in between.
- See Also:
Source.ignoreWhenParsing(Collection segments)
public final boolean isWhiteSpace()
Indicates whether this segment consists entirely of white space.
- Returns:
trueif this segment consists entirely of white space, otherwisefalse.
public static final boolean isWhiteSpace(char ch)
Indicates whether the specified character is white space. The HTML 4.01 specification section 9.1 specifies the following white space characters:Despite the explicit inclusion of the zero-width space in the HTML specification, Microsoft IE6 does not recognise them as white space and renders them as an unprintable character (empty square). Even zero-width spaces included using the numeric character reference
- space (U+0020)
- tab (U+0009)
- form feed (U+000C)
- line feed (U+000A)
- carriage return (U+000D)
- zero-width space (U+200B)
​are rendered this way.
- Parameters:
ch- the character to test.
- Returns:
trueif the specified character is white space, otherwisefalse.
public final int length()
Returns the length of the segment. This is defined as the number of characters between the begin and end positions.
- Returns:
- the length of the segment.
public Attributes parseAttributes()
Parses anyAttributeswithin this segment. This method is only used in the unusual situation where attributes exist outside of a start tag. TheStartTag.getAttributes()method should be used in normal situations. This is equivalent tosource.parseAttributes(getBegin(),getEnd()).
- Returns:
- the
Attributeswithin this segment, ornullif too many errors occur while parsing.
public final CharSequence subSequence(int beginIndex,
int endIndex)Returns a new character sequence that is a subsequence of this sequence. This is logically equivalent totoString().subSequence(beginIndex,endIndex)for valid values ofbeginIndexandendIndex. However because this implementation works directly on the underlying document source string, it should not be assumed that anIndexOutOfBoundsExceptionis thrown for invalid argument values as described in theString.subSequence(int,int)method.
- Parameters:
beginIndex- the begin index, inclusive.endIndex- the end index, exclusive.
- Returns:
- a new character sequence that is a subsequence of this sequence.
public String toString()
Returns the source text of this segment as aString. The returnedStringis newly created with every call to this method, unless this segment is itself an instance ofSource. Note that before version 2.0 this returned a representation of this object useful for debugging purposes, which can now be obtained via thegetDebugInfo()method.
- Returns:
- the source text of this segment as a
String.