avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: avalon-phoenix/src/java/org/apache/avalon/phoenix/components/util PropertyUtil.java
Date Sat, 01 Mar 2003 08:16:17 GMT
donaldp     2003/03/01 00:16:17

  Modified:    src/java/org/apache/avalon/phoenix/components/classloader
                        SarPolicyResolver.java
               src/java/org/apache/avalon/phoenix/components/configuration
                        FileSystemPersistentConfigurationRepository.java
  Added:       src/java/org/apache/avalon/phoenix/components/util
                        PropertyUtil.java
  Removed:     src/java/org/apache/avalon/phoenix/components/classloader
                        PropertyUtil.java
  Log:
  Decouple from util jar
  
  Revision  Changes    Path
  1.6       +2 -1      avalon-phoenix/src/java/org/apache/avalon/phoenix/components/classloader/SarPolicyResolver.java
  
  Index: SarPolicyResolver.java
  ===================================================================
  RCS file: /home/cvs/avalon-phoenix/src/java/org/apache/avalon/phoenix/components/classloader/SarPolicyResolver.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SarPolicyResolver.java	22 Feb 2003 04:03:26 -0000	1.5
  +++ SarPolicyResolver.java	1 Mar 2003 08:16:16 -0000	1.6
  @@ -20,6 +20,7 @@
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.phoenix.BlockContext;
   import org.apache.avalon.phoenix.components.util.ResourceUtil;
  +import org.apache.avalon.phoenix.components.util.PropertyUtil;
   import org.apache.excalibur.policy.builder.PolicyResolver;
   
   /**
  
  
  
  1.15      +2 -4      avalon-phoenix/src/java/org/apache/avalon/phoenix/components/configuration/FileSystemPersistentConfigurationRepository.java
  
  Index: FileSystemPersistentConfigurationRepository.java
  ===================================================================
  RCS file: /home/cvs/avalon-phoenix/src/java/org/apache/avalon/phoenix/components/configuration/FileSystemPersistentConfigurationRepository.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- FileSystemPersistentConfigurationRepository.java	22 Feb 2003 05:34:44 -0000	1.14
  +++ FileSystemPersistentConfigurationRepository.java	1 Mar 2003 08:16:16 -0000	1.15
  @@ -12,8 +12,6 @@
   import org.apache.avalon.excalibur.i18n.ResourceManager;
   import org.apache.avalon.excalibur.i18n.Resources;
   import org.apache.avalon.excalibur.io.FileUtil;
  -import org.apache.avalon.excalibur.property.PropertyException;
  -import org.apache.avalon.excalibur.property.PropertyUtil;
   import org.apache.avalon.framework.CascadingRuntimeException;
   import org.apache.avalon.framework.activity.Initializable;
   import org.apache.avalon.framework.configuration.Configurable;
  @@ -26,6 +24,7 @@
   import org.apache.avalon.framework.context.ContextException;
   import org.apache.avalon.framework.context.Contextualizable;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
  +import org.apache.avalon.phoenix.components.util.PropertyUtil;
   import org.apache.avalon.phoenix.interfaces.ConfigurationRepository;
   import org.apache.avalon.phoenix.interfaces.ConfigurationRepositoryMBean;
   import org.apache.excalibur.configuration.merged.ConfigurationMerger;
  @@ -102,7 +101,6 @@
           try
           {
               final Object opath = PropertyUtil.resolveProperty( path, m_context, false );
  -
               if( opath instanceof String )
               {
                   return FileUtil.normalize( (String)opath );
  @@ -115,7 +113,7 @@
                   throw new ConfigurationException( message );
               }
           }
  -        catch( PropertyException e )
  +        catch( Exception e )
           {
               final String message = REZ.getString( "config.error.missingproperty",
                                                     configuration.getLocation() );
  
  
  
  1.1                  avalon-phoenix/src/java/org/apache/avalon/phoenix/components/util/PropertyUtil.java
  
  Index: PropertyUtil.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.phoenix.components.util;
  
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.Resolvable;
  
  /**
   * This provides utility methods for properties.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version CVS $Revision: 1.1 $ $Date: 2003/03/01 08:16:17 $
   * @since 4.0
   */
  public final class PropertyUtil
  {
      private PropertyUtil()
      {
      }
  
      /**
       * Resolve a string property. This evaluates all property
       * substitutions based on specified context.
       *
       * @param property the property to resolve
       * @param context the context in which to resolve property
       * @param ignoreUndefined if false will throw an PropertyException if property is not
found
       * @return the reolved property
       * @exception java.lang.Exception if an error occurs
       */
      public static Object resolveProperty( final String property,
                                            final Context context,
                                            final boolean ignoreUndefined )
          throws Exception
      {
          int start = findBeginning( property, 0 );
          if( -1 == start )
          {
              return property;
          }
  
          int end = findEnding( property, start );
  
          final int length = property.length();
  
          if( 0 == start && end == ( length - 1 ) )
          {
              return resolveValue( property.substring( start + 2, end ),
                                   context,
                                   ignoreUndefined );
          }
  
          final StringBuffer sb = new StringBuffer( length * 2 );
          int lastPlace = 0;
  
          while( true )
          {
              final Object value =
                  resolveValue( property.substring( start + 2, end ),
                                context,
                                ignoreUndefined );
  
              sb.append( property.substring( lastPlace, start ) );
              sb.append( value );
  
              lastPlace = end + 1;
  
              start = findBeginning( property, lastPlace );
              if( -1 == start )
              {
                  break;
              }
  
              end = findEnding( property, start );
          }
  
          sb.append( property.substring( lastPlace, length ) );
  
          return sb.toString();
      }
  
      private static int findBeginning( final String property, final int currentPosition )
      {
          //TODO: Check if it is commented out
          return property.indexOf( "${", currentPosition );
      }
  
      private static int findEnding( final String property, final int currentPosition )
          throws Exception
      {
          //TODO: Check if it is commented out
          final int index = property.indexOf( '}', currentPosition );
          if( -1 == index )
          {
              throw new Exception( "Malformed property with mismatched }'s" );
          }
  
          return index;
      }
  
      /**
       * Retrieve a value from the specified context using the specified key.
       * If there is no such value and ignoreUndefined is not false then a
       * Exception is generated.
       *
       * @param key the key of value in context
       * @param context the Context
       * @param ignoreUndefined true if undefined variables are ignored
       * @return the object retrieved from context
       * @exception java.lang.Exception if an error occurs
       */
      private static Object resolveValue( final String key,
                                          final Context context,
                                          final boolean ignoreUndefined )
          throws Exception
      {
          Object value = null;
  
          try
          {
              value = context.get( key );
          }
          catch( final ContextException ce )
          {
              //ignore
          }
  
          try
          {
              while( null != value && value instanceof Resolvable )
              {
                  value = ( (Resolvable)value ).resolve( context );
              }
          }
          catch( final ContextException ce )
          {
              throw new Exception( "Unable to resolve value for key " + key );
          }
  
          if( null == value )
          {
              if( ignoreUndefined )
              {
                  return "";
              }
              else
              {
                  throw new Exception( "Unable to find " + key + " to expand during "
                                       + "property resolution." );
              }
          }
  
          return value;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message