KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > factory > xml > XmlFormRepository


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.factory.xml;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.activation.MimetypesFileTypeMap JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.riotfamily.common.image.ImageCropper;
37 import org.riotfamily.common.xml.BeanConfigurationWatcher;
38 import org.riotfamily.common.xml.ConfigurableBean;
39 import org.riotfamily.common.xml.ConfigurationEventListener;
40 import org.riotfamily.common.xml.DocumentReader;
41 import org.riotfamily.common.xml.ValidatingDocumentReader;
42 import org.riotfamily.forms.factory.AbstractFormRepository;
43 import org.riotfamily.forms.factory.FormFactory;
44 import org.springframework.beans.factory.BeanFactory;
45 import org.springframework.beans.factory.BeanFactoryAware;
46 import org.springframework.beans.factory.InitializingBean;
47 import org.springframework.core.io.Resource;
48
49 /**
50  *
51  */

52 public class XmlFormRepository extends AbstractFormRepository implements
53         BeanFactoryAware, InitializingBean, ConfigurableBean {
54
55     private Log log = LogFactory.getLog(XmlFormRepository.class);
56     
57     private List JavaDoc configLocations;
58     
59     private boolean reloadable = true;
60
61     private BeanConfigurationWatcher configWatcher =
62             new BeanConfigurationWatcher(this);
63
64     private XmlFormRepositoryDigester digester;
65     
66     private BeanFactory beanFactory;
67         
68     private Class JavaDoc defaultBeanClass;
69     
70     private MimetypesFileTypeMap JavaDoc mimetypesMap;
71     
72     private ImageCropper imageCropper;
73     
74     private HashMap JavaDoc customElements;
75     
76     private HashMap JavaDoc customElementsWithoutNS;
77     
78     
79     public void setCustomElements(Map JavaDoc elements)
80             throws ClassNotFoundException JavaDoc {
81         
82         customElements = new HashMap JavaDoc();
83         customElementsWithoutNS = new HashMap JavaDoc();
84         
85         Iterator JavaDoc it = elements.entrySet().iterator();
86         while (it.hasNext()) {
87             Map.Entry JavaDoc prop = (Map.Entry JavaDoc) it.next();
88             String JavaDoc className = (String JavaDoc) prop.getValue();
89             if (className != null) {
90                 String JavaDoc type = (String JavaDoc) prop.getKey();
91                 Class JavaDoc elementClass = Class.forName(className);
92                 customElements.put(type, elementClass);
93                 int i = type.indexOf('}');
94                 if (i != -1 && i < type.length() - 1) {
95                     type = type.substring(i + 1);
96                     customElementsWithoutNS.put(type, elementClass);
97                 }
98             }
99         }
100     }
101
102     public Class JavaDoc getElementClass(String JavaDoc type) {
103         if (customElements == null) {
104             return null;
105         }
106         Class JavaDoc elementClass = (Class JavaDoc) customElements.get(type);
107         if (elementClass == null) {
108             elementClass = (Class JavaDoc) customElementsWithoutNS.get(type);
109         }
110         return elementClass;
111     }
112     
113     public void setConfig(Resource config) {
114         setConfigLocations(new Resource[] { config });
115     }
116     
117     public void setConfigLocations(Resource[] configLocations) {
118         this.configLocations = new ArrayList JavaDoc();
119         if (configLocations != null) {
120             for (int i = 0; i < configLocations.length; i++) {
121                 this.configLocations.add(configLocations[i]);
122             }
123         }
124         configWatcher.setResources(this.configLocations);
125     }
126
127     public Class JavaDoc getDefaultBeanClass() {
128         return this.defaultBeanClass;
129     }
130
131     public void setDefaultBeanClass(Class JavaDoc defaultBeanClass) {
132         this.defaultBeanClass = defaultBeanClass;
133     }
134
135     public boolean isReloadable() {
136         return this.reloadable;
137     }
138
139     public void setReloadable(boolean reloadable) {
140         if (!reloadable) {
141             log.info("Modification checks have been disabled.");
142         }
143         this.reloadable = reloadable;
144     }
145     
146     public void addListener(ConfigurationEventListener listener) {
147         configWatcher.addListener(listener);
148     }
149
150     public void setBeanFactory(BeanFactory beanFactory) {
151         this.beanFactory = beanFactory;
152     }
153     
154     public void setMimetypesMap(MimetypesFileTypeMap JavaDoc mimetypesMap) {
155         this.mimetypesMap = mimetypesMap;
156     }
157     
158     public MimetypesFileTypeMap JavaDoc getMimetypesMap() {
159         return this.mimetypesMap;
160     }
161     
162     public ImageCropper getImageCropper() {
163         return this.imageCropper;
164     }
165
166     public void setImageCropper(ImageCropper imageCropper) {
167         this.imageCropper = imageCropper;
168     }
169
170     public final void afterPropertiesSet() throws Exception JavaDoc {
171         digester = new XmlFormRepositoryDigester(this, beanFactory);
172         configure();
173     }
174
175     public FormFactory getFormFactory(String JavaDoc id) {
176         configWatcher.checkForModifications();
177         return super.getFormFactory(id);
178     }
179     
180     public void configure() {
181         getFactories().clear();
182         Iterator JavaDoc it = configLocations.iterator();
183         while (it.hasNext()) {
184             Resource res = (Resource) it.next();
185             if (res.exists()) {
186                 DocumentReader reader = new ValidatingDocumentReader(res);
187                 digester.digest(reader.readDocument(), res);
188             }
189         }
190     }
191     
192 }
193
Popular Tags