KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > util > PropertyUtil


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.util;
88
89 import org.apache.avalon.framework.context.Context;
90 import org.apache.avalon.framework.context.ContextException;
91
92 /**
93  * This provides utility methods for properties.
94  *
95  * @author Peter Donald
96  * @version CVS $Revision: 1.3 $ $Date: 2004/08/17 23:14:32 $
97  * @since 4.0
98  */

99 public final class PropertyUtil
100 {
101     private PropertyUtil()
102     {
103     }
104
105     /**
106      * Resolve a string property. This evaluates all property substitutions
107      * based on specified context.
108      *
109      * @param property the property to resolve
110      * @param context the context in which to resolve property
111      * @param ignoreUndefined if false will throw an PropertyException if
112      * property is not found
113      * @return the reolved property
114      * @throws java.lang.Exception if an error occurs
115      */

116     public static Object JavaDoc resolveProperty( final String JavaDoc property,
117                                           final Context context,
118                                           final boolean ignoreUndefined )
119         throws Exception JavaDoc
120     {
121         int start = findBeginning( property, 0 );
122         if( -1 == start )
123         {
124             return property;
125         }
126
127         int end = findEnding( property, start );
128
129         final int length = property.length();
130
131         if( 0 == start && end == ( length - 1 ) )
132         {
133             return resolveValue( property.substring( start + 2, end ),
134                                  context,
135                                  ignoreUndefined );
136         }
137
138         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc( length * 2 );
139         int lastPlace = 0;
140
141         while( true )
142         {
143             final Object JavaDoc value =
144                 resolveValue( property.substring( start + 2, end ),
145                               context,
146                               ignoreUndefined );
147
148             sb.append( property.substring( lastPlace, start ) );
149             sb.append( value );
150
151             lastPlace = end + 1;
152
153             start = findBeginning( property, lastPlace );
154             if( -1 == start )
155             {
156                 break;
157             }
158
159             end = findEnding( property, start );
160         }
161
162         sb.append( property.substring( lastPlace, length ) );
163
164         return sb.toString();
165     }
166
167     private static int findBeginning( final String JavaDoc property,
168                                       final int currentPosition )
169     {
170         //TODO: Check if it is commented out
171
return property.indexOf( "${", currentPosition );
172     }
173
174     private static int findEnding( final String JavaDoc property,
175                                    final int currentPosition )
176         throws Exception JavaDoc
177     {
178         //TODO: Check if it is commented out
179
final int index = property.indexOf( '}', currentPosition );
180         if( -1 == index )
181         {
182             throw new Exception JavaDoc( "Malformed property with mismatched }'s" );
183         }
184
185         return index;
186     }
187
188     /**
189      * Retrieve a value from the specified context using the specified key. If
190      * there is no such value and ignoreUndefined is not false then a Exception
191      * is generated.
192      *
193      * @param key the key of value in context
194      * @param context the Context
195      * @param ignoreUndefined true if undefined variables are ignored
196      * @return the object retrieved from context
197      * @throws java.lang.Exception if an error occurs
198      */

199     private static Object JavaDoc resolveValue( final String JavaDoc key,
200                                         final Context context,
201                                         final boolean ignoreUndefined )
202         throws Exception JavaDoc
203     {
204         Object JavaDoc value = null;
205
206         try
207         {
208             value = context.get( key );
209         }
210         catch( final ContextException ce )
211         {
212             //ignore
213
}
214
215         if( null == value )
216         {
217             if( ignoreUndefined )
218             {
219                 return "";
220             }
221             else
222             {
223                 throw new Exception JavaDoc( "Unable to find " +
224                                      key +
225                                      " to expand during "
226                                      + "property resolution." );
227             }
228         }
229
230         return value;
231     }
232 }
233
Popular Tags