1 16 package org.apache.cocoon.forms.binding; 17 18 import java.util.Map ; 19 20 import org.apache.avalon.framework.logger.LogEnabled; 21 import org.apache.avalon.framework.logger.Logger; 22 import org.apache.cocoon.forms.util.DomHelper; 23 import org.apache.commons.lang.BooleanUtils; 24 import org.w3c.dom.Element ; 25 26 40 public abstract class JXPathBindingBuilderBase implements LogEnabled { 41 42 private Logger logger; 43 44 47 public void enableLogging(Logger logger) { 48 this.logger = logger; 49 if (logger.isDebugEnabled()) { 50 logger.debug("JXPathBindingBuilderBase got logger..."); 51 } 52 } 53 54 55 59 protected Logger getLogger() { 60 return this.logger; 61 } 62 63 72 public abstract JXPathBindingBase buildBinding( 73 Element bindingElm, 74 JXPathBindingManager.Assistant assistant) throws BindingException; 75 76 99 protected static CommonAttributes getCommonAttributes(Element bindingElm) throws BindingException { 100 try { 101 String location = DomHelper.getLocation(bindingElm); 102 if (DomHelper.getAttributeAsBoolean(bindingElm, "readonly", false)) { 105 throw new BindingException("Error in binding file " + location 106 + "\nThe usage of the attribute @readonly has been deprecated in favour of @direction."); 107 } 108 if (DomHelper.getAttributeAsBoolean(bindingElm, "read-only", false)) { 109 throw new BindingException("Error in binding file " + location 110 + "\nThe usage of the attribute @read-only has been deprecated in favour of @direction."); 111 } 112 113 String direction = DomHelper.getAttribute(bindingElm, "direction", "both"); 114 115 String leniency = DomHelper.getAttribute(bindingElm, "lenient", null); 116 117 Map nsDeclarationMap = DomHelper.getInheritedNSDeclarations(bindingElm); 126 if (nsDeclarationMap != null && nsDeclarationMap.values().contains(null)) 129 throw new BindingException("Error in binding file " + location 130 + "\nBinding doesn't support having namespace-declarations without explicit prefixes."); 131 132 return new CommonAttributes(location, direction, leniency, nsDeclarationMap); 133 } catch (BindingException e) { 134 throw e; 135 } catch (Exception e) { 136 throw new BindingException("Error building binding defined at " + DomHelper.getLocation(bindingElm), e); 137 } 138 } 139 140 public static CommonAttributes mergeCommonAttributes(CommonAttributes existing, CommonAttributes extra) { 141 142 if (extra == null) 143 return existing; 144 145 Boolean leniency = null; 146 if(existing.leniency==null) 147 leniency = extra.leniency; 148 else 149 leniency = existing.leniency; 150 151 String strLeniency = null; 152 if(leniency != null) 153 strLeniency = leniency.toString(); 154 155 String direction = existing.direction; 156 if(extra.direction!=null) direction = extra.direction; 158 159 160 return new CommonAttributes(extra.location,direction,strLeniency,extra.nsDeclarations); 161 } 162 163 168 protected static class CommonAttributes{ 169 170 173 String direction; 174 177 final String location; 178 181 final boolean loadEnabled; 182 185 final boolean saveEnabled; 186 190 final Boolean leniency; 191 194 final Map nsDeclarations; 195 196 final static CommonAttributes DEFAULT = new CommonAttributes("location unknown", true, true, null, null); 197 198 CommonAttributes(String location, String direction, String leniency, Map nsDeclarations){ 199 this(location, isLoadEnabled(direction), isSaveEnabled(direction), decideLeniency(leniency), nsDeclarations); 200 this.direction = direction; 201 } 202 203 CommonAttributes(String location, boolean loadEnabled, boolean saveEnabled, Boolean leniency, Map nsDeclarations){ 204 this.direction = null; 205 this.location = location; 206 this.loadEnabled = loadEnabled; 207 this.saveEnabled = saveEnabled; 208 this.leniency = leniency; 209 this.nsDeclarations = nsDeclarations; 210 } 211 212 217 private static boolean isLoadEnabled(String direction) { 218 return "both".equals(direction) || "load".equals(direction); 219 } 220 221 226 private static boolean isSaveEnabled(String direction) { 227 return "both".equals(direction) || "save".equals(direction); 228 } 229 230 231 237 private static Boolean decideLeniency(String leniency) { 238 return BooleanUtils.toBooleanObject(leniency); 239 } 240 241 } 242 } 243 | Popular Tags |