You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Leon Messerschmidt <le...@opticode.co.za> on 2001/03/20 15:25:35 UTC
Comma Escapes "\," does not work in Configuration.java
Hi,
The escaping of commas with a backslash "\," does not work in the
Configuration.java. The reason is the recursive call to setProperties:
if (token instanceof String && ((String)token).indexOf(",") > 0)
{
PropertiesTokenizer tokenizer =
new PropertiesTokenizer((String)token);
while (tokenizer.hasMoreTokens())
{
String value = tokenizer.nextToken();
setProperty(key,value);
}
}
If a string has escaped commas (for example "one\,two\,three") is parsed by
PropertiesTokenizer it returns a string with "\"-escapes. (example
"one,two,three"). setProperties() is now recursively called with this
string and the next time it reaches this code it is inserted as a Vector!
My solution is to create a new method and not to use recursion. Something
like this:
public void setStringProperty(String key, String token)
{
Object o = this.get(key);
if (o instanceof String)
{
Vector v = new Vector(2);
v.addElement(o);
v.addElement(token);
put(key, v);
}
else if (o instanceof Vector)
{
((Vector) o).addElement(token);
}
else
{
put(key,toke);
}
}
It will still be able to handle examples like "one\,two,three\,four" but it
will successfully create one string for "one\,two\,three".
Any thoughts on this?
~ Leon
Re: Comma Escapes "\," does not work in Configuration.java
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Leon Messerschmidt wrote:
>
> Hi,
>
> The escaping of commas with a backslash "\," does not work in the
> Configuration.java. The reason is the recursive call to setProperties:
>
> if (token instanceof String && ((String)token).indexOf(",") > 0)
> {
> PropertiesTokenizer tokenizer =
> new PropertiesTokenizer((String)token);
>
> while (tokenizer.hasMoreTokens())
> {
> String value = tokenizer.nextToken();
> setProperty(key,value);
> }
> }
>
> If a string has escaped commas (for example "one\,two\,three") is parsed by
> PropertiesTokenizer it returns a string with "\"-escapes. (example
> "one,two,three"). setProperties() is now recursively called with this
> string and the next time it reaches this code it is inserted as a Vector!
>
> My solution is to create a new method and not to use recursion. Something
> like this:
>
> public void setStringProperty(String key, String token)
> {
> Object o = this.get(key);
>
> if (o instanceof String)
> {
> Vector v = new Vector(2);
> v.addElement(o);
> v.addElement(token);
> put(key, v);
> }
> else if (o instanceof Vector)
> {
> ((Vector) o).addElement(token);
> }
> else
> {
> put(key,toke);
> }
> }
>
> It will still be able to handle examples like "one\,two,three\,four" but it
> will successfully create one string for "one\,two\,three".
>
> Any thoughts on this?
>
> ~ Leon
So you are saying call setStringProperty() in the tokenizer loop?
I am in the middle of some small fixes in there right now, so I can do
something. I will try that.
geir
--
Geir Magnusson Jr. geirm@optonline.net
Developing for the web? See http://jakarta.apache.org/velocity/