Class TagParser
java.lang.Object
|
+----TagParser
- public class TagParser
- extends Object
This class takes some input as stream or string and returns defined text parts
defined by user defined patterns.
Usage:
String[][] patterns={{"t","t"},{"hier "," mit"}};
TagParser tp=new TagParser(patterns, true);
tp.setSource("Ein Testtext. und hier noch mit zweitem pattern.");
while (tp.hasMoreTags()) System.out.println(tp.nextTag());
This example should return:
Test
text
hier noch mit
tem pat
Another example analyzing a html file:
String[][] patterns={{"",""}};
TagParser tp=new TagParser(patterns,false);
tp.setSource("Hello User!");
tp.hasMoreTags(); System.out.println(tp.nextTag());
will return "<b>User</b>
".
Quellcode mit GPL-Lizenz.
-
ignore_case
- if true, at comparing the patterns case must be ignored
-
TagParser()
- Creates a new TagParser with no patterns.
-
TagParser(String[][])
- Creates a new TagParser with the given tag patterns and no case ignore.
-
TagParser(String[][], boolean)
- Creates a new TagParser with the given tag patterns and no case ignore.
-
compare(char, char)
- compares the two characters
-
extractTagAttribute(String, String)
- Extracts the specified attribute from the given tag text.
-
getInputPosition()
- Gets the number of characters read till now.
-
getTag()
-
-
getTagIndex()
- Gets the index of the founded tag returned by
nexttag()
.
-
hasMoreTags()
- Checks if there are more tags contained in input.
-
nextTag()
- Gets the tag found by the last call of
hasMoreTags()
.
-
setPattern(String[][])
- sets the tag patterns to the given pairs, without ignoring cases.
-
setPattern(String[][], boolean)
- sets the tag patterns to the given pairs.
-
setSource(InputStream)
- Sets the data source to be used for input.
-
setSource(Reader)
- Sets the data source to be used for input.
-
setSource(String)
- Sets the data source to be used for input.
ignore_case
protected boolean ignore_case
- if true, at comparing the patterns case must be ignored
TagParser
public TagParser()
- Creates a new TagParser with no patterns.
- See Also:
- setPattern
TagParser
public TagParser(String pts[][])
- Creates a new TagParser with the given tag patterns and no case ignore.
- Parameters:
- pts - an array of all tags, each given as array of length 2 with start and end pattern
- See Also:
- setPattern
TagParser
public TagParser(String pts[][],
boolean ignore_case)
- Creates a new TagParser with the given tag patterns and no case ignore.
- Parameters:
- pts - an array of all tags, each given as array of length 2 with start and end pattern
- ignore_case - if true, cases will be ignored at pattern matching
- See Also:
- setPattern
setSource
public void setSource(InputStream is)
- Sets the data source to be used for input.
- Parameters:
- is - an InputStream to read characters from using a InputStreamReader
setSource
public void setSource(String s)
- Sets the data source to be used for input.
- Parameters:
- s - a String to read characters from using a StringReader
setSource
public final void setSource(Reader r)
- Sets the data source to be used for input.
setPattern
public final void setPattern(String pts[][])
- sets the tag patterns to the given pairs, without ignoring cases.
- Parameters:
- pts - an array of all tags, each given as array of length 2 with start and end pattern
setPattern
public final void setPattern(String pts[][],
boolean ignore_case)
- sets the tag patterns to the given pairs.
- Parameters:
- pts - an array of all tags, each given as array of length 2 with start and end pattern
- ignore_case - if true, cases will be ignored at pattern matching
compare
protected boolean compare(char a,
char b)
- compares the two characters
hasMoreTags
public final boolean hasMoreTags() throws IOException
- Checks if there are more tags contained in input.
Only complete tags delimited by start and pattern as defined are recognized.
Note: This methods performs the input evaluation!
- Returns:
- true if there are more tags
nextTag
public final String nextTag()
- Gets the tag found by the last call of
hasMoreTags()
.
If you use hasMoreTags()
properly, you should never get null
.
- Returns:
- the tag delimited by start and end pattern, or null if no tag present
getTag
public final String getTag()
getInputPosition
public final long getInputPosition()
- Gets the number of characters read till now.
getTagIndex
public final int getTagIndex()
- Gets the index of the founded tag returned by
nexttag()
.
extractTagAttribute
public static String extractTagAttribute(String tag,
String attribute)
- Extracts the specified attribute from the given tag text.
Note: This is a supporting function that could be useful, but not necessary for fcuntion of TagParser.