001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang;
019:
020: import java.net.URL;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Locale;
025: import java.util.Map;
026:
027: import org.ofbiz.base.util.UtilURL;
028: import org.ofbiz.base.util.UtilXml;
029: import org.ofbiz.base.util.cache.UtilCache;
030: import org.ofbiz.minilang.operation.MapProcessor;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033:
034: /**
035: * SimpleMapProcessor Mini Language
036: */
037: public class SimpleMapProcessor {
038:
039: protected static UtilCache simpleMapProcessorsResourceCache = new UtilCache(
040: "minilang.SimpleMapProcessorsResource", 0, 0);
041: protected static UtilCache simpleMapProcessorsURLCache = new UtilCache(
042: "minilang.SimpleMapProcessorsURL", 0, 0);
043:
044: public static void runSimpleMapProcessor(String xmlResource,
045: String name, Map inMap, Map results, List messages,
046: Locale locale) throws MiniLangException {
047: runSimpleMapProcessor(xmlResource, name, inMap, results,
048: messages, locale, null);
049: }
050:
051: public static void runSimpleMapProcessor(String xmlResource,
052: String name, Map inMap, Map results, List messages,
053: Locale locale, ClassLoader loader) throws MiniLangException {
054: if (loader == null)
055: loader = Thread.currentThread().getContextClassLoader();
056:
057: Map mapProcessors = getProcessors(xmlResource, name, loader);
058: MapProcessor processor = (MapProcessor) mapProcessors.get(name);
059:
060: if (processor == null) {
061: throw new MiniLangException(
062: "Could not find SimpleMapProcessor named " + name
063: + " in XML document resource: "
064: + xmlResource);
065: }
066:
067: if (processor != null)
068: processor.exec(inMap, results, messages, locale, loader);
069: }
070:
071: public static void runSimpleMapProcessor(URL xmlURL, String name,
072: Map inMap, Map results, List messages, Locale locale,
073: ClassLoader loader) throws MiniLangException {
074: if (loader == null)
075: loader = Thread.currentThread().getContextClassLoader();
076:
077: Map mapProcessors = getProcessors(xmlURL, name);
078: MapProcessor processor = (MapProcessor) mapProcessors.get(name);
079:
080: if (processor == null) {
081: throw new MiniLangException(
082: "Could not find SimpleMapProcessor named " + name
083: + " in XML document: " + xmlURL.toString());
084: }
085:
086: if (processor != null)
087: processor.exec(inMap, results, messages, locale, loader);
088: }
089:
090: protected static Map getProcessors(String xmlResource, String name,
091: ClassLoader loader) throws MiniLangException {
092: Map simpleMapProcessors = (Map) simpleMapProcessorsResourceCache
093: .get(xmlResource);
094:
095: if (simpleMapProcessors == null) {
096: synchronized (SimpleMapProcessor.class) {
097: simpleMapProcessors = (Map) simpleMapProcessorsResourceCache
098: .get(xmlResource);
099: if (simpleMapProcessors == null) {
100: URL xmlURL = UtilURL.fromResource(xmlResource,
101: loader);
102:
103: if (xmlURL == null) {
104: throw new MiniLangException(
105: "Could not find SimpleMapProcessor XML document in resource: "
106: + xmlResource);
107: }
108: simpleMapProcessors = getAllProcessors(xmlURL);
109:
110: // put it in the cache
111: simpleMapProcessorsResourceCache.put(xmlResource,
112: simpleMapProcessors);
113: }
114: }
115: }
116:
117: return simpleMapProcessors;
118: }
119:
120: protected static Map getProcessors(URL xmlURL, String name)
121: throws MiniLangException {
122: Map simpleMapProcessors = (Map) simpleMapProcessorsURLCache
123: .get(xmlURL);
124:
125: if (simpleMapProcessors == null) {
126: synchronized (SimpleMapProcessor.class) {
127: simpleMapProcessors = (Map) simpleMapProcessorsURLCache
128: .get(xmlURL);
129: if (simpleMapProcessors == null) {
130: simpleMapProcessors = getAllProcessors(xmlURL);
131:
132: // put it in the cache
133: simpleMapProcessorsURLCache.put(xmlURL,
134: simpleMapProcessors);
135: }
136: }
137: }
138:
139: return simpleMapProcessors;
140: }
141:
142: protected static Map getAllProcessors(URL xmlURL)
143: throws MiniLangException {
144: Map mapProcessors = new HashMap();
145:
146: // read in the file
147: Document document = null;
148:
149: try {
150: document = UtilXml.readXmlDocument(xmlURL, true);
151: } catch (java.io.IOException e) {
152: throw new MiniLangException("Could not read XML file", e);
153: } catch (org.xml.sax.SAXException e) {
154: throw new MiniLangException("Could not parse XML file", e);
155: } catch (javax.xml.parsers.ParserConfigurationException e) {
156: throw new MiniLangException(
157: "XML parser not setup correctly", e);
158: }
159:
160: if (document == null) {
161: throw new MiniLangException(
162: "Could not find SimpleMapProcessor XML document: "
163: + xmlURL.toString());
164: }
165:
166: Element rootElement = document.getDocumentElement();
167: List simpleMapProcessorElements = UtilXml.childElementList(
168: rootElement, "simple-map-processor");
169: Iterator strProcorIter = simpleMapProcessorElements.iterator();
170:
171: while (strProcorIter.hasNext()) {
172: Element simpleMapProcessorElement = (Element) strProcorIter
173: .next();
174: MapProcessor processor = new MapProcessor(
175: simpleMapProcessorElement);
176:
177: mapProcessors.put(simpleMapProcessorElement
178: .getAttribute("name"), processor);
179: }
180:
181: return mapProcessors;
182: }
183: }
|