org.apache.fulcrum.parser.StringValueParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fulcrum-parser Show documentation
Show all versions of fulcrum-parser Show documentation
This Service functions as a repository for Parser components.
package org.apache.fulcrum.parser;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.net.URLDecoder;
import java.util.StringTokenizer;
/**
* An extension that parses a String for name/value pairs.
*
* @author John McNally
* @version $Id: StringValueParser.java 670329 2008-06-22 09:35:12Z tv $
*/
public class StringValueParser
extends BaseValueParser
{
/**
* Parses a String using a single delimiter.
*
* @param s a String
value
* @param delim a char
value
* @param urlDecode a boolean
value
* @exception Exception Error decoding name/value pairs.
*/
public void parse(String s, char delim, boolean urlDecode)
throws Exception
{
String delimChar = String.valueOf(delim);
StringTokenizer st = new StringTokenizer(s, delimChar);
boolean isNameTok = true;
String pathPart = null;
String key = null;
while (st.hasMoreTokens())
{
String tok = st.nextToken();
if ( urlDecode )
{
tok = URLDecoder.decode(tok, getCharacterEncoding());
}
if (isNameTok)
{
key = tok;
isNameTok = false;
}
else
{
pathPart = tok;
if (key != null && key.length() > 0)
{
add (convert(key), pathPart);
}
isNameTok = true;
}
}
}
public void parse(String s, char paramDelim, char pairDelim,
boolean urlDecode)
throws Exception
{
if ( paramDelim == pairDelim )
{
parse(s, paramDelim, urlDecode);
}
else
{
String delimChar = String.valueOf(paramDelim);
StringTokenizer st = new StringTokenizer(s, delimChar);
while (st.hasMoreTokens())
{
String pair = st.nextToken();
int pos = pair.indexOf(pairDelim);
String name = pair.substring(0, pos);
String value = pair.substring(pos+1);
if ( urlDecode )
{
name = URLDecoder.decode(name, getCharacterEncoding());
value = URLDecoder.decode(value, getCharacterEncoding());
}
if (name.length() > 0)
{
add (convert(name), value);
}
}
}
}
}