com.sun.hk2.component
Class KeyValuePairParser

java.lang.Object
  extended by com.sun.hk2.component.KeyValuePairParser

public final class KeyValuePairParser
extends java.lang.Object

Parses a string like key=value,key=value,key=value.

More specifically the format of the line satisfies the following BNF constructions:

 LINE = TOKEN ( ',' LINE )?               // LINE is ','-separated TOKENs
 TOKEN = KEY ( '=' VALUE )?               // TOKEN is a key/value pair. value is optional.
 KEY = [^,=]+                             // KEY is a non-empty string that doesn't contain ',' nor '='
 VALUE = '"' ([^"]| '\' ANYCHAR )* '"'    // VALUE is any string surrounded by quotes/doublequotes (where \ is used as escape), or ...
 VALUE = ''' ([^']| '\' ANYCHAR )* '''
 VALUE = [^,]*                //       possibly empty string that doesn't contain ','
 

This class works like StringTokenizer; each time parseNext() is invoked, the parser "moves" to the next key/value pair, which you can then obtain with getKey() and getValue(). The hasNext() method shall be used to check if there's a next key/value pair after the current position. TODO: improved error check

Author:
Kohsuke Kawaguchi

Constructor Summary
KeyValuePairParser()
           
KeyValuePairParser(java.lang.String s)
           
 
Method Summary
 java.lang.String find(java.lang.String key)
          Skips until we find the value of the given key, and leaves the parser at that point.
 java.lang.Iterable<java.lang.String> findAll(java.lang.String key)
          Finds all the values of the given key.
 java.lang.String getKey()
           
 java.lang.String getLine()
          Gets the whole line.
 java.lang.String getValue()
           
 boolean hasNext()
           
 void parseNext()
           
 void rewind()
          Moves the parser to the head of the key/value pair, to reparse the same string.
 void set(java.lang.String str)
          Resets the parser to parse the given string that looks like "key=value,key=value,..."
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeyValuePairParser

public KeyValuePairParser()

KeyValuePairParser

public KeyValuePairParser(java.lang.String s)
Method Detail

set

public void set(java.lang.String str)
Resets the parser to parse the given string that looks like "key=value,key=value,..."


hasNext

public boolean hasNext()

parseNext

public void parseNext()

getKey

public java.lang.String getKey()

getValue

public java.lang.String getValue()

rewind

public void rewind()
Moves the parser to the head of the key/value pair, to reparse the same string.


find

public java.lang.String find(java.lang.String key)
Skips until we find the value of the given key, and leaves the parser at that point.

This method is not terribly efficient, but it is convenient.


findAll

public java.lang.Iterable<java.lang.String> findAll(java.lang.String key)
Finds all the values of the given key. Calling the iterator will rewind the parser.


getLine

public java.lang.String getLine()
Gets the whole line.



Copyright © 2009 Sun Microsystems, Inc.. All Rights Reserved.