Search this API

y.io.gml
Class ItemParser

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

public class ItemParser
extends java.lang.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 top scope level 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. 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. childFinished(y.io.gml.ItemParser) will be called as soon as the parser leaves the scope of a child ItemParser, after the child's end() method has been called. 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  java.util.Map attributes
          the attribute map, used to store key value pairs which belong to the top level only
protected  java.util.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 top level, and positive if in a subsection
 
Constructor Summary
ItemParser()
          constructs a new ItemParser with no children.
 
Method Summary
 void addAttribute(java.lang.String key, java.lang.Object value)
          Puts the key value pair into the local attribute map
 void addChild(java.lang.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(java.lang.String key, java.lang.Number value)
          Note: subclass implementers should make a super call, if they want to overwrite this method.
 void attribute(java.lang.String key, java.lang.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(java.lang.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(java.lang.String endScope)
          Note: subclass implementers should make a super call, if they want to overwrite this method.
 java.util.Map getAttributes()
          Getter for property attributes.
 boolean getBoolean(java.lang.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(java.lang.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(java.lang.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(java.lang.String key)
          Convenience method, which returns a double if there is an numeric attribute in the table for the key, or 0
 int getInt(java.lang.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
 java.lang.Object getItem()
          Returns the "product" of this scope.
 int getLevel()
          Getter for property level.
 java.lang.String getString(java.lang.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 top level, and positive if in a subsection


childMap

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


attributes

protected java.util.Map attributes
the attribute map, used to store key value pairs which belong to the top level 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(java.lang.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 responsible ItemParser

beginScope

public void beginScope(java.lang.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(java.lang.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(java.lang.String key,
                      java.lang.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(java.lang.String key,
                      java.lang.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(java.lang.String key,
                         java.lang.Object value)
Puts the key value pair into the local attribute map

Parameters:
key - the key
value - the value

getBoolean

public boolean getBoolean(java.lang.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 java.lang.String getString(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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 java.lang.Object getItem()
Returns the "product" of this scope. This method should be called after this parser has left the scope, i.e. after 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 java.util.Map getAttributes()
Getter for property attributes.

Returns:
Value of property attributes.

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