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.HashMap;
020: import java.util.Map;
021: import java.util.Stack;
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.script.event.Characters;
027: import org.apache.cocoon.template.script.event.Event;
028: import org.apache.cocoon.template.script.event.IgnorableWhitespace;
029: import org.apache.cocoon.template.script.event.StartElement;
030: import org.apache.cocoon.template.script.event.TextEvent;
031: import org.apache.cocoon.xml.XMLConsumer;
032: import org.apache.commons.lang.StringUtils;
033: import org.xml.sax.Attributes;
034: import org.xml.sax.SAXException;
035: import org.xml.sax.SAXParseException;
036:
037: /**
038: * @version SVN $Id: Define.java 449189 2006-09-23 06:52:29Z crossley $
039: */
040: public class Define extends Instruction {
041:
042: private final String name;
043: private final String namespace;
044: private final String qname;
045: private final Map parameters;
046: private Event body;
047:
048: public Define(ParsingContext parsingContext, StartElement raw,
049: Attributes attrs, Stack stack) throws SAXException {
050:
051: super (raw);
052:
053: // <macro name="myTag" targetNamespace="myNamespace">
054: // <parameter name="paramName" required="Boolean"
055: // default="value"/>
056: // body
057: // </macro>
058: this .namespace = StringUtils.defaultString(attrs
059: .getValue("targetNamespace"));
060: this .name = attrs.getValue("name");
061: if (this .name == null) {
062: throw new SAXParseException("macro: \"name\" is required",
063: getLocation(), null);
064: }
065:
066: this .qname = "{" + namespace + "}" + name;
067: this .parameters = new HashMap();
068: }
069:
070: public Event execute(final XMLConsumer consumer,
071: ExpressionContext expressionContext,
072: ExecutionContext executionContext,
073: MacroContext macroContext, Event startEvent, Event endEvent)
074: throws SAXException {
075: executionContext.getDefinitions().put(this .qname, this );
076: return getEndInstruction().getNext();
077: }
078:
079: public void endNotify() throws SAXException {
080: Event e = next;
081: boolean params = true;
082: while (e != this .getEndInstruction()) {
083: if (e instanceof Parameter) {
084: Parameter startParam = (Parameter) e;
085: if (!params) {
086: throw new SAXParseException(
087: "<parameter> not allowed here: \""
088: + startParam.name + "\"",
089: startParam.getLocation(), null);
090: }
091: Object prev = this .parameters.put(startParam.name,
092: startParam);
093: if (prev != null) {
094: throw new SAXParseException(
095: "duplicate parameter: \"" + startParam.name
096: + "\"", location, null);
097: }
098: e = startParam.getEndInstruction();
099: } else if (e instanceof IgnorableWhitespace) {
100: // EMPTY
101: } else if (e instanceof Characters) {
102: // check for whitespace
103: char[] ch = ((TextEvent) e).getRaw();
104: int len = ch.length;
105: for (int i = 0; i < len; i++) {
106: if (!Character.isWhitespace(ch[i])) {
107: if (params) {
108: params = false;
109: this .body = e;
110: }
111: break;
112: }
113: }
114: } else {
115: if (params) {
116: params = false;
117: this .body = e;
118: }
119: }
120: e = e.getNext();
121: }
122: if (this .body == null) {
123: this .body = this .getEndInstruction();
124: }
125: }
126:
127: public Map getParameters() {
128: return parameters;
129: }
130:
131: public Event getBody() {
132: return body;
133: }
134:
135: public String getQname() {
136: return qname;
137: }
138: }
|