1 24 package org.ofbiz.minilang; 25 26 import java.net.URL ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 import org.ofbiz.base.util.UtilURL; 34 import org.ofbiz.base.util.UtilXml; 35 import org.ofbiz.base.util.cache.UtilCache; 36 import org.ofbiz.minilang.operation.MapProcessor; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 40 47 public class SimpleMapProcessor { 48 49 protected static UtilCache simpleMapProcessorsResourceCache = new UtilCache("minilang.SimpleMapProcessorsResource", 0, 0); 50 protected static UtilCache simpleMapProcessorsURLCache = new UtilCache("minilang.SimpleMapProcessorsURL", 0, 0); 51 52 public static void runSimpleMapProcessor(String xmlResource, String name, Map inMap, Map results, List messages, Locale locale) throws MiniLangException { 53 runSimpleMapProcessor(xmlResource, name, inMap, results, messages, locale, null); 54 } 55 56 public static void runSimpleMapProcessor(String xmlResource, String name, Map inMap, Map results, List messages, Locale locale, ClassLoader loader) throws MiniLangException { 57 if (loader == null) 58 loader = Thread.currentThread().getContextClassLoader(); 59 60 Map mapProcessors = getProcessors(xmlResource, name, loader); 61 MapProcessor processor = (MapProcessor) mapProcessors.get(name); 62 63 if (processor == null) { 64 throw new MiniLangException("Could not find SimpleMapProcessor named " + name + " in XML document resource: " + xmlResource); 65 } 66 67 if (processor != null) 68 processor.exec(inMap, results, messages, locale, loader); 69 } 70 71 public static void runSimpleMapProcessor(URL xmlURL, String name, Map inMap, Map results, List messages, Locale locale, ClassLoader loader) throws MiniLangException { 72 if (loader == null) 73 loader = Thread.currentThread().getContextClassLoader(); 74 75 Map mapProcessors = getProcessors(xmlURL, name); 76 MapProcessor processor = (MapProcessor) mapProcessors.get(name); 77 78 if (processor == null) { 79 throw new MiniLangException("Could not find SimpleMapProcessor named " + name + " in XML document: " + xmlURL.toString()); 80 } 81 82 if (processor != null) 83 processor.exec(inMap, results, messages, locale, loader); 84 } 85 86 protected static Map getProcessors(String xmlResource, String name, ClassLoader loader) throws MiniLangException { 87 Map simpleMapProcessors = (Map ) simpleMapProcessorsResourceCache.get(xmlResource); 88 89 if (simpleMapProcessors == null) { 90 synchronized (SimpleMapProcessor.class) { 91 simpleMapProcessors = (Map ) simpleMapProcessorsResourceCache.get(xmlResource); 92 if (simpleMapProcessors == null) { 93 URL xmlURL = UtilURL.fromResource(xmlResource, loader); 94 95 if (xmlURL == null) { 96 throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource); 97 } 98 simpleMapProcessors = getAllProcessors(xmlURL); 99 100 simpleMapProcessorsResourceCache.put(xmlResource, simpleMapProcessors); 102 } 103 } 104 } 105 106 return simpleMapProcessors; 107 } 108 109 protected static Map getProcessors(URL xmlURL, String name) throws MiniLangException { 110 Map simpleMapProcessors = (Map ) simpleMapProcessorsURLCache.get(xmlURL); 111 112 if (simpleMapProcessors == null) { 113 synchronized (SimpleMapProcessor.class) { 114 simpleMapProcessors = (Map ) simpleMapProcessorsURLCache.get(xmlURL); 115 if (simpleMapProcessors == null) { 116 simpleMapProcessors = getAllProcessors(xmlURL); 117 118 simpleMapProcessorsURLCache.put(xmlURL, simpleMapProcessors); 120 } 121 } 122 } 123 124 return simpleMapProcessors; 125 } 126 127 protected static Map getAllProcessors(URL xmlURL) throws MiniLangException { 128 Map mapProcessors = new HashMap (); 129 130 Document document = null; 132 133 try { 134 document = UtilXml.readXmlDocument(xmlURL, true); 135 } catch (java.io.IOException e) { 136 throw new MiniLangException("Could not read XML file", e); 137 } catch (org.xml.sax.SAXException e) { 138 throw new MiniLangException("Could not parse XML file", e); 139 } catch (javax.xml.parsers.ParserConfigurationException e) { 140 throw new MiniLangException("XML parser not setup correctly", e); 141 } 142 143 if (document == null) { 144 throw new MiniLangException("Could not find SimpleMapProcessor XML document: " + xmlURL.toString()); 145 } 146 147 Element rootElement = document.getDocumentElement(); 148 List simpleMapProcessorElements = UtilXml.childElementList(rootElement, "simple-map-processor"); 149 Iterator strProcorIter = simpleMapProcessorElements.iterator(); 150 151 while (strProcorIter.hasNext()) { 152 Element simpleMapProcessorElement = (Element ) strProcorIter.next(); 153 MapProcessor processor = new MapProcessor(simpleMapProcessorElement); 154 155 mapProcessors.put(simpleMapProcessorElement.getAttribute("name"), processor); 156 } 157 158 return mapProcessors; 159 } 160 } 161 | Popular Tags |