/*\
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
* _____________________________________________________________________________________________________________________
*
* This class is "Open Source" as defined by the Open Source Initiative (OSI). You can redistribute it and/or modify it
* under the terms of the BSD License. The license text is appended to the end of this file.
\*/
package de.klaro.base.util;
import java.util.Iterator;
import java.util.StringTokenizer;
/**
* Properties, die ihren Inhalt aus einem String erzeugen oder diesen in einem String ablegen.
*
* Das String-Format muss "key1 = val1; key2 = val2; ..." oder "key1: val1; key2: val2; ..." sein. Leerzeichen vor und
* nach den Separatoren ';' und '=' bzw. ':' werden ignoriert. Das Separatorzeichen ';' kann auch auch ein anderes,
* beliebiges Zeichen sein.
*/
public class StringTokenProperties extends HierarchicalProperties
{
/* ______________________________________________________________________________________________________________ *\
\* Konstanten */
/* ______________________________________________________________________________________________________________ *\
\* Klassenvariablen */
/* ______________________________________________________________________________________________________________ *\
\* Instanzvariablen */
private char separator$;
/* ______________________________________________________________________________________________________________ *\
\* Konstruktoren */
/**
* Erzeugt neue, leere String-Properties, wobei der Separator ';' zur Trennung von Tokens benutzt wird.
*/
public StringTokenProperties()
{
this(null,';');
}
/**
* Erzeugt neue, leere String-Properties mit dem angegebenen Zeichen als Separator zur Trennung von Tokens.
*
* @param separator Der Separator zur Trennung einzelner Properties
*/
public StringTokenProperties(char separator)
{
this(null,separator);
}
/**
* Erzeugt neue String-Properties, wobei der Separator ';' zur Trennung von Tokens benutzt wird.
*
* Die Properties werden aus dem angegeben String initialisiert. Das String-Format muss "key1=val1; key2=val2; ..."
* sein.
*
* @param string Der String
*/
public StringTokenProperties(String string)
{
this(string,';');
}
/**
* Erzeugt neue String-Properties mit dem angegebenen Zeichen als Separator zur Trennung von Tokens.
*
* Die Properties werden aus dem angegeben String initialisiert. Das String-Format muss
* "key1 = val1; key2 = val2; ..." oder "key1: val1; key2: val2; ..." sein (hier mit Semikolons als Trennzeichen).
*
* @param string Der String
* @param separator Der Separator zur Trennung einzelner Properties
*/
public StringTokenProperties(String string,char separator)
{
super();
separator$ = separator;
if (!Strings.isNullOrEmpty(string))
{
StringTokenizer st = new StringTokenizer(string,String.valueOf(separator$));
while (st.hasMoreTokens())
{
String token = st.nextToken();
int index1 = token.indexOf('=');
int index2 = token.indexOf(':');
int index = (index1 > 0 && index2 > 0)? Math.min(index1,index2): ((index1 <= 0)? index2: index1);
if (index > 1)
setProperty(token.substring(0,index).trim(),token.substring(index + 1).trim());
else
setProperty(token.trim(),"");
}
}
}
/* ______________________________________________________________________________________________________________ *\
\* Instanzmethoden */
/**
* Liefert die Properties als String, wobei als Separator das Zeichen benutzt wird, welches im Konstruktor
* gltig war.
*
* Das String-Format ist "key1=val1; key2=val2; ...".
*
* @return Die Properties in Form eines Strings
*/
public String getString()
{
return getString(separator$);
}
/**
* Liefert die Properties als String, wobei als Separator das angebenene Zeichen benutzt wird.
*
* Das String-Format ist "key1=val1; key2=val2; ..." (hier mit Semikolons als Trennzeichen).
*
* @param separator Der Separator zur Trennung einzelner Properties
* @return Die Properties als Tokens in einem String
*/
public String getString(char separator)
{
StringBuffer result = new StringBuffer();
Iterator<Object> iter = keySet().iterator();
for (int i = 0; iter.hasNext(); i++)
{
String key = (String)iter.next();
if (i > 0) result.append(separator);
result.append(key + "=" + get(key));
}
return result.toString();
}
/* ______________________________________________________________________________________________________________ *\
\* Klassenmethoden */
/* ______________________________________________________________________________________________________________ *\
\* Klassen */
}
/*\
* _____________________________________________________________________________________________________________________
*
* This software is distributed under the terms of the BSD License:
*
* Copyright 2006 Klaus Rogall, Hamburg, Germany (klaus.rogall@web.de). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of the Klaus Rogall nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* _____________________________________________________________________________________________________________________
\*/