1 16 package org.apache.cocoon.woody.binding; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.logger.LogEnabled; 22 import org.apache.avalon.framework.logger.Logger; 23 import org.apache.cocoon.util.jxpath.DOMFactory; 24 import org.apache.cocoon.woody.formmodel.Widget; 25 import org.apache.commons.jxpath.JXPathContext; 26 import org.w3c.dom.Node ; 27 28 35 public abstract class JXPathBindingBase implements Binding, LogEnabled { 36 37 40 private Logger logger; 41 42 45 private final JXPathBindingBuilderBase.CommonAttributes commonAtts; 46 47 50 protected Binding parent; 51 52 55 protected Map classes; 56 57 private JXPathBindingBase() { 58 this(JXPathBindingBuilderBase.CommonAttributes.DEFAULT); 59 } 60 61 protected JXPathBindingBase( 62 JXPathBindingBuilderBase.CommonAttributes commonAtts) { 63 this.commonAtts = commonAtts; 64 } 65 66 69 public void setParent(Binding binding) { 70 this.parent = binding; 71 } 72 73 76 public String getId() { 77 return null; 78 } 79 80 public Binding getClass(String id) { 81 Binding classBinding = null; 82 if (classes != null) { 83 classBinding = (Binding)classes.get(id); 85 } 86 if (classBinding == null) { 87 if (parent != null) { 89 classBinding = parent.getClass(id); 90 if (classes == null) { 92 classes = new HashMap (); 93 } 94 classes.put(id, classBinding); 95 } else { 96 throw new RuntimeException ("Class \"" + id + "\" not found."); 98 } 99 } 100 return classBinding; 101 } 102 103 protected Widget getWidget(Widget widget, String id) { 104 Widget childWidget = widget.getWidget(id); 105 if (childWidget != null) { 106 return childWidget; 107 } else { 108 throw new RuntimeException (getClass().getName() + ": Widget \"" + 109 id + "\" does not exist in container \"" + 110 widget.getFullyQualifiedId() + "\" (" + 111 widget.getLocation() + ")."); 112 } 113 } 114 115 119 public abstract void doLoad(Widget frmModel, JXPathContext jxpc) 120 throws BindingException; 121 122 128 public final void loadFormFromModel(Widget frmModel, JXPathContext jxpc) 129 throws BindingException { 130 boolean inheritedLeniency = jxpc.isLenient(); 131 applyLeniency(jxpc); 132 if (this.commonAtts.loadEnabled) { 133 doLoad(frmModel, jxpc); 134 } 135 jxpc.setLenient(inheritedLeniency); 136 } 137 138 143 public final void loadFormFromModel(Widget frmModel, Object objModel) 144 throws BindingException { 145 if (objModel != null) { 146 JXPathContext jxpc = makeJXPathContext(objModel); 147 loadFormFromModel(frmModel, jxpc); 148 } else { 149 throw new NullPointerException ( 150 "null object passed to loadFormFromModel() method"); 151 } 152 } 153 154 158 public abstract void doSave(Widget frmModel, JXPathContext jxpc) 159 throws BindingException; 160 161 167 public final void saveFormToModel(Widget frmModel, JXPathContext jxpc) 168 throws BindingException{ 169 boolean inheritedLeniency = jxpc.isLenient(); 170 applyLeniency(jxpc); 171 if (this.commonAtts.saveEnabled) { 172 doSave(frmModel, jxpc); 173 } 174 jxpc.setLenient(inheritedLeniency); 175 } 176 177 182 public void saveFormToModel(Widget frmModel, Object objModel) 183 throws BindingException { 184 if (objModel != null) { 185 JXPathContext jxpc = makeJXPathContext(objModel); 186 saveFormToModel(frmModel, jxpc); 187 } else { 188 throw new NullPointerException ( 189 "null object passed to saveFormToModel() method"); 190 } 191 } 192 193 private void applyLeniency(JXPathContext jxpc) { 194 if (this.commonAtts.leniency != null) { 195 jxpc.setLenient(this.commonAtts.leniency.booleanValue()); 196 } 197 } 198 199 private JXPathContext makeJXPathContext(Object objModel) { 200 JXPathContext jxpc; 201 if (!(objModel instanceof JXPathContext)) { 202 jxpc = JXPathContext.newContext(objModel); 203 jxpc.setLenient(true); 204 if (objModel instanceof Node ) { 205 jxpc.setFactory(new DOMFactory()); 206 } 207 } else { 208 jxpc = (JXPathContext) objModel; 209 } 210 return jxpc; 211 } 212 213 218 public void enableLogging(Logger logger) { 219 this.logger = logger; 220 } 221 222 protected Logger getLogger() { 223 return logger; 224 } 225 } 226 | Popular Tags |