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.expression;
018:
019: import java.io.CharArrayReader;
020: import java.io.Reader;
021: import java.io.StringReader;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.cocoon.components.expression.ExpressionContext;
026: import org.apache.cocoon.template.environment.ErrorHolder;
027: import org.apache.cocoon.template.environment.ParsingContext;
028: import org.xml.sax.Locator;
029: import org.xml.sax.SAXException;
030: import org.xml.sax.SAXParseException;
031:
032: /**
033: * @version $Id: Substitutions.java 449189 2006-09-23 06:52:29Z crossley $
034: */
035: public class Substitutions {
036:
037: final private List substitutions;
038: final private boolean hasSubstitutions;
039:
040: public Substitutions(ParsingContext parsingContext,
041: Locator location, String stringTemplate)
042: throws SAXException {
043: this (parsingContext, location, new StringReader(stringTemplate));
044: }
045:
046: public Substitutions(ParsingContext parsingContext,
047: Locator location, char[] chars, int start, int length)
048: throws SAXException {
049: this (parsingContext, location, new CharArrayReader(chars,
050: start, length));
051: }
052:
053: private Substitutions(ParsingContext parsingContext,
054: Locator location, Reader in) throws SAXException {
055: this .substitutions = parsingContext.getStringTemplateParser()
056: .parseSubstitutions(in, "", location);
057: this .hasSubstitutions = !substitutions.isEmpty();
058: }
059:
060: public boolean hasSubstitutions() {
061: return this .hasSubstitutions;
062: }
063:
064: public Iterator iterator() {
065: return this .substitutions.iterator();
066: }
067:
068: public int size() {
069: return this .substitutions.size();
070: }
071:
072: public Object get(int pos) {
073: return this .substitutions.get(pos);
074: }
075:
076: public String toString(Locator location,
077: ExpressionContext expressionContext) throws SAXException {
078: StringBuffer buf = new StringBuffer();
079: Iterator iterSubst = iterator();
080: while (iterSubst.hasNext()) {
081: Subst subst = (Subst) iterSubst.next();
082: if (subst instanceof Literal) {
083: Literal lit = (Literal) subst;
084: buf.append(lit.getValue());
085: } else if (subst instanceof JXTExpression) {
086: JXTExpression expr = (JXTExpression) subst;
087: Object val;
088: try {
089: val = expr.getValue(expressionContext);
090: } catch (Exception e) {
091: throw new SAXParseException(e.getMessage(),
092: location, e);
093: } catch (Error err) {
094: throw new SAXParseException(err.getMessage(),
095: location, new ErrorHolder(err));
096: }
097: buf.append(val != null ? val.toString() : "");
098: }
099: }
100: return buf.toString();
101: }
102: }
|