KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > editor > PartEditorManager


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.frontend.editor;
17
18 import org.apache.excalibur.store.Store;
19 import org.apache.excalibur.source.Source;
20 import org.apache.excalibur.source.SourceResolver;
21 import org.apache.avalon.framework.service.ServiceManager;
22 import org.apache.avalon.framework.context.Context;
23 import org.apache.avalon.framework.logger.Logger;
24 import org.apache.cocoon.components.ContextHelper;
25 import org.apache.xmlbeans.XmlOptions;
26 import org.outerj.daisy.frontend.WikiHelper;
27 import org.outerj.daisy.frontend.util.CacheHelper;
28 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
29 import org.outerx.daisy.x10Parteditor.PartEditorDocument;
30
31 import java.io.InputStream JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Collections JavaDoc;
35
36 /**
37  * Manages the construction of {@link PartEditor}s for parts, based on configuration.
38  */

39 public class PartEditorManager {
40     private static final String JavaDoc STORE_PREFIX = PartEditorManager.class.getName();
41
42     public static PartEditor getPartEditor(String JavaDoc partTypeName, ServiceManager serviceManager, Context context, Logger logger) throws Exception JavaDoc {
43         SourceResolver sourceResolver = null;
44         Source source = null;
45         Store store = null;
46         try {
47             sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
48             source = sourceResolver.resolveURI("wikidata:/resources/parteditors/" + partTypeName + ".xml");
49             if (!source.exists()) {
50                 return null;
51             } else {
52                 store = (Store)serviceManager.lookup(Store.TRANSIENT_STORE);
53                 PartEditorInfo partEditorInfo = (PartEditorInfo)CacheHelper.getFromCache(store, source, STORE_PREFIX);
54                 if (partEditorInfo == null) {
55                     partEditorInfo = buildPartEditorInfo(source, logger);
56                     CacheHelper.setInCache(store, partEditorInfo, source, STORE_PREFIX);
57                 }
58
59                 if (partEditorInfo.isValid()) {
60                     return partEditorInfo.getBuilder().getPartEditor(partEditorInfo.getProperties(), serviceManager, context);
61                 }
62             }
63         } finally {
64             if (source != null)
65                 sourceResolver.release(source);
66             if (sourceResolver != null)
67                 serviceManager.release(sourceResolver);
68             if (store != null)
69                 serviceManager.release(store);
70         }
71         return null;
72     }
73
74     private static PartEditorInfo buildPartEditorInfo(Source source, Logger logger) {
75         PartEditorDocument partEditorDocument;
76         try {
77             InputStream JavaDoc is = null;
78             try {
79                 is = source.getInputStream();
80                 XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
81                 partEditorDocument = PartEditorDocument.Factory.parse(is, xmlOptions);
82             } finally {
83                 if (is != null)
84                     is.close();
85             }
86
87             String JavaDoc className = partEditorDocument.getPartEditor().getClass1();
88             if (className == null)
89                 throw new Exception JavaDoc("Missing class attribute for partEditor specification in " + source.getURI());
90
91             Class JavaDoc clazz;
92             try {
93                 clazz = PartEditorManager.class.getClassLoader().loadClass(className);
94             } catch (ClassNotFoundException JavaDoc e) {
95                 throw new Exception JavaDoc("Class for partEditor not found: " + className + " specified in " + source.getURI());
96             }
97
98             if (!PartEditorFactory.class.isAssignableFrom(clazz)) {
99                 throw new Exception JavaDoc("Class specified for part editor is not a " + PartEditorFactory.class.getName() + " at " + source.getURI());
100             }
101
102             PartEditorFactory factory = (PartEditorFactory)clazz.newInstance();
103
104             Map JavaDoc properties = new HashMap JavaDoc();
105             PartEditorDocument.PartEditor.Properties propertiesXml = partEditorDocument.getPartEditor().getProperties();
106             if (propertiesXml != null) {
107                 PartEditorDocument.PartEditor.Properties.Entry entries[] = propertiesXml.getEntryArray();
108                 for (int i = 0; i < entries.length; i++) {
109                     String JavaDoc key = entries[i].getKey();
110                     if (key != null) {
111                         properties.put(key, entries[i].getStringValue());
112                     }
113                 }
114             }
115
116             return new PartEditorInfo(factory, Collections.unmodifiableMap(properties), true);
117         } catch (Throwable JavaDoc e) {
118             logger.error("Error in part editor specification at " + source.getURI(), e);
119             return new PartEditorInfo(null, null, false);
120         }
121     }
122
123     static class PartEditorInfo {
124         private PartEditorFactory factory;
125         private Map JavaDoc properties;
126         private boolean valid;
127
128         public PartEditorInfo(PartEditorFactory factory, Map JavaDoc properties, boolean valid) {
129             this.factory = factory;
130             this.properties = properties;
131             this.valid = valid;
132
133             if (valid && (factory == null || properties == null)) {
134                 throw new IllegalArgumentException JavaDoc("if valid argument is true, the other arguments cannot be null");
135             }
136         }
137
138         public PartEditorFactory getBuilder() {
139             return factory;
140         }
141
142         public Map JavaDoc getProperties() {
143             return properties;
144         }
145
146         public boolean isValid() {
147             return valid;
148         }
149     }
150 }
151
Popular Tags