r1047 - in lutinjaxx/trunk/jaxx-core: . src/main/java/jaxx/runtime src/test/java/jaxx/runtime
Author: tchemit Date: 2008-12-01 23:59:46 +0000 (Mon, 01 Dec 2008) New Revision: 1047 Modified: lutinjaxx/trunk/jaxx-core/changelog lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/Decorator.java lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/JAXXContextEntryDef.java lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/JXPathDecorator.java lutinjaxx/trunk/jaxx-core/src/test/java/jaxx/runtime/JXPathDecoratorTest.java Log: implements jaxx.runtime.JXPathDecorator add setcontextValue and removeContextValue on JAXXContextEntryDef Modified: lutinjaxx/trunk/jaxx-core/changelog =================================================================== --- lutinjaxx/trunk/jaxx-core/changelog 2008-12-01 17:10:17 UTC (rev 1046) +++ lutinjaxx/trunk/jaxx-core/changelog 2008-12-01 23:59:46 UTC (rev 1047) @@ -1,8 +1,10 @@ - 0.7 chemit - * 20081201 [chemit] introduce scope in BeanValidator (ERROR or WARNING) - * 20081201 [chemit] only enter once in $initialize method in generated code + 0.7 chemit 200812?? + * 20081201 [chemit] - implements jaxx.runtime.JXPathDecorator + - add setcontextValue and removeContextValue on JAXXContextEntryDef + - introduce scope in BeanValidator (ERROR or WARNING) and related swing stuff + - only enter once in $initialize method in generated code - 0.6 chemit 200811 + 0.6 chemit 20081117 * 20081118 [chemit] introduce NavigationUtil, save in context selected node * 20081107 [chemit] improve data binding and code generation : - make possible inheritance in binding Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/Decorator.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/Decorator.java 2008-12-01 17:10:17 UTC (rev 1046) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/Decorator.java 2008-12-01 23:59:46 UTC (rev 1047) @@ -10,7 +10,14 @@ protected final Class<O> internalClass; private static final long serialVersionUID = -1L; - public Decorator(Class<O> internalClass) { + /** + * @param internalClass the class of objects to be decorated. + * @throws NullPointerException if internalClass parameter is null + */ + public Decorator(Class<O> internalClass) throws NullPointerException { + if (internalClass == null) { + throw new NullPointerException("internalClass can not be null."); + } this.internalClass = internalClass; } Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/JAXXContextEntryDef.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/JAXXContextEntryDef.java 2008-12-01 17:10:17 UTC (rev 1046) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/JAXXContextEntryDef.java 2008-12-01 23:59:46 UTC (rev 1047) @@ -52,6 +52,14 @@ return context.getContextValue(klass, name); } + public void removeContextValue(JAXXContext context) { + context.removeContextValue(klass, name); + } + + public void setContextValue(JAXXContext context, O value) { + context.setContextValue(value, name); + } + @Override public String toString() { return super.toString() + "<" + klass + ":" + name + ">"; @@ -103,11 +111,8 @@ } public boolean accept2(Class<?> klass, String name) { - if (klass == Object.class && this.klass != Object.class) { - // block if looking for Object.class - return false; - } - return this.klass.isAssignableFrom(klass) && (this.name == null && name == null - || (this.name != null && name != null && this.name.equals(name))); + return !(klass == Object.class && this.klass != Object.class) && + this.klass.isAssignableFrom(klass) && (this.name == null && + name == null || (this.name != null && name != null && this.name.equals(name))); } } Modified: lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/JXPathDecorator.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/JXPathDecorator.java 2008-12-01 17:10:17 UTC (rev 1046) +++ lutinjaxx/trunk/jaxx-core/src/main/java/jaxx/runtime/JXPathDecorator.java 2008-12-01 23:59:46 UTC (rev 1047) @@ -5,9 +5,28 @@ import org.apache.commons.logging.LogFactory; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -/** @author chemit */ +/** + * JXPath decorator based on {@link String#format(String, Object[])} method. + * <p/> + * To use it, give to him a expression where all jxpath to apply on bean are boxed in <code>${}</code>. + * <p/> + * After the jxpath token you must specifiy the formatter to apply of the jxpath token. + * <p/> + * For example : + * <pre> + * Decorator<Object> d = JXPathDecorator.newDecorator(JXPathDecorator.class,"expr = ${expressions}$s"); + * assert "expr = %1$s" == d.getExpression(); + * assert 1 == d.getNbToken(); + * assert java.util.Arrays.asList("expression") == d.getTokens(); + * assert "expr = %1$s" == d.toString(d); + * </pre> + * + * @author chemit + * @see Decorator + */ public class JXPathDecorator<O> extends Decorator<O> { /** to use log facility, just put in your code: log.info(\"...\"); */ @@ -15,76 +34,103 @@ private static final long serialVersionUID = 1L; + /** + * Factory method to instanciate a new {@link JXPathDecorator} for the given class {@link O} and expression. + * + * @param internalClass the class of the objects decorated by the new decorator + * @param expression the expression to use to decorated objects + * @param <O> the generic type of class to be decorated by the new decorator + * @return the new instanciated decorator + * @throws IllegalArgumentException if the expression is not valid, says: + * <p/> + * - a missing right brace was detected. + * <p/> + * - a ${ was found in a jxpath token. + * @throws NullPointerException if internalClass parameter is null. + */ + public static <O> JXPathDecorator<O> newDecorator(Class<O> internalClass, String expression) + throws IllegalArgumentException, NullPointerException { + return new JXPathDecorator<O>(internalClass, expression); + } + + /** + * expression to format using {@link String#format(String, Object[])}, all variables are compute + * using using the jxpath tokens. + */ protected String expression; - protected String[] data; + /** list of jxpath tokens to apply on expression */ + protected String[] tokens; - protected Boolean[] jxPath; - + /** nb jxpath tokens to compute */ protected int nbToken; - public JXPathDecorator(Class<O> internalClass, String expression) { + protected JXPathDecorator(Class<O> internalClass, String expression) throws IllegalArgumentException, NullPointerException { super(internalClass); - this.expression = expression; - List<String> lData = new ArrayList<String>(); - List<Boolean> ljxPath = new ArrayList<Boolean>(); - - int index = expression.indexOf("${"); - boolean first = true; - int end = expression.length(); - while (index > -1) { - if (first) { - first = false; - if (index > 0) { - // prefix before first jxpath - lData.add(expression.substring(0, index )); - ljxPath.add(false); - } + List<String> lTokens = new ArrayList<String>(); + StringBuilder buffer = new StringBuilder(); + int size = expression.length(); + int end = -1; + int start; + while ((start = expression.indexOf("${", end + 1)) > -1) { + if (start > end + 1) { + // prefix of next jxpath token + buffer.append(expression.substring(end + 1, start)); } - // seek next } - int last = expression.indexOf("}", index + 1); - if (last != -1) { - // jxpath detected - String s = expression.substring(index + 2, last); - lData.add(s); - ljxPath.add(true); + // seek end of jxpath + end = expression.indexOf("}", start + 1); + if (end == -1) { + throw new IllegalArgumentException("could not find the rigth brace starting at car " + start + " : " + expression.substring(start + 2)); } - - index = expression.indexOf("${", last + 1); - if (index > -1) { - // inter jxpath code - String s = expression.substring(last + 1, index); - lData.add(s); - ljxPath.add(false); - } else { - if (end > (last + 1)) { - // suffix after last jxpath - String s = expression.substring(last + 1); - lData.add(s); - ljxPath.add(false); - } + String jxpath = expression.substring(start + 2, end); + // not allowed ${ inside a jxpath token + if (jxpath.indexOf("${") > -1) { + throw new IllegalArgumentException("could not find a ${ inside a jxpath expression at car " + (start + 2) + " : " + jxpath); } + // save the jxpath token + lTokens.add(jxpath); + // replace jxpath token in expresion with a string format variable + buffer.append("%").append(lTokens.size()); } - data = lData.toArray(new String[lData.size()]); - jxPath = ljxPath.toArray(new Boolean[ljxPath.size()]); - nbToken = data.length; + if (size > (end + 1)) { + // suffix after end jxpath (or all expression if no jxpath) + buffer.append(expression.substring(end + 1)); + } + + this.tokens = lTokens.toArray(new String[lTokens.size()]); + this.nbToken = tokens.length; + this.expression = buffer.toString(); if (log.isDebugEnabled()) { - log.debug("lData : " + lData); - log.debug("ljxPath : " + ljxPath); + log.debug(expression + " --> " + this); } - } public String toString(Object bean) { + if (bean == null) { + return null; + } JXPathContext jxcontext = JXPathContext.newContext(bean); - StringBuilder buffer = new StringBuilder(); + Object[] args = new Object[nbToken]; for (int i = 0; i < nbToken; i++) { - String s = data[i]; - if (jxPath[i]) { - s = String.valueOf(jxcontext.getValue(s)); - } - buffer.append(s); + args[i] = jxcontext.getValue(tokens[i]); } - return buffer.toString(); + return String.format(expression, args); } + + public String getExpression() { + return expression; + } + + public String[] getTokens() { + return tokens; + } + + public int getNbToken() { + return nbToken; + } + + @Override + public String toString() { + return super.toString() + "<expression:" + expression + ", tokens:" + Arrays.toString(tokens) + ">"; + } } Modified: lutinjaxx/trunk/jaxx-core/src/test/java/jaxx/runtime/JXPathDecoratorTest.java =================================================================== --- lutinjaxx/trunk/jaxx-core/src/test/java/jaxx/runtime/JXPathDecoratorTest.java 2008-12-01 17:10:17 UTC (rev 1046) +++ lutinjaxx/trunk/jaxx-core/src/test/java/jaxx/runtime/JXPathDecoratorTest.java 2008-12-01 23:59:46 UTC (rev 1047) @@ -1,65 +1,90 @@ package jaxx.runtime; +import org.junit.After; +import static org.junit.Assert.assertEquals; import org.junit.Test; -import org.junit.Assert; /** @author chemit */ public class JXPathDecoratorTest { - public static class Model { - protected String name; + protected JXPathDecorator<?> decorator; + protected String expected; + protected String result; - protected int integervalue; + @After + public void after() { + decorator = null; + } - public Model(String name, int integervalue) { - this.name = name; - this.integervalue = integervalue; - } + @Test(expected = NullPointerException.class) + public void testNullInternalClass() throws Exception { + decorator = JXPathDecorator.newDecorator(null, "hello"); + } - public String getName() { - return name; - } + @Test(expected = IllegalArgumentException.class) + public void testMissingRightBrace() throws Exception { + decorator = JXPathDecorator.newDecorator(Object.class, "${haha"); + } - public void setName(String name) { - this.name = name; - } + @Test(expected = IllegalArgumentException.class) + public void testMissingRightBrace2() throws Exception { + decorator = JXPathDecorator.newDecorator(Object.class, "${haha${hum}"); + } - public int getIntegervalue() { - return integervalue; - } + @Test + public void testNullBean() throws Exception { + decorator = JXPathDecorator.newDecorator(Object.class, "hello"); + expected = "hello"; + assertEquals(expected, decorator.expression); + assertEquals(0, decorator.nbToken); + assertEquals(0, decorator.tokens.length); - public void setIntegervalue(int integervalue) { - this.integervalue = integervalue; - } + result = decorator.toString(null); + assertEquals(null, result); } @Test + public void testNoJXPath() throws Exception { + decorator = JXPathDecorator.newDecorator(Object.class, "hello"); + expected = "hello"; + assertEquals(expected, decorator.expression); + assertEquals(0, decorator.nbToken); + assertEquals(0, decorator.tokens.length); + + result = decorator.toString(this); + assertEquals(expected, result); + } + + + @Test public void testDecorator() throws Exception { - Decorator<Model> decorator; - String expected; - String result; - Model m = new Model("name", 10); + decorator = JXPathDecorator.newDecorator(JXPathDecorator.class, "${expression}$s - ${nbToken}$d"); + assertEquals("%1$s - %2$d", decorator.expression); + assertDecoratorInternal(); - decorator = new JXPathDecorator<Model>(Model.class,"${name} - ${integervalue}"); - expected = m.getName() + " - " + m.getIntegervalue(); - result = decorator.toString(m); - Assert.assertEquals(expected, result); + decorator = JXPathDecorator.newDecorator(JXPathDecorator.class, "${expression}${nbToken}"); + assertEquals("%1%2", decorator.expression); + assertDecoratorInternal(); - decorator = new JXPathDecorator<Model>(Model.class,"${name}${integervalue}"); - expected = m.getName() + m.getIntegervalue(); - result = decorator.toString(m); - Assert.assertEquals(expected, result); + decorator = JXPathDecorator.newDecorator(JXPathDecorator.class, "before ${expression}$s - ${nbToken}$d after"); + assertEquals("before %1$s - %2$d after", decorator.expression); + assertDecoratorInternal(); - decorator = new JXPathDecorator<Model>(Model.class,"before ${name} - ${integervalue} after"); - expected = "before "+m.getName() + " - " + m.getIntegervalue()+" after"; - result = decorator.toString(m); - Assert.assertEquals(expected, result); + decorator = JXPathDecorator.newDecorator(JXPathDecorator.class, "before${expression}$s-${nbToken}$dafter"); + assertEquals("before%1$s-%2$dafter", decorator.expression); + assertDecoratorInternal(); + } - decorator = new JXPathDecorator<Model>(Model.class,"before${name}-${integervalue}after"); - expected = "before"+m.getName() + "-" + m.getIntegervalue()+"after"; - result = decorator.toString(m); - Assert.assertEquals(expected, result); + public void assertDecoratorInternal() { + assertEquals(2, decorator.nbToken); + assertEquals(2, decorator.tokens.length); + assertEquals("expression", decorator.tokens[0]); + assertEquals("nbToken", decorator.tokens[1]); + expected = String.format(decorator.expression, decorator.getExpression(), decorator.getNbToken()); + result = decorator.toString(decorator); + assertEquals(expected, result); } + }
participants (1)
-
tchemit@users.labs.libre-entreprise.org