Index: lutinutil/src/java/org/codelutin/util/OptionParserUtil.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/OptionParserUtil.java:1.1 --- /dev/null Sun Dec 2 07:08:57 2007 +++ lutinutil/src/java/org/codelutin/util/OptionParserUtil.java Sun Dec 2 07:08:52 2007 @@ -0,0 +1,144 @@ +package org.codelutin.util; + +import org.codelutin.log.LutinLog; +import org.codelutin.log.LutinLogFactory; +import org.codelutin.util.OptionParserAnnotationHelper.OptionA; +import org.codelutin.util.OptionParserAnnotationHelper.OptionArgumentA; +import org.codelutin.util.OptionParserAnnotationHelper.OptionGroupArgumentA; +import org.codelutin.util.OptionParserAnnotationHelper.OptionParserA; + +import java.util.ArrayList; +import java.util.List; + +/** + * Usefull methods for OptionParser + * + * @author chemit + */ + +public class OptionParserUtil { + + public static String toString(OptionParserA anno, String prefix) { + StringBuilder builder = new StringBuilder(); + for (OptionA definition : anno.options()) { + builder.append(prefix).append(OptionParserUtil.toString(definition)); + builder.append("\n ").append(definition.description()).append("\n\n"); + } + return builder.toString(); + } + + public static String toString(OptionA anno) { + StringBuilder builder = new StringBuilder(); + StringBuffer sb = new StringBuffer(); + int min = anno.min(); + int max = anno.max(); + boolean isMandatory = min > 0; + for (String alia : anno.alias()) { + sb.append('|').append(alia); + } + String s1 = sb.toString().substring(1); + if (min == 0 && max == 1) { + StringUtil.printCardinalite(builder, s1, min, max, isMandatory, "", "", "", ""); + } else { + StringUtil.printCardinalite(builder, s1, min, max, isMandatory, "(", ")", "[", "]"); + } + if (anno.groups().length > 0) { + for (OptionGroupArgumentA group : anno.groups()) { + builder.append(' ').append(toString(group)); + } + } + return builder.toString(); + } + + public static String toString(OptionGroupArgumentA anno) { + StringBuilder builder = new StringBuilder(); + StringBuffer s = new StringBuffer(); + for (OptionArgumentA argument : anno.arguments()) { + s.append('|').append(toString(argument)); + } + String s1 = s.toString(); + StringUtil.printCardinalite(builder, s1.length() > 0 ? s1.substring(1) : s1, anno.min(), anno.max(), anno.pos() != -1, "<", ">", "[", "]"); + return builder.toString(); + } + + public static String toString(OptionArgumentA anno) { + StringBuilder sb = new StringBuilder(); + StringBuffer s = new StringBuffer(anno.key()); + switch (anno.type()) { + case constant: + break; + case namedAndValued: + s.append('=').append(anno.valueType().name().substring(1)); + break; + case valued: + s.append(':').append(anno.valueType().name().substring(1)); + break; + } + StringUtil.printCardinalite(sb, s.toString(), anno.min(), anno.max(), anno.min() > 0, "", "", "", ""); + return sb.toString(); + } + + /** + * Un contexte abstrait pour parseur (qui peut contenir un contexte parent + * et des contextes enfants). + * + * @author chemit + */ + + public abstract static class ParserContext
{
+
+ /** logger non statique pour être dans la bonne catégorie */
+ protected final LutinLog log = LutinLogFactory.getLutinLog(getClass());
+
+ /** le contexte parent (peut être null) */
+ protected final P parent;
+
+ /** la liste des contextes enfants (peut être null)*/
+ protected final List contexts;
+
+ /** flag pour marquer l'état du context */
+ protected boolean valid = true;
+
+ protected ParserContext(P parent, boolean withSons) {
+ this.parent = parent;
+ this.contexts = withSons ? new ArrayList() : null;
+ }
+
+ void addSon(S context) {
+ preAddSonHook(context);
+ if (valid) {
+ log.info("[" + getClass().getSimpleName() + " : " + context + "]");
+ contexts.add(context);
+ postAddSonHook(context);
+ }
+ }
+
+ protected void preAddSonHook(S context) {
+ if (contexts == null) {
+ return;
+ }
+ if (context == null || !context.valid) {
+ unvalidate();
+ }
+ }
+
+ protected void postAddSonHook(S context) {
+ }
+
+ void clear() {
+ if (contexts != null) {
+ for (S context : contexts) {
+ context.clear();
+ }
+ }
+ //contexts.clear();
+ }
+
+ public void unvalidate() {
+ this.valid = false;
+ if (parent != null) {
+ parent.unvalidate();
+ }
+ }
+ }
+}