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.script.event;
018:
019: import java.util.Iterator;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import org.apache.cocoon.components.expression.ExpressionContext;
024: import org.apache.cocoon.template.environment.ExecutionContext;
025: import org.apache.cocoon.template.environment.ParsingContext;
026: import org.apache.cocoon.template.expression.Substitutions;
027: import org.apache.cocoon.template.instruction.MacroContext;
028: import org.apache.cocoon.xml.XMLConsumer;
029: import org.xml.sax.Attributes;
030: import org.xml.sax.Locator;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.helpers.AttributesImpl;
033:
034: /**
035: * @version SVN $Id: StartElement.java 449189 2006-09-23 06:52:29Z crossley $
036: */
037: public class StartElement extends Event {
038: public StartElement(ParsingContext parsingContext,
039: Locator location, String namespaceURI, String localName,
040: String raw, Attributes attrs) throws SAXException {
041: super (location);
042: this .namespaceURI = namespaceURI;
043: this .localName = localName;
044: this .raw = raw;
045: this .qname = "{" + namespaceURI + "}" + localName;
046: int len = attrs.getLength();
047: for (int i = 0; i < len; i++) {
048: String uri = attrs.getURI(i);
049: String local = attrs.getLocalName(i);
050: String qname = attrs.getQName(i);
051: String type = attrs.getType(i);
052: String value = attrs.getValue(i);
053: Substitutions substitutions = new Substitutions(
054: parsingContext, getLocation(), value);
055: if (substitutions.hasSubstitutions()) {
056: getAttributeEvents().add(
057: new SubstituteAttribute(uri, local, qname,
058: type, substitutions));
059: } else {
060: getAttributeEvents().add(
061: new CopyAttribute(uri, local, qname, type,
062: value));
063: }
064: }
065: this .attributes = new AttributesImpl(attrs);
066: }
067:
068: final String namespaceURI;
069: final String localName;
070: final String raw;
071: private final String qname;
072: private final List attributeEvents = new LinkedList();
073: final Attributes attributes;
074: EndElement endElement;
075:
076: public EndElement getEndElement() {
077: return endElement;
078: }
079:
080: public String getLocalName() {
081: return localName;
082: }
083:
084: public String getNamespaceURI() {
085: return namespaceURI;
086: }
087:
088: public String getRaw() {
089: return raw;
090: }
091:
092: public String getQname() {
093: return qname;
094: }
095:
096: public List getAttributeEvents() {
097: return attributeEvents;
098: }
099:
100: public void setEndElement(EndElement endElement) {
101: this .endElement = endElement;
102:
103: }
104:
105: public Event execute(XMLConsumer consumer,
106: ExpressionContext expressionContext,
107: ExecutionContext executionContext,
108: MacroContext macroContext, Event startEvent, Event endEvent)
109: throws SAXException {
110: Iterator i = getAttributeEvents().iterator();
111: AttributesImpl attrs = new AttributesImpl();
112: while (i.hasNext()) {
113: AttributeEvent attrEvent = (AttributeEvent) i.next();
114: if (attrEvent instanceof CopyAttribute) {
115: CopyAttribute copy = (CopyAttribute) attrEvent;
116: attrs.addAttribute(copy.getNamespaceURI(), copy
117: .getLocalName(), copy.getRaw(), copy.getType(),
118: copy.getValue());
119: } else if (attrEvent instanceof SubstituteAttribute) {
120: SubstituteAttribute substEvent = (SubstituteAttribute) attrEvent;
121: String attributeValue = substEvent.getSubstitutions()
122: .toString(getLocation(), expressionContext);
123: attrs.addAttribute(attrEvent.getNamespaceURI(),
124: attrEvent.getLocalName(), attrEvent.getRaw(),
125: attrEvent.getType(), attributeValue);
126: }
127: }
128:
129: // Send any pending startPrefixMapping events
130: expressionContext.getNamespaces().enterScope(consumer);
131: consumer.startElement(getNamespaceURI(), getLocalName(),
132: getRaw(), attrs);
133: return getNext();
134: }
135: }
|