001: /*
002: * $Id: SimpleMapProcessor.java,v 1.1 2003/08/17 06:06:13 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang;
025:
026: import java.net.URL;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.Locale;
031: import java.util.Map;
032:
033: import org.ofbiz.base.util.UtilCache;
034: import org.ofbiz.base.util.UtilURL;
035: import org.ofbiz.base.util.UtilXml;
036: import org.ofbiz.minilang.operation.MapProcessor;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039:
040: /**
041: * SimpleMapProcessor Mini Language
042: *
043: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
044: * @version $Revision: 1.1 $
045: * @since 2.0
046: */
047: public class SimpleMapProcessor {
048:
049: protected static UtilCache simpleMapProcessorsResourceCache = new UtilCache(
050: "minilang.SimpleMapProcessorsResource", 0, 0);
051: protected static UtilCache simpleMapProcessorsURLCache = new UtilCache(
052: "minilang.SimpleMapProcessorsURL", 0, 0);
053:
054: public static void runSimpleMapProcessor(String xmlResource,
055: String name, Map inMap, Map results, List messages,
056: Locale locale) throws MiniLangException {
057: runSimpleMapProcessor(xmlResource, name, inMap, results,
058: messages, locale, null);
059: }
060:
061: public static void runSimpleMapProcessor(String xmlResource,
062: String name, Map inMap, Map results, List messages,
063: Locale locale, ClassLoader loader) throws MiniLangException {
064: if (loader == null)
065: loader = Thread.currentThread().getContextClassLoader();
066:
067: Map mapProcessors = getProcessors(xmlResource, name, loader);
068: MapProcessor processor = (MapProcessor) mapProcessors.get(name);
069:
070: if (processor == null) {
071: throw new MiniLangException(
072: "Could not find SimpleMapProcessor named " + name
073: + " in XML document resource: "
074: + xmlResource);
075: }
076:
077: if (processor != null)
078: processor.exec(inMap, results, messages, locale, loader);
079: }
080:
081: public static void runSimpleMapProcessor(URL xmlURL, String name,
082: Map inMap, Map results, List messages, Locale locale,
083: ClassLoader loader) throws MiniLangException {
084: if (loader == null)
085: loader = Thread.currentThread().getContextClassLoader();
086:
087: Map mapProcessors = getProcessors(xmlURL, name);
088: MapProcessor processor = (MapProcessor) mapProcessors.get(name);
089:
090: if (processor == null) {
091: throw new MiniLangException(
092: "Could not find SimpleMapProcessor named " + name
093: + " in XML document: " + xmlURL.toString());
094: }
095:
096: if (processor != null)
097: processor.exec(inMap, results, messages, locale, loader);
098: }
099:
100: protected static Map getProcessors(String xmlResource, String name,
101: ClassLoader loader) throws MiniLangException {
102: Map simpleMapProcessors = (Map) simpleMapProcessorsResourceCache
103: .get(xmlResource);
104:
105: if (simpleMapProcessors == null) {
106: synchronized (SimpleMapProcessor.class) {
107: simpleMapProcessors = (Map) simpleMapProcessorsResourceCache
108: .get(xmlResource);
109: if (simpleMapProcessors == null) {
110: URL xmlURL = UtilURL.fromResource(xmlResource,
111: loader);
112:
113: if (xmlURL == null) {
114: throw new MiniLangException(
115: "Could not find SimpleMapProcessor XML document in resource: "
116: + xmlResource);
117: }
118: simpleMapProcessors = getAllProcessors(xmlURL);
119:
120: // put it in the cache
121: simpleMapProcessorsResourceCache.put(xmlResource,
122: simpleMapProcessors);
123: }
124: }
125: }
126:
127: return simpleMapProcessors;
128: }
129:
130: protected static Map getProcessors(URL xmlURL, String name)
131: throws MiniLangException {
132: Map simpleMapProcessors = (Map) simpleMapProcessorsURLCache
133: .get(xmlURL);
134:
135: if (simpleMapProcessors == null) {
136: synchronized (SimpleMapProcessor.class) {
137: simpleMapProcessors = (Map) simpleMapProcessorsURLCache
138: .get(xmlURL);
139: if (simpleMapProcessors == null) {
140: simpleMapProcessors = getAllProcessors(xmlURL);
141:
142: // put it in the cache
143: simpleMapProcessorsURLCache.put(xmlURL,
144: simpleMapProcessors);
145: }
146: }
147: }
148:
149: return simpleMapProcessors;
150: }
151:
152: protected static Map getAllProcessors(URL xmlURL)
153: throws MiniLangException {
154: Map mapProcessors = new HashMap();
155:
156: // read in the file
157: Document document = null;
158:
159: try {
160: document = UtilXml.readXmlDocument(xmlURL, true);
161: } catch (java.io.IOException e) {
162: throw new MiniLangException("Could not read XML file", e);
163: } catch (org.xml.sax.SAXException e) {
164: throw new MiniLangException("Could not parse XML file", e);
165: } catch (javax.xml.parsers.ParserConfigurationException e) {
166: throw new MiniLangException(
167: "XML parser not setup correctly", e);
168: }
169:
170: if (document == null) {
171: throw new MiniLangException(
172: "Could not find SimpleMapProcessor XML document: "
173: + xmlURL.toString());
174: }
175:
176: Element rootElement = document.getDocumentElement();
177: List simpleMapProcessorElements = UtilXml.childElementList(
178: rootElement, "simple-map-processor");
179: Iterator strProcorIter = simpleMapProcessorElements.iterator();
180:
181: while (strProcorIter.hasNext()) {
182: Element simpleMapProcessorElement = (Element) strProcorIter
183: .next();
184: MapProcessor processor = new MapProcessor(
185: simpleMapProcessorElement);
186:
187: mapProcessors.put(simpleMapProcessorElement
188: .getAttribute("name"), processor);
189: }
190:
191: return mapProcessors;
192: }
193: }
|