KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > repository > impl > ArtifactSequenceParameter


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.repository.impl;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import org.apache.avalon.repository.Artifact;
24
25 import org.apache.avalon.util.criteria.Parameter;
26 import org.apache.avalon.util.criteria.CriteriaException;
27
28
29 /**
30  * A parameter descriptor that supports transformation of a
31  * a string to a string array based on a supplied token.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version $Revision: 1.1 $
35  */

36 public class ArtifactSequenceParameter extends Parameter
37 {
38     //--------------------------------------------------------------
39
// immutable state
40
//--------------------------------------------------------------
41

42     private final String JavaDoc m_delimiter;
43
44     //--------------------------------------------------------------
45
// constructors
46
//--------------------------------------------------------------
47

48    /**
49     * Transform a string to a string array.
50     * @param key the parameter key
51     * @param delimiter the delimiter character
52     * @param defaults the default string array
53     */

54     public ArtifactSequenceParameter(
55       final String JavaDoc key, final String JavaDoc delimiter, Artifact[] defaults )
56     {
57         super( key, Artifact[].class, defaults );
58         m_delimiter = delimiter;
59     }
60
61    /**
62     * Resolve a supplied string or string array to a artifact array value.
63     * @param value the value to resolve
64     * @exception CriteriaException if an error occurs
65     */

66     public Object JavaDoc resolve( Object JavaDoc value )
67       throws CriteriaException
68     {
69         if( value == null ) return null;
70         if( value instanceof Artifact[] )
71         {
72             return value;
73         }
74         if( value instanceof String JavaDoc[] )
75         {
76             String JavaDoc[] specs = (String JavaDoc[]) value;
77             Artifact[] artifacts = new Artifact[ specs.length ];
78             for( int i=0; i<specs.length; i++ )
79             {
80                 final String JavaDoc spec = specs[i];
81                 artifacts[i] = Artifact.createArtifact( spec );
82             }
83             return artifacts;
84         }
85         else if( value instanceof String JavaDoc )
86         {
87             ArrayList JavaDoc list = new ArrayList JavaDoc();
88             String JavaDoc s = (String JavaDoc) value;
89             StringTokenizer JavaDoc tokenizer =
90             new StringTokenizer JavaDoc( s, m_delimiter );
91             while( tokenizer.hasMoreTokens() )
92             {
93                final String JavaDoc spec = tokenizer.nextToken();
94                Artifact artifact = Artifact.createArtifact( spec );
95                list.add( artifact );
96             }
97             return list.toArray( new Artifact[0] );
98         }
99         else
100         {
101             final String JavaDoc error =
102               "Don't know how to transform an instance of ["
103               + value.getClass().getName()
104               + " to a Artifact[].";
105             throw new CriteriaException( error );
106         }
107     }
108 }
109
Popular Tags