You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@forrest.apache.org by ni...@apache.org on 2004/01/26 19:34:53 UTC

cvs commit: xml-forrest/src/java/org/apache/cocoon/components/modules/input AntPropertiesModule.java

nicolaken    2004/01/26 10:34:53

  Added:       src/java/org/apache/cocoon/components/modules/input
                        AntPropertiesModule.java
  Log:
   * Input module for accessing properties in a properties file
   * roughly compatible with Ant property files, where ${name}
   * is replaced with the value of the property 'name' if
   * declared beforehand.
  
  Revision  Changes    Path
  1.1                  xml-forrest/src/java/org/apache/cocoon/components/modules/input/AntPropertiesModule.java
  
  Index: AntPropertiesModule.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.components.modules.input;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.BufferedReader;
  import java.util.Map;
  import java.util.Properties;
  import java.util.Enumeration;
  
  import org.apache.avalon.framework.configuration.Configurable;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.Serviceable;
  import org.apache.avalon.framework.thread.ThreadSafe;
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.SourceResolver;
  
  import org.apache.commons.lang.StringUtils;
  
  /**
   * Input module for accessing properties in a properties file 
   * roughly compatible with Ant property files, where ${name}
   * is replaced with the value of the property 'name' if
   * declared beforehand.
   * 
   * <p>
   *  The properties file can only be configured statically and
   *  is resolved via the SourceResolver system.
   * </p>
   * 
   * @author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
   * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
  */
  public class AntPropertiesModule extends AbstractJXPathModule 
  implements InputModule, Serviceable, Configurable, ThreadSafe {
      
      private SourceResolver m_resolver;
      private Properties m_properties;
      
      public void service(ServiceManager manager) throws ServiceException {
          m_resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
      }
      
      /**
       * Configure the location of the properties file:
       * <p>
       *  <code>&lt;file src="resource://my.properties" /&gt;</code>
       * </p>
       */
      public void configure(Configuration configuration) throws ConfigurationException {
          super.configure(configuration);
          String file = configuration.getChild("file").getAttribute("src");
          load(file);
      }
      
      private void load(String file) throws ConfigurationException {
          Source source = null;
          BufferedReader in = null;
          try {
              source = m_resolver.resolveURI(file);
             
              in = new BufferedReader(new InputStreamReader(source.getInputStream()));
              
              m_properties = new Properties();
              String currentLine, name, value;
              int splitIndex;
              Enumeration names;
               
              while((currentLine = in.readLine()) != null) {
                  
                  if(!currentLine.startsWith("#")){ 
                      
                      splitIndex =  currentLine.indexOf('=');  
                      
                      if(splitIndex != -1 ) {
                              
                          name = currentLine.substring(0, splitIndex).trim() ;
                          value = currentLine.substring(splitIndex+1).trim() ;
                         
                          names = m_properties.propertyNames();
                          
                          while( names.hasMoreElements() ) {
                              String currentName = (String) names.nextElement();
                              String valueToSearchFor = "${"+currentName+"}";
                              String valueToReplaceWith = (String) m_properties.get(currentName);
                              value = StringUtils.replace(value, valueToSearchFor, valueToReplaceWith);
                          }
                              
                          m_properties.put(name,value);
                      }   
                  }    
              }
          }
          catch (IOException e) {
              throw new ConfigurationException("Cannot load properties file " + file);
          }
          finally {
              if (source != null) {
                  m_resolver.release(source);
              }
              if (in != null) {
                  try {
                      in.close();
                  }
                  catch (IOException e) {
                  }
              }
          }
      }
      
      protected Object getContextObject(Configuration modeConf, Map objectModel)
          throws ConfigurationException {
          
          return m_properties;
      }
  
  }