001: /*
002: * $Id: RegionTag.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 Sun Microsystems Inc., published in "Advanced Java Server Pages" by Prentice Hall PTR
005: * Copyright (c) 2001-2002 The Open For Business Project - www.ofbiz.org
006: *
007: * Permission is hereby granted, free of charge, to any person obtaining a
008: * copy of this software and associated documentation files (the "Software"),
009: * to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
011: * and/or sell copies of the Software, and to permit persons to whom the
012: * Software is furnished to do so, subject to the following conditions:
013: *
014: * The above copyright notice and this permission notice shall be included
015: * in all copies or substantial portions of the Software.
016: *
017: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
018: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
019: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
020: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
021: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
022: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
023: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
024: *
025: */
026: package org.ofbiz.content.webapp.region;
027:
028: import java.net.URL;
029:
030: import javax.servlet.jsp.JspException;
031: import javax.servlet.jsp.tagext.TagSupport;
032:
033: /**
034: * Base tag for other region tags, uses "/WEB-INF/regions.xml" file
035: *
036: * @author David M. Geary in the book "Advanced Java Server Pages"
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.2 $
039: * @since 2.0
040: */
041: public class RegionTag extends TagSupport {
042: protected Region regionObj = null;
043: protected String template = null;
044: private String region = null;
045:
046: public void setTemplate(String template) {
047: this .template = template;
048: }
049:
050: public void setRegion(String region) {
051: this .region = region;
052: }
053:
054: protected boolean findRegionByKey() throws JspException {
055: URL regionFile = null;
056:
057: try {
058: regionFile = pageContext.getServletContext().getResource(
059: "/WEB-INF/regions.xml");
060: } catch (java.net.MalformedURLException e) {
061: throw new IllegalArgumentException(
062: "regions.xml file URL invalid: " + e.getMessage());
063: }
064:
065: if (region != null) {
066: regionObj = RegionManager.getRegion(regionFile, region);
067: if (regionObj == null) {
068: throw new JspException(
069: "can't find page definition attribute with this key: "
070: + region);
071: }
072: }
073: return regionObj != null;
074: }
075:
076: protected void createRegionFromTemplate(String id)
077: throws JspException {
078: if (template == null)
079: throw new JspException("can't find template");
080:
081: regionObj = new Region(id, template);
082: }
083:
084: protected void createRegionFromRegion(String id)
085: throws JspException {
086: findRegionByKey();
087:
088: if (regionObj == null)
089: return;
090:
091: // made from template and sections
092: regionObj = new Region(id, regionObj.getContent(), regionObj
093: .getSections());
094: }
095:
096: public void put(Section section) {
097: regionObj.put(section);
098: }
099:
100: public void release() {
101: super.release();
102: regionObj = null;
103: region = null;
104: template = null;
105: }
106: }
|