KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > library > Library


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel.library;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
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 JavaDoc;
34
35 /**
36  * @version $Id: Library.java 327169 2005-10-21 13:04:31Z sylvain $
37  */

38 public class Library {
39
40     public static final String JavaDoc SEPARATOR = ":";
41     
42     
43     // managed instances
44
protected ServiceSelector widgetDefinitionBuilderSelector;
45     
46     // own references
47
protected LibraryManager manager = null;
48     
49     // own instances
50
protected Map JavaDoc definitions = new HashMap JavaDoc();
51     protected Map JavaDoc inclusions = new HashMap JavaDoc();
52     
53     // shared object with dependencies
54
protected Object JavaDoc shared = new Object JavaDoc();
55     
56     protected String JavaDoc 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 JavaDoc uri) {
66         sourceURI = uri;
67     }
68     public String JavaDoc getSourceURI() {
69         return sourceURI;
70     }
71     
72     public void setWidgetDefinitionBuilderSelector(ServiceSelector selector) {
73         this.widgetDefinitionBuilderSelector = selector;
74     }
75     
76     public boolean dependenciesHaveChanged() throws Exception JavaDoc {
77         
78         Iterator JavaDoc 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     /**
89      * "Registers" a library to be referenced later under a certain key or prefix.
90      * Definitions will be accessible locally through prefixing: "prefix:definitionid"
91      *
92      * @param key the key
93      * @param librarysource the source of the library to be know as "key"
94      * @return true if there was no such key used before, false otherwise
95      */

96     public boolean includeAs(String JavaDoc key, String JavaDoc librarysource)
97         throws LibraryException
98     {
99         try {
100             // library keys may not contain ":"!
101
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 JavaDoc e) {
108             throw new LibraryException("Could not include library '"+librarysource+"'",e);
109         }
110         
111     }
112     
113     public WidgetDefinition getDefinition(String JavaDoc key) throws LibraryException {
114         
115         String JavaDoc librarykey = null;
116         String JavaDoc definitionkey = key;
117         
118         if(key.indexOf(SEPARATOR)>-1) {
119             String JavaDoc[] 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 JavaDoc 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 JavaDoc libraryElement) throws Exception JavaDoc {
143         sourceURI = LocationAttributes.getURI(libraryElement);
144         Element JavaDoc widgetsElement = DomHelper.getChildElement(libraryElement, FormsConstants.DEFINITION_NS, "widgets", true);
145         // All child elements of the widgets element are widgets
146
Element JavaDoc[] widgetElements = DomHelper.getChildElements(widgetsElement, FormsConstants.DEFINITION_NS);
147         for (int i = 0; i < widgetElements.length; i++) {
148             Element JavaDoc 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 JavaDoc widgetDefinition) throws Exception JavaDoc {
166         String JavaDoc 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 JavaDoc 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     /**
187      * Encapsulates a uri to designate an import plus a timestamp so previously reloaded
188      *
189      * @author Max Pfingsthorn (mpfingsthorn@hippo.nl)
190      *
191      */

192     public class Dependency {
193         
194         private String JavaDoc dependencySourceURI;
195         private Object JavaDoc shared;
196         
197         public Dependency(String JavaDoc dependencySourceURI) throws Exception JavaDoc {
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 JavaDoc forward) {
216                 throw new LibraryException("Exception occured while checking dependency validity!",forward);
217             }
218             
219         }
220     }
221     
222 }
223
Popular Tags