KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > binding > 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.binding.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.service.ServiceException;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.cocoon.forms.binding.Binding;
25 import org.apache.cocoon.forms.binding.BindingManager;
26 import org.apache.cocoon.forms.binding.JXPathBindingManager;
27 import org.apache.cocoon.forms.util.DomHelper;
28 import org.apache.cocoon.util.location.LocationAttributes;
29 import org.apache.commons.lang.StringUtils;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * @version $Id: Library.java 327169 2005-10-21 13:04:31Z sylvain $
34  *
35  */

36 public class Library {
37
38     public static final String JavaDoc SEPARATOR = ":";
39     
40     // own references
41
protected LibraryManager manager = null;
42     
43     // own instances
44
protected Map JavaDoc definitions = new HashMap JavaDoc();
45     protected Map JavaDoc inclusions = new HashMap JavaDoc();
46     
47     // shared object with dependencies
48
protected Object JavaDoc shared = new Object JavaDoc();
49     
50     protected String JavaDoc sourceURI = null;
51     protected JXPathBindingManager.Assistant assistant = null;
52     
53     public Library(ServiceManager sm) throws ServiceException {
54         manager = (LibraryManager)sm.lookup(LibraryManager.ROLE);
55     }
56     
57     public Library(LibraryManager lm) {
58         manager = lm;
59     }
60     
61     public void setAssistant(JXPathBindingManager.Assistant assistant) {
62         this.assistant = assistant;
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 boolean dependenciesHaveChanged() throws Exception JavaDoc {
73         
74         Iterator JavaDoc it = this.inclusions.values().iterator();
75         while(it.hasNext()) {
76             Dependency dep = (Dependency)it.next();
77             if(!dep.isValid())
78                 return true;
79         }
80         
81         return false;
82     }
83     
84     /**
85      * "Registers" a library to be referenced later under a certain key or prefix.
86      * Definitions will be accessible locally through prefixing: "prefix:definitionid"
87      *
88      * @param key the key
89      * @param librarysource the source of the library to be know as "key"
90      * @return true if there was no such key used before, false otherwise
91      */

92     public boolean includeAs(String JavaDoc key, String JavaDoc librarysource)
93         throws LibraryException
94     {
95         try {
96             // library keys may not contain ":"!
97
if( (!inclusions.containsKey(key) || key.indexOf(SEPARATOR)>-1)
98                     && manager.getLibrary(librarysource, sourceURI)!=null) {
99                 inclusions.put(key,new Dependency(librarysource));
100                 return true;
101             }
102             return false;
103         } catch(Exception JavaDoc e) {
104             throw new LibraryException("Could not include library '"+librarysource+"'",e);
105         }
106         
107     }
108     
109     public Binding getBinding(String JavaDoc key) throws LibraryException {
110         
111         String JavaDoc librarykey = null;
112         String JavaDoc definitionkey = key;
113         
114         if(key.indexOf(SEPARATOR)>-1) {
115             String JavaDoc[] parts = StringUtils.split(key,SEPARATOR);
116             librarykey = parts[0];
117             definitionkey = parts[1];
118             for(int i=2; i<parts.length; i++) {
119                 definitionkey += SEPARATOR+parts[i];
120             }
121         }
122         
123         if(librarykey!=null) {
124             if(inclusions.containsKey(librarykey)) {
125                 try {
126                     return manager.getLibrary(((Dependency)inclusions.get(librarykey)).dependencySourceURI, sourceURI).getBinding(definitionkey);
127                 } catch(Exception JavaDoc e) {
128                     throw new LibraryException("Couldn't get Library key='"+librarykey+"' source='"+inclusions.get(librarykey)+"",e);
129                 }
130             } else {
131                 throw new LibraryException("Library '"+librarykey+"' does not exist! (lookup: '"+key+"')");
132             }
133         } else {
134             return (Binding)definitions.get(definitionkey);
135         }
136     }
137     
138     public void buildLibrary(Element JavaDoc libraryElement) throws Exception JavaDoc {
139         sourceURI = LocationAttributes.getURI(libraryElement);
140         this.assistant.getContext().setLocalLibrary(this);
141         Element JavaDoc[] bindingElements = DomHelper.getChildElements(libraryElement, BindingManager.NAMESPACE);
142         for (int i = 0; i < bindingElements.length; i++) {
143             Element JavaDoc bindingElement = bindingElements[i];
144             Binding binding = this.assistant.getBindingForConfigurationElement(bindingElement);
145             addBinding(binding);
146         }
147     }
148     
149     public void addBinding(Binding binding) throws LibraryException {
150         if(definitions.containsKey(binding.getId()))
151             throw new LibraryException("Library already contains a binding with this ID!");
152         
153         definitions.put(binding.getId(),binding);
154         manager.debug(this+": Put binding with id: "+binding.getId());
155     }
156     
157     
158     /**
159      * Encapsulates a uri to designate an import plus a timestamp so previously reloaded
160      *
161      * @author Max Pfingsthorn (mpfingsthorn@hippo.nl)
162      *
163      */

164     public class Dependency {
165         
166         private String JavaDoc dependencySourceURI;
167         private Object JavaDoc shared;
168         
169         public Dependency(String JavaDoc dependencySourceURI) throws Exception JavaDoc {
170             this.dependencySourceURI = dependencySourceURI;
171             
172             Library lib = manager.getLibrary(this.dependencySourceURI,sourceURI);
173             this.shared = lib.shared;
174         }
175         
176         public boolean isValid() throws LibraryException {
177             try {
178                 
179                 if(manager.libraryInCache(dependencySourceURI,sourceURI)) {
180                     Library lib = manager.getLibrary(dependencySourceURI,sourceURI);
181                     
182                     if(this.shared == lib.shared)
183                         return true;
184                 }
185                 
186                 return false;
187             } catch(Exception JavaDoc forward) {
188                 throw new LibraryException("Exception occured while checking dependency validity!",forward);
189             }
190             
191         }
192     }
193     
194 }
195
Popular Tags