Package dk.valtech.octools.file

Source Code of dk.valtech.octools.file.HierarchicalVfsPropertyFileLoader

/*

  This library is part of octools
  Copyright (c) 2000-2007 Valtech A/S (http://www.valtech.dk)

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

 
  Alkacon OpenCms and the OpenCms logo are registered trademarks of
  Alkacon Software GmbH in Germany, the USA and other countries
 
  For further information about Alkacon Software GmbH, please see the
  company website: http://www.alkacon.com

  For further information about OpenCms, please see the
  project website: http://www.opencms.org
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
package dk.valtech.octools.file;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.main.CmsException;

public class HierarchicalVfsPropertyFileLoader {
  private String propertyFileName;
  private String startFolder;
  private CmsObject cms;
 
  private List<String> searchPaths;
  private boolean allowOverwrite;
 
  private Properties properties;
 
  /**
   * This constructs a top-down Hierarchical PropertyFile loader, it looks for the propertyfile named
   * propertyFileName from the top folder, and all the way down to the folder of the requested resource.
   *
   * By default, the lastly specified properties win. This behaviour can be changed by setting
   * allowOverwrite to false.
   *
   * @param requestContext
   * @param propertyFileName
   */
  public HierarchicalVfsPropertyFileLoader(CmsObject cmsObject, String startFolder, String propertyFileName) {
    super();
    this.cms = cmsObject;
    this.startFolder = startFolder;
    this.propertyFileName = propertyFileName;
    searchPaths = new ArrayList<String>();
    allowOverwrite = true;
  }

  /**
   * This loads the properties from the found files into a Properties object, which is returned.
   * @return
   * @throws IOException
   * @throws CmsException
   */
  public Properties getProperties() throws CmsException, IOException {
    if (properties == null) {
      loadProperties();
    }
    return properties;
  }
 
 
  protected void loadProperties() throws CmsException, IOException {
    properties = new Properties();
   
    String uri = startFolder;
    int index = -1;
    while ((index = uri.indexOf('/', index+1)) > -1 && index < uri.length()) {
      addSearchPath(uri.substring(0, index));
    }
   
    Iterator<String> paths = searchPaths.iterator();
    String path, propertyFile, key;
    Properties props;
    CmsFile file;
    while (paths.hasNext()) {
      path = paths.next();
      propertyFile = cms.getRequestContext().removeSiteRoot(path.concat("/").concat(propertyFileName));
      if (cms.existsResource(propertyFile)) {
        file = cms.readFile(propertyFile);
        props = new Properties();
        props.load(new ByteArrayInputStream(file.getContents()));
       
        Iterator<Object> keys = props.keySet().iterator();
        while (keys.hasNext()) {
          key = (String) keys.next();
          if (!properties.containsKey(key) || allowOverwrite) {
            properties.put(key, props.get(key));
          }
        }
      }
    }
  }

  public boolean isAllowOverwrite() {
    return allowOverwrite;
  }


  /**
   * Sets the allowOvewrite. If set to false, the properties will not be overwritten by more specific
   * propertyfiles. E.g:
   *
   * if the currently requested resource is:
   *  /sites/mySite/en/contact/contact.html
   * and we are looking for contact.properties files located in:
   *  /sites/mySite and
   *  /sites/mySite/en/contact
   * 
   *  This class would first find the file from /sites/mySite and if the allowOverwite is set to
   *  false, the properties specified in both files will carry the values from the firstly loaded
   *  file. In this case, the one in /sites/mySite. Otherwise, the newest loaded file will win.
   *
   * @param allowOverwrite
   */
  public void setAllowOverwrite(boolean allowOverwrite) {
    this.allowOverwrite = allowOverwrite;
  }
 
  /**
   * Adds a searchPath. This searchPath will be before the currently requested path, to search for
   * properties. Theese will be overwritten by the ones in the current path, if the allowOverwrite
   * is set to true.
   *
   * @param path
   */
  public void addSearchPath(String path) {
    searchPaths.add(path);
  }
 
}
TOP

Related Classes of dk.valtech.octools.file.HierarchicalVfsPropertyFileLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.