1 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 ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.Collections ; 35 36 39 public class PartEditorManager { 40 private static final String STORE_PREFIX = PartEditorManager.class.getName(); 41 42 public static PartEditor getPartEditor(String partTypeName, ServiceManager serviceManager, Context context, Logger logger) throws Exception { 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 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 className = partEditorDocument.getPartEditor().getClass1(); 88 if (className == null) 89 throw new Exception ("Missing class attribute for partEditor specification in " + source.getURI()); 90 91 Class clazz; 92 try { 93 clazz = PartEditorManager.class.getClassLoader().loadClass(className); 94 } catch (ClassNotFoundException e) { 95 throw new Exception ("Class for partEditor not found: " + className + " specified in " + source.getURI()); 96 } 97 98 if (!PartEditorFactory.class.isAssignableFrom(clazz)) { 99 throw new Exception ("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 properties = new HashMap (); 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 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 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 properties; 126 private boolean valid; 127 128 public PartEditorInfo(PartEditorFactory factory, Map 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 ("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 getProperties() { 143 return properties; 144 } 145 146 public boolean isValid() { 147 return valid; 148 } 149 } 150 } 151 | Popular Tags |