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.io.StringWriter;
020: import java.util.Stack;
021:
022: import org.apache.cocoon.components.expression.ExpressionContext;
023: import org.apache.cocoon.template.JXTemplateGenerator;
024: import org.apache.cocoon.template.environment.ExecutionContext;
025: import org.apache.cocoon.template.environment.ParsingContext;
026: import org.apache.cocoon.template.expression.JXTExpression;
027: import org.apache.cocoon.template.script.Invoker;
028: import org.apache.cocoon.template.script.event.Event;
029: import org.apache.cocoon.template.script.event.StartElement;
030: import org.apache.cocoon.template.xml.AttributeAwareXMLConsumer;
031: import org.apache.cocoon.xml.ContentHandlerWrapper;
032: import org.apache.cocoon.xml.XMLConsumer;
033: import org.apache.xml.serialize.TextSerializer;
034: import org.xml.sax.Attributes;
035: import org.xml.sax.Locator;
036: import org.xml.sax.SAXException;
037: import org.xml.sax.SAXParseException;
038: import org.xml.sax.helpers.AttributesImpl;
039:
040: /**
041: * @version $Id: Attribute.java 449189 2006-09-23 06:52:29Z crossley $
042: */
043: public class Attribute extends Instruction {
044:
045: private JXTExpression name;
046: private JXTExpression value;
047:
048: public Attribute(ParsingContext parsingContext, StartElement raw,
049: Attributes attrs, Stack stack) throws SAXException {
050: super (raw);
051:
052: Locator locator = getLocation();
053: String name = attrs.getValue("name");
054: if (name == null) {
055: throw new SAXParseException(
056: "parameter: \"name\" is required", locator, null);
057: }
058: this .name = parsingContext.getStringTemplateParser()
059: .compileExpr(name, "parameter: \"name\": ", locator);
060:
061: String value = attrs.getValue("value");
062:
063: this .value = parsingContext.getStringTemplateParser()
064: .compileExpr(value, "parameter: \"value\": ", locator);
065: }
066:
067: public Event execute(final XMLConsumer consumer,
068: ExpressionContext expressionContext,
069: ExecutionContext executionContext,
070: MacroContext macroContext, Event startEvent, Event endEvent)
071: throws SAXException {
072:
073: String nameStr = null;
074: String valueStr = "";
075: try {
076: nameStr = this .name.getStringValue(expressionContext);
077:
078: if (this .value != null)
079: valueStr = this .value.getStringValue(expressionContext);
080: else {
081: final Attributes EMPTY_ATTRS = new AttributesImpl();
082: String elementName = "attribute";
083:
084: TextSerializer serializer = new TextSerializer();
085: StringWriter writer = new StringWriter();
086: serializer.setOutputCharStream(writer);
087:
088: ContentHandlerWrapper contentHandler = new ContentHandlerWrapper(
089: serializer, serializer);
090: contentHandler.startDocument();
091:
092: // TODO is root element necessary for TextSerializer?
093: contentHandler.startElement(JXTemplateGenerator.NS,
094: elementName, elementName, EMPTY_ATTRS);
095: Invoker.execute(contentHandler, expressionContext,
096: executionContext, macroContext, this .getNext(),
097: this .getEndInstruction());
098: contentHandler.endElement(JXTemplateGenerator.NS,
099: elementName, elementName);
100: contentHandler.endDocument();
101: valueStr = writer.toString();
102: }
103: } catch (Exception exc) {
104: throw new SAXParseException(exc.getMessage(),
105: getLocation(), exc);
106: }
107: if (consumer instanceof AttributeAwareXMLConsumer) {
108: AttributeAwareXMLConsumer c = (AttributeAwareXMLConsumer) consumer;
109: c.attribute("", nameStr, nameStr, "CDATA",
110: valueStr == null ? "" : valueStr);
111: } else
112: throw new SAXParseException(
113: "consumer is not attribute aware", getLocation());
114: return getEndInstruction().getNext();
115: }
116:
117: }
|