001: /*
002: * Copyright 2005-2006 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.engine.node.var;
018:
019: import java.io.StringReader;
020:
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023:
024: import org.apache.log4j.Logger;
025: import org.w3c.dom.Document;
026: import org.w3c.dom.Element;
027: import org.xml.sax.InputSource;
028:
029: import edu.iu.uis.eden.KEWServiceLocator;
030: import edu.iu.uis.eden.engine.RouteContext;
031: import edu.iu.uis.eden.engine.RouteHelper;
032: import edu.iu.uis.eden.engine.node.BranchState;
033: import edu.iu.uis.eden.engine.node.PropertiesUtil;
034: import edu.iu.uis.eden.engine.node.SimpleNode;
035: import edu.iu.uis.eden.engine.node.SimpleResult;
036: import edu.iu.uis.eden.exception.InvalidXmlException;
037:
038: /**
039: * A simple node that allows setting of document variables.
040: * The definition of SetVarNode takes the following config parameter elements:
041: * <dl>
042: * <dt>name</dt>
043: * <dd>The name of the variable to set</dd>
044: * <dt>value</dt>
045: * <dd>The value to which to set the variable. This value is parsed according to
046: * Property/PropertyScheme syntax</dd>
047: * </dl>
048: * The default PropertySchme is LiteralScheme, which will evaluate the value simply
049: * as a literal (won't do anything but return it)
050: * @see PropertyScheme
051: * @see Property
052: *
053: * @author Aaron Hamid (arh14 at cornell dot edu)
054: */
055: public class SetVarNode implements SimpleNode {
056: private static final Logger LOG = Logger
057: .getLogger(SetVarNode.class);
058:
059: public SimpleResult process(RouteContext context, RouteHelper helper)
060: throws Exception {
061: LOG.debug("processing");
062: DocumentBuilder db = DocumentBuilderFactory.newInstance()
063: .newDocumentBuilder();
064: String contentFragment = context.getNodeInstance()
065: .getRouteNode().getContentFragment();
066: LOG.debug("contentFragment=" + contentFragment);
067: Document d = db.parse(new InputSource(new StringReader(
068: contentFragment)));
069: Element e = d.getDocumentElement();
070: String name = e.getElementsByTagName("name").item(0)
071: .getTextContent();
072: // FIXME: validation should be checked in documenttypexmlparser to start with
073: if (name == null) {
074: throw new InvalidXmlException(
075: "SetVar node required 'name' attribute to be specified");
076: }
077: String valueRef = e.getElementsByTagName("value").item(0)
078: .getTextContent();
079: Object retrievedVal = PropertiesUtil.retrieveProperty(valueRef,
080: PropertyScheme.LITERAL_SCHEME, context);
081: LOG.debug("retrieved value '" + retrievedVal + " for value '"
082: + valueRef);
083: // hack to make variable value fit in column size
084: // need to just alter that column
085: String stringVal = null;
086: if (retrievedVal != null) {
087: stringVal = retrievedVal.toString();
088: if (stringVal.length() > 255) {
089: stringVal = stringVal.substring(0, 255);
090: }
091: }
092: LOG.debug("setting value '" + stringVal + "' for variable "
093: + name);
094: KEWServiceLocator.getBranchService().setScopedVariableValue(
095: context.getNodeInstance().getBranch(),
096: BranchState.VARIABLE_PREFIX + name, stringVal);
097:
098: return new SimpleResult(true);
099: }
100:
101: }
|