📄 java14configstorage.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner;
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* 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.
*
* 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.
*
* ------------------------------
* Java14ConfigStorage.java
* ------------------------------
* (C)opyright 2003, by Thomas Morgner and Contributors.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: Java14ConfigStorage.java,v 1.4 2003/09/12 22:05:08 taqua Exp $
*
* Changes
* -------------------------
* 23-Jul-2003 : Initial version
*
*/
package org.jfree.report.ext.modules.java14config;
import java.util.Enumeration;
import java.util.Properties;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import org.jfree.report.modules.misc.configstore.base.ConfigFactory;
import org.jfree.report.modules.misc.configstore.base.ConfigStorage;
import org.jfree.report.modules.misc.configstore.base.ConfigStoreException;
/**
* A configuration storage provider which stores the entries using the
* JDK 1.4 configuration API.
*
* @author Thomas Morgner
*/
public class Java14ConfigStorage implements ConfigStorage
{
/** The preferences node used to store the configuration. */
private Preferences base;
/**
* Creates a new storage, which uses the given preferences
* node as base for all operations.
*
* @param base the base node.
*/
public Java14ConfigStorage(Preferences base)
{
this.base = base;
}
/**
* Stores the given properties on the defined path within the base node.
*
* @param configPath the path on where to store the properties.
* @param properties the properties which should be stored.
* @throws ConfigStoreException if an error occured.
* @see ConfigStorage#storeProperties(java.lang.String, java.util.Properties)
*/
public void storeProperties(String configPath, Properties properties)
throws ConfigStoreException
{
if (ConfigFactory.isValidPath(configPath) == false)
{
throw new IllegalArgumentException("The give path is not valid.");
}
try
{
Enumeration enum = properties.keys();
Preferences pref = base.node(configPath);
pref.clear();
while (enum.hasMoreElements())
{
String key = (String) enum.nextElement();
String value = properties.getProperty(key);
if (value != null)
{
pref.put(key,value);
}
}
pref.sync();
}
catch (BackingStoreException be)
{
throw new ConfigStoreException("Failed to store config" + configPath, be);
}
}
/**
* Loads the properties from the given path, specifying the given properties
* as default.
*
* @param configPath the configuration path from where to read the properties.
* @param defaults the property set that acts as fallback to provide default
* values.
* @return the loaded properties
* @throws ConfigStoreException if an error occured.
*/
public Properties loadProperties(String configPath, Properties defaults)
throws ConfigStoreException
{
if (ConfigFactory.isValidPath(configPath) == false)
{
throw new IllegalArgumentException("The give path is not valid.");
}
try
{
Properties props = new Properties();
Preferences pref = base.node(configPath);
String [] keys = pref.keys();
for (int i = 0; i < keys.length; i++)
{
String key = keys[i];
String value = pref.get(key, null);
if (value != null)
{
props.setProperty(key,value);
}
}
return props;
}
catch (BackingStoreException be)
{
throw new ConfigStoreException("Failed to load config" + configPath, be);
}
}
/**
* Tests, whether some configuration data exists for the given configuration.
*
* @param configPath the configuration path to the property storage.
* @return true, if there are properties under this path, false otherwise.
*/
public boolean existsProperties(String configPath)
{
if (ConfigFactory.isValidPath(configPath) == false)
{
throw new IllegalArgumentException("The give path is not valid.");
}
try
{
return base.nodeExists(configPath);
}
catch (BackingStoreException bse)
{
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -