ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core PropertyLoader.java LoadProperties.java Resources.properties
Date Sat, 05 Jan 2002 03:20:03 GMT
donaldp     02/01/04 19:20:03

  Modified:    proposal/myrmidon/src/java/org/apache/antlib/core
                        Resources.properties
  Added:       proposal/myrmidon/src/java/org/apache/antlib/core
                        PropertyLoader.java LoadProperties.java
  Log:
  Add a task to load properties from a property file - optionally with a specified prefix
  
  Revision  Changes    Path
  1.4       +5 -0      jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties
  
  Index: Resources.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Resources.properties	29 Dec 2001 21:54:15 -0000	1.3
  +++ Resources.properties	5 Jan 2002 03:20:03 -0000	1.4
  @@ -4,6 +4,11 @@
   property.no-name.error=Name must be specified.
   property.no-value.error=Value must be specified.
   
  +loadprop.no-file.error=No file specified to load properties from.
  +loadprop.file.notice=Loading proeprties from {0}.
  +loadprop.missing-file.notice=Unable to find property file: {0}.
  +loadprop.bad-resolve.error=Unable to resolve and set property named "{0}" to value "{1}".
  +
   convert.bad-byte.error=Error converting object ({0}) to Byte.
   convert.bad-class.error=Error converting object ({0}) to Class.
   convert.bad-double.error=Error converting object ({0}) to Double.
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/PropertyLoader.java
  
  Index: PropertyLoader.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.antlib.core;
  
  import java.util.Properties;
  
  /**
   * This class is an UGLY HACK utility class to enable us to reuse
   * the property parsing and loading code from Properties object.
   */
  class PropertyLoader
      extends Properties
  {
      private LoadProperties m_loadProperties;
  
      public PropertyLoader( LoadProperties loadProperties )
      {
          m_loadProperties = loadProperties;
      }
  
      /**
       * Overidden put to add unresolved values.
       */
      public synchronized Object put( Object key, Object value )
      {
          m_loadProperties.addUnresolvedValue( key.toString(), value.toString() );
          return null;
      }
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/LoadProperties.java
  
  Index: LoadProperties.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.antlib.core;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.util.Properties;
  import org.apache.avalon.excalibur.i18n.ResourceManager;
  import org.apache.avalon.excalibur.i18n.Resources;
  import org.apache.avalon.excalibur.io.IOUtil;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.myrmidon.framework.AbstractContainerTask;
  
  /**
   * This task loads properties from a property file and places them in the context.
   *
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   */
  public class LoadProperties
      extends AbstractContainerTask
  {
      private static final Resources REZ =
          ResourceManager.getPackageResources( LoadProperties.class );
  
      private String m_prefix;
      private File m_file;
  
      /**
       * Specify the prefix to be placed before all properties (if any).
       */
      public void setPrefix( final String prefix )
      {
          m_prefix = prefix;
      }
  
      public void setFile( final File file )
      {
          m_file = file;
      }
  
      public void execute()
          throws TaskException
      {
          if( null == m_file )
          {
              final String message = REZ.getString( "loadprop.no-file.error" );
              throw new TaskException( message );
          }
  
          //Make sure prefix ends with a '.' if specified
          if( null == m_prefix )
          {
              m_prefix = "";
          }
          else if( !m_prefix.endsWith( "." ) )
          {
              m_prefix += ".";
          }
  
          loadFile( m_file );
      }
  
      /**
       * Utility method to load properties file.
       */
      private void loadFile( final File file )
          throws TaskException
      {
          if( getLogger().isDebugEnabled() )
          {
              final String message =
                  REZ.getString( "loadprop.file.notice", file.getAbsolutePath() );
              getLogger().debug( message );
          }
  
          if( !file.exists() )
          {
              final String message =
                  REZ.getString( "loadprop.missing-file.notice", file.getAbsolutePath() );
              getLogger().debug( message );
          }
          else
          {
              FileInputStream input = null;
  
              try
              {
                  input = new FileInputStream( file );
                  final Properties properties = new PropertyLoader( this );
                  properties.load( input );
              }
              catch( final IOException ioe )
              {
                  throw new TaskException( ioe.getMessage(), ioe );
              }
  
              IOUtil.shutdownStream( input );
          }
      }
  
      /**
       * Utility method that will resolve and add specified proeprty.
       * Used by external PropertyLoader class as a call back method.
       */
      protected final void addUnresolvedValue( final String name, final String value )
      {
          try
          {
              final Object objectValue = resolveValue( value.toString() );
              setProperty( m_prefix + name, objectValue );
          }
          catch( final TaskException te )
          {
              final String message = REZ.getString( "loadprop.bad-resolve.error", name, value
);
              getLogger().info( message, te );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message