1 package org.codehaus.groovy.sandbox.markup; 2 46 47 import groovy.lang.Closure; 48 import groovy.lang.GroovyObjectSupport; 49 50 import java.util.HashMap ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import java.util.Map ; 54 55 public abstract class Builder extends GroovyObjectSupport { 56 protected final Map namespaceMethodMap = new HashMap (); 57 58 public Builder(final Map namespaceMethodMap) { 59 final Iterator keyIterator = namespaceMethodMap.keySet().iterator(); 60 61 while (keyIterator.hasNext()) { 62 final Object key = keyIterator.next(); 63 final List value = (List )namespaceMethodMap.get(key); 64 final Closure dg = ((Closure)value.get(1)).asWritable(); 65 66 this.namespaceMethodMap.put(key, new Object []{value.get(0), dg, fettleMethodMap(dg, (Map )value.get(2))}); 67 } 68 } 69 70 private static Map fettleMethodMap(final Closure defaultGenerator, final Map methodMap) { 71 final Map newMethodMap = new HashMap (); 72 final Iterator keyIterator = methodMap.keySet().iterator(); 73 74 while (keyIterator.hasNext()) { 75 final Object key = keyIterator.next(); 76 final Object value = methodMap.get(key); 77 78 if ((value instanceof Closure)) { 79 newMethodMap.put(key, value); 80 } else { 81 newMethodMap.put(key, defaultGenerator.curry((Object [])value)); 82 } 83 } 84 85 return newMethodMap; 86 } 87 88 abstract public Object bind(Closure root); 89 90 protected static abstract class Built extends GroovyObjectSupport { 91 protected final Closure root; 92 protected final Map namespaceSpecificTags = new HashMap (); 93 94 public Built(final Closure root, final Map namespaceTagMap) { 95 this.namespaceSpecificTags.putAll(namespaceTagMap); 96 97 this.root = (Closure)root.clone(); 98 99 this.root.setDelegate(this); 100 } 101 } 102 } 103 | Popular Tags |