KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > region > RegionManager


1 /*
2  * $Id: RegionManager.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2003 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  */

25 package org.ofbiz.webapp.region;
26
27 import java.net.URL JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.UtilValidate;
35 import org.ofbiz.base.util.UtilXml;
36 import org.ofbiz.base.util.cache.UtilCache;
37
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40
41 /**
42  * A class to manage the region cache and read a region XML file
43  *
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class RegionManager {
49     
50     public static final String JavaDoc module = RegionManager.class.getName();
51
52     protected static UtilCache regionCache = new UtilCache("webapp.Regions.Config", 0, 0);
53     
54     protected URL JavaDoc regionFile = null;
55     
56     public RegionManager(URL JavaDoc regionFile) {
57         this.regionFile = regionFile;
58         if (regionFile == null) throw new IllegalArgumentException JavaDoc("regionFile cannot be null");
59         
60         //This may seem a bit funny, but we want to keep it in the cache so that it can be reloaded easily
61
// Also note that we do not check to see if it is already there, in all cases we want to re-load the definition
62
regionCache.put(regionFile, readRegionXml(regionFile));
63     }
64     
65     public Map JavaDoc getRegions() {
66         Map JavaDoc regions = (Map JavaDoc) regionCache.get(regionFile);
67         if (regions == null) {
68             synchronized (this) {
69                 regions = (Map JavaDoc) regionCache.get(regionFile);
70                 if (regions == null) {
71                     if (Debug.verboseOn()) Debug.logVerbose("Regions not loaded for " + regionFile + ", loading now", module);
72                     regions = readRegionXml(regionFile);
73                     regionCache.put(regionFile, regions);
74                 }
75             }
76         }
77         return regions;
78     }
79
80     public Region getRegion(String JavaDoc regionName) {
81         if (regionFile == null) return null;
82         return (Region) getRegions().get(regionName);
83     }
84
85     public void putRegion(Region region) {
86         getRegions().put(region.getId(), region);
87     }
88
89     public Map JavaDoc readRegionXml(URL JavaDoc regionFile) {
90         Map JavaDoc regions = new HashMap JavaDoc();
91
92         Document JavaDoc document = null;
93
94         try {
95             document = UtilXml.readXmlDocument(regionFile, true);
96         } catch (java.io.IOException JavaDoc e) {
97             Debug.logError(e, module);
98         } catch (org.xml.sax.SAXException JavaDoc e) {
99             Debug.logError(e, module);
100         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
101             Debug.logError(e, module);
102         }
103
104         if (document == null) return regions;
105
106         Element JavaDoc rootElement = document.getDocumentElement();
107
108         List JavaDoc defineElements = UtilXml.childElementList(rootElement, "define");
109         Iterator JavaDoc defineIter = defineElements.iterator();
110
111         while (defineIter.hasNext()) {
112             Element JavaDoc defineElement = (Element JavaDoc) defineIter.next();
113
114             addRegion(defineElement, regions);
115         }
116
117         return regions;
118     }
119
120     protected void addRegion(Element JavaDoc defineElement, Map JavaDoc regions) {
121         Region newRegion = null;
122
123         String JavaDoc idAttr = defineElement.getAttribute("id");
124         String JavaDoc templateAttr = defineElement.getAttribute("template");
125         String JavaDoc regionAttr = defineElement.getAttribute("region");
126
127         if (UtilValidate.isNotEmpty(templateAttr) && UtilValidate.isNotEmpty(regionAttr)) {
128             throw new IllegalArgumentException JavaDoc("Cannot use both template and region attributes");
129         }
130
131         if (UtilValidate.isNotEmpty(templateAttr)) {
132             newRegion = new Region(idAttr, templateAttr, null);
133         } else {
134             if (UtilValidate.isNotEmpty(regionAttr)) {
135                 Region parentRegion = (Region) regions.get(regionAttr);
136
137                 if (parentRegion == null) {
138                     throw new IllegalArgumentException JavaDoc("can't find page definition attribute with this key: " + regionAttr);
139                 }
140                 newRegion = new Region(idAttr, parentRegion.getContent(), parentRegion.getSections());
141             } else {
142                 throw new IllegalArgumentException JavaDoc("Must specify either the template or the region attribute");
143             }
144         }
145
146         regions.put(idAttr, newRegion);
147
148         List JavaDoc putElements = UtilXml.childElementList(defineElement, "put");
149         Iterator JavaDoc putIter = putElements.iterator();
150
151         while (putIter.hasNext()) {
152             Element JavaDoc putElement = (Element JavaDoc) putIter.next();
153
154             newRegion.put(makeSection(putElement));
155         }
156     }
157
158     protected Section makeSection(Element JavaDoc putElement) {
159         String JavaDoc bodyContent = UtilXml.elementValue(putElement);
160         String JavaDoc section = putElement.getAttribute("section");
161         String JavaDoc info = putElement.getAttribute("info");
162         String JavaDoc content = putElement.getAttribute("content");
163         String JavaDoc type = putElement.getAttribute("type");
164
165         if (UtilValidate.isEmpty(type)) type = "default";
166
167         if (UtilValidate.isNotEmpty(bodyContent) && UtilValidate.isNotEmpty(content)) {
168             throw new IllegalArgumentException JavaDoc("Cannot use both content attribute and tag body text");
169         }
170
171         if (UtilValidate.isNotEmpty(bodyContent)) {
172             content = bodyContent;
173             type = "direct";
174         }
175
176         return new Section(section, info, content, type, this);
177     }
178
179     public static Region getRegion(URL JavaDoc regionFile, String JavaDoc regionName) {
180         if (regionFile == null) return null;
181         Map JavaDoc regions = (Map JavaDoc) regionCache.get(regionFile);
182         if (regions == null) return null;
183         return (Region) regions.get(regionName);
184     }
185 }
186
Popular Tags