I'm guessing you could do it like this:

   @Test
   public void test() {
      Registry registry = ...;
      BindingSource bindingSource =
registry.getService(BindingSource.class);
      TemplateParser parser = registry.getService(TemplateParser.class);
      Map<String, Object> params = new HashMap<String, Object>();
      params.put("foo", "value1");
      params.put("bar", "value2");
      String xml = "<t:mycomponent param1='test:foo' param2='test:bar' />";
      DynamicTemplate template = createDynamicTemplate(xml, params,
bindingSource, parser);
   }

   protected DynamicTemplate createDynamicTemplate(String xml, final
Map<String, Object> params,
         final BindingSource bindingSource, TemplateParser parser) {
      // TODO
      Resource resource = createResource(xml);
      BindingSource wrappedBindingSource = new BindingSource() {

         public Binding newBinding(String description, ComponentResources
container, ComponentResources component,
               String defaultPrefix, String expression, Location location) {
            if (expression.startsWith("test:")) {
               return new TestBinding(expression.substring(5), params);
            }
            return bindingSource.newBinding(description, container,
component, defaultPrefix, expression, location);
         }

         public Binding newBinding(String description, ComponentResources
container, String defaultPrefix, String expression) {
            if (expression.startsWith("test:")) {
               return new TestBinding(expression.substring(5), params);
            }
            return bindingSource.newBinding(description, container,
defaultPrefix, expression);
         }
      };
      return new DynamicTemplateSaxParser(resource, wrappedBindingSource,
parser.getDTDURLMappings()).parse();
   }

   public class TestBinding implements Binding {
      private Map<String, Object> map;
      private String key;

      public TestBinding(String key, Map<String, Object> map) {
         super();
         this.key = key;
         this.map = map;
      }

      public Class getBindingType() { return Object.class; }
      public boolean isInvariant() { return false; }
      public void set(Object value) { map.put(key, value); }
      public Object get() { return map.get(key); }
      public <T extends Annotation> T getAnnotation(Class<T>
annotationClass) { return null; }
   }



On 18 October 2013 08:49, Martin Kersten <martin.kersten...@gmail.com>wrote:

> That's exactly what I want. I'll take a look thanks.
>
>
> 2013/10/18 Lance Java <lance.j...@googlemail.com>
>
> > Hey Martin, you're lucky because I have a fair idea of what you're up to
> > :). I think you want to write a custom binding so that your dynamic
> > template can reference values from a map. I'm thinking you'll contribute
> a
> > test: binding which can read and / or write to a map defined in your test
> > case. Try Googling custom bindings and take a look at tapestry-stitch and
> > it's map: binding as an example. I'm off to bed :)
> > On 17 Oct 2013 19:11, "Martin Kersten" <martin.kersten...@gmail.com>
> > wrote:
> >
> > > I am currently continue to explore the test page capabilities. Now I
> need
> > > to think about being able to inject any property of any component under
> > > test using a generic page.
> > >
> > > So what I want is being able to pass all parameters of a component
> using
> > > html and providing those values via a Map (or something) via the
> Request
> > > object so I can easy get this via the RequestGlobals.request value.
> > >
> > > Is there a generic way to express such a property? I know we have
> certain
> > > types like asset: and alike. So if I want to access a map of objects
> and
> > > pass single values to the component, what can I do?
> > >
> > > You know something like <div
> t:componentIndex="parameters.get('index')">
> > Is
> > > this or similar possible (I can also use indexed properties like
> > providing
> > > getParameter(1) or firstParameter).
> > >
> > > Any Idea to make it as strict and readable as possible?
> > >
> > >
> > > Cheers,
> > >
> > > Martin (Kersten),
> > > Germany
> > >
> >
>

Reply via email to