Index: maven-commandline-plugin/src/java/org/codelutin/option/def/loader/LoaderEntry.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/def/loader/LoaderEntry.java:1.1 --- /dev/null Sun Mar 23 00:22:41 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/def/loader/LoaderEntry.java Sun Mar 23 00:22:36 2008 @@ -0,0 +1,60 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.def.loader; + +import java.util.List; +import java.util.Properties; + +/** @author chemit */ +public abstract class LoaderEntry implements Comparable { + + protected final String fullKey; + + protected final String name; + + public abstract void load(Properties source, List result); + + protected LoaderEntry(String fullKey, String name) { + this.fullKey = fullKey; + this.name = name; + } + + public String getFullKey() { + return fullKey; + } + + public String getName() { + return name; + } + + public int compareTo(E o) { + return name.compareTo(o.name); + } + + @Override + public boolean equals(Object o) { + return this == o || o instanceof LoaderEntry && name.equals(((LoaderEntry) o).name); + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public String toString() { + return getClass().getSimpleName() + " < name:" + name + " >"; + } +} Index: maven-commandline-plugin/src/java/org/codelutin/option/def/loader/OptionLoader.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/def/loader/OptionLoader.java:1.1 --- /dev/null Sun Mar 23 00:22:41 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/def/loader/OptionLoader.java Sun Mar 23 00:22:36 2008 @@ -0,0 +1,77 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.def.loader; + +import org.codelutin.option.def.DefaultOptionAction; + +import java.util.List; +import java.util.Properties; + +/** @author chemit */ +public class OptionLoader extends Loader { + + public static final String OPTION_PATTERN = "(\\w*)\\.option\\.definition"; + + protected List defaultOptions; + protected String categoriesStr; + private List options; + + public OptionLoader(String categoriesStr) { + super(OPTION_PATTERN, OptionLoaderEntry.class); + this.defaultOptions = DefaultOptionAction.getConfigKeys(); + this.categoriesStr = categoriesStr; + } + + public void doLoad(Properties source) throws Exception { + // get configs + options = load(source); + } + + public List getOptions() { + return options; + } + + public void setCategoriesStr(String categoriesStr) { + this.categoriesStr = categoriesStr; + } + + @Override + public List load(Properties source) throws Exception { + List result = super.load(source); + // add default options + if (!defaultOptions.isEmpty()) { + for (DefaultOptionAction optionAction : DefaultOptionAction.values()) { + OptionLoaderEntry loaderEntry = new OptionLoaderEntry(optionAction.configKey(), optionAction.name()); + String definition = optionAction.def(); + loaderEntry.setDefinition(definition.replace("@categories@", categoriesStr)); + result.add(loaderEntry); + } + } + return result; + } + + @Override + protected boolean needLoad(String key, String value, List result, Properties source) { + boolean need = true; + if (defaultOptions.contains(key)) { + // do not add this, this is a generated option + log.warn("generic option detected '" + value+ "', will use generic version instead..."); + need = false; + source.remove(key); + } + return need; + + } +} Index: maven-commandline-plugin/src/java/org/codelutin/option/def/loader/OptionLoaderEntry.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/def/loader/OptionLoaderEntry.java:1.1 --- /dev/null Sun Mar 23 00:22:41 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/def/loader/OptionLoaderEntry.java Sun Mar 23 00:22:36 2008 @@ -0,0 +1,46 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.def.loader; + +import java.util.List; +import java.util.Properties; + +/** @author chemit */ +public class OptionLoaderEntry extends LoaderEntry { + + protected String definition; + + public OptionLoaderEntry(String key, String name) { + super(key, name); + } + + public void load(Properties source, List result) { + this.definition = source.get(getFullKey())+""; + source.remove(getFullKey()); + } + + public String getDefinition() { + return definition; + } + + @Override + public String toString() { + return super.toString() + " ( definition:" + definition + " )"; + } + + public void setDefinition(String definition) { + this.definition = definition; + } +} Index: maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigPropertyLoader.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigPropertyLoader.java:1.1 --- /dev/null Sun Mar 23 00:22:41 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigPropertyLoader.java Sun Mar 23 00:22:36 2008 @@ -0,0 +1,26 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.def.loader; + +/** @author chemit */ +public class ConfigPropertyLoader extends Loader { + + public static final String CONFIG_DEFINITION_PATTERN = "\\.config\\.definition\\.(\\w+)"; + + public ConfigPropertyLoader(String category) { + super(category + CONFIG_DEFINITION_PATTERN, ConfigPropertyLoaderEntry.class); + } + +} \ No newline at end of file Index: maven-commandline-plugin/src/java/org/codelutin/option/def/loader/Loader.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/def/loader/Loader.java:1.1 --- /dev/null Sun Mar 23 00:22:41 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/def/loader/Loader.java Sun Mar 23 00:22:36 2008 @@ -0,0 +1,96 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.def.loader; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Classe responsable tomapping entre un fichier de properties et un bean + * + * @author chemit + */ + +public abstract class Loader> { + + protected static final Log log = LogFactory.getLog(Loader.class); + + protected final Pattern pattern; + + protected Constructor eConstructor; + + protected Loader(String pattern, Class entryImpl) { + + this.pattern = Pattern.compile(pattern); + try { + eConstructor = entryImpl.getConstructor(String.class, String.class); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + + public List load(Properties source) throws Exception { + List result = new ArrayList(); + + SortedMap keys = getKeys(source); + + for (Map.Entry entry : keys.entrySet()) { + String key = entry.getKey(); + if (needLoad(key, entry.getValue(), result,source)) { + // found a entry to treate + E e = eConstructor.newInstance(key, entry.getValue()); + e.load(source, result); + result.add(e); + } + } + for (E e : result) { + postLoad(e, source); + } + return result; + } + + protected boolean needLoad(String key, String value, List result, Properties source) { + // by default accept all entries detected + return true; + } + + protected void postLoad(E e, Properties source) throws Exception { + // by default, do nothing + } + + private SortedMap getKeys(Properties source) { + SortedMap keys = new TreeMap(); + for (Object o : source.keySet()) { + String key = o + ""; + Matcher matcher = pattern.matcher(key); + if (matcher.matches()) { + // found a entry to treate + keys.put(key, matcher.group(1)); + } + } + return keys; + } + +} Index: maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigLoaderEntry.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigLoaderEntry.java:1.1 --- /dev/null Sun Mar 23 00:22:41 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigLoaderEntry.java Sun Mar 23 00:22:36 2008 @@ -0,0 +1,39 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.def.loader; + +import java.util.List; +import java.util.Properties; + +/** @author chemit */ +public class ConfigLoaderEntry extends LoaderEntry { + + private List properties; + + public ConfigLoaderEntry(String key, String name) { + super(key, name); + } + + public void load(Properties source, List result) { + } + + public List getProperties() { + return properties; + } + + public void setProperties(List properties) { + this.properties = properties; + } +} Index: maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigLoader.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigLoader.java:1.1 --- /dev/null Sun Mar 23 00:22:41 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigLoader.java Sun Mar 23 00:22:36 2008 @@ -0,0 +1,80 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.def.loader; + +import java.util.List; +import java.util.Properties; + +/** @author chemit */ +public class ConfigLoader extends Loader { + + public static final String CONFIG_DEFINITION_PATTERN = "(\\w+)\\.config\\.definition\\.\\w+"; + + protected String categoriesStr; + + protected List configs; + + public ConfigLoader() { + super(CONFIG_DEFINITION_PATTERN, ConfigLoaderEntry.class); + } + + public void doLoad(Properties source) throws Exception { + + // get configs + configs = load(source); + + //TODO Check we have mandatories config properties... + + // build @catagories@ var + categoriesStr = categories(); + } + + public String getCategoriesStr() { + return categoriesStr; + } + + public List getConfigs() { + return configs; + } + + @Override + protected boolean needLoad(String key, String value, List result, Properties source) { + for (ConfigLoaderEntry entry : result) { + if (entry.getName().equals(value)) { + return false; + } + } + return true; + } + + @Override + protected void postLoad(ConfigLoaderEntry entry, Properties source) throws Exception { + ConfigPropertyLoader loader = new ConfigPropertyLoader(entry.getName()); + entry.setProperties(loader.load(source)); + } + + + protected String categories() { + String str = ""; + if (!configs.isEmpty()) { + StringBuilder sb = new StringBuilder(); + for (ConfigLoaderEntry category : configs) { + sb.append('|').append(category.getName()); + } + str = sb.substring(1); + } + return str; + } +} \ No newline at end of file Index: maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigPropertyLoaderEntry.java diff -u /dev/null maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigPropertyLoaderEntry.java:1.1 --- /dev/null Sun Mar 23 00:22:41 2008 +++ maven-commandline-plugin/src/java/org/codelutin/option/def/loader/ConfigPropertyLoaderEntry.java Sun Mar 23 00:22:36 2008 @@ -0,0 +1,56 @@ +/** + * # #% Copyright (C) 2008 Code Lutin, Tony Chemit + * This program is free software; you + * can redistribute it and/or modify it under the terms of the GNU General + * Public License as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. This program 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 General Public License for more details. You + * should have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place + * - Suite 330, Boston, MA 02111-1307, USA. + * # #% + */ +package org.codelutin.option.def.loader; + +import java.util.List; +import java.util.Properties; + +/** @author chemit */ +public class ConfigPropertyLoaderEntry extends LoaderEntry { + + public final static String CMODIFIERS_KEY_FACTOR = ".config.modifiers."; + + protected String definition; + protected String modifiers; + protected final String category; + + public ConfigPropertyLoaderEntry(String key, String name) { + super(key, name); + this.category = key.substring(0, key.indexOf('.')); + } + + public void load(Properties source, List result) { + definition = source.get(getFullKey())+"";; + source.remove(getFullKey()); + String modifiersKey = category + CMODIFIERS_KEY_FACTOR + name; + Object cv = source.get(modifiersKey); + if (cv == null) { + //TODO treate missing modifiers entry + } else { + modifiers = cv + ""; + source.remove(modifiersKey); + } + } + + public String getDefinition() { + return definition; + } + + @Override + public String toString() { + return super.toString() + " ( category:" + category + ", def: " + definition + ", modifiers:" + modifiers + " )"; + } + +}