001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
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 org.apache.cocoon.template.instruction;
018:
019: import java.util.Stack;
020:
021: import org.apache.cocoon.components.expression.ExpressionContext;
022: import org.apache.cocoon.template.environment.ExecutionContext;
023: import org.apache.cocoon.template.environment.ParsingContext;
024: import org.apache.cocoon.template.expression.JXTExpression;
025: import org.apache.cocoon.template.script.Invoker;
026: import org.apache.cocoon.template.script.event.Event;
027: import org.apache.cocoon.template.script.event.StartElement;
028: import org.apache.cocoon.xml.XMLConsumer;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031: import org.xml.sax.Attributes;
032: import org.xml.sax.Locator;
033: import org.xml.sax.SAXException;
034: import org.xml.sax.SAXParseException;
035:
036: /**
037: * @version SVN $Id: Set.java 449189 2006-09-23 06:52:29Z crossley $
038: */
039: public class Set extends Instruction {
040:
041: private final JXTExpression var;
042: private final JXTExpression value;
043:
044: public Set(ParsingContext parsingContext, StartElement raw,
045: Attributes attrs, Stack stack) throws SAXException {
046:
047: super (raw);
048:
049: Locator locator = getLocation();
050: String var = attrs.getValue("var");
051: String value = attrs.getValue("value");
052: JXTExpression varExpr = null;
053: JXTExpression valueExpr = null;
054: if (var != null) {
055: varExpr = parsingContext.getStringTemplateParser()
056: .compileExpr(var, "set: \"var\":", locator);
057: }
058: if (value != null) {
059: valueExpr = parsingContext.getStringTemplateParser()
060: .compileExpr(value, "set: \"value\":", locator);
061: }
062: this .var = varExpr;
063: this .value = valueExpr;
064: }
065:
066: public Event execute(final XMLConsumer consumer,
067: ExpressionContext expressionContext,
068: ExecutionContext executionContext,
069: MacroContext macroContext, Event startEvent, Event endEvent)
070: throws SAXException {
071:
072: Object value = null;
073: String var = null;
074: try {
075: if (this .var != null) {
076: var = this .var.getStringValue(expressionContext);
077: }
078: if (this .value != null) {
079: value = this .value.getNode(expressionContext);
080: }
081: } catch (Exception exc) {
082: throw new SAXParseException(exc.getMessage(),
083: getLocation(), exc);
084: }
085: if (this .value == null) {
086: NodeList nodeList = Invoker.toDOMNodeList("set", this ,
087: expressionContext, executionContext, macroContext);
088: // JXPath doesn't handle NodeList, so convert it to an array
089: int len = nodeList.getLength();
090: Node[] nodeArr = new Node[len];
091: for (int i = 0; i < len; i++) {
092: nodeArr[i] = nodeList.item(i);
093: }
094: value = nodeArr;
095: }
096: if (var != null) {
097: expressionContext.put(var, value);
098: }
099: return getEndInstruction().getNext();
100: }
101: }
|