001: /*
002: * $Id: RegionManager.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
003: *
004: * Copyright (c) 2002-2003 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: */
025: package org.ofbiz.content.webapp.region;
026:
027: import java.net.URL;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032:
033: import org.ofbiz.base.util.Debug;
034: import org.ofbiz.base.util.UtilCache;
035: import org.ofbiz.base.util.UtilValidate;
036: import org.ofbiz.base.util.UtilXml;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039:
040: /**
041: * A class to manage the region cache and read a region XML file
042: *
043: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
044: * @version $Revision: 1.2 $
045: * @since 2.0
046: */
047: public class RegionManager {
048:
049: public static final String module = RegionManager.class.getName();
050:
051: protected static UtilCache regionCache = new UtilCache(
052: "webapp.Regions.Config", 0, 0);
053:
054: protected URL regionFile = null;
055:
056: public RegionManager(URL regionFile) {
057: this .regionFile = regionFile;
058: if (regionFile == null)
059: throw new IllegalArgumentException(
060: "regionFile cannot be null");
061:
062: //This may seem a bit funny, but we want to keep it in the cache so that it can be reloaded easily
063: // Also note that we do not check to see if it is already there, in all cases we want to re-load the definition
064: regionCache.put(regionFile, readRegionXml(regionFile));
065: }
066:
067: public Map getRegions() {
068: Map regions = (Map) regionCache.get(regionFile);
069: if (regions == null) {
070: synchronized (this ) {
071: regions = (Map) regionCache.get(regionFile);
072: if (regions == null) {
073: if (Debug.verboseOn())
074: Debug.logVerbose("Regions not loaded for "
075: + regionFile + ", loading now", module);
076: regions = readRegionXml(regionFile);
077: regionCache.put(regionFile, regions);
078: }
079: }
080: }
081: return regions;
082: }
083:
084: public Region getRegion(String regionName) {
085: if (regionFile == null)
086: return null;
087: return (Region) getRegions().get(regionName);
088: }
089:
090: public void putRegion(Region region) {
091: getRegions().put(region.getId(), region);
092: }
093:
094: public Map readRegionXml(URL regionFile) {
095: Map regions = new HashMap();
096:
097: Document document = null;
098:
099: try {
100: document = UtilXml.readXmlDocument(regionFile, true);
101: } catch (java.io.IOException e) {
102: Debug.logError(e, module);
103: } catch (org.xml.sax.SAXException e) {
104: Debug.logError(e, module);
105: } catch (javax.xml.parsers.ParserConfigurationException e) {
106: Debug.logError(e, module);
107: }
108:
109: if (document == null)
110: return regions;
111:
112: Element rootElement = document.getDocumentElement();
113:
114: List defineElements = UtilXml.childElementList(rootElement,
115: "define");
116: Iterator defineIter = defineElements.iterator();
117:
118: while (defineIter.hasNext()) {
119: Element defineElement = (Element) defineIter.next();
120:
121: addRegion(defineElement, regions);
122: }
123:
124: return regions;
125: }
126:
127: protected void addRegion(Element defineElement, Map regions) {
128: Region newRegion = null;
129:
130: String idAttr = defineElement.getAttribute("id");
131: String templateAttr = defineElement.getAttribute("template");
132: String regionAttr = defineElement.getAttribute("region");
133:
134: if (UtilValidate.isNotEmpty(templateAttr)
135: && UtilValidate.isNotEmpty(regionAttr)) {
136: throw new IllegalArgumentException(
137: "Cannot use both template and region attributes");
138: }
139:
140: if (UtilValidate.isNotEmpty(templateAttr)) {
141: newRegion = new Region(idAttr, templateAttr, null);
142: } else {
143: if (UtilValidate.isNotEmpty(regionAttr)) {
144: Region parentRegion = (Region) regions.get(regionAttr);
145:
146: if (parentRegion == null) {
147: throw new IllegalArgumentException(
148: "can't find page definition attribute with this key: "
149: + regionAttr);
150: }
151: newRegion = new Region(idAttr, parentRegion
152: .getContent(), parentRegion.getSections());
153: } else {
154: throw new IllegalArgumentException(
155: "Must specify either the template or the region attribute");
156: }
157: }
158:
159: regions.put(idAttr, newRegion);
160:
161: List putElements = UtilXml.childElementList(defineElement,
162: "put");
163: Iterator putIter = putElements.iterator();
164:
165: while (putIter.hasNext()) {
166: Element putElement = (Element) putIter.next();
167:
168: newRegion.put(makeSection(putElement));
169: }
170: }
171:
172: protected Section makeSection(Element putElement) {
173: String bodyContent = UtilXml.elementValue(putElement);
174: String section = putElement.getAttribute("section");
175: String info = putElement.getAttribute("info");
176: String content = putElement.getAttribute("content");
177: String type = putElement.getAttribute("type");
178:
179: if (UtilValidate.isEmpty(type))
180: type = "default";
181:
182: if (UtilValidate.isNotEmpty(bodyContent)
183: && UtilValidate.isNotEmpty(content)) {
184: throw new IllegalArgumentException(
185: "Cannot use both content attribute and tag body text");
186: }
187:
188: if (UtilValidate.isNotEmpty(bodyContent)) {
189: content = bodyContent;
190: type = "direct";
191: }
192:
193: return new Section(section, info, content, type, this );
194: }
195:
196: public static Region getRegion(URL regionFile, String regionName) {
197: if (regionFile == null)
198: return null;
199: Map regions = (Map) regionCache.get(regionFile);
200: if (regions == null)
201: return null;
202: return (Region) regions.get(regionName);
203: }
204: }
|