KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > classloader > PropertyUtil


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.classloader;
9
10 import org.apache.avalon.framework.context.Context;
11 import org.apache.avalon.framework.context.ContextException;
12 import org.apache.avalon.framework.context.Resolvable;
13
14 /**
15  * This provides utility methods for properties.
16  *
17  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
18  * @version CVS $Revision: 1.5 $ $Date: 2002/08/06 11:57:39 $
19  * @since 4.0
20  */

21 final class PropertyUtil
22 {
23     private PropertyUtil()
24     {
25     }
26
27     /**
28      * Resolve a string property. This evaluates all property
29      * substitutions based on specified context.
30      *
31      * @param property the property to resolve
32      * @param context the context in which to resolve property
33      * @param ignoreUndefined if false will throw an PropertyException if property is not found
34      * @return the reolved property
35      * @exception Exception if an error occurs
36      */

37     public static Object JavaDoc resolveProperty( final String JavaDoc property,
38                                           final Context context,
39                                           final boolean ignoreUndefined )
40         throws Exception JavaDoc
41     {
42         int start = findBeginning( property, 0 );
43         if( -1 == start )
44         {
45             return property;
46         }
47
48         int end = findEnding( property, start );
49
50         final int length = property.length();
51
52         if( 0 == start && end == (length - 1) )
53         {
54             return resolveValue( property.substring( start + 2, end ),
55                                  context,
56                                  ignoreUndefined );
57         }
58
59         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc( length * 2 );
60         int lastPlace = 0;
61
62         while( true )
63         {
64             final Object JavaDoc value =
65                 resolveValue( property.substring( start + 2, end ),
66                               context,
67                               ignoreUndefined );
68
69             sb.append( property.substring( lastPlace, start ) );
70             sb.append( value );
71
72             lastPlace = end + 1;
73
74             start = findBeginning( property, lastPlace );
75             if( -1 == start )
76             {
77                 break;
78             }
79
80             end = findEnding( property, start );
81         }
82
83         sb.append( property.substring( lastPlace, length ) );
84
85         return sb.toString();
86     }
87
88     /**
89      * Resolve a string property. This recursively evaluates all property
90      * substitutions based on specified context.
91      *
92      * @param property the property to resolve
93      * @param context the context in which to resolve property
94      * @param ignoreUndefined if false will throw an Exception if property is not found
95      * @return the reolved property
96      * @exception Exception if an error occurs
97      */

98     public static Object JavaDoc recursiveResolveProperty( final String JavaDoc property,
99                                                    final Context context,
100                                                    final boolean ignoreUndefined )
101         throws Exception JavaDoc
102     {
103         int start = findBeginning( property, 0 );
104         if( -1 == start )
105         {
106             return property;
107         }
108
109         int end = findNestedEnding( property, start );
110
111         final int length = property.length();
112
113         if( 0 == start && end == (length - 1) )
114         {
115             final String JavaDoc propertyName = property.substring( start + 2, end );
116             final Object JavaDoc key = recursiveResolveProperty( propertyName, context, ignoreUndefined );
117             return resolveValue( key.toString(), context, ignoreUndefined );
118         }
119
120         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc( length * 2 );
121
122         int lastPlace = 0;
123
124         while( true )
125         {
126             final String JavaDoc propertyName = property.substring( start + 2, end );
127             final Object JavaDoc key = recursiveResolveProperty( propertyName, context, ignoreUndefined );
128             final Object JavaDoc value = resolveValue( key.toString(), context, ignoreUndefined );
129
130             sb.append( property.substring( lastPlace, start ) );
131             sb.append( value );
132
133             lastPlace = end + 1;
134
135             start = findBeginning( property, lastPlace );
136             if( -1 == start )
137             {
138                 break;
139             }
140
141             end = findNestedEnding( property, start );
142         }
143
144         sb.append( property.substring( lastPlace, length ) );
145
146         return sb.toString();
147     }
148
149     private static int findBeginning( final String JavaDoc property, final int currentPosition )
150     {
151         //TODO: Check if it is commented out
152
return property.indexOf( "${", currentPosition );
153     }
154
155     private static int findEnding( final String JavaDoc property, final int currentPosition )
156         throws Exception JavaDoc
157     {
158         //TODO: Check if it is commented out
159
final int index = property.indexOf( '}', currentPosition );
160         if( -1 == index )
161         {
162             throw new Exception JavaDoc( "Malformed property with mismatched }'s" );
163         }
164
165         return index;
166     }
167
168     private static int findNestedEnding( final String JavaDoc property, final int currentPosition )
169         throws Exception JavaDoc
170     {
171         final int length = property.length();
172         final int start = currentPosition + 2;
173
174         int weight = 1;
175         for( int i = start; (weight > 0) && (i < length); i++ )
176         {
177             final char ch = property.charAt( i );
178             switch( ch )
179             {
180                 case '}':
181                     //TODO: Check if it is commented out
182
weight--;
183                     if( weight == 0 )
184                     {
185                         return i;
186                     }
187                     break;
188
189                 case '$':
190                     {
191                         //TODO: Check if it is commented out
192
final int next = i + 1;
193                         if( next < length && '{' == property.charAt( next ) )
194                         {
195                             weight++;
196                         }
197                     }
198                     break;
199             }
200         }
201
202         throw new Exception JavaDoc( "Malformed property with mismatched }'s" );
203     }
204
205     /**
206      * Retrieve a value from the specified context using the specified key.
207      * If there is no such value and ignoreUndefined is not false then a
208      * Exception is generated.
209      *
210      * @param key the key of value in context
211      * @param context the Context
212      * @param ignoreUndefined true if undefined variables are ignored
213      * @return the object retrieved from context
214      * @exception Exception if an error occurs
215      */

216     private static Object JavaDoc resolveValue( final String JavaDoc key,
217                                         final Context context,
218                                         final boolean ignoreUndefined )
219         throws Exception JavaDoc
220     {
221         Object JavaDoc value = null;
222
223         try
224         {
225             value = context.get( key );
226         }
227         catch( final ContextException ce )
228         {
229             //ignore
230
}
231
232         try
233         {
234             while( null != value && value instanceof Resolvable )
235             {
236                 value = ((Resolvable)value).resolve( context );
237             }
238         }
239         catch( final ContextException ce )
240         {
241             throw new Exception JavaDoc( "Unable to resolve value for key " + key );
242         }
243
244         if( null == value )
245         {
246             if( ignoreUndefined )
247             {
248                 return "";
249             }
250             else
251             {
252                 throw new Exception JavaDoc( "Unable to find " + key + " to expand during "
253                                      + "property resolution." );
254             }
255         }
256
257         return value;
258     }
259 }
260
Popular Tags