1 16 package org.apache.cocoon.forms.binding; 17 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.context.Context; 24 import org.apache.avalon.framework.context.ContextException; 25 import org.apache.avalon.framework.context.Contextualizable; 26 import org.apache.cocoon.forms.binding.JXPathBindingManager.Assistant; 27 import org.apache.cocoon.forms.util.DomHelper; 28 import org.apache.cocoon.forms.util.JavaScriptHelper; 29 import org.mozilla.javascript.Function; 30 import org.w3c.dom.Element ; 31 32 71 public class JavaScriptJXPathBindingBuilder extends JXPathBindingBuilderBase implements Contextualizable { 72 73 private Context avalonContext; 74 75 public void contextualize(Context context) throws ContextException { 76 this.avalonContext = context; 77 } 78 79 public JXPathBindingBase buildBinding(Element element, Assistant assistant) throws BindingException { 80 try { 81 CommonAttributes commonAtts = JXPathBindingBuilderBase.getCommonAttributes(element); 82 83 String id = DomHelper.getAttribute(element, "id", null); 84 String path = DomHelper.getAttribute(element, "path", null); 85 86 JavaScriptJXPathBinding otherBinding = (JavaScriptJXPathBinding)assistant.getContext().getSuperBinding(); 87 88 if(otherBinding!=null) { 89 commonAtts = JXPathBindingBuilderBase.mergeCommonAttributes(otherBinding.getCommonAtts(),commonAtts); 90 91 if(id==null) 92 id=otherBinding.getId(); 93 if(path==null) 94 path=otherBinding.getPath(); 95 } 96 97 Function loadScript = null; 99 if (commonAtts.loadEnabled) { 100 if (otherBinding != null) 101 loadScript = otherBinding.getLoadScript(); 102 103 Element loadElem = DomHelper.getChildElement(element, BindingManager.NAMESPACE, "load-form"); 104 if (loadElem != null) { 105 loadScript = JavaScriptHelper.buildFunction(loadElem, "loadForm", JavaScriptJXPathBinding.LOAD_PARAMS); 106 } 107 } 108 109 Function saveScript = null; 111 if (commonAtts.saveEnabled) { 112 if (otherBinding != null) 113 saveScript = otherBinding.getSaveScript(); 114 115 Element saveElem = DomHelper.getChildElement(element, BindingManager.NAMESPACE, "save-form"); 116 if (saveElem != null) { 117 saveScript = JavaScriptHelper.buildFunction(saveElem, "saveForm", JavaScriptJXPathBinding.SAVE_PARAMS); 118 } 119 } 120 121 Map childBindings = new HashMap (); 123 124 if (otherBinding != null) { 125 Map otherChildren = otherBinding.getChildBindingsMap(); 126 Iterator it = otherChildren.entrySet().iterator(); 127 while(it.hasNext()) { 128 Map.Entry entry = (Map.Entry )it.next(); 129 childBindings.put(entry.getKey(),entry.getValue()); 130 } 131 } 132 133 Element [] children = DomHelper.getChildElements(element, BindingManager.NAMESPACE, "child-binding"); 134 if (children.length != 0) { 135 for (int i = 0; i < children.length; i++) { 136 Element child = children[i]; 137 138 String name = DomHelper.getAttribute(child, "name"); 140 141 JXPathBindingBase[] otherBindings = null; 142 if (childBindings.containsKey(name)) { 143 otherBindings = ((ComposedJXPathBindingBase)childBindings.get(name)).getChildBindings(); 145 } 146 147 JXPathBindingBase[] bindings = assistant.makeChildBindings(child,otherBindings); 149 if (bindings == null) { 150 bindings = new JXPathBindingBase[0]; 151 } 152 153 ComposedJXPathBindingBase composedBinding = new ComposedJXPathBindingBase(commonAtts, bindings); 154 composedBinding.enableLogging(getLogger()); 155 childBindings.put(name, composedBinding); 156 } 157 } 158 159 JXPathBindingBase result = new JavaScriptJXPathBinding(this.avalonContext, commonAtts, id, path, loadScript, saveScript, 160 Collections.unmodifiableMap(childBindings)); 161 result.enableLogging(getLogger()); 162 return result; 163 164 } catch(BindingException be) { 165 throw be; 166 } catch(Exception e) { 167 throw new BindingException("Cannot build binding at " + DomHelper.getLocation(element), e); 168 } 169 } 170 } 171 | Popular Tags |