01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.template.instruction;
18:
19: import java.util.Properties;
20: import java.util.Stack;
21:
22: import org.apache.cocoon.ProcessingException;
23: import org.apache.cocoon.components.expression.ExpressionContext;
24: import org.apache.cocoon.template.environment.ExecutionContext;
25: import org.apache.cocoon.template.environment.ParsingContext;
26: import org.apache.cocoon.template.script.Invoker;
27: import org.apache.cocoon.template.script.event.Event;
28: import org.apache.cocoon.template.script.event.StartElement;
29: import org.apache.cocoon.xml.XMLConsumer;
30: import org.apache.cocoon.xml.XMLUtils;
31: import org.w3c.dom.NodeList;
32: import org.xml.sax.Attributes;
33: import org.xml.sax.SAXException;
34: import org.xml.sax.SAXParseException;
35:
36: /**
37: * @version SVN $Id: Comment.java 452142 2006-10-02 17:41:45Z joerg $
38: */
39: public class Comment extends Instruction {
40: public Comment(ParsingContext parsingContext, StartElement raw,
41: Attributes attrs, Stack stack) {
42: // <jx:comment>This will be parsed</jx:comment>
43: super (raw);
44: }
45:
46: public Event execute(final XMLConsumer consumer,
47: ExpressionContext expressionContext,
48: ExecutionContext executionContext,
49: MacroContext macroContext, Event startEvent, Event endEvent)
50: throws SAXException {
51: // Parse the body of the comment
52: NodeList nodeList = Invoker.toDOMNodeList("comment", this ,
53: expressionContext, executionContext, macroContext);
54: // JXPath doesn't handle NodeList, so convert it to an array
55: int len = nodeList.getLength();
56: final StringBuffer buf = new StringBuffer();
57: Properties omit = XMLUtils.createPropertiesForXML(true);
58: for (int i = 0; i < len; i++) {
59: try {
60: buf.append(XMLUtils.serializeNode(nodeList.item(i),
61: omit));
62: } catch (ProcessingException e) {
63: throw new SAXParseException(e.getMessage(),
64: getLocation(), e);
65: }
66: }
67: char[] chars = new char[buf.length()];
68: buf.getChars(0, chars.length, chars, 0);
69: consumer.comment(chars, 0, chars.length);
70: return getEndInstruction().getNext();
71: }
72: }
|