y.io.gml
Class ItemParser

java.lang.Object
  |
  +--y.io.gml.ItemParser
All Implemented Interfaces:
GMLTokenizer.Callback
Direct Known Subclasses:
EdgeGraphicsParser, EdgeParser, GraphParser, InterEdgeParser, LabelGraphicsParser, LineParser, NodeGraphicsParser, NodeParser

public class ItemParser
extends Object
implements GMLTokenizer.Callback

This class can be used as a base class for parsers, that want to parse items. It will intercept events which are delivered by the GMLTOkenizer, store topscopelevel attributes in a Map. It can be provided with child Itemparser instances, that will be automatically be invoked for subsections. Implementers basically need to overwrite three methods in order to get a working parser. ItemParser.begin() will be automatically called when the parsers enters the scope level. Subclasses, that overwrite this method should should make a super call, since the internal attribute map will be cleared there. ItemParser.childFinished(y.io.gml.ItemParser) will be called as soon as the parser leaves the scope of a child ItemParser, after the child's ItemParser.end() method has been called. ItemParser.end() will be called as soon as the current scope will be left. Instances can than process the attributes and the results of the child parsers to produce a suitable item.


Field Summary
protected  Map attributes
          the attribute map, used to store key value pairs which belong to the toplevel only
protected  Map childMap
          the map of section names to child ItemParser instances
protected  ItemParser delegate
          the currently active child
protected  int level
          the relative scope depth of the parser, i.e. 0 if we are on the current toplevel, and positive if in a subsection
 
Constructor Summary
ItemParser()
          constructs a new ItemParser with no children.
 
Method Summary
 void addAttribute(String key, Object value)
          Puts the key value pair into the local attribute map
 void addChild(String scopeName, ItemParser child)
          add a child ItemParser, to which events will be delegated if the parser comes across a section with the specified scope name.
 void attribute(String key, Number value)
          Note: subclass implementers should make a super call, if they want to overwrite this method.
 void attribute(String key, String value)
          Note: subclass implementers should make a super call, if they want to overwrite this method.
 void begin()
          this method will be called as soon as the parser enters the scope.
 void beginScope(String scopeName)
          Note: subclass implementers should make a super call, if they want to overwrite this method.
 void childFinished(ItemParser p)
          this method will be called, as soon as the a child ItemParser finished and returned from its end() method.
 void end()
          this method will be called as soon as the parser leaves the scope.
 void endScope(String endScope)
          Note: subclass implementers should make a super call, if they want to overwrite this method.
 Map getAttributes()
          Getter for property attributes.
 boolean getBoolean(String key)
          Convenience method, which returns true if and only if there is an integer attribute valued >0 in the table for the key.
 double getDouble(String key)
          Convenience method, which returns a double if there is an numeric attribute in the table for the key, or 0.0d
 double getDouble(String key, double defaultValue)
          Convenience method, which returns a double if there is an numeric attribute in the table for the key, or the default value
 int getInt(String key)
          Convenience method, which returns a double if there is an numeric attribute in the table for the key, or 0
 int getInt(String key, int defaultValue)
          Convenience method, which returns an int if there is an numeric attribute in the table for the key, or the default value
 Object getItem()
          Returns the "product" of this scope.
 int getLevel()
          Getter for property level.
 String getString(String key)
          Returns a String representation of the value in the map for key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

level

protected int level
the relative scope depth of the parser, i.e. 0 if we are on the current toplevel, and positive if in a subsection


childMap

protected Map childMap
the map of section names to child ItemParser instances


attributes

protected Map attributes
the attribute map, used to store key value pairs which belong to the toplevel only


delegate

protected ItemParser delegate
the currently active child

Constructor Detail

ItemParser

public ItemParser()
constructs a new ItemParser with no children. This can be used to just collect all attributes at section level.

Method Detail

addChild

public void addChild(String scopeName,
                     ItemParser child)
add a child ItemParser, to which events will be delegated if the parser comes across a section with the specified scope name.

Parameters:
scopeName - the name of the sub section
child - the responsibler ItemParser

beginScope

public void beginScope(String scopeName)
Note: subclass implementers should make a super call, if they want to overwrite this method.

Specified by:
beginScope in interface GMLTokenizer.Callback
Parameters:
scopeName - the name of the scope

endScope

public void endScope(String endScope)
Note: subclass implementers should make a super call, if they want to overwrite this method.

Specified by:
endScope in interface GMLTokenizer.Callback
Parameters:
endScope - the name of the scope which was left

attribute

public void attribute(String key,
                      String value)
Note: subclass implementers should make a super call, if they want to overwrite this method.

Specified by:
attribute in interface GMLTokenizer.Callback
Parameters:
key - the key of the attribute value pair
value - the string value

attribute

public void attribute(String key,
                      Number value)
Note: subclass implementers should make a super call, if they want to overwrite this method.

Specified by:
attribute in interface GMLTokenizer.Callback
Parameters:
key - the key of the attribute value pair
value - the numeric value

addAttribute

public void addAttribute(String key,
                         Object value)
Puts the key value pair into the local attribute map

Parameters:
key - the key
value - the value

getBoolean

public boolean getBoolean(String key)
Convenience method, which returns true if and only if there is an integer attribute valued >0 in the table for the key.

Parameters:
key - the key
Returns:
true, iff map.get(key)!= null and the value is an positive Integer

getString

public String getString(String key)
Returns a String representation of the value in the map for key.

Parameters:
key - the key
Returns:
a String representation, which is not null

getDouble

public double getDouble(String key)
Convenience method, which returns a double if there is an numeric attribute in the table for the key, or 0.0d

Parameters:
key - the key
Returns:
the numeric value or 0.0d

getDouble

public double getDouble(String key,
                        double defaultValue)
Convenience method, which returns a double if there is an numeric attribute in the table for the key, or the default value

Parameters:
key - the key
defaultValue - a default value
Returns:
the value in the map or the default value, if there is no such entry in the map

getInt

public int getInt(String key)
Convenience method, which returns a double if there is an numeric attribute in the table for the key, or 0

Parameters:
key - the key
Returns:
the value in the map or 0

getInt

public int getInt(String key,
                  int defaultValue)
Convenience method, which returns an int if there is an numeric attribute in the table for the key, or the default value

Parameters:
key - the key
defaultValue - the default value
Returns:
the value in the map or the default value

begin

public void begin()
this method will be called as soon as the parser enters the scope. This implementation will clear the attribute map. Subclasses should therefore make a super call.


end

public void end()
this method will be called as soon as the parser leaves the scope. This implementation does nothing. Subclasses should nevertheless make a super call.


getItem

public Object getItem()
Returns the "product" of this scope. This method should be called after this parser has left the scope, i.e. after ItemParser.end() has been called. The latter should construct the resulting item from the collected data and this item should be returned by this method. By default, this method return the map of the attributes.

Returns:
the result of the parsing, in this case a Map

childFinished

public void childFinished(ItemParser p)
this method will be called, as soon as the a child ItemParser finished and returned from its end() method. This implementation does nothing. Subclasses should nevertheless make a super call.

Parameters:
p - the child ItemParser, which just finished

getLevel

public int getLevel()
Getter for property level.

Returns:
Value of property level.

getAttributes

public Map getAttributes()
Getter for property attributes.

Returns:
Value of property attributes.

© Copyright 2000-2003,
yWorks GmbH.
All rights reserved.

2003