001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.test.web.framework.actions;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.w3c.dom.Node;
025: import org.w3c.dom.NodeList;
026:
027: import edu.iu.uis.eden.test.web.framework.Property;
028: import edu.iu.uis.eden.test.web.framework.PropertyScheme;
029: import edu.iu.uis.eden.test.web.framework.Script;
030: import edu.iu.uis.eden.test.web.framework.Util;
031: import edu.iu.uis.eden.util.XmlHelper;
032:
033: /**
034: * ScriptAction that sets request parameters
035: * <pre>
036: * <parameters [ retain="true|false" ]>
037: * <param [ overwrite="true|false" ] ( value="..." | variable="..." | literal="..." | resource="..." | url="..." )/>
038: * <param [ overwrite="true|false" ]>...</param>
039: * </parameters>
040: * <pre>
041: * The 'value' attribute of param element is resolved via {@link edu.iu.uis.eden.test.web.framework.Util#getResolvableAttribute(Node, String, PropertyScheme)},
042: * defaulting to literal scheme.
043: * If a value variant is not present, the algorithm proceeds again without a specific prefix (i.e., just looks for variable, literal, etc.).
044: * If no attribute is found, element content is used.
045: * @author Aaron Hamid (arh14 at cornell dot edu)
046: * @author Aaron Hamid (arh14 at cornell dot edu)
047: */
048: public class ParametersAction extends BaseScriptAction {
049: private static final String[] NAMES = { "parameters" };
050:
051: public String[] getNames() {
052: return NAMES;
053: }
054:
055: public void process(Script script, Node node) {
056: Map parameters = script.getState().getRequest();
057:
058: if (!"true".equals(Util.getAttribute(node, "retain"))) {
059: log.info("Clearing parameters");
060: parameters.clear();
061: }
062:
063: log.info("Setting parameters");
064: NodeList params = node.getChildNodes();
065: for (int j = 0; j < params.getLength(); j++) {
066: Node param = params.item(j);
067: if (param.getNodeType() != Node.ELEMENT_NODE) {
068: continue;
069: }
070: if (!"param".equals(param.getNodeName())) {
071: log.error("Invalid element: " + param);
072: continue;
073: }
074: String pname = Util.getAttribute(param, "name");
075: if (pname == null) {
076: log.error("Nameless parameter: " + param);
077: continue;
078: }
079:
080: Object pvalue = null;
081: /* first try looking for the attribute "value" */
082: Property property = Util.getResolvableAttribute(param,
083: "value", PropertyScheme.LITERAL_SCHEME);
084: /* if not found, fall back to looking for any of the property scheme types: literal, variable, resource, url, etc. */
085: property = Util.getResolvableAttribute(param, null, null);
086: if (property != null) {
087: log.info("Obtaining value for parameter '" + pname
088: + "' from variable '" + property + "'");
089: pvalue = script.getState().retrieveProperty(property);
090: } else {
091: log.info("Obtaining value for parameter '" + pname
092: + "' from node content");
093: pvalue = Util.getContent(param);
094: }
095: if (pvalue == null) {
096: log.warn("Node does not have any text content: "
097: + XmlHelper.jotNode(param));
098: log.info("Removing parameter '" + pname + "'");
099: parameters.remove(pname);
100: } else {
101: String overwrite = Util
102: .getAttribute(param, "overwrite");
103: boolean overwriting = Boolean.valueOf(overwrite)
104: .booleanValue();
105: List l = (List) parameters.get(pname);
106: if (l == null) {
107: l = new ArrayList();
108: parameters.put(pname, l);
109: }
110: if (overwriting) {
111: log.info("Overwriting parameter '" + pname
112: + "' with value '" + pvalue + "'");
113: l.clear();
114: } else {
115: log.info("Adding parameter '" + pname
116: + "' with value '" + pvalue + "'");
117: }
118: if (pvalue instanceof Collection) {
119: l.addAll((Collection) pvalue);
120: } else {
121: l.add(pvalue);
122: }
123: }
124: }
125: }
126: }
|