1 16 package org.apache.cocoon.forms.formmodel.library; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.CascadingException; 23 import org.apache.avalon.framework.service.ServiceException; 24 import org.apache.avalon.framework.service.ServiceSelector; 25 import org.apache.cocoon.forms.FormsConstants; 26 import org.apache.cocoon.forms.formmodel.WidgetDefinition; 27 import org.apache.cocoon.forms.formmodel.WidgetDefinitionBuilder; 28 import org.apache.cocoon.forms.formmodel.WidgetDefinitionBuilderContext; 29 import org.apache.cocoon.forms.util.DomHelper; 30 import org.apache.cocoon.util.location.LocationAttributes; 31 import org.apache.commons.lang.StringUtils; 32 33 import org.w3c.dom.Element ; 34 35 38 public class Library { 39 40 public static final String SEPARATOR = ":"; 41 42 43 protected ServiceSelector widgetDefinitionBuilderSelector; 45 46 protected LibraryManager manager = null; 48 49 protected Map definitions = new HashMap (); 51 protected Map inclusions = new HashMap (); 52 53 protected Object shared = new Object (); 55 56 protected String sourceURI = null; 57 protected WidgetDefinitionBuilderContext context; 58 59 public Library(LibraryManager lm) { 60 manager = lm; 61 context = new WidgetDefinitionBuilderContext(); 62 context.setLocalLibrary(this); 63 } 64 65 public void setSourceURI(String uri) { 66 sourceURI = uri; 67 } 68 public String getSourceURI() { 69 return sourceURI; 70 } 71 72 public void setWidgetDefinitionBuilderSelector(ServiceSelector selector) { 73 this.widgetDefinitionBuilderSelector = selector; 74 } 75 76 public boolean dependenciesHaveChanged() throws Exception { 77 78 Iterator it = this.inclusions.values().iterator(); 79 while(it.hasNext()) { 80 Dependency dep = (Dependency)it.next(); 81 if(!dep.isValid()) 82 return true; 83 } 84 85 return false; 86 } 87 88 96 public boolean includeAs(String key, String librarysource) 97 throws LibraryException 98 { 99 try { 100 if( (!inclusions.containsKey(key) || key.indexOf(SEPARATOR)>-1) 102 && manager.getLibrary(librarysource, sourceURI)!=null) { 103 inclusions.put(key,new Dependency(librarysource)); 104 return true; 105 } 106 return false; 107 } catch(Exception e) { 108 throw new LibraryException("Could not include library '"+librarysource+"'",e); 109 } 110 111 } 112 113 public WidgetDefinition getDefinition(String key) throws LibraryException { 114 115 String librarykey = null; 116 String definitionkey = key; 117 118 if(key.indexOf(SEPARATOR)>-1) { 119 String [] parts = StringUtils.split(key,SEPARATOR); 120 librarykey = parts[0]; 121 definitionkey = parts[1]; 122 for(int i=2; i<parts.length; i++) { 123 definitionkey += SEPARATOR+parts[i]; 124 } 125 } 126 127 if(librarykey!=null) { 128 if(inclusions.containsKey(librarykey)) { 129 try { 130 return manager.getLibrary(((Dependency)inclusions.get(librarykey)).dependencySourceURI, sourceURI).getDefinition(definitionkey); 131 } catch(Exception e) { 132 throw new LibraryException("Couldn't get Library key='"+librarykey+"' source='"+inclusions.get(librarykey)+"",e); 133 } 134 } else { 135 throw new LibraryException("Library '"+librarykey+"' does not exist! (lookup: '"+key+"')"); 136 } 137 } else { 138 return (WidgetDefinition)definitions.get(definitionkey); 139 } 140 } 141 142 public void buildLibrary(Element libraryElement) throws Exception { 143 sourceURI = LocationAttributes.getURI(libraryElement); 144 Element widgetsElement = DomHelper.getChildElement(libraryElement, FormsConstants.DEFINITION_NS, "widgets", true); 145 Element [] widgetElements = DomHelper.getChildElements(widgetsElement, FormsConstants.DEFINITION_NS); 147 for (int i = 0; i < widgetElements.length; i++) { 148 Element widgetElement = widgetElements[i]; 149 WidgetDefinition widgetDefinition = buildWidgetDefinition(widgetElement); 150 addDefinition(widgetDefinition); 151 } 152 } 153 154 public void addDefinition(WidgetDefinition definition) throws LibraryException { 155 if(definition == null) 156 return; 157 158 if(definitions.containsKey(definition.getId())) 159 throw new LibraryException("Library already contains a widget with this ID!"); 160 161 definitions.put(definition.getId(),definition); 162 manager.debug(this+": Put definition with id: "+definition.getId()); 163 } 164 165 protected WidgetDefinition buildWidgetDefinition(Element widgetDefinition) throws Exception { 166 String widgetName = widgetDefinition.getLocalName(); 167 WidgetDefinitionBuilder builder = null; 168 try { 169 builder = (WidgetDefinitionBuilder)widgetDefinitionBuilderSelector.select(widgetName); 170 } catch (ServiceException e) { 171 throw new CascadingException("Unknown kind of widget '" + widgetName + "' at " + 172 DomHelper.getLocation(widgetDefinition), e); 173 } 174 175 context.setSuperDefinition(null); 176 String extend = DomHelper.getAttribute(widgetDefinition, "extends", null); 177 178 if (extend != null) 179 context.setSuperDefinition(getDefinition(extend)); 180 181 182 return builder.buildWidgetDefinition(widgetDefinition,context); 183 } 184 185 186 192 public class Dependency { 193 194 private String dependencySourceURI; 195 private Object shared; 196 197 public Dependency(String dependencySourceURI) throws Exception { 198 this.dependencySourceURI = dependencySourceURI; 199 200 Library lib = manager.getLibrary(this.dependencySourceURI,sourceURI); 201 this.shared = lib.shared; 202 } 203 204 public boolean isValid() throws LibraryException { 205 try { 206 207 if(manager.libraryInCache(dependencySourceURI,sourceURI)) { 208 Library lib = manager.getLibrary(dependencySourceURI,sourceURI); 209 210 if(this.shared == lib.shared) 211 return true; 212 } 213 214 return false; 215 } catch(Exception forward) { 216 throw new LibraryException("Exception occured while checking dependency validity!",forward); 217 } 218 219 } 220 } 221 222 } 223 | Popular Tags |