org.mmbase.util
Class StringTagger

java.lang.Object
  extended by org.mmbase.util.StringTagger
All Implemented Interfaces:
Map

public class StringTagger
extends Object
implements Map

StringTagger, Creates a object with tags and fields from a String. Its ideal for name-value pairs and name-value pairs with multivalues. It also provides support for quoted values, and recognizes values that are 'function' calls with their own parameter list (allowing to ignore any tokens within these lists when parsing).

Version:
$Id: StringTagger.java 41055 2010-02-15 23:59:16Z michiel $
Author:
Daniel Ockeloen, Pierre van Rooden
Application:
SCAN
Code-conventions:
Some methods (Values, Value etc) have wrong names (and are duplicating Map methods btw)

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
StringTagger(String line)
          Creates a StringTag for the given line.
StringTagger(String line, char tagStart, char tagSeparator, char fieldSeparator, char quote)
          Creates a StringTag for the given line.
StringTagger(String line, char tagStart, char tagSeparator, char fieldSeparator, char quote, char functionOpen, char functionClose)
          Creates a StringTag for the given line.
 
Method Summary
 void clear()
          Clears all data
 boolean containsKey(Object ob)
          Checks whether a key exits.
 boolean containsValue(Object ob)
          Checks whether a value exits.
protected  void createTagger(String line)
          Parses the given line, and stores all value-pairs found in the tokens and multitokens fields.
 Enumeration elements()
          Returns a Enumeration of the values as String.
 Set entrySet()
          returns all values
 boolean equals(Object ob)
          Returns whether two objects are the same
 Object get(Object ob)
          Returns the value of a key as an Object.
 int hashCode()
          Hashcode for sorting and comparing
 boolean isEmpty()
          Checks whether the tagger is empty
 Enumeration keys()
          Returns a Enumeration of the name keys.
 Set keySet()
          Returns a Set of the name keys.
static void main(String[] args)
          For testing
 Enumeration multiElements(String token)
          Returns a Enumeration of the values as Vectors that contain the seperated values.
 Object put(Object key, Object value)
          sets a value (for the Map interface).
 void putAll(Map map)
          Manually sets a set of values (for the Map interface).
 Object remove(Object key)
          remove a value (for the Map interface).
 void setValue(String token, String val)
          Manually sets a single value.
 void setValues(String token, Vector values)
          Manually sets a multi-value value.
 int size()
          sets a value (for the Map interface).
protected  void splitTag(String tag)
          Handles and splits a tag in its component parts, and store the elemements in the tokens and multitokens fields.
 String toString()
          toString
 String Value(String token)
          Returns the first value as a String.
 Collection values()
          returns all values
 Vector Values(String token)
          Returns the values as a Vector that contains the separated values.
 String ValuesString(String token)
          Returns the original parsed line
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringTagger

public StringTagger(String line,
                    char tagStart,
                    char tagSeparator,
                    char fieldSeparator,
                    char quote,
                    char functionOpen,
                    char functionClose)
Creates a StringTag for the given line. Example : StringTagger("cmd=lookup names='Daniel Ockeloen, Rico Jansen'",' ','=',','\'','('.')')

Parameters:
line - : to be tagged line
tagStart - : Seperator for the Tags
tagSeparator - : Seperator inside the Tag (between name and value)
fieldSeparator - : Seperator inside the value
quote - : Char used if a quoted value
functionOpen - char used to open a function parameter list
functionClose - char used to close a function parameter list

StringTagger

public StringTagger(String line,
                    char tagStart,
                    char tagSeparator,
                    char fieldSeparator,
                    char quote)
Creates a StringTag for the given line. Uses default characters for the function parameter list tokens. Example : StringTagger("cmd=lookup names='Daniel Ockeloen, Rico Jansen'",' ','=',','\'')

Parameters:
line - : to be tagged line
tagStart - : Seperator for the Tags
tagSeparator - : Seperator inside the Tag (between name and value)
fieldSeparator - : Seperator inside the value
quote - : Char used if a quoted value

StringTagger

public StringTagger(String line)
Creates a StringTag for the given line. Uses default characters for all tokens.

Parameters:
line - : to be tagged line
Method Detail

createTagger

protected void createTagger(String line)
Parses the given line, and stores all value-pairs found in the tokens and multitokens fields.

Parameters:
line - : to be tagged line (why is this a parameter when it can eb retrieved from startline?)
Since:
MMBase-1.7

splitTag

protected void splitTag(String tag)
Handles and splits a tag in its component parts, and store the elemements in the tokens and multitokens fields.

Parameters:
tag - the string containing the tag
Since:
MMBase-1.7

clear

public void clear()
Clears all data

Specified by:
clear in interface Map

containsKey

public boolean containsKey(Object ob)
Checks whether a key exits.

Specified by:
containsKey in interface Map

containsValue

public boolean containsValue(Object ob)
Checks whether a value exits.

Specified by:
containsValue in interface Map

entrySet

public Set entrySet()
returns all values

Specified by:
entrySet in interface Map

equals

public boolean equals(Object ob)
Returns whether two objects are the same

Specified by:
equals in interface Map
Overrides:
equals in class Object
Parameters:
ob - the key of the value to retrieve

get

public Object get(Object ob)
Returns the value of a key as an Object. The value returned is a single, unseparated, string.
Use Values(java.lang.String) to get a list of multi-values as a Vector.
Use Value(java.lang.String) to get the first value as a String

Specified by:
get in interface Map
Parameters:
ob - the key of the value to retrieve

hashCode

public int hashCode()
Hashcode for sorting and comparing

Specified by:
hashCode in interface Map
Overrides:
hashCode in class Object

isEmpty

public boolean isEmpty()
Checks whether the tagger is empty

Specified by:
isEmpty in interface Map

keySet

public Set keySet()
Returns a Set of the name keys.

Specified by:
keySet in interface Map

put

public Object put(Object key,
                  Object value)
sets a value (for the Map interface).

Specified by:
put in interface Map

putAll

public void putAll(Map map)
Manually sets a set of values (for the Map interface).

Specified by:
putAll in interface Map

remove

public Object remove(Object key)
remove a value (for the Map interface).

Specified by:
remove in interface Map

size

public int size()
sets a value (for the Map interface).

Specified by:
size in interface Map

values

public Collection values()
returns all values

Specified by:
values in interface Map

keys

public Enumeration keys()
Returns a Enumeration of the name keys.


toString

public String toString()
toString

Overrides:
toString in class Object

elements

public Enumeration elements()
Returns a Enumeration of the values as String. The values returned are all single, unsepartated, strings. Use multiElements(java.lang.String) to get a list of multi-values.


multiElements

public Enumeration multiElements(String token)
Returns a Enumeration of the values as Vectors that contain the seperated values. Use elements() to get a list of single, unseparated, values.


Values

public Vector Values(String token)
Returns the values as a Vector that contains the separated values.
Use get(java.lang.Object) to get the list of values as a String
Use Value(java.lang.String) to get the first value as a String

Parameters:
token - the key of the value to retrieve

ValuesString

public String ValuesString(String token)
Returns the original parsed line

Parameters:
token - unused

Value

public String Value(String token)
Returns the first value as a String. In case of a single value, it returns that value. In case of multiple values, it returns the Use get(java.lang.Object) to get the list of values as a String
Use Values(java.lang.String) to get a list of multi-values as a Vector.

Parameters:
token - the key of the value to retrieve

setValue

public void setValue(String token,
                     String val)
Manually sets a single value.


setValues

public void setValues(String token,
                      Vector values)
Manually sets a multi-value value.


main

public static void main(String[] args)
For testing



MMBase 2.0-SNAPSHOT - null