|
|
@@ -4,8 +4,10 @@ import lombok.Data;
|
|
|
|
|
|
import net.ranides.assira.reflection.*;
|
|
|
import net.ranides.assira.reflection.walker.ObjectVisitor.PrintVisitor;
|
|
|
+import net.ranides.assira.reflection.walker.WalkerContexts.ArrayContext;
|
|
|
import net.ranides.assira.reflection.walker.WalkerContexts.ObjectContext;
|
|
|
import net.ranides.assira.text.Charsets;
|
|
|
+import net.ranides.assira.text.FormatHex;
|
|
|
import net.ranides.assira.text.ResolveFormat;
|
|
|
|
|
|
import javax.xml.datatype.XMLGregorianCalendar;
|
|
|
@@ -20,7 +22,7 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.BiConsumer;
|
|
|
import java.util.function.Consumer;
|
|
|
-import java.util.function.Predicate;
|
|
|
+import java.util.function.Function;
|
|
|
|
|
|
public class WalkerRules {
|
|
|
|
|
|
@@ -33,15 +35,26 @@ public class WalkerRules {
|
|
|
.term("{id,text,4}.{namePath,text,-50} ...")
|
|
|
.rule()
|
|
|
.match(ObjectContext::isNull)
|
|
|
- .term("{id,text,4}.{namePath,text,-50} = <null>")
|
|
|
+ .term("{id,text,4}.{namePath,text,-50} {typeName} = <null>")
|
|
|
+ .rule()
|
|
|
+ .match(String.class)
|
|
|
+ .term("{id,text,4}.{namePath,text,-50} {actual.shortname} = '{value}'")
|
|
|
+ .rule()
|
|
|
+ .match(byte[].class)
|
|
|
+ .text(ctx -> FormatHex.format(ctx.value()))
|
|
|
+ .term("{id,text,4}.{namePath,text,-50} {actual.shortname} = '{text}'")
|
|
|
+ .rule()
|
|
|
+ .match(char[].class)
|
|
|
+ .text(ctx -> String.valueOf(ctx.value()))
|
|
|
+ .term("{id,text,4}.{namePath,text,-50} {actual.shortname} = '{text}'")
|
|
|
+ .rule()
|
|
|
+ .match(ArrayContext.class::isInstance)
|
|
|
+ .hint("{id,text,4}.{namePath,text,-50} {actual.shortname}.size = {size}")
|
|
|
.rule()
|
|
|
.match(List.class)
|
|
|
.match(Collection.class)
|
|
|
.match(Map.class)
|
|
|
- .hint("{id,text,4}.{namePath,text,-50} {typeName}")
|
|
|
- .rule()
|
|
|
- .match(String.class)
|
|
|
- .term("{id,text,4}.{namePath,text,-50} {actual.shortname} = '{value}'")
|
|
|
+ .hint("{id,text,4}.{namePath,text,-50} {typeName}.size = {size}")
|
|
|
.rule()
|
|
|
.match(java.util.Date.class)
|
|
|
.match(java.sql.Date.class)
|
|
|
@@ -65,7 +78,12 @@ public class WalkerRules {
|
|
|
return new PrintVisitor(new PrintWriter(target)){
|
|
|
@Override
|
|
|
public boolean visitObject(ObjectContext<?> context) {
|
|
|
- return apply(this, context);
|
|
|
+ for (RuleEntry rule : rules) {
|
|
|
+ if(rule.test(context)) {
|
|
|
+ return rule.consume(this, context);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|
|
|
};
|
|
|
}
|
|
|
@@ -79,29 +97,12 @@ public class WalkerRules {
|
|
|
return new RuleBuilderRoot();
|
|
|
}
|
|
|
|
|
|
- public <U> WalkerRules rule(RulePredicate<?> predicate, RuleHandler<U> handler) {
|
|
|
- rules.add(new RuleEntry(predicate, handler));
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public <U> WalkerRules rule(List<RulePredicate<?>> predicates, RuleHandler<U> handler) {
|
|
|
- predicates.forEach(predicate -> rule(predicate, handler));
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean apply(PrintVisitor visitor, ObjectContext<?> ctx) {
|
|
|
- for (RuleEntry rule : rules) {
|
|
|
- if(rule.test(ctx)) {
|
|
|
- return rule.consume(visitor, ctx);
|
|
|
- }
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
public abstract class RuleBuilder<F> {
|
|
|
|
|
|
protected final List<RulePredicate<?>> predicates;
|
|
|
|
|
|
+ protected Consumer<ObjectContext<F>> formatter = x -> { };
|
|
|
+
|
|
|
protected RuleBuilder(List<RulePredicate<?>> predicates) {
|
|
|
this.predicates = predicates;
|
|
|
}
|
|
|
@@ -116,11 +117,7 @@ public class WalkerRules {
|
|
|
}
|
|
|
|
|
|
public WalkerRules hint(BiConsumer<PrintVisitor, ObjectContext<F>> handler) {
|
|
|
- RuleHandler<F> action = (target, ctx) -> {
|
|
|
- handler.accept(target, ctx);
|
|
|
- return true;
|
|
|
- };
|
|
|
- return rule(predicates, action);
|
|
|
+ return rule(predicates, asHandler(true, handler));
|
|
|
}
|
|
|
|
|
|
public WalkerRules term(String text) {
|
|
|
@@ -133,11 +130,20 @@ public class WalkerRules {
|
|
|
}
|
|
|
|
|
|
public WalkerRules term(BiConsumer<PrintVisitor, ObjectContext<F>> handler) {
|
|
|
- RuleHandler<F> action = (target, ctx) -> {
|
|
|
+ return rule(predicates, asHandler(false, handler));
|
|
|
+ }
|
|
|
+
|
|
|
+ private RuleHandler<F> asHandler(boolean follow, BiConsumer<PrintVisitor, ObjectContext<F>> handler) {
|
|
|
+ return (target, ctx) -> {
|
|
|
+ formatter.accept(ctx);
|
|
|
handler.accept(target, ctx);
|
|
|
- return false;
|
|
|
+ return follow;
|
|
|
};
|
|
|
- return rule(predicates, action);
|
|
|
+ }
|
|
|
+
|
|
|
+ private <U> WalkerRules rule(List<RulePredicate<?>> predicates, RuleHandler<U> handler) {
|
|
|
+ predicates.forEach(predicate -> rules.add(new RuleEntry(predicate, handler)));
|
|
|
+ return WalkerRules.this;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -148,6 +154,11 @@ public class WalkerRules {
|
|
|
super(new ArrayList<>());
|
|
|
}
|
|
|
|
|
|
+ public RuleBuilderRoot text(Function<ObjectContext<Object>, String> formatter) {
|
|
|
+ this.formatter = ctx -> ctx.text(formatter.apply(ctx));
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
public <T> RuleBuilderType<T> match(Class<T> type) {
|
|
|
return match(IClass.typeinfo(type));
|
|
|
}
|
|
|
@@ -170,6 +181,11 @@ public class WalkerRules {
|
|
|
super(source.predicates);
|
|
|
}
|
|
|
|
|
|
+ public RuleBuilderType<F> text(Function<ObjectContext<F>, String> formatter) {
|
|
|
+ this.formatter = ctx -> ctx.text(formatter.apply(ctx));
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
public RuleBuilderMore match(Class<?> type) {
|
|
|
return match(IClass.typeinfo(type));
|
|
|
}
|
|
|
@@ -192,15 +208,22 @@ public class WalkerRules {
|
|
|
super(source.predicates);
|
|
|
}
|
|
|
|
|
|
+ public RuleBuilderMore text(Function<ObjectContext<Object>, String> formatter) {
|
|
|
+ this.formatter = ctx -> ctx.text(formatter.apply(ctx));
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
public RuleBuilderMore match(Class<?> type) {
|
|
|
return match(IClass.typeinfo(type));
|
|
|
}
|
|
|
|
|
|
public RuleBuilderMore match(IClass<?> type) {
|
|
|
+ predicates.add(ctx -> ctx.isSubclass(type));
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- public RuleBuilderMore match(Predicate<ObjectContext<?>> predicate) {
|
|
|
+ public RuleBuilderMore match(RulePredicate<?> predicate) {
|
|
|
+ predicates.add(predicate);
|
|
|
return this;
|
|
|
}
|
|
|
|