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.ErrorHolder;
023: import org.apache.cocoon.template.environment.ParsingContext;
024: import org.apache.cocoon.template.expression.JXTExpression;
025: import org.apache.cocoon.template.script.event.AttributeEvent;
026: import org.apache.cocoon.template.script.event.CopyAttribute;
027: import org.apache.cocoon.template.script.event.StartElement;
028: import org.apache.cocoon.template.script.event.SubstituteAttribute;
029: import org.xml.sax.Attributes;
030: import org.xml.sax.Locator;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.SAXParseException;
033:
034: /**
035: * @version SVN $Id: ParameterInstance.java 449189 2006-09-23 06:52:29Z crossley $
036: */
037: public class ParameterInstance extends Instruction {
038: final String name;
039: private final Object value;
040:
041: public ParameterInstance(ParsingContext parsingContext,
042: StartElement raw, Attributes attrs, Stack stack)
043: throws SAXException {
044: super (raw);
045: Locator locator = getLocation();
046: if (stack.size() == 0 || !(stack.peek() instanceof Call)) {
047: throw new SAXParseException("<parameter> not allowed here",
048: locator, null);
049: } else {
050: this .name = attrs.getValue("name");
051: if (this .name == null) {
052: throw new SAXParseException(
053: "parameter: \"name\" is required", locator,
054: null);
055: }
056:
057: String val = attrs.getValue("value");
058: if (val == null)
059: throw new SAXParseException(
060: "parameter: \"value\" is required", locator,
061: null);
062:
063: this .value = parsingContext
064: .getStringTemplateParser()
065: .compileExpr(val, "parameter: \"value\": ", locator);
066: }
067: }
068:
069: public ParameterInstance(AttributeEvent event) {
070: super ((Locator) null);
071: this .name = event.getLocalName();
072: this .value = event;
073: }
074:
075: public String getName() {
076: return name;
077: }
078:
079: public Object getValue(ExpressionContext expressionContext)
080: throws SAXException {
081: if (this .value instanceof JXTExpression)
082: return getExpressionValue((JXTExpression) this .value,
083: expressionContext);
084: else if (this .value instanceof CopyAttribute) {
085: CopyAttribute copy = (CopyAttribute) this .value;
086: return copy.getValue();
087: } else if (this .value instanceof SubstituteAttribute) {
088: SubstituteAttribute substEvent = (SubstituteAttribute) this .value;
089: if (substEvent.getSubstitutions().size() == 1
090: && substEvent.getSubstitutions().get(0) instanceof JXTExpression)
091: return getExpressionValue((JXTExpression) substEvent
092: .getSubstitutions().get(0), expressionContext);
093: else
094: return substEvent.getSubstitutions().toString(
095: getLocation(), expressionContext);
096:
097: } else {
098: throw new Error("this shouldn't have happened");
099: }
100: }
101:
102: private Object getExpressionValue(JXTExpression expr,
103: ExpressionContext expressionContext) throws SAXException {
104: Object val;
105: try {
106: val = expr.getNode(expressionContext);
107: } catch (Exception e) {
108: throw new SAXParseException(e.getMessage(), getLocation(),
109: e);
110: } catch (Error err) {
111: throw new SAXParseException(err.getMessage(),
112: getLocation(), new ErrorHolder(err));
113: }
114: return val != null ? val : "";
115: }
116: }
|