001 package org.apache.fulcrum.parser; 002 003 004 /* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024 import java.net.URLDecoder; 025 import java.util.StringTokenizer; 026 027 /** 028 * An extension that parses a String for name/value pairs. 029 * 030 * @author <a href="mailto:jmcnally@collab.net">John McNally</a> 031 * @version $Id: StringValueParser.java 670329 2008-06-22 09:35:12Z tv $ 032 */ 033 public class StringValueParser 034 extends BaseValueParser 035 { 036 /** 037 * Parses a String using a single delimiter. 038 * 039 * @param s a <code>String</code> value 040 * @param delim a <code>char</code> value 041 * @param urlDecode a <code>boolean</code> value 042 * @exception Exception Error decoding name/value pairs. 043 */ 044 public void parse(String s, char delim, boolean urlDecode) 045 throws Exception 046 { 047 String delimChar = String.valueOf(delim); 048 StringTokenizer st = new StringTokenizer(s, delimChar); 049 boolean isNameTok = true; 050 String pathPart = null; 051 String key = null; 052 while (st.hasMoreTokens()) 053 { 054 String tok = st.nextToken(); 055 if ( urlDecode ) 056 { 057 tok = URLDecoder.decode(tok, getCharacterEncoding()); 058 } 059 060 if (isNameTok) 061 { 062 key = tok; 063 isNameTok = false; 064 } 065 else 066 { 067 pathPart = tok; 068 if (key != null && key.length() > 0) 069 { 070 add (convert(key), pathPart); 071 } 072 isNameTok = true; 073 } 074 } 075 } 076 077 public void parse(String s, char paramDelim, char pairDelim, 078 boolean urlDecode) 079 throws Exception 080 { 081 if ( paramDelim == pairDelim ) 082 { 083 parse(s, paramDelim, urlDecode); 084 } 085 else 086 { 087 String delimChar = String.valueOf(paramDelim); 088 StringTokenizer st = new StringTokenizer(s, delimChar); 089 090 while (st.hasMoreTokens()) 091 { 092 String pair = st.nextToken(); 093 int pos = pair.indexOf(pairDelim); 094 String name = pair.substring(0, pos); 095 String value = pair.substring(pos+1); 096 097 if ( urlDecode ) 098 { 099 name = URLDecoder.decode(name, getCharacterEncoding()); 100 value = URLDecoder.decode(value, getCharacterEncoding()); 101 } 102 103 if (name.length() > 0) 104 { 105 add (convert(name), value); 106 } 107 } 108 } 109 } 110 }