1 16 package org.apache.cocoon.forms.datatype; 17 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.avalon.framework.service.ServiceException; 20 import org.apache.avalon.framework.service.ServiceManager; 21 import org.apache.avalon.framework.service.Serviceable; 22 import org.apache.avalon.framework.context.Contextualizable; 23 import org.apache.avalon.framework.context.Context; 24 import org.apache.avalon.framework.context.ContextException; 25 import org.apache.avalon.framework.thread.ThreadSafe; 26 import org.apache.avalon.framework.component.Component; 27 28 import org.apache.cocoon.components.LifecycleHelper; 29 import org.apache.cocoon.forms.util.DomHelper; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.NamedNodeMap ; 32 import org.w3c.dom.Node ; 33 34 39 public class JavaSelectionListBuilder extends AbstractLogEnabled 40 implements SelectionListBuilder, 41 Contextualizable, Serviceable, ThreadSafe, Component { 42 43 46 private Context context; 47 48 51 private ServiceManager manager; 52 53 public void contextualize(Context context) throws ContextException { 54 this.context = context; 55 } 56 57 public void service(ServiceManager manager) throws ServiceException { 58 this.manager = manager; 59 } 60 61 public SelectionList build(Element selectionListElement, Datatype datatype) 62 throws Exception { 63 String className = DomHelper.getAttribute(selectionListElement, "class"); 64 boolean nullable = DomHelper.getAttributeAsBoolean(selectionListElement, "nullable", true); 65 66 try { 67 Class clasz = Class.forName(className); 68 if (JavaSelectionList.class.isAssignableFrom(clasz)) { 69 JavaSelectionList list = (JavaSelectionList) clasz.newInstance(); 70 LifecycleHelper.setupComponent(list, getLogger(), this.context, this.manager, null, true); 71 list.setDatatype(datatype); 72 list.setNullable(nullable); 73 74 NamedNodeMap attrs = selectionListElement.getAttributes(); 76 final int size = attrs.getLength(); 77 for (int i = 0; i < size; i++) { 78 final Node attr = attrs.item(i); 79 final String name = attr.getNodeName(); 80 list.setAttribute(name, attr.getNodeValue()); 81 } 82 83 return list; 84 } else { 85 getLogger().warn("Class " + className + " does not implement JavaSelectionList, returning empty selection list."); 86 return new StaticSelectionList(datatype); 87 } 88 } catch (Exception e) { 89 if (getLogger().isDebugEnabled()) { 90 getLogger().debug("Got exception in build, re-throwing", e); 91 } 92 throw e; 93 } 94 } 95 } 96 | Popular Tags |