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.Reader;
020: import java.io.StringReader;
021: import java.util.List;
022:
023: import org.apache.avalon.framework.activity.Disposable;
024: import org.apache.avalon.framework.logger.AbstractLogEnabled;
025: import org.apache.avalon.framework.service.ServiceException;
026: import org.apache.avalon.framework.service.ServiceManager;
027: import org.apache.avalon.framework.service.Serviceable;
028: import org.apache.avalon.framework.thread.ThreadSafe;
029: import org.apache.cocoon.components.expression.ExpressionFactory;
030: import org.apache.cocoon.template.environment.ErrorHolder;
031: import org.xml.sax.Locator;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.SAXParseException;
034:
035: public abstract class AbstractStringTemplateParser extends
036: AbstractLogEnabled implements Serviceable, Disposable,
037: ThreadSafe, StringTemplateParser {
038:
039: private ServiceManager manager;
040: private ExpressionFactory expressionFactory;
041:
042: /**
043: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
044: */
045: public void service(ServiceManager manager) throws ServiceException {
046: this .manager = manager;
047: this .expressionFactory = (ExpressionFactory) this .manager
048: .lookup(ExpressionFactory.ROLE);
049: }
050:
051: /**
052: * @see org.apache.avalon.framework.activity.Disposable#dispose()
053: */
054: public void dispose() {
055: if (this .manager != null) {
056: this .manager.release(this .expressionFactory);
057: this .manager = null;
058: this .expressionFactory = null;
059: }
060: }
061:
062: protected JXTExpression compile(final String expression)
063: throws Exception {
064: return new JXTExpression(expression, this .expressionFactory
065: .getExpression(expression));
066: }
067:
068: protected JXTExpression compile(final String expression,
069: String language) throws Exception {
070: return new JXTExpression(expression, this .expressionFactory
071: .getExpression(language, expression));
072: }
073:
074: /**
075: * @see org.apache.cocoon.template.expression.StringTemplateParser#compileBoolean(java.lang.String, java.lang.String, org.xml.sax.Locator)
076: */
077: public JXTExpression compileBoolean(String val, String msg,
078: Locator location) throws SAXException {
079: JXTExpression res = compileExpr(val, msg, location);
080: if (res != null && res.getCompiledExpression() == null
081: && res.getRaw() != null) {
082: res.setCompiledExpression(Boolean.valueOf(res.getRaw()));
083: }
084: return res;
085: }
086:
087: /**
088: * @see org.apache.cocoon.template.expression.StringTemplateParser#compileInt(java.lang.String, java.lang.String, org.xml.sax.Locator)
089: */
090: public JXTExpression compileInt(String val, String msg,
091: Locator location) throws SAXException {
092: JXTExpression res = compileExpr(val, msg, location);
093: if (res != null && res.getCompiledExpression() == null
094: && res.getRaw() != null) {
095: res.setCompiledExpression(Integer.valueOf(res.getRaw()));
096: }
097: return res;
098: }
099:
100: /**
101: * @see org.apache.cocoon.template.expression.StringTemplateParser#compileExpr(java.lang.String, java.lang.String, org.xml.sax.Locator)
102: */
103: public JXTExpression compileExpr(String inStr, String errorPrefix,
104: Locator location) throws SAXParseException {
105: if (inStr == null) {
106: return null;
107: }
108: StringReader in = new StringReader(inStr.trim());
109: List substitutions = parseSubstitutions(in, errorPrefix,
110: location);
111: if (substitutions.size() == 0
112: || !(substitutions.get(0) instanceof JXTExpression))
113: return new JXTExpression(inStr, null);
114:
115: return (JXTExpression) substitutions.get(0);
116: }
117:
118: /**
119: * @see org.apache.cocoon.template.expression.StringTemplateParser#parseSubstitutions(java.io.Reader, java.lang.String, org.xml.sax.Locator)
120: */
121: public List parseSubstitutions(Reader in, String errorPrefix,
122: Locator location) throws SAXParseException {
123: try {
124: return parseSubstitutions(in);
125: } catch (Exception exc) {
126: throw new SAXParseException(errorPrefix + exc.getMessage(),
127: location, exc);
128: } catch (Error err) {
129: throw new SAXParseException(errorPrefix + err.getMessage(),
130: location, new ErrorHolder(err));
131: }
132: }
133: }
|