EchoPoint
1.0

echopoint.ui.template
Class SGMLTag

java.lang.Object
  extended byechopoint.ui.template.SGMLTag
All Implemented Interfaces:
java.io.Serializable

public class SGMLTag
extends java.lang.Object
implements java.io.Serializable

Convenient class for parsing SGML tokens from a page. The Reader is required to support the mark() operation.

Tags are only read enough to find out what the tag name is; If you want to read the full tag call parse(inputReader). This is done so that applications don't spend time processing tags about which they care little.

Here's a sample piece of code which uses this class to read all SGML tags on a page:

 void showTags(PrintWriter out, Reader input)
 {
      SGMLTag tag = new SGMLTag(input);
      while (!tag.finished()) {
          out.println ("tag: " + tag.toString());
          tag = new SGMLTag (input);
      }
 }
 

See Also:
Serialized Form

Field Summary
static char doubleQuote
           
static char singleQuote
           
 
Constructor Summary
SGMLTag(PositionReader input)
          Create new SGML tag reference, starting at current location of the PositionReader.
SGMLTag(PositionReader input, boolean parseIt)
          Create new SGML tag reference, starting at current location of the PositionReader.
 
Method Summary
 java.util.Iterator attributes(boolean upperCase)
          Get list of attribute names.
 boolean finished()
          Checked whether this tag indicates we're at the end of the list.
 java.lang.String getAttribute(java.lang.String key, java.lang.String defaultValue)
          Return value of attribute (parameter) setting in SGML tag.
 java.util.HashMap getAttributesWithValues()
          Return tag attributes and values.
 long getEnd()
          Gets the end position of the tag in the stream ie: just AFTERE the closing > character.
 java.lang.String getName()
          get the Name of this SGML tag, in uppercase format.
 int getOffset()
          returns the number of chars skipped before the starting '<'
 long getStart()
          Gets the start position of the tag in the stream ie: just AFTER the starting < character
static boolean isDelimiter(char c)
          Decide whether character is SGML delimiter or equals.
 boolean isNamed(java.lang.String name)
          Check name of tag.
 boolean isSelfTerminated()
           
 boolean isWellFormed()
          Check for well-formedness of this tag.
 java.lang.String nextToken(PositionReader input)
          Read next token from string.
 java.lang.String nextToken(PositionReader input, boolean skipWhitespaces)
          Read next token from string.
 void parse(PositionReader input)
          Attempts to read all the attribuets of the tag up to its closing > character.
protected  void searchStart(PositionReader input)
          Skip over any HTML-style comments, as denoted by matched <-- ...
static int skipWhiteSpace(PositionReader r)
          could be overwritten
 java.lang.String toString()
          Render this tag as a string.
 java.lang.String value(java.lang.String attributeName, java.lang.String defaultValue)
          Get attribute value, or default if not set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

doubleQuote

public static final char doubleQuote
See Also:
Constant Field Values

singleQuote

public static final char singleQuote
See Also:
Constant Field Values
Constructor Detail

SGMLTag

public SGMLTag(PositionReader input)
        throws java.io.IOException
Create new SGML tag reference, starting at current location of the PositionReader. Read all attributes.

Note that this constructor skips over any HTML-style comments, as denoted by matched <-- ... --> pairs.


SGMLTag

public SGMLTag(PositionReader input,
               boolean parseIt)
        throws java.io.IOException
Create new SGML tag reference, starting at current location of the PositionReader. At first, only the type of tag (first argument) is read if parseIt is false. Tag may not be well-formed: if interested, call "parse(input)" directly afterwards (without reading any characters from the Reader) to get the attributes.

Note that this constructor skips over any HTML-style comments, as denoted by matched <-- ... --> pairs.

Method Detail

attributes

public java.util.Iterator attributes(boolean upperCase)
Get list of attribute names.

Returns:
enumeration of attribute names specified as strings, or null if this tag is poorly formed

finished

public boolean finished()
Checked whether this tag indicates we're at the end of the list. Note: The end tag is not usuable as an SGML tag.

Returns:
true if this tag represents end of tags, and is not usuable

getAttribute

public java.lang.String getAttribute(java.lang.String key,
                                     java.lang.String defaultValue)
Return value of attribute (parameter) setting in SGML tag.

Returns:
value of that attribute, or default if not defined

getAttributesWithValues

public java.util.HashMap getAttributesWithValues()
Return tag attributes and values. case of values alone

Returns:
parameter key / value pairs

getName

public java.lang.String getName()
get the Name of this SGML tag, in uppercase format. For example, P for paragraph, B for bold, etc. This value is set to null when whitespace or another problem was encountered where the tag would be.


getOffset

public int getOffset()
returns the number of chars skipped before the starting '<'


isSelfTerminated

public boolean isSelfTerminated()

isNamed

public boolean isNamed(java.lang.String name)
Check name of tag. (Comparision is case-insensitive.)

Returns:
true if passed tag matches this one.

isWellFormed

public boolean isWellFormed()
Check for well-formedness of this tag. Note that calling this method causes rest of tag to be parsed.

Returns:
true if tag is a well-formed SGML tag, false otherwise

nextToken

public java.lang.String nextToken(PositionReader input)
                           throws java.io.IOException
Read next token from string. A token is a space-delimited word, a string in quotes (returned with quotes), a delimiter such as a greater-than, less-than, or equals sign. Quotes marks inside quoted strings may be escaped with a backslash (\) character.

Returns:
next token, or null if whitespace was encountered
Throws:
java.io.IOException

nextToken

public java.lang.String nextToken(PositionReader input,
                                  boolean skipWhitespaces)
                           throws java.io.IOException
Read next token from string. A token is a space-delimited word, a string in quotes (returned with quotes), a delimiter such as a greater-than, less-than, or equals sign. Quotes marks inside quoted strings may be escaped with a backslash (\) character.

Throws:
java.io.IOException

parse

public void parse(PositionReader input)
           throws java.io.IOException
Attempts to read all the attribuets of the tag up to its closing > character.

Parameters:
input - - the input PositionReader
Throws:
java.io.IOException - - of course

searchStart

protected void searchStart(PositionReader input)
                    throws java.io.IOException
Skip over any HTML-style comments, as denoted by matched <-- ... --> pairs.

Throws:
java.io.IOException

toString

public java.lang.String toString()
Render this tag as a string.

Returns:
SGML tag as string, showing range and values

value

public java.lang.String value(java.lang.String attributeName,
                              java.lang.String defaultValue)
Get attribute value, or default if not set. Case is ignored, value("a") will return the same result as value("A"). Note also that if wish to check whether value was set, you can pass null as the defaultValue.

Returns:
value of attribute, or defaultValue if not available

isDelimiter

public static boolean isDelimiter(char c)
Decide whether character is SGML delimiter or equals.

Returns:
true if character is an SGML delimiter

skipWhiteSpace

public static int skipWhiteSpace(PositionReader r)
                          throws java.io.IOException
could be overwritten

Throws:
java.io.IOException

getEnd

public long getEnd()
Gets the end position of the tag in the stream ie: just AFTERE the closing > character.

Note this is the same as the tag start if the attributes of the tag have not been parsed.

Returns:
the end position of the tag in the stream

getStart

public long getStart()
Gets the start position of the tag in the stream ie: just AFTER the starting < character

Returns:
the start position of the tag in the stream

EchoPoint
1.0