001: /*
002: * (C) Copyright 2004 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 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.util;
022:
023: import java.util.HashMap;
024:
025: import javax.portlet.PortletException;
026:
027: import org.w3c.dom.Element;
028:
029: import com.nabhinc.condition.Condition;
030: import com.nabhinc.condition.ConditionFactory;
031: import com.nabhinc.core.NamedObject;
032: import com.nabhinc.core.XMLInitable;
033: import com.nabhinc.portlet.mvcportlet.core.Constants;
034: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
035: import com.nabhinc.portlet.mvcportlet.core.Initable;
036: import com.nabhinc.util.XMLUtil;
037:
038: /**
039: * Utility functions used in configuration.
040: *
041: * @author Padmanabh Dabke
042: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
043: */
044: public class ConfigUtil {
045: public static String constructPathKey(String name,
046: String displayType) {
047: if (displayType == null || displayType.equals(""))
048: return name;
049: else
050: return name + ":" + displayType;
051: }
052:
053: public static int getScope(String scopeStr) throws PortletException {
054: if (scopeStr.equals(Constants.REQUEST_SCOPE)) {
055: return Constants.SCOPE_REQUEST;
056: } else if (scopeStr.equals(Constants.PORTLET_SESSION_SCOPE)) {
057: return Constants.SCOPE_PORTLET_SESSION;
058: } else if (scopeStr.equals(Constants.APPLICATION_SESSION_SCOPE)) {
059: return Constants.SCOPE_APPLICATION_SESSION;
060: } else if (scopeStr.equals(Constants.PORTLET_CONTEXT_SCOPE)) {
061: return Constants.SCOPE_PORTLET_CONTEXT;
062: } else {
063: throw new PortletException(
064: "Invalid scope. Must be one of: request, portlet_session, application_session, portlet_context.");
065:
066: }
067:
068: }
069:
070: @SuppressWarnings("unchecked")
071: public static HashMap processObjectCollection(Element root,
072: String collectionTag, String itemTag, Class itemClass,
073: boolean isXMLInitable, HashMap map,
074: ControllerPortletConfig config) throws PortletException {
075: Element validatorRoot = XMLUtil.getSubElement(root,
076: collectionTag);
077:
078: boolean isNamed = NamedObject.class.isAssignableFrom(itemClass);
079: if (validatorRoot != null) {
080: Element[] validators = XMLUtil.getSubElements(
081: validatorRoot, itemTag);
082: if (validators != null) {
083:
084: try {
085: for (int i = 0; i < validators.length; i++) {
086: String vName = validators[i]
087: .getAttribute("name");
088: if (vName == null || vName.trim().equals("")) {
089: throw new PortletException(
090: "Missing required " + itemTag
091: + " attribute: name.");
092: }
093: String vClass = validators[i]
094: .getAttribute("class");
095: if (vClass == null || vClass.trim().equals("")) {
096: throw new PortletException(
097: "Missing required " + itemTag
098: + " attribute: class");
099: }
100:
101: Object obj = Class.forName(vClass)
102: .newInstance();
103: if (isXMLInitable) {
104: XMLInitable validator = (XMLInitable) obj;
105: if (!itemClass.isAssignableFrom(validator
106: .getClass())) {
107: throw new PortletException("Invalid "
108: + itemTag + " class: "
109: + validator.getClass());
110: }
111: validator.init(validators[i]);
112: } else {
113: Initable validator = (Initable) obj;
114: if (!itemClass.isAssignableFrom(validator
115: .getClass())) {
116: throw new PortletException("Invalid "
117: + itemTag + " class: "
118: + validator.getClass());
119: }
120: validator.init(validators[i], config);
121: }
122: map.put(vName, obj);
123: if (isNamed)
124: ((NamedObject) obj).setName(vName);
125: }
126: } catch (PortletException pex) {
127: throw pex;
128: } catch (Exception ex) {
129: throw new PortletException(
130: "Error during processing a validator entry: "
131: + ex.toString(), ex);
132: }
133: }
134: }
135: return map;
136:
137: }
138:
139: @SuppressWarnings("unchecked")
140: public static HashMap processConditions(Element root,
141: String collectionTag, String itemTag, HashMap map,
142: ControllerPortletConfig config) throws PortletException {
143: Element validatorRoot = XMLUtil.getSubElement(root,
144: collectionTag);
145:
146: if (validatorRoot != null) {
147: Element[] validators = XMLUtil.getSubElements(
148: validatorRoot, itemTag);
149: if (validators != null) {
150: try {
151: for (int i = 0; i < validators.length; i++) {
152: String vName = validators[i]
153: .getAttribute("name");
154: if (vName == null || vName.trim().equals("")) {
155: throw new PortletException(
156: "Missing required " + itemTag
157: + " attribute: name.");
158: }
159: Condition cond = ConditionFactory
160: .create(XMLUtil
161: .getFirstSubElement(validators[i]));
162: map.put(vName, cond);
163: }
164: } catch (PortletException pex) {
165: throw pex;
166: } catch (Exception ex) {
167: throw new PortletException(
168: "Error during processing a validator entry: "
169: + ex.toString(), ex);
170: }
171: }
172: }
173: return map;
174:
175: }
176:
177: }
|