KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > util > criteria > PackedParameter


1 /*
2  * Copyright 2004 Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.avalon.util.criteria;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 /**
24  * A parameter descriptor that supports transformation of a
25  * a string to a string array based on a supplied token.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version $Revision: 1.5 $
29  */

30 public class PackedParameter extends Parameter
31 {
32     //--------------------------------------------------------------
33
// immutable state
34
//--------------------------------------------------------------
35

36     private final String JavaDoc m_delimiter;
37
38     //--------------------------------------------------------------
39
// constructors
40
//--------------------------------------------------------------
41

42    /**
43     * Transform a string to a string array.
44     * @param key the parameter key
45     * @param delimiter the delimiter character
46     * @param defaults the default string array
47     */

48     public PackedParameter(
49       final String JavaDoc key, final String JavaDoc delimiter, String JavaDoc[] defaults )
50     {
51         super( key, String JavaDoc[].class, defaults );
52         m_delimiter = delimiter;
53     }
54
55    /**
56     * Resolve a supplied string or string array to a sttring array value.
57     * @param value the value to resolve
58     * @exception CriteriaException if an error occurs
59     */

60     public Object JavaDoc resolve( Object JavaDoc value )
61       throws CriteriaException
62     {
63         if( value == null ) return null;
64         if( value instanceof String JavaDoc[] )
65         {
66             return value;
67         }
68         else if( value instanceof String JavaDoc )
69         {
70             ArrayList JavaDoc list = new ArrayList JavaDoc();
71             String JavaDoc s = (String JavaDoc) value;
72             StringTokenizer JavaDoc tokenizer =
73             new StringTokenizer JavaDoc( s, m_delimiter );
74             while( tokenizer.hasMoreTokens() )
75             {
76                list.add( tokenizer.nextToken() );
77             }
78             return list.toArray( new String JavaDoc[0] );
79         }
80         else
81         {
82             final String JavaDoc error =
83               "Don't know how to transform an instance of ["
84               + value.getClass().getName()
85               + " to a String[].";
86             throw new CriteriaException( error );
87         }
88     }
89 }
90
Popular Tags