org.mmbase.util
Class StringTagger

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

public class StringTagger
extends java.lang.Object
implements java.util.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,v 1.14 2003/09/01 13:29:46 pierre Exp $
Author:
Daniel Ockeloen, Pierre van Rooden

Nested Class Summary
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
StringTagger(java.lang.String line)
          Creates a StringTag for the given line.
StringTagger(java.lang.String line, char tagStart, char tagSeparator, char fieldSeparator, char quote)
          Creates a StringTag for the given line.
StringTagger(java.lang.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(java.lang.Object ob)
          Checks whether a key exits.
 boolean containsValue(java.lang.Object ob)
          Checks whether a value exits.
protected  void createTagger(java.lang.String line)
          Parses the given line, and stores all value-pairs found in the tokens and multitokens fields.
 java.util.Enumeration elements()
          Returns a Enumeration of the values as String.
 java.util.Set entrySet()
          returns all values
 boolean equals(java.lang.Object ob)
          Returns whether two objects are the same
 java.lang.Object get(java.lang.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
 java.util.Enumeration keys()
          Returns a Enumeration of the name keys.
 java.util.Set keySet()
          Returns a Set of the name keys.
static void main(java.lang.String[] args)
          For testing
 java.util.Enumeration multiElements(java.lang.String token)
          Returns a Enumeration of the values as Vectors that contain the seperated values.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          sets a value (for the Map interface).
 void putAll(java.util.Map map)
          Manually sets a set of values (for the Map interface).
 java.lang.Object remove(java.lang.Object key)
          remove a value (for the Map interface).
 void setValue(java.lang.String token, java.lang.String val)
          Manually sets a single value.
 void setValues(java.lang.String token, java.util.Vector values)
          Manually sets a multi-value value.
 int size()
          sets a value (for the Map interface).
protected  void splitTag(java.lang.String tag)
          Handles and splits a tag in its component parts, and store the elemements in the tokens and multitokens fields.
 java.lang.String toString()
          toString
 java.lang.String Value(java.lang.String token)
          Returns the first value as a String.
 java.util.Collection values()
          returns all values
 java.util.Vector Values(java.lang.String token)
          Returns the values as a Vector that contains the separated values.
 java.lang.String ValuesString(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.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?)

splitTag

protected void splitTag(java.lang.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

clear

public void clear()
Clears all data

Specified by:
clear in interface java.util.Map

containsKey

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

Specified by:
containsKey in interface java.util.Map

containsValue

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

Specified by:
containsValue in interface java.util.Map

entrySet

public java.util.Set entrySet()
returns all values

Specified by:
entrySet in interface java.util.Map

equals

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

Specified by:
equals in interface java.util.Map
Parameters:
ob - the key of the value to retrieve

get

public java.lang.Object get(java.lang.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 java.util.Map
Parameters:
ob - the key of the value to retrieve

hashCode

public int hashCode()
Hashcode for sorting and comparing

Specified by:
hashCode in interface java.util.Map

isEmpty

public boolean isEmpty()
Checks whether the tagger is empty

Specified by:
isEmpty in interface java.util.Map

keySet

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

Specified by:
keySet in interface java.util.Map

put

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

Specified by:
put in interface java.util.Map

putAll

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

Specified by:
putAll in interface java.util.Map

remove

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

Specified by:
remove in interface java.util.Map

size

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

Specified by:
size in interface java.util.Map

values

public java.util.Collection values()
returns all values

Specified by:
values in interface java.util.Map

keys

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


toString

public java.lang.String toString()
toString


elements

public java.util.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 java.util.Enumeration multiElements(java.lang.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 java.util.Vector Values(java.lang.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 java.lang.String ValuesString(java.lang.String token)
Returns the original parsed line

Parameters:
token - unused

Value

public java.lang.String Value(java.lang.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(java.lang.String token,
                     java.lang.String val)
Manually sets a single value.


setValues

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


main

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



MMBase build 1.7.4.20050922