1 16 package org.apache.cocoon.forms.binding; 17 18 import org.apache.avalon.framework.CascadingRuntimeException; 19 import org.apache.cocoon.forms.binding.JXPathBindingBuilderBase.CommonAttributes; 20 import org.apache.cocoon.forms.formmodel.Widget; 21 import org.apache.cocoon.util.ClassUtils; 22 import org.apache.commons.jxpath.AbstractFactory; 23 import org.apache.commons.jxpath.JXPathContext; 24 import org.apache.commons.jxpath.Pointer; 25 26 33 public class ContextJXPathBinding extends ComposedJXPathBindingBase { 34 35 38 private final String xpath; 39 40 44 private AbstractFactory factory; 45 46 49 public ContextJXPathBinding(CommonAttributes commonAtts, String contextPath, JXPathBindingBase[] childBindings) { 50 super(commonAtts, childBindings); 51 this.xpath = contextPath; 52 } 53 54 57 public ContextJXPathBinding(JXPathBindingBuilderBase.CommonAttributes commonAtts, String contextPath, String factoryClassName, JXPathBindingBase[] childBindings) { 58 super(commonAtts, childBindings); 59 this.xpath = contextPath; 60 if (factoryClassName != null) { 61 try { 62 this.factory = (AbstractFactory) ClassUtils.newInstance(factoryClassName); 63 } catch (Exception e) { 64 throw new CascadingRuntimeException("Cannot create an instance of " + factoryClassName, e); 65 } 66 } 67 } 68 69 public String getFactoryClassName() { 70 return this.factory == null ? null : this.factory.getClass().getName(); 71 } 72 73 77 public void doLoad(Widget frmModel, JXPathContext jxpc) throws BindingException { 78 Pointer ptr = jxpc.getPointer(this.xpath); 79 if (ptr.getNode() != null) { 80 JXPathContext subContext = jxpc.getRelativeContext(ptr); 81 super.doLoad(frmModel, subContext); 82 if (getLogger().isDebugEnabled()) 83 getLogger().debug("done loading " + toString()); 84 } else { 85 if (getLogger().isDebugEnabled()) { 86 getLogger().debug("non-existent path: skipping " + toString()); 87 } 88 } 89 } 90 91 95 public void doSave(Widget frmModel, JXPathContext jxpc) throws BindingException { 96 if (this.factory != null) { 97 jxpc.setFactory(this.factory); 98 } 99 Pointer ptr = jxpc.getPointer(this.xpath); 100 if (ptr.getNode() == null) { 101 jxpc.createPath(this.xpath); 102 ptr = jxpc.getPointer(this.xpath); 104 } 105 JXPathContext subContext = jxpc.getRelativeContext(ptr); 106 super.doSave(frmModel, subContext); 107 if (getLogger().isDebugEnabled()) { 108 getLogger().debug("done saving " + toString()); 109 } 110 } 111 112 113 public String getXPath() { 114 return this.xpath; 115 } 116 117 public String toString() { 118 return "ContextJXPathBinding [xpath=" + this.xpath + "]"; 119 } 120 } 121 | Popular Tags |