All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.util.PropertiesUtil Maven / Gradle / Ivy

package org.springframework.util;

import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

public class PropertiesUtil {
  private static final Log logger = LogFactory.getLog(PropertiesUtil.class);

  private static final String ENC = "ENC";
  private static final Pattern PATTERN = Pattern.compile(ENC + "[(](.*?)[)]");

  private static Properties load(Resource location) throws IOException {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(location);
    propertiesFactoryBean.setSingleton(false);
    return propertiesFactoryBean.getObject();
  }

  // private static Properties loadFromYml(Resource location) throws IOException {
  // YamlFactoryBean yamlFactoryBean = new YamlFactoryBean();
  // yamlFactoryBean.setSingleton(false);
  // try (InputStream inputStream = location.getInputStream()) {    
  // return yamlFactoryBean.getObject().loadAs(inputStream, Properties.class);
  // }
  // catch (YAMLException e) {
  // throw new IOException(e);
  // }
  // }

  public static void store(Properties properties, File file, String comments) {
    Assert.isTrue(file != null && file.getParentFile() != null && (file.getParentFile().mkdirs() || file.getParentFile().isDirectory()), "Create Directory fail..." + file);
    // try (FileWriter fileWriter = new FileWriter(file)) {
    // if (properties instanceof StoreProperties) {
    // ((StoreProperties) properties).store(fileWriter, comments);
    // }
    // else {
    // new StoreProperties(properties).store(fileWriter, comments);
    // }
    // }
    // catch (IOException e) {
    // throw new IllegalStateException(e);
    // }

    // try (OutputStream outputStream = new FileOutputStream(file)) {
    // if (properties instanceof StoreProperties) {
    // ((StoreProperties) properties).store(outputStream, comments);
    // }
    // else {
    // new StoreProperties(properties).store(outputStream, comments);
    // }
    // }
    // catch (IOException e) {
    // throw new IllegalStateException(e);
    // }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy