[Schmitzm-commits] r1288 - trunk/src/schmitzm/lang
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Thu Nov 18 15:06:37 CET 2010
Author: alfonx
Date: 2010-11-18 15:06:36 +0100 (Thu, 18 Nov 2010)
New Revision: 1288
Modified:
trunk/src/schmitzm/lang/ResourceProviderOperator.java
Log:
Try to output the trasnaltions a \hex UTF8-in-ASCII instread of real UTF8
Modified: trunk/src/schmitzm/lang/ResourceProviderOperator.java
===================================================================
--- trunk/src/schmitzm/lang/ResourceProviderOperator.java 2010-11-18 13:28:11 UTC (rev 1287)
+++ trunk/src/schmitzm/lang/ResourceProviderOperator.java 2010-11-18 14:06:36 UTC (rev 1288)
@@ -36,6 +36,7 @@
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.OutputStream;
import java.io.Writer;
import java.util.HashSet;
import java.util.Locale;
@@ -53,579 +54,654 @@
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
+import schmitzm.io.FileOutputStream;
import schmitzm.io.IOUtil;
/**
- * This class is a main program, that performs several administrative
- * functions on the bundle files of {@link ResourceProvider}:
+ * This class is a main program, that performs several administrative functions
+ * on the bundle files of {@link ResourceProvider}:
* <ul>
- * <li>remove all keys (from all language files) which are
- * marked with "REMOVEME_" prefix in the root bundle.</li>
+ * <li>remove all keys (from all language files) which are marked with
+ * "REMOVEME_" prefix in the root bundle.</li>
* </ul>
+ *
* @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
- *
+ *
*/
public class ResourceProviderOperator {
- private static enum Operation {
- /** Operation: remove marked keys from bundle files. */
- REMOVE_KEYS
- }
-
- private static final Parser cliParser = new PosixParser();
- private static Options cliOptions = null;
- private static CommandLine cliOptionValues = null;
-
- private static final char RESOURCE_BUNDLES_SEP = ';';
-
- // constants for config file interpretation
- private static final String CFG_KEY_OPERATION = "operation";
- private static final String CFG_KEY_RESOURCE_BUNDLES = "resource.bundles";
- private static final String CFG_KEY_RESOURCE_BUNDLES_ROOT_FOLDER = "resource.bundles.src.root.folder";
- private static final String CFG_KEY_BUNDLE_KEYS_INDICATOR_PREFIX = "bundle.keys.indicator.prefix";
+ private static enum Operation {
+ /** Operation: remove marked keys from bundle files. */
+ REMOVE_KEYS
+ }
- /** Command line option key (-h) to show the command line help messages.
- * Alias for "--help". */
- protected static final String CLI_KEY_HELP = "h";
- /** Command line option key (--help) to show the command line help messages.
- * Alias for "-h". */
- protected static final String CLI_LONGKEY_HELP = "help";
-
- /** Command line option key (-o) to specify the operation.
- * Alias for "--operation". */
- protected static final String CLI_KEY_OPERATION = "o";
- /** Command line option key (--operation) to specify the operation.
- * Alias for "-o". */
- protected static final String CLI_LONGKEY_OPERATION = "operation";
- /** Default for the command line option -o (--operation): "REMOVE". */
- protected static final String CLI_DEFAULT_OPERATION = Operation.REMOVE_KEYS.toString();
+ private static final Parser cliParser = new PosixParser();
+ private static Options cliOptions = null;
+ private static CommandLine cliOptionValues = null;
- /** Command line option key (-b) to specify the bundles.
- * Alias for "--bundles". */
- protected static final String CLI_KEY_RESOURCE_BUNDLES = "b";
- /** Command line option key (--bundles) to specify the bundles.
- * Alias for "-b". */
- protected static final String CLI_LONGKEY_RESOURCE_BUNDLES = "bundles";
+ private static final char RESOURCE_BUNDLES_SEP = ';';
- /** Command line option key (-s) to specify the root folder the bundles
- * source files are stored relatively to.
- * Alias for "--src-root". */
- protected static final String CLI_KEY_RESOURCE_BUNDLES_ROOT_FOLDER = "s";
- /** Command line option key (-s) to specify the root folder the bundles
- * source files are stored relatively to. Alias for "-s".*/
- protected static final String CLI_LONGKEY_RESOURCE_BUNDLES_ROOT_FOLDER = "src-root";
- /** Default for the command line option -s (--src-root): "src/main/resources" (as it is the maven default) */
- protected static final String CLI_DEFAULT_RESOURCE_BUNDLES_ROOT_FOLDER = "src/main/resources";
+ // constants for config file interpretation
+ private static final String CFG_KEY_OPERATION = "operation";
+ private static final String CFG_KEY_RESOURCE_BUNDLES = "resource.bundles";
+ private static final String CFG_KEY_RESOURCE_BUNDLES_ROOT_FOLDER = "resource.bundles.src.root.folder";
+ private static final String CFG_KEY_BUNDLE_KEYS_INDICATOR_PREFIX = "bundle.keys.indicator.prefix";
- /** Command line option key (-i) to specify the indicator prefix the bundle
- * keys, which should be processed, are marked with.
- * Alias for "-indicator". */
- protected static final String CLI_KEY_BUNDLE_KEYS_INDICATOR_PREFIX = "i";
- /** Command line option key (--key-indicator) to specify the indicator prefix the bundle
- * keys, which should be processed, are marked with.
- * Alias for "-i". */
- protected static final String CLI_LONGKEY_BUNDLE_KEYS_INDICATOR_PREFIX = "key-indicator";
- /** Default for the command line option -i (--key-indicator): "REMOVEME_" (because REMOVE is the default operation) */
- protected static final String CLI_DEFAULT_BUNDLE_KEYS_INDICATOR_PREFIX = "REMOVEME_";
-
- /** Command line option key (-cff) to specify a property file the {@link ResourceProviderOperator}
- * configuration is read from. Additional command line parameters have higher
- * priority and overload the file configuration!
- * Alias for "--config-from-file". */
- protected static final String CLI_KEY_FILE_CONFIG_IN = "cff";
- /** Command line option key (--configfromfile) to specify a property file the {@link ResourceProviderOperator}
- * configuration is read from. Additional command line parameters have higher
- * priority and overload the file configuration!
- * Alias for "-cff". */
- protected static final String CLI_LONGKEY_FILE_CONFIG_IN = "config-from-file";
- /** Default for the command line option -cff (--config-from-file): "ResourceProviderOperator.cfg" */
- protected static final String CLI_DEFAULT_FILE_CONFIG_IN = "ResourceProviderOperator.cfg";
+ /**
+ * Command line option key (-h) to show the command line help messages.
+ * Alias for "--help".
+ */
+ protected static final String CLI_KEY_HELP = "h";
+ /**
+ * Command line option key (--help) to show the command line help messages.
+ * Alias for "-h".
+ */
+ protected static final String CLI_LONGKEY_HELP = "help";
- /** Command line option key (-ctf) to specify a file the {@link ResourceProviderOperator}
- * configuration (specified by config file or command line parameters) is
- * stored in. Alias for "--config-to-file". */
- protected static final String CLI_KEY_FILE_CONFIG_OUT = "ctf";
- /** Command line option key (--config-to-file) to specify a file the {@link ResourceProviderOperator}
- * configuration (specified by config file or command line parameters) is
- * stored in. Alias for "-ctf". */
- protected static final String CLI_LONGKEY_FILE_CONFIG_OUT = "config-to-file";
- /** Default for the command line option -ctf (--config-to-file): "ResourceProviderOperator.cfg" */
- protected static final String CLI_DEFAULT_FILE_CONFIG_OUT = "ResourceProviderOperator.cfg";
+ /**
+ * Command line option key (-o) to specify the operation. Alias for
+ * "--operation".
+ */
+ protected static final String CLI_KEY_OPERATION = "o";
+ /**
+ * Command line option key (--operation) to specify the operation. Alias for
+ * "-o".
+ */
+ protected static final String CLI_LONGKEY_OPERATION = "operation";
+ /** Default for the command line option -o (--operation): "REMOVE". */
+ protected static final String CLI_DEFAULT_OPERATION = Operation.REMOVE_KEYS
+ .toString();
-
-
-
- protected static final Logger LOGGER = LangUtil.createLogger(ResourceProviderOperator.class);
- /** Holds the resource bundles to process. */
- protected ResourceProvider[] resourceBundles = new ResourceProvider[0];
- /** Holds the prefix, which indicates the bundles keys to process. */
- protected String keyIndicatorPrefix = "REMOVEME_";
- /** Root folder where the bundle source files can be found relatively to.*/
- protected File resourceBundlesRootFolder = new File(CLI_DEFAULT_RESOURCE_BUNDLES_ROOT_FOLDER);
-
- /**
- * Configures a {@link ResourceProviderOperator} for the currently registered
- * {@link ResourceProvider}.
- * @param indicator the indicator prefix for the bundle keys to process;
- * if {@code null}, the default "REMOVEME_" is used
- * @param rootFolder the root folder the bundle source files are located (relatively to);
- * if {@code null}, the default "src/main/resources/" is used
- * @return
- */
- public static ResourceProviderOperator createForCurrentResourceProvider(String indicator, File rootFolder) {
- ResourceProvider[] rp = ResourceProvider.getRegisteredResourceProvider().toArray(new ResourceProvider[0]);
- ResourceProviderOperator rpo = new ResourceProviderOperator();
- rpo.setResourceBundles(rp);
- if ( indicator != null )
- rpo.setKeyIndicatorPrefix( indicator );
- if ( rootFolder != null )
- rpo.setResourceBundlesRootFolder( rootFolder );
+ /**
+ * Command line option key (-b) to specify the bundles. Alias for
+ * "--bundles".
+ */
+ protected static final String CLI_KEY_RESOURCE_BUNDLES = "b";
+ /**
+ * Command line option key (--bundles) to specify the bundles. Alias for
+ * "-b".
+ */
+ protected static final String CLI_LONGKEY_RESOURCE_BUNDLES = "bundles";
- return rpo;
- }
-
- /**
- * Sets the resource bundles to perform the operations on.
- */
- public void setResourceBundles(ResourceProvider[] resourceBundles) {
- if ( resourceBundles == null )
- resourceBundles = new ResourceProvider[0];
- this.resourceBundles = resourceBundles;
- }
-
- /**
- * Sets the resource bundles to perform the operations on.
- */
- public void setResourceBundles(String[] resourceBundleNames) {
- if ( resourceBundleNames == null ) {
- setResourceBundles((ResourceProvider[])null);
- return;
- }
- this.resourceBundles = new ResourceProvider[resourceBundleNames.length];
- for (int i=0; i<resourceBundleNames.length; i++) {
- // Create a new resource provider for the given bundle
- // Note: the root locale is not necessary for our purpose!
- this.resourceBundles[i] = ResourceProvider.newInstance(
- resourceBundleNames[i], null
- );
- }
- }
-
- /**
- * Returns the resource bundles to perform the operations on.
- */
- public ResourceProvider[] getResourceBundles() {
- return this.resourceBundles;
- }
+ /**
+ * Command line option key (-s) to specify the root folder the bundles
+ * source files are stored relatively to. Alias for "--src-root".
+ */
+ protected static final String CLI_KEY_RESOURCE_BUNDLES_ROOT_FOLDER = "s";
+ /**
+ * Command line option key (-s) to specify the root folder the bundles
+ * source files are stored relatively to. Alias for "-s".
+ */
+ protected static final String CLI_LONGKEY_RESOURCE_BUNDLES_ROOT_FOLDER = "src-root";
+ /**
+ * Default for the command line option -s (--src-root): "src/main/resources"
+ * (as it is the maven default)
+ */
+ protected static final String CLI_DEFAULT_RESOURCE_BUNDLES_ROOT_FOLDER = "src/main/resources";
- /**
- * Sets the prefix string the keys, which should be processed, are marked
- * with.
- */
- public void setKeyIndicatorPrefix(String keyIndicatorPrefix) {
- this.keyIndicatorPrefix = keyIndicatorPrefix;
- }
-
- /**
- * Returns the prefix string the keys, which should be processed, are marked
- * with.
- */
- public String getKeyIndicatorPrefix() {
- if ( keyIndicatorPrefix == null )
- return "";
- return this.keyIndicatorPrefix;
- }
+ /**
+ * Command line option key (-i) to specify the indicator prefix the bundle
+ * keys, which should be processed, are marked with. Alias for "-indicator".
+ */
+ protected static final String CLI_KEY_BUNDLE_KEYS_INDICATOR_PREFIX = "i";
+ /**
+ * Command line option key (--key-indicator) to specify the indicator prefix
+ * the bundle keys, which should be processed, are marked with. Alias for
+ * "-i".
+ */
+ protected static final String CLI_LONGKEY_BUNDLE_KEYS_INDICATOR_PREFIX = "key-indicator";
+ /**
+ * Default for the command line option -i (--key-indicator): "REMOVEME_"
+ * (because REMOVE is the default operation)
+ */
+ protected static final String CLI_DEFAULT_BUNDLE_KEYS_INDICATOR_PREFIX = "REMOVEME_";
- /**
- * Sets the root folder (on the local file system) the resource bundles
- * are relatively determined from.
- */
- public void setResourceBundlesRootFolder(File resourceBundlesRootFolder) {
- this.resourceBundlesRootFolder = resourceBundlesRootFolder;
- }
+ /**
+ * Command line option key (-cff) to specify a property file the
+ * {@link ResourceProviderOperator} configuration is read from. Additional
+ * command line parameters have higher priority and overload the file
+ * configuration! Alias for "--config-from-file".
+ */
+ protected static final String CLI_KEY_FILE_CONFIG_IN = "cff";
+ /**
+ * Command line option key (--configfromfile) to specify a property file the
+ * {@link ResourceProviderOperator} configuration is read from. Additional
+ * command line parameters have higher priority and overload the file
+ * configuration! Alias for "-cff".
+ */
+ protected static final String CLI_LONGKEY_FILE_CONFIG_IN = "config-from-file";
+ /**
+ * Default for the command line option -cff (--config-from-file):
+ * "ResourceProviderOperator.cfg"
+ */
+ protected static final String CLI_DEFAULT_FILE_CONFIG_IN = "ResourceProviderOperator.cfg";
- /**
- * Returns the root folder (on the local file system) the resource bundles
- * are relatively determined from.
- */
- public File getResourceBundlesRootFolder() {
- return resourceBundlesRootFolder;
- }
-
- /**
- * Removes all keys from the resource bundle files, which are marked with the
- * special prefix. It is not necessary, that the mark is set in all languages.
- * The keys are automatically removed from all (sub-)language bundles, either
- * the mark is set in the root locale or in a sub-language.
- * @see #setRemoveIndicatorPrefix(String)
- */
- public void processRemoveUnusedKeys() throws Exception {
- for (ResourceProvider rp : getResourceBundles())
- processRemoveUnusedKeys(rp);
- }
-
- /**
- * Removes all marked keys from all locales of the given {@link ResourceProvider}.
- * The bundle files in the source folder are rewritten. The removed key/value pairs are
- * archived in {@code .removedKeys} files.
- * @param rp resource provider to process
- */
- protected void processRemoveUnusedKeys(ResourceProvider rp) throws Exception {
- try {
- // First: Determine all keys which must be removed, either with
- // or without the prefix
- String removeIndicator = getKeyIndicatorPrefix();
- Set<String> keysToRemove = new HashSet<String>();
- for ( String key : rp.getKeys() )
- if ( key.startsWith( removeIndicator ) ) {
- // The keys which are marked explicitly with the
- // indicator must be removed as well as the
- // "originals" (in the sub-bundles)
- // --> so add both to ''eysToRemove
- keysToRemove.add( key );
- keysToRemove.add( eliminateKeyIndicatorPrefix(key) );
- }
-
- // Remove keys from the root bundle
- processRemoveUnusedKeys(rp,null,keysToRemove);
- // Remove keys from the language sub-bundles
- for (Locale locale : rp.getAvailableLocales(false))
- processRemoveUnusedKeys(rp,locale,keysToRemove);
- } catch (MissingResourceException err) {
- LOGGER.error(err.getMessage());
- }
- }
-
- /**
- * Removes a set of keys from only the specified locale and rewrites the
- * bundle file in the source folder. The removed key/value pairs are
- * archived in a {@code .removedKeys.properties} file.
- * @param rp resource provider to process
- * @param locale locale to process
- * @param set of keys to remove
- */
- protected void processRemoveUnusedKeys(ResourceProvider rp, Locale locale, Set<String> keysToRemove) throws Exception {
- if ( locale == null )
- locale = new Locale("");
+ /**
+ * Command line option key (-ctf) to specify a file the
+ * {@link ResourceProviderOperator} configuration (specified by config file
+ * or command line parameters) is stored in. Alias for "--config-to-file".
+ */
+ protected static final String CLI_KEY_FILE_CONFIG_OUT = "ctf";
+ /**
+ * Command line option key (--config-to-file) to specify a file the
+ * {@link ResourceProviderOperator} configuration (specified by config file
+ * or command line parameters) is stored in. Alias for "-ctf".
+ */
+ protected static final String CLI_LONGKEY_FILE_CONFIG_OUT = "config-to-file";
+ /**
+ * Default for the command line option -ctf (--config-to-file):
+ * "ResourceProviderOperator.cfg"
+ */
+ protected static final String CLI_DEFAULT_FILE_CONFIG_OUT = "ResourceProviderOperator.cfg";
- // To check for existing keys in the specified locale, create a new
- // ResourceProvider explicitly for this locale to avoid fallback!
- ResourceProvider checkBundle = rp;
- if ( !"".equals(locale.toString()) )
- checkBundle = new ResourceProvider(
- rp.getDefaultBundleName()+"_"+locale.toString(),
- locale,
- false,
- "???",
- false
- );
+ protected static final Logger LOGGER = LangUtil
+ .createLogger(ResourceProviderOperator.class);
+ /** Holds the resource bundles to process. */
+ protected ResourceProvider[] resourceBundles = new ResourceProvider[0];
+ /** Holds the prefix, which indicates the bundles keys to process. */
+ protected String keyIndicatorPrefix = "REMOVEME_";
+ /** Root folder where the bundle source files can be found relatively to. */
+ protected File resourceBundlesRootFolder = new File(
+ CLI_DEFAULT_RESOURCE_BUNDLES_ROOT_FOLDER);
- // split the key/value pairs of the bundle in the removed
- // pairs and the remaining pairs
- Properties removedKeys = new Properties();
- Properties remainingKeys = new Properties();
- checkBundle.setIgnoreMissingResource(false);
- for ( String key : checkBundle.getKeys() ) {
- String value = null;
- try {
- value = checkBundle.getString(key, locale);
- } catch (MissingResourceException err) {
- // Key not defined in specified explicitly for the locale
- // --> ignore key
- continue;
- }
-
- // add key/value to removing or remaining pairs
- if ( keysToRemove.contains(key) )
- // ignore the prefix of the removing key
- removedKeys.put( eliminateKeyIndicatorPrefix(key), value);
- else
- remainingKeys.put(key, value);
- }
+ /**
+ * Configures a {@link ResourceProviderOperator} for the currently
+ * registered {@link ResourceProvider}.
+ *
+ * @param indicator
+ * the indicator prefix for the bundle keys to process; if
+ * {@code null}, the default "REMOVEME_" is used
+ * @param rootFolder
+ * the root folder the bundle source files are located
+ * (relatively to); if {@code null}, the default
+ * "src/main/resources/" is used
+ * @return
+ */
+ public static ResourceProviderOperator createForCurrentResourceProvider(
+ String indicator, File rootFolder) {
+ ResourceProvider[] rp = ResourceProvider
+ .getRegisteredResourceProvider().toArray(
+ new ResourceProvider[0]);
+ ResourceProviderOperator rpo = new ResourceProviderOperator();
+ rpo.setResourceBundles(rp);
+ if (indicator != null)
+ rpo.setKeyIndicatorPrefix(indicator);
+ if (rootFolder != null)
+ rpo.setResourceBundlesRootFolder(rootFolder);
- // Determine original bundle source file
- File bundleFile = ResourceProvider.getPropertyFile(rp, locale, getResourceBundlesRootFolder());
- // only rewrite files, when keys are removed
- if ( removedKeys.size() > 0 ) {
- // store remaining pairs
- String comments = getInitialCommentLines(bundleFile, "#", true);
- Writer remainingKeysOutput = IOUtil.createUTF8FileWriter(bundleFile,false);
-// Writer remainingKeysOutput = IOUtil.createUTF8FileWriter( IOUtil.changeFileExt(bundleFile, "remainingKeys"), false );
- remainingKeysOutput.write(comments); // write comments as their were
- if ( !"".equals(comments) )
- remainingKeysOutput.write("\n");
- remainingKeys.store(
- remainingKeysOutput,
- "Revised by "+LangUtil.getSimpleClassName(this)+" (Unused keys removed)");
- remainingKeysOutput.flush();
- remainingKeysOutput.close();
-
- // store removed pairs
- File removedKeysFile = IOUtil.changeFileExt(bundleFile, "removedKeys.properties");
- Writer removedKeysOutput = IOUtil.createUTF8FileWriter(removedKeysFile,true);
- removedKeys.store(
- removedKeysOutput,
- "======================================================\n" +
- "Removed Keys: "+rp.getDefaultBundleName()
- );
- removedKeysOutput.flush();
- removedKeysOutput.close();
- }
- LOGGER.info("Removed "+removedKeys.size()+" keys from bundle '"+rp.getDefaultBundleName()+"' ("+locale+") --> "+bundleFile.getAbsolutePath());
- }
+ return rpo;
+ }
+ /**
+ * Sets the resource bundles to perform the operations on.
+ */
+ public void setResourceBundles(ResourceProvider[] resourceBundles) {
+ if (resourceBundles == null)
+ resourceBundles = new ResourceProvider[0];
+ this.resourceBundles = resourceBundles;
+ }
- /**
- * Returns the key without the indicator prefix, if the key
- * starts with it. Otherwise the given key is returned unchanged.
- */
- private String eliminateKeyIndicatorPrefix(String key) {
- String removeIndicator = getKeyIndicatorPrefix();
- if ( key.startsWith( removeIndicator ) )
- return key.substring( removeIndicator.length() );
- return key;
- }
-
- private String getInitialCommentLines(File file, String commentPrefix, boolean includeEmptyLines) throws IOException {
- StringBuffer commentLines = new StringBuffer();
- BufferedReader reader = new BufferedReader( new FileReader(file) );
- String line = null;
- while ( reader.ready() && (line = reader.readLine()) != null ) {
- if ( line.startsWith(commentPrefix) ||
- includeEmptyLines && line.trim().equals("") )
- commentLines.append( line ).append("\n");
- else
- break;
- }
- reader.close();
- return commentLines.toString();
- }
-
- /**
- * Initializes the command line options in {@link #cliOptions}.
- */
- protected static void initCliOptions() {
- cliOptions = new Options();
- cliOptions.addOption( new Option(
- CLI_KEY_HELP,
- CLI_LONGKEY_HELP,
- false,
- LangUtil.RESOURCE.getString("ResourceProviderOperator.cli.info.help")
- ));
- Option operationOption = new Option(
- CLI_KEY_OPERATION,
- CLI_LONGKEY_OPERATION,
- true,
- LangUtil.RESOURCE.getString(
- "ResourceProviderOperator.cli.info.operation",
- LangUtil.stringConcatWithSep(" | ", Operation.values()),
- CLI_DEFAULT_OPERATION
- )
- );
- operationOption.setValueSeparator(RESOURCE_BUNDLES_SEP);
- cliOptions.addOption( operationOption );
- cliOptions.addOption( new Option(
- CLI_KEY_RESOURCE_BUNDLES,
- CLI_LONGKEY_RESOURCE_BUNDLES,
- true,
- LangUtil.RESOURCE.getString("ResourceProviderOperator.cli.info.bundles")
- ));
- cliOptions.addOption( new Option(
- CLI_KEY_RESOURCE_BUNDLES_ROOT_FOLDER,
- CLI_LONGKEY_RESOURCE_BUNDLES_ROOT_FOLDER,
- true,
- LangUtil.RESOURCE.getString(
- "ResourceProviderOperator.cli.info.rootfolder",
- CLI_DEFAULT_RESOURCE_BUNDLES_ROOT_FOLDER
- )
- ));
- cliOptions.addOption( new Option(
- CLI_KEY_BUNDLE_KEYS_INDICATOR_PREFIX,
- CLI_LONGKEY_BUNDLE_KEYS_INDICATOR_PREFIX,
- true,
- LangUtil.RESOURCE.getString(
- "ResourceProviderOperator.cli.info.indicator",
- CLI_DEFAULT_BUNDLE_KEYS_INDICATOR_PREFIX
- )
- ));
- cliOptions.addOption( new Option(
- CLI_KEY_FILE_CONFIG_IN,
- CLI_LONGKEY_FILE_CONFIG_IN,
- true,
- LangUtil.RESOURCE.getString(
- "ResourceProviderOperator.cli.info.filein",
- CLI_DEFAULT_FILE_CONFIG_IN
- )
- ));
- cliOptions.addOption( new Option(
- CLI_KEY_FILE_CONFIG_OUT,
- CLI_LONGKEY_FILE_CONFIG_OUT,
- true,
- LangUtil.RESOURCE.getString(
- "ResourceProviderOperator.cli.info.fileout",
- CLI_DEFAULT_FILE_CONFIG_OUT
- )
- ));
- }
-
- /**
- * Prints the command line help to console.
- */
- public static void printCliHelp() {
- HelpFormatter formatter = new HelpFormatter();
- formatter.printHelp(ResourceProviderOperator.class.getName(), cliOptions);
- }
-
- /**
- * Starts an operation on resource bundle files.
- * @param args command line arguments
- */
- public static void main(String[] args) throws Exception {
- LangUtil.initializeDefaultLogger(Level.DEBUG,null);
- initCliOptions();
- try {
- cliOptionValues = cliParser.parse(cliOptions, args);
- } catch (UnrecognizedOptionException err) {
- LOGGER.error(err.getMessage()+"\n");
- printCliHelp();
- return;
- }
-
-
- if ( cliOptionValues.hasOption(CLI_KEY_HELP) ) {
- printCliHelp();
- return;
- };
-
- // Configuration from config file
- ResourceProviderOperator rpo = new ResourceProviderOperator();
- if ( cliOptionValues.hasOption(CLI_KEY_FILE_CONFIG_IN) )
- initResourceOperator(
- rpo,
- cliOptionValues.getOptionValue(CLI_KEY_FILE_CONFIG_IN,CLI_DEFAULT_FILE_CONFIG_IN)
- );
-
- // Configuration from command line parameters (overwrite config file
- // configuration)
- initResourceOperator(rpo, cliOptionValues, !cliOptionValues.hasOption(CLI_KEY_FILE_CONFIG_IN));
-
- // Perform the operation
- try {
- String operationStr = cliOptionValues.getOptionValue(CLI_KEY_OPERATION,CLI_DEFAULT_OPERATION);
- switch ( Operation.valueOf(operationStr.toUpperCase()) ) {
- case REMOVE_KEYS: rpo.processRemoveUnusedKeys(); break;
- }
- } catch (IllegalArgumentException err) {
- LOGGER.error("Unsupported operation specified: "+cliOptionValues.getOptionValue(CLI_KEY_OPERATION,CLI_DEFAULT_OPERATION));
- return;
- }
-
- // When a special command line parameter "-ctf <file>" is set
- // then store the configuration in the specified file
- if ( cliOptionValues.hasOption(CLI_KEY_FILE_CONFIG_OUT) )
- storeResourceOperator(
- rpo,
- new File(cliOptionValues.getOptionValue(CLI_KEY_FILE_CONFIG_OUT,CLI_DEFAULT_FILE_CONFIG_OUT))
- );
- }
-
- /**
- * Configures a {@link ResourceProviderOperator} from configuration property
- * file.
- * @param rpo {@link ResourceProviderOperator} to configure (if {@code null} a
- * new {@link ResourceProviderOperator} is created and returned)
- * @param configFile property file with the configuration
- */
- public static ResourceProviderOperator initResourceOperator(ResourceProviderOperator rpo, String configFile) {
- if ( rpo == null )
- rpo = new ResourceProviderOperator();
-
- FileInputStream input = null;
- try {
- input = new FileInputStream( new File(configFile) );
- Properties parameters = new Properties();
- parameters.load(input);
-
- // Configure the ResourceProviderOperator
- String removeIndicatorPrefix = parameters.getProperty(CFG_KEY_BUNDLE_KEYS_INDICATOR_PREFIX);
- if ( removeIndicatorPrefix != null )
- rpo.setKeyIndicatorPrefix(removeIndicatorPrefix);
- String resourceBundleRootFolder = parameters.getProperty(CFG_KEY_RESOURCE_BUNDLES_ROOT_FOLDER);
- if ( resourceBundleRootFolder != null )
- rpo.setResourceBundlesRootFolder(new File(resourceBundleRootFolder));
- String resourceBundlesList = parameters.getProperty(CFG_KEY_RESOURCE_BUNDLES);
- if ( resourceBundlesList != null ) {
- String[] resourceBundles = resourceBundlesList.split(""+RESOURCE_BUNDLES_SEP);
- rpo.setResourceBundles(resourceBundles);
- }
- } catch (IOException err) {
- LOGGER.error(err.getMessage());
- }
-
- return rpo;
- }
+ /**
+ * Sets the resource bundles to perform the operations on.
+ */
+ public void setResourceBundles(String[] resourceBundleNames) {
+ if (resourceBundleNames == null) {
+ setResourceBundles((ResourceProvider[]) null);
+ return;
+ }
+ this.resourceBundles = new ResourceProvider[resourceBundleNames.length];
+ for (int i = 0; i < resourceBundleNames.length; i++) {
+ // Create a new resource provider for the given bundle
+ // Note: the root locale is not necessary for our purpose!
+ this.resourceBundles[i] = ResourceProvider.newInstance(
+ resourceBundleNames[i], null);
+ }
+ }
- /**
- * Configures a {@link ResourceProviderOperator} from command line parameters.
- * @param rpo {@link ResourceProviderOperator} to configure (if {@code null} a
- * new {@link ResourceProviderOperator} is created and returned)
- * @param cli command line parameters
- * @param initCompletely If {@code true} the {@link ResourceProviderOperator} is
- * initialized (with the default) even if an CLI option is not
- * set. If {@code false} the properties remain on their
- * previous state when the CLI option is not set.
- */
- public static ResourceProviderOperator initResourceOperator(ResourceProviderOperator rpo, CommandLine cli, boolean initCompletely) {
- if ( rpo == null )
- rpo = new ResourceProviderOperator();
+ /**
+ * Returns the resource bundles to perform the operations on.
+ */
+ public ResourceProvider[] getResourceBundles() {
+ return this.resourceBundles;
+ }
-
- // Configure the ResourceProviderOperator
- if ( initCompletely || cli.hasOption( CLI_KEY_BUNDLE_KEYS_INDICATOR_PREFIX ) )
- rpo.setKeyIndicatorPrefix( cli.getOptionValue(
- CLI_KEY_BUNDLE_KEYS_INDICATOR_PREFIX, CLI_DEFAULT_BUNDLE_KEYS_INDICATOR_PREFIX )
- );
+ /**
+ * Sets the prefix string the keys, which should be processed, are marked
+ * with.
+ */
+ public void setKeyIndicatorPrefix(String keyIndicatorPrefix) {
+ this.keyIndicatorPrefix = keyIndicatorPrefix;
+ }
- if ( initCompletely || cli.hasOption( CLI_KEY_RESOURCE_BUNDLES_ROOT_FOLDER ) )
- rpo.setResourceBundlesRootFolder( new File( cli.getOptionValue(
- CLI_KEY_RESOURCE_BUNDLES_ROOT_FOLDER, CLI_DEFAULT_RESOURCE_BUNDLES_ROOT_FOLDER )
- ));
+ /**
+ * Returns the prefix string the keys, which should be processed, are marked
+ * with.
+ */
+ public String getKeyIndicatorPrefix() {
+ if (keyIndicatorPrefix == null)
+ return "";
+ return this.keyIndicatorPrefix;
+ }
- if ( initCompletely || cli.hasOption( CLI_KEY_RESOURCE_BUNDLES ) ) {
- String resourceBundlesList = cli.getOptionValue(CLI_KEY_RESOURCE_BUNDLES);
- String[] resourceBundles = resourceBundlesList.split(""+RESOURCE_BUNDLES_SEP);
- rpo.setResourceBundles( resourceBundles );
- }
-
- return rpo;
- }
+ /**
+ * Sets the root folder (on the local file system) the resource bundles are
+ * relatively determined from.
+ */
+ public void setResourceBundlesRootFolder(File resourceBundlesRootFolder) {
+ this.resourceBundlesRootFolder = resourceBundlesRootFolder;
+ }
- /**
- * Stores the {@link ResourceProviderOperator} configuration in
- * a property file.
- * @param rpo {@link ResourceProviderOperator} to store the configuration for (
- * {@code null} not allowed)
- * @param cli command line parameters
- */
- public static void storeResourceOperator(ResourceProviderOperator rpo, File outFile) throws IOException {
- FileWriter output = null;
- try {
- Properties config = new Properties();
- output = new FileWriter(outFile);
- if ( rpo.getKeyIndicatorPrefix() != null )
- config.put(CFG_KEY_BUNDLE_KEYS_INDICATOR_PREFIX, rpo.getKeyIndicatorPrefix());
- if ( rpo.getResourceBundlesRootFolder() != null )
- config.put(CFG_KEY_RESOURCE_BUNDLES_ROOT_FOLDER, rpo.getResourceBundlesRootFolder().getPath());
- if ( rpo.getResourceBundles().length > 0 ) {
- config.put(CFG_KEY_RESOURCE_BUNDLES, LangUtil.stringConcatWithSep(""+RESOURCE_BUNDLES_SEP, rpo.getResourceBundles()));
- ResourceProvider sample = rpo.getResourceBundles()[0];
- }
-
- config.store(output, "Configuration for ResourceProviderOperator");
- output.flush();
- } finally {
- if ( output != null ) output.close();
- }
- }
+ /**
+ * Returns the root folder (on the local file system) the resource bundles
+ * are relatively determined from.
+ */
+ public File getResourceBundlesRootFolder() {
+ return resourceBundlesRootFolder;
+ }
+ /**
+ * Removes all keys from the resource bundle files, which are marked with
+ * the special prefix. It is not necessary, that the mark is set in all
+ * languages. The keys are automatically removed from all (sub-)language
+ * bundles, either the mark is set in the root locale or in a sub-language.
+ *
+ * @see #setRemoveIndicatorPrefix(String)
+ */
+ public void processRemoveUnusedKeys() throws Exception {
+ for (ResourceProvider rp : getResourceBundles())
+ processRemoveUnusedKeys(rp);
+ }
+
+ /**
+ * Removes all marked keys from all locales of the given
+ * {@link ResourceProvider}. The bundle files in the source folder are
+ * rewritten. The removed key/value pairs are archived in
+ * {@code .removedKeys} files.
+ *
+ * @param rp
+ * resource provider to process
+ */
+ protected void processRemoveUnusedKeys(ResourceProvider rp)
+ throws Exception {
+ try {
+ // First: Determine all keys which must be removed, either with
+ // or without the prefix
+ String removeIndicator = getKeyIndicatorPrefix();
+ Set<String> keysToRemove = new HashSet<String>();
+ for (String key : rp.getKeys())
+ if (key.startsWith(removeIndicator)) {
+ // The keys which are marked explicitly with the
+ // indicator must be removed as well as the
+ // "originals" (in the sub-bundles)
+ // --> so add both to ''eysToRemove
+ keysToRemove.add(key);
+ keysToRemove.add(eliminateKeyIndicatorPrefix(key));
+ }
+
+ // Remove keys from the root bundle
+ processRemoveUnusedKeys(rp, null, keysToRemove);
+ // Remove keys from the language sub-bundles
+ for (Locale locale : rp.getAvailableLocales(false))
+ processRemoveUnusedKeys(rp, locale, keysToRemove);
+ } catch (MissingResourceException err) {
+ LOGGER.error(err.getMessage());
+ }
+ }
+
+ /**
+ * Removes a set of keys from only the specified locale and rewrites the
+ * bundle file in the source folder. The removed key/value pairs are
+ * archived in a {@code .removedKeys.properties} file.
+ *
+ * @param rp
+ * resource provider to process
+ * @param locale
+ * locale to process
+ * @param set
+ * of keys to remove
+ */
+ protected void processRemoveUnusedKeys(ResourceProvider rp, Locale locale,
+ Set<String> keysToRemove) throws Exception {
+ if (locale == null)
+ locale = new Locale("");
+
+ // To check for existing keys in the specified locale, create a new
+ // ResourceProvider explicitly for this locale to avoid fallback!
+ ResourceProvider checkBundle = rp;
+ if (!"".equals(locale.toString()))
+ checkBundle = new ResourceProvider(rp.getDefaultBundleName() + "_"
+ + locale.toString(), locale, false, "???", false);
+
+ // split the key/value pairs of the bundle in the removed
+ // pairs and the remaining pairs
+ Properties removedKeys = new Properties();
+ Properties remainingKeys = new Properties();
+ checkBundle.setIgnoreMissingResource(false);
+ for (String key : checkBundle.getKeys()) {
+ String value = null;
+ try {
+ value = checkBundle.getString(key, locale);
+ } catch (MissingResourceException err) {
+ // Key not defined in specified explicitly for the locale
+ // --> ignore key
+ continue;
+ }
+
+ // add key/value to removing or remaining pairs
+ if (keysToRemove.contains(key))
+ // ignore the prefix of the removing key
+ removedKeys.put(eliminateKeyIndicatorPrefix(key), value);
+ else
+ remainingKeys.put(key, value);
+ }
+
+ // Determine original bundle source file
+ File bundleFile = ResourceProvider.getPropertyFile(rp, locale,
+ getResourceBundlesRootFolder());
+ // only rewrite files, when keys are removed
+ if (removedKeys.size() > 0) {
+ // store remaining pairs
+ String comments = getInitialCommentLines(bundleFile, "#", true);
+
+ // Writer remainingKeysOutput =
+ // IOUtil.createUTF8FileWriter(bundleFile,false);
+ OutputStream remainingKeysOutput = new FileOutputStream(bundleFile);
+ // Writer remainingKeysOutput = IOUtil.createUTF8FileWriter(
+ // IOUtil.changeFileExt(bundleFile, "remainingKeys"), false );
+
+ remainingKeysOutput.write(comments.getBytes()); // write comments as
+ // their were
+ if (!"".equals(comments))
+ remainingKeysOutput.write("\n".getBytes());
+ remainingKeys.store(remainingKeysOutput,
+ "Revised by " + LangUtil.getSimpleClassName(this)
+ + " (Unused keys removed)");
+ remainingKeysOutput.flush();
+ remainingKeysOutput.close();
+
+ // store removed pairs
+ File removedKeysFile = IOUtil.changeFileExt(bundleFile,
+ "removedKeys.properties");
+ Writer removedKeysOutput = IOUtil.createUTF8FileWriter(
+ removedKeysFile, true);
+ removedKeys.store(removedKeysOutput,
+ "======================================================\n"
+ + "Removed Keys: " + rp.getDefaultBundleName());
+ removedKeysOutput.flush();
+ removedKeysOutput.close();
+ }
+ LOGGER.info("Removed " + removedKeys.size() + " keys from bundle '"
+ + rp.getDefaultBundleName() + "' (" + locale + ") --> "
+ + bundleFile.getAbsolutePath());
+ }
+
+ /**
+ * Returns the key without the indicator prefix, if the key starts with it.
+ * Otherwise the given key is returned unchanged.
+ */
+ private String eliminateKeyIndicatorPrefix(String key) {
+ String removeIndicator = getKeyIndicatorPrefix();
+ if (key.startsWith(removeIndicator))
+ return key.substring(removeIndicator.length());
+ return key;
+ }
+
+ private String getInitialCommentLines(File file, String commentPrefix,
+ boolean includeEmptyLines) throws IOException {
+ StringBuffer commentLines = new StringBuffer();
+ BufferedReader reader = new BufferedReader(new FileReader(file));
+ String line = null;
+ while (reader.ready() && (line = reader.readLine()) != null) {
+ if (line.startsWith(commentPrefix) || includeEmptyLines
+ && line.trim().equals(""))
+ commentLines.append(line).append("\n");
+ else
+ break;
+ }
+ reader.close();
+ return commentLines.toString();
+ }
+
+ /**
+ * Initializes the command line options in {@link #cliOptions}.
+ */
+ protected static void initCliOptions() {
+ cliOptions = new Options();
+ cliOptions.addOption(new Option(CLI_KEY_HELP, CLI_LONGKEY_HELP, false,
+ LangUtil.RESOURCE
+ .getString("ResourceProviderOperator.cli.info.help")));
+ Option operationOption = new Option(
+ CLI_KEY_OPERATION,
+ CLI_LONGKEY_OPERATION,
+ true,
+ LangUtil.RESOURCE.getString(
+ "ResourceProviderOperator.cli.info.operation",
+ LangUtil.stringConcatWithSep(" | ", Operation.values()),
+ CLI_DEFAULT_OPERATION));
+ operationOption.setValueSeparator(RESOURCE_BUNDLES_SEP);
+ cliOptions.addOption(operationOption);
+ cliOptions
+ .addOption(new Option(
+ CLI_KEY_RESOURCE_BUNDLES,
+ CLI_LONGKEY_RESOURCE_BUNDLES,
+ true,
+ LangUtil.RESOURCE
+ .getString("ResourceProviderOperator.cli.info.bundles")));
+ cliOptions.addOption(new Option(CLI_KEY_RESOURCE_BUNDLES_ROOT_FOLDER,
+ CLI_LONGKEY_RESOURCE_BUNDLES_ROOT_FOLDER, true,
+ LangUtil.RESOURCE.getString(
+ "ResourceProviderOperator.cli.info.rootfolder",
+ CLI_DEFAULT_RESOURCE_BUNDLES_ROOT_FOLDER)));
+ cliOptions.addOption(new Option(CLI_KEY_BUNDLE_KEYS_INDICATOR_PREFIX,
+ CLI_LONGKEY_BUNDLE_KEYS_INDICATOR_PREFIX, true,
+ LangUtil.RESOURCE.getString(
+ "ResourceProviderOperator.cli.info.indicator",
+ CLI_DEFAULT_BUNDLE_KEYS_INDICATOR_PREFIX)));
+ cliOptions.addOption(new Option(CLI_KEY_FILE_CONFIG_IN,
+ CLI_LONGKEY_FILE_CONFIG_IN, true, LangUtil.RESOURCE.getString(
+ "ResourceProviderOperator.cli.info.filein",
+ CLI_DEFAULT_FILE_CONFIG_IN)));
+ cliOptions.addOption(new Option(CLI_KEY_FILE_CONFIG_OUT,
+ CLI_LONGKEY_FILE_CONFIG_OUT, true, LangUtil.RESOURCE.getString(
+ "ResourceProviderOperator.cli.info.fileout",
+ CLI_DEFAULT_FILE_CONFIG_OUT)));
+ }
+
+ /**
+ * Prints the command line help to console.
+ */
+ public static void printCliHelp() {
+ HelpFormatter formatter = new HelpFormatter();
+ formatter.printHelp(ResourceProviderOperator.class.getName(),
+ cliOptions);
+ }
+
+ /**
+ * Starts an operation on resource bundle files.
+ *
+ * @param args
+ * command line arguments
+ */
+ public static void main(String[] args) throws Exception {
+ LangUtil.initializeDefaultLogger(Level.DEBUG, null);
+ initCliOptions();
+ try {
+ cliOptionValues = cliParser.parse(cliOptions, args);
+ } catch (UnrecognizedOptionException err) {
+ LOGGER.error(err.getMessage() + "\n");
+ printCliHelp();
+ return;
+ }
+
+ if (cliOptionValues.hasOption(CLI_KEY_HELP)) {
+ printCliHelp();
+ return;
+ }
+ ;
+
+ // Configuration from config file
+ ResourceProviderOperator rpo = new ResourceProviderOperator();
+ if (cliOptionValues.hasOption(CLI_KEY_FILE_CONFIG_IN))
+ initResourceOperator(rpo, cliOptionValues.getOptionValue(
+ CLI_KEY_FILE_CONFIG_IN, CLI_DEFAULT_FILE_CONFIG_IN));
+
+ // Configuration from command line parameters (overwrite config file
+ // configuration)
+ initResourceOperator(rpo, cliOptionValues,
+ !cliOptionValues.hasOption(CLI_KEY_FILE_CONFIG_IN));
+
+ // Perform the operation
+ try {
+ String operationStr = cliOptionValues.getOptionValue(
+ CLI_KEY_OPERATION, CLI_DEFAULT_OPERATION);
+ switch (Operation.valueOf(operationStr.toUpperCase())) {
+ case REMOVE_KEYS:
+ rpo.processRemoveUnusedKeys();
+ break;
+ }
+ } catch (IllegalArgumentException err) {
+ LOGGER.error("Unsupported operation specified: "
+ + cliOptionValues.getOptionValue(CLI_KEY_OPERATION,
+ CLI_DEFAULT_OPERATION));
+ return;
+ }
+
+ // When a special command line parameter "-ctf <file>" is set
+ // then store the configuration in the specified file
+ if (cliOptionValues.hasOption(CLI_KEY_FILE_CONFIG_OUT))
+ storeResourceOperator(
+ rpo,
+ new File(cliOptionValues.getOptionValue(
+ CLI_KEY_FILE_CONFIG_OUT,
+ CLI_DEFAULT_FILE_CONFIG_OUT)));
+ }
+
+ /**
+ * Configures a {@link ResourceProviderOperator} from configuration property
+ * file.
+ *
+ * @param rpo
+ * {@link ResourceProviderOperator} to configure (if {@code null}
+ * a new {@link ResourceProviderOperator} is created and
+ * returned)
+ * @param configFile
+ * property file with the configuration
+ */
+ public static ResourceProviderOperator initResourceOperator(
+ ResourceProviderOperator rpo, String configFile) {
+ if (rpo == null)
+ rpo = new ResourceProviderOperator();
+
+ FileInputStream input = null;
+ try {
+ input = new FileInputStream(new File(configFile));
+ Properties parameters = new Properties();
+ parameters.load(input);
+
+ // Configure the ResourceProviderOperator
+ String removeIndicatorPrefix = parameters
+ .getProperty(CFG_KEY_BUNDLE_KEYS_INDICATOR_PREFIX);
+ if (removeIndicatorPrefix != null)
+ rpo.setKeyIndicatorPrefix(removeIndicatorPrefix);
+ String resourceBundleRootFolder = parameters
+ .getProperty(CFG_KEY_RESOURCE_BUNDLES_ROOT_FOLDER);
+ if (resourceBundleRootFolder != null)
+ rpo.setResourceBundlesRootFolder(new File(
+ resourceBundleRootFolder));
+ String resourceBundlesList = parameters
+ .getProperty(CFG_KEY_RESOURCE_BUNDLES);
+ if (resourceBundlesList != null) {
+ String[] resourceBundles = resourceBundlesList.split(""
+ + RESOURCE_BUNDLES_SEP);
+ rpo.setResourceBundles(resourceBundles);
+ }
+ } catch (IOException err) {
+ LOGGER.error(err.getMessage());
+ }
+
+ return rpo;
+ }
+
+ /**
+ * Configures a {@link ResourceProviderOperator} from command line
+ * parameters.
+ *
+ * @param rpo
+ * {@link ResourceProviderOperator} to configure (if {@code null}
+ * a new {@link ResourceProviderOperator} is created and
+ * returned)
+ * @param cli
+ * command line parameters
+ * @param initCompletely
+ * If {@code true} the {@link ResourceProviderOperator} is
+ * initialized (with the default) even if an CLI option is not
+ * set. If {@code false} the properties remain on their previous
+ * state when the CLI option is not set.
+ */
+ public static ResourceProviderOperator initResourceOperator(
+ ResourceProviderOperator rpo, CommandLine cli,
+ boolean initCompletely) {
+ if (rpo == null)
+ rpo = new ResourceProviderOperator();
+
+ // Configure the ResourceProviderOperator
+ if (initCompletely
+ || cli.hasOption(CLI_KEY_BUNDLE_KEYS_INDICATOR_PREFIX))
+ rpo.setKeyIndicatorPrefix(cli.getOptionValue(
+ CLI_KEY_BUNDLE_KEYS_INDICATOR_PREFIX,
+ CLI_DEFAULT_BUNDLE_KEYS_INDICATOR_PREFIX));
+
+ if (initCompletely
+ || cli.hasOption(CLI_KEY_RESOURCE_BUNDLES_ROOT_FOLDER))
+ rpo.setResourceBundlesRootFolder(new File(cli.getOptionValue(
+ CLI_KEY_RESOURCE_BUNDLES_ROOT_FOLDER,
+ CLI_DEFAULT_RESOURCE_BUNDLES_ROOT_FOLDER)));
+
+ if (initCompletely || cli.hasOption(CLI_KEY_RESOURCE_BUNDLES)) {
+ String resourceBundlesList = cli
+ .getOptionValue(CLI_KEY_RESOURCE_BUNDLES);
+ String[] resourceBundles = resourceBundlesList.split(""
+ + RESOURCE_BUNDLES_SEP);
+ rpo.setResourceBundles(resourceBundles);
+ }
+
+ return rpo;
+ }
+
+ /**
+ * Stores the {@link ResourceProviderOperator} configuration in a property
+ * file.
+ *
+ * @param rpo
+ * {@link ResourceProviderOperator} to store the configuration
+ * for ( {@code null} not allowed)
+ * @param cli
+ * command line parameters
+ */
+ public static void storeResourceOperator(ResourceProviderOperator rpo,
+ File outFile) throws IOException {
+ FileWriter output = null;
+ try {
+ Properties config = new Properties();
+ output = new FileWriter(outFile);
+ if (rpo.getKeyIndicatorPrefix() != null)
+ config.put(CFG_KEY_BUNDLE_KEYS_INDICATOR_PREFIX,
+ rpo.getKeyIndicatorPrefix());
+ if (rpo.getResourceBundlesRootFolder() != null)
+ config.put(CFG_KEY_RESOURCE_BUNDLES_ROOT_FOLDER, rpo
+ .getResourceBundlesRootFolder().getPath());
+ if (rpo.getResourceBundles().length > 0) {
+ config.put(CFG_KEY_RESOURCE_BUNDLES, LangUtil
+ .stringConcatWithSep("" + RESOURCE_BUNDLES_SEP,
+ rpo.getResourceBundles()));
+ ResourceProvider sample = rpo.getResourceBundles()[0];
+ }
+
+ config.store(output, "Configuration for ResourceProviderOperator");
+ output.flush();
+ } finally {
+ if (output != null)
+ output.close();
+ }
+ }
+
}
-
More information about the Schmitzm-commits
mailing list