KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > SimpleMapProcessor


1 /*
2  * $Id: SimpleMapProcessor.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang;
25
26 import java.net.URL JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
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 JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39
40 /**
41  * SimpleMapProcessor Mini Language
42  *
43  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
44  * @version $Rev: 5462 $
45  * @since 2.0
46  */

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 JavaDoc xmlResource, String JavaDoc name, Map JavaDoc inMap, Map JavaDoc results, List JavaDoc messages, Locale JavaDoc locale) throws MiniLangException {
53         runSimpleMapProcessor(xmlResource, name, inMap, results, messages, locale, null);
54     }
55
56     public static void runSimpleMapProcessor(String JavaDoc xmlResource, String JavaDoc name, Map JavaDoc inMap, Map JavaDoc results, List JavaDoc messages, Locale JavaDoc locale, ClassLoader JavaDoc loader) throws MiniLangException {
57         if (loader == null)
58             loader = Thread.currentThread().getContextClassLoader();
59
60         Map JavaDoc 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 JavaDoc xmlURL, String JavaDoc name, Map JavaDoc inMap, Map JavaDoc results, List JavaDoc messages, Locale JavaDoc locale, ClassLoader JavaDoc loader) throws MiniLangException {
72         if (loader == null)
73             loader = Thread.currentThread().getContextClassLoader();
74
75         Map JavaDoc 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 JavaDoc getProcessors(String JavaDoc xmlResource, String JavaDoc name, ClassLoader JavaDoc loader) throws MiniLangException {
87         Map JavaDoc simpleMapProcessors = (Map JavaDoc) simpleMapProcessorsResourceCache.get(xmlResource);
88
89         if (simpleMapProcessors == null) {
90             synchronized (SimpleMapProcessor.class) {
91                 simpleMapProcessors = (Map JavaDoc) simpleMapProcessorsResourceCache.get(xmlResource);
92                 if (simpleMapProcessors == null) {
93                     URL JavaDoc 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                     // put it in the cache
101
simpleMapProcessorsResourceCache.put(xmlResource, simpleMapProcessors);
102                 }
103             }
104         }
105
106         return simpleMapProcessors;
107     }
108
109     protected static Map JavaDoc getProcessors(URL JavaDoc xmlURL, String JavaDoc name) throws MiniLangException {
110         Map JavaDoc simpleMapProcessors = (Map JavaDoc) simpleMapProcessorsURLCache.get(xmlURL);
111
112         if (simpleMapProcessors == null) {
113             synchronized (SimpleMapProcessor.class) {
114                 simpleMapProcessors = (Map JavaDoc) simpleMapProcessorsURLCache.get(xmlURL);
115                 if (simpleMapProcessors == null) {
116                     simpleMapProcessors = getAllProcessors(xmlURL);
117
118                     // put it in the cache
119
simpleMapProcessorsURLCache.put(xmlURL, simpleMapProcessors);
120                 }
121             }
122         }
123
124         return simpleMapProcessors;
125     }
126
127     protected static Map JavaDoc getAllProcessors(URL JavaDoc xmlURL) throws MiniLangException {
128         Map JavaDoc mapProcessors = new HashMap JavaDoc();
129
130         // read in the file
131
Document JavaDoc document = null;
132
133         try {
134             document = UtilXml.readXmlDocument(xmlURL, true);
135         } catch (java.io.IOException JavaDoc e) {
136             throw new MiniLangException("Could not read XML file", e);
137         } catch (org.xml.sax.SAXException JavaDoc e) {
138             throw new MiniLangException("Could not parse XML file", e);
139         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc 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 JavaDoc rootElement = document.getDocumentElement();
148         List JavaDoc simpleMapProcessorElements = UtilXml.childElementList(rootElement, "simple-map-processor");
149         Iterator JavaDoc strProcorIter = simpleMapProcessorElements.iterator();
150
151         while (strProcorIter.hasNext()) {
152             Element JavaDoc simpleMapProcessorElement = (Element JavaDoc) 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