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.script.event;
18:
19: import java.util.Iterator;
20:
21: import org.apache.cocoon.components.expression.ExpressionContext;
22: import org.apache.cocoon.template.environment.ErrorHolder;
23: import org.apache.cocoon.template.environment.ExecutionContext;
24: import org.apache.cocoon.template.environment.ParsingContext;
25: import org.apache.cocoon.template.expression.JXTExpression;
26: import org.apache.cocoon.template.expression.Literal;
27: import org.apache.cocoon.template.expression.Substitutions;
28: import org.apache.commons.lang.ArrayUtils;
29: import org.xml.sax.Locator;
30: import org.xml.sax.SAXException;
31: import org.xml.sax.SAXParseException;
32:
33: /**
34: * @version SVN $Id: TextEvent.java 449189 2006-09-23 06:52:29Z crossley $
35: */
36: public class TextEvent extends Event {
37: public TextEvent(ParsingContext parsingContext, Locator location,
38: char[] chars, int start, int length) throws SAXException {
39: super (location);
40: this .raw = new char[length];
41: System.arraycopy(chars, start, this .raw, 0, length);
42: this .substitutions = new Substitutions(parsingContext,
43: getLocation(), chars, start, length);
44: }
45:
46: final Substitutions substitutions;
47: final char[] raw;
48:
49: public char[] getRaw() {
50: return raw;
51: }
52:
53: public Substitutions getSubstitutions() {
54: return substitutions;
55: }
56:
57: interface CharHandler {
58: public void characters(char[] ch, int offset, int length)
59: throws SAXException;
60: }
61:
62: protected static void characters(
63: ExpressionContext expressionContext,
64: ExecutionContext executionContext, TextEvent event,
65: CharHandler handler) throws SAXException {
66: Iterator iter = event.getSubstitutions().iterator();
67: while (iter.hasNext()) {
68: Object subst = iter.next();
69: char[] chars;
70: if (subst instanceof Literal) {
71: chars = ((Literal) subst).getCharArray();
72: } else {
73: JXTExpression expr = (JXTExpression) subst;
74: try {
75: Object val = expr.getValue(expressionContext);
76: chars = val != null ? val.toString().toCharArray()
77: : ArrayUtils.EMPTY_CHAR_ARRAY;
78: } catch (Exception e) {
79: throw new SAXParseException(e.getMessage(), event
80: .getLocation(), e);
81: } catch (Error err) {
82: throw new SAXParseException(err.getMessage(), event
83: .getLocation(), new ErrorHolder(err));
84: }
85: }
86: handler.characters(chars, 0, chars.length);
87: }
88: }
89:
90: }
|