1 25 package org.ofbiz.webapp.region; 26 27 import java.net.URL ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 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 ; 39 import org.w3c.dom.Element ; 40 41 48 public class RegionManager { 49 50 public static final String module = RegionManager.class.getName(); 51 52 protected static UtilCache regionCache = new UtilCache("webapp.Regions.Config", 0, 0); 53 54 protected URL regionFile = null; 55 56 public RegionManager(URL regionFile) { 57 this.regionFile = regionFile; 58 if (regionFile == null) throw new IllegalArgumentException ("regionFile cannot be null"); 59 60 regionCache.put(regionFile, readRegionXml(regionFile)); 63 } 64 65 public Map getRegions() { 66 Map regions = (Map ) regionCache.get(regionFile); 67 if (regions == null) { 68 synchronized (this) { 69 regions = (Map ) 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 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 readRegionXml(URL regionFile) { 90 Map regions = new HashMap (); 91 92 Document document = null; 93 94 try { 95 document = UtilXml.readXmlDocument(regionFile, true); 96 } catch (java.io.IOException e) { 97 Debug.logError(e, module); 98 } catch (org.xml.sax.SAXException e) { 99 Debug.logError(e, module); 100 } catch (javax.xml.parsers.ParserConfigurationException e) { 101 Debug.logError(e, module); 102 } 103 104 if (document == null) return regions; 105 106 Element rootElement = document.getDocumentElement(); 107 108 List defineElements = UtilXml.childElementList(rootElement, "define"); 109 Iterator defineIter = defineElements.iterator(); 110 111 while (defineIter.hasNext()) { 112 Element defineElement = (Element ) defineIter.next(); 113 114 addRegion(defineElement, regions); 115 } 116 117 return regions; 118 } 119 120 protected void addRegion(Element defineElement, Map regions) { 121 Region newRegion = null; 122 123 String idAttr = defineElement.getAttribute("id"); 124 String templateAttr = defineElement.getAttribute("template"); 125 String regionAttr = defineElement.getAttribute("region"); 126 127 if (UtilValidate.isNotEmpty(templateAttr) && UtilValidate.isNotEmpty(regionAttr)) { 128 throw new IllegalArgumentException ("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 ("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 ("Must specify either the template or the region attribute"); 143 } 144 } 145 146 regions.put(idAttr, newRegion); 147 148 List putElements = UtilXml.childElementList(defineElement, "put"); 149 Iterator putIter = putElements.iterator(); 150 151 while (putIter.hasNext()) { 152 Element putElement = (Element ) putIter.next(); 153 154 newRegion.put(makeSection(putElement)); 155 } 156 } 157 158 protected Section makeSection(Element putElement) { 159 String bodyContent = UtilXml.elementValue(putElement); 160 String section = putElement.getAttribute("section"); 161 String info = putElement.getAttribute("info"); 162 String content = putElement.getAttribute("content"); 163 String 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 ("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 regionFile, String regionName) { 180 if (regionFile == null) return null; 181 Map regions = (Map ) regionCache.get(regionFile); 182 if (regions == null) return null; 183 return (Region) regions.get(regionName); 184 } 185 } 186 | Popular Tags |