001: /*
002: * (C) Copyright 2005 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.renderprocessor;
022:
023: import java.io.IOException;
024:
025: import javax.portlet.PortletException;
026: import javax.portlet.RenderRequest;
027: import javax.portlet.RenderResponse;
028:
029: import org.w3c.dom.Element;
030:
031: import com.nabhinc.condition.Condition;
032: import com.nabhinc.condition.ConditionFactory;
033: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
034: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
035: import com.nabhinc.portlet.mvcportlet.core.RenderConfig;
036: import com.nabhinc.portlet.mvcportlet.core.RenderProcessor;
037: import com.nabhinc.util.XMLUtil;
038:
039: /**
040: * To be used as a switch.
041: * Each branch in the switch is specified via a pre-condition and a label.
042: *
043: * @author Padmanabh Dabke
044: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
045: * @since MVCPortlet 0.9.4
046: */
047: public class Switch extends BaseRequestProcessor implements
048: RenderProcessor {
049:
050: private String[] sResults = null;
051: private Condition[] sConditions = null;
052:
053: /* (non-Javadoc)
054: * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
055: */
056: public String process(RenderRequest request,
057: RenderResponse response, RenderConfig renderConfig)
058: throws PortletException, IOException {
059: try {
060: for (int i = 0; i < sConditions.length; i++) {
061: if (sConditions[i].isSatisfied(request, renderConfig
062: .getPrecondition())) {
063: return sResults[i];
064: }
065: }
066: } catch (PortletException ex) {
067: throw ex;
068: } catch (Exception ex) {
069: throw new PortletException(
070: "Failed to evaluate a condition.", ex);
071: }
072: return "failure";
073: }
074:
075: /* (non-Javadoc)
076: * @see com.nabhinc.portlet.mvcportlet.core.Initable#init(org.w3c.dom.Element, com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig)
077: */
078: public void init(Element xmlConfig, ControllerPortletConfig config)
079: throws PortletException {
080: super .init(xmlConfig, config);
081: Element[] condElems = XMLUtil.getSubElements(xmlConfig, "case");
082: if (condElems == null || condElems.length == 0) {
083: throw new PortletException(
084: "At least one case must be defined in a switch.");
085: }
086:
087: sResults = new String[condElems.length];
088: sConditions = new Condition[condElems.length];
089: for (int i = 0; i < condElems.length; i++) {
090: sResults[i] = condElems[i].getAttribute("label");
091: if (sResults[i] == null || sResults[i].equals("")) {
092: throw new PortletException(
093: "Missing required attribute in a case: label.");
094: }
095: //String condClass = XMLUtil.getSubElementText(condElems[i], "class");
096: //if (condClass == null) throw new PortletException("Missing pre-condition class element.");
097: try {
098: //Class cl = Class.forName(condClass);
099: //sConditions[i] = (Condition) cl.newInstance();
100: //sConditions[i].init(condElems[i]);
101: sConditions[i] = ConditionFactory.create(XMLUtil
102: .getFirstSubElement(condElems[i]));
103: /*
104: } catch (ClassNotFoundException ex) {
105: throw new PortletException("Failed to load pre-condition class: " + condClass);
106: } catch (ClassCastException ex) {
107: throw new PortletException("Pre-condition class does not implement Precondition interface: " + condClass);
108: } catch (IllegalAccessException ex) {
109: throw new PortletException("Precondition class constructor not accessible.");
110: } catch (InstantiationException ex) {
111: throw new PortletException("Exception instantiating pre-condition class");
112: */
113: } catch (Exception ex) {
114: throw new PortletException(
115: "Exception initializing pre-condition class.",
116: ex);
117: }
118: }
119:
120: }
121:
122: }
|