org.wings.template.parser
Class SGMLTag

java.lang.Object
  extended by org.wings.template.parser.SGMLTag

public class SGMLTag
extends Object

Convenient class for parsing SGML tokens from a page.

This class is optimized for speed, not ease of use. (Though I'd contend its fairly easy to use anyway!).

Other than earlier versions of this class this one reads its content from a Reader to avoid reading the whole file into a String before parsing it. 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);
      }
 }
 

Author:
Tim Williams, Henner Zeller

Field Summary
static char doubleQuote
           
static char singleQuote
           
 
Constructor Summary
SGMLTag(Reader input)
          Create new SGML tag reference, starting at current location of the Reader.
SGMLTag(Reader input, boolean parseIt)
          Create new SGML tag reference, starting at current location of the Reader.
 
Method Summary
 Iterator attributes(boolean upperCase)
          Get list of attribute names.
 boolean finished()
          Checked whether this tag indicates we're at the end of the list.
 String getAttribute(String key, String defaultValue)
          Deprecated. use attributes() and value() instead
 HashMap getAttributes()
          Deprecated. use attributes() and value() instead
 String getName()
          get the Name of this SGML tag, in uppercase format.
 int getOffset()
          returns the number of chars skipped before the starting '<'
 boolean isNamed(String name)
          Check name of tag.
 boolean isWellFormed()
          Check for well-formedness of this tag.
 String nextToken(Reader input)
          Read next token from string.
 String nextToken(Reader input, boolean skipWhitespaces)
          Read next token from string.
 void parse(Reader input)
           
protected  void searchStart(Reader input)
          Skip over any HTML-style comments, as denoted by matched <-- ...
static int skipWhiteSpace(Reader r)
          could be overwritten
 String toString()
          Render this tag as a string.
 String value(String attributeName, 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

singleQuote

public static final char singleQuote
See Also:
Constant Field Values

doubleQuote

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

SGMLTag

public SGMLTag(Reader input,
               boolean parseIt)
        throws IOException
Create new SGML tag reference, starting at current location of the Reader. 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.

Parameters:
input - the Reader being parsed for SGML tags
parseIt - boolean which denotes if SGMLTag should be parsed fully
Throws:
IOException
See Also:
attributes(boolean)

SGMLTag

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

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

Parameters:
input - the Reader being parsed for SGML tags
Throws:
IOException
See Also:
attributes(boolean)
Method Detail

parse

public void parse(Reader input)
           throws IOException
Throws:
IOException

searchStart

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

Parameters:
input - the reader being parsed for SGMLtags
Throws:
IOException

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

isNamed

public boolean isNamed(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

getOffset

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


getName

public 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.


attributes

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

Parameters:
upperCase - true returns names in all uppercase (good for case-insensitive applications), false returns attribute names with same case as in original text
Returns:
enumeration of attribute names specified as strings, or null if this tag is poorly formed

value

public String value(String attributeName,
                    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.

Parameters:
attributeName - attribute for which to check
defaultValue - value if attribute unset
Returns:
value of attribute, or defaultValue if not available

nextToken

public String nextToken(Reader input)
                 throws 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:
IOException

nextToken

public String nextToken(Reader input,
                        boolean skipWhitespaces)
                 throws 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:
IOException

skipWhiteSpace

public static int skipWhiteSpace(Reader r)
                          throws IOException
could be overwritten

Throws:
IOException

getAttribute

public String getAttribute(String key,
                           String defaultValue)
Deprecated. use attributes() and value() instead

Return value of attribute (parameter) setting in SGML tag.

Parameters:
key - name (uppercase) of attribute for which to check
defaultValue - value if attribute unset
Returns:
value of that attribute, or default if not defined
See Also:
attributes(boolean), value(java.lang.String, java.lang.String)

getAttributes

public HashMap getAttributes()
Deprecated. use attributes() and value() instead

Return tag attributes and values.

Returns:
parameter key / value pairs
See Also:
attributes(boolean), value(java.lang.String, java.lang.String)

toString

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

Overrides:
toString in class Object
Returns:
SGML tag as string, showing range and values


wingS Swings ;-)