1 16 package org.apache.cocoon.forms.binding; 17 18 import java.lang.reflect.Method ; 19 20 import org.apache.cocoon.forms.binding.JXPathBindingManager.Assistant; 21 import org.apache.cocoon.forms.util.DomHelper; 22 import org.w3c.dom.Element ; 23 24 51 public class CustomJXPathBindingBuilder extends JXPathBindingBuilderBase { 52 private static final Class [] DOMELEMENT_METHODARGS; 53 private static final Class [] EMPTY_METHODARGS; 54 55 static { 56 DOMELEMENT_METHODARGS = new Class [1]; 57 DOMELEMENT_METHODARGS[0] = Element .class; 58 EMPTY_METHODARGS = null; 59 } 60 61 69 public JXPathBindingBase buildBinding(Element bindingElm, 70 Assistant assistant) throws BindingException { 71 72 try { 73 CommonAttributes commonAtts = 74 JXPathBindingBuilderBase.getCommonAttributes(bindingElm); 75 String xpath = DomHelper.getAttribute(bindingElm, "path", "."); 76 String widgetId = DomHelper.getAttribute(bindingElm, "id", null); 77 78 Object bindingInstance = null; 79 80 String className = DomHelper.getAttribute(bindingElm, "class", null); 81 if(className != null) { 82 Class clazz = Class.forName(className); 83 bindingInstance = clazz.newInstance(); 84 85 } else { 86 String builderClassName = 87 DomHelper.getAttribute(bindingElm, "builderclass"); 88 String factoryMethodName = 89 DomHelper.getAttribute(bindingElm, "factorymethod"); 90 Element configNode = 91 DomHelper.getChildElement(bindingElm, BindingManager.NAMESPACE, "config"); 92 93 if(! (builderClassName == null || factoryMethodName == null) ) { 95 Class builderClass = Class.forName(builderClassName); 96 Method factoryMethod = null; 97 Object [] args = null; 98 try { 99 factoryMethod = builderClass.getMethod(factoryMethodName, DOMELEMENT_METHODARGS); 100 args = new Object [1]; 101 args[0] = configNode; 102 } catch (NoSuchMethodException e) { 103 factoryMethod = null; 104 } 105 106 if (factoryMethod == null) { 107 factoryMethod = builderClass.getMethod(factoryMethodName, EMPTY_METHODARGS); 108 args = null; 109 } 110 111 bindingInstance = factoryMethod.invoke(null, args); 113 } 114 } 115 116 CustomJXPathBinding otherBinding = (CustomJXPathBinding)assistant.getContext().getSuperBinding(); 118 if(otherBinding!=null) { 119 commonAtts = JXPathBindingBuilderBase.mergeCommonAttributes(otherBinding.getCommonAtts(),commonAtts); 120 121 if(xpath==null) 122 xpath = otherBinding.getXPath(); 123 if(widgetId==null) 124 widgetId = otherBinding.getId(); 125 if(bindingInstance==null) 126 bindingInstance = otherBinding.getWrappedBinding(); 127 } 128 129 CustomJXPathBinding customBinding = 130 new CustomJXPathBinding(commonAtts, widgetId, xpath, (AbstractCustomBinding)bindingInstance); 131 return customBinding; 132 } catch (BindingException e) { 133 throw e; 134 } catch (Exception e) { 135 throw new BindingException("Error building custom binding defined at " + DomHelper.getLocation(bindingElm), e); 136 } 137 } 138 } 139 | Popular Tags |