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.util.Iterator;
020:
021: import org.apache.cocoon.components.expression.Expression;
022: import org.apache.cocoon.components.expression.ExpressionContext;
023: import org.apache.cocoon.components.expression.jxpath.JXPathExpression;
024:
025: /**
026: * @version $Id: JXTExpression.java 449189 2006-09-23 06:52:29Z crossley $
027: */
028: public class JXTExpression extends Subst {
029:
030: private String raw;
031: private Object compiledExpression;
032:
033: public JXTExpression(String raw, Object expr) {
034: this .raw = raw;
035: this .compiledExpression = expr;
036: }
037:
038: public Object getCompiledExpression() {
039: return compiledExpression;
040: }
041:
042: public void setCompiledExpression(Object compiledExpression) {
043: this .compiledExpression = compiledExpression;
044: }
045:
046: public String getRaw() {
047: return raw;
048: }
049:
050: // Geting the value of the expression in various forms
051:
052: // Hack: try to prevent JXPath from converting result to a String
053: public Object getNode(ExpressionContext expressionContext)
054: throws Exception {
055: Object compiled = this .getCompiledExpression();
056: if (compiled instanceof Expression)
057: return ((Expression) compiled).getNode(expressionContext);
058: return this .getRaw();
059: }
060:
061: public Iterator getIterator(ExpressionContext expressionContext)
062: throws Exception {
063: Iterator iter = null;
064: if (this .getCompiledExpression() != null
065: || this .getRaw() != null) {
066: if (this .getCompiledExpression() instanceof Expression) {
067: iter = ((Expression) this .getCompiledExpression())
068: .iterate(expressionContext);
069: } else {
070: // literal value
071: iter = new Iterator() {
072: Object val = this ;
073:
074: public boolean hasNext() {
075: return val != null;
076: }
077:
078: public Object next() {
079: Object res = val;
080: val = null;
081: return res;
082: }
083:
084: public void remove() {
085: // EMPTY
086: }
087: };
088: }
089: } else {
090: iter = NULL_ITER;
091: }
092: return iter;
093: }
094:
095: public Boolean getBooleanValue(ExpressionContext expressionContext)
096: throws Exception {
097: Object res = getValue(expressionContext);
098: return res instanceof Boolean ? (Boolean) res : null;
099: }
100:
101: public String getStringValue(ExpressionContext expressionContext)
102: throws Exception {
103: Object res = getValue(expressionContext);
104: if (res != null) {
105: return res.toString();
106: }
107: if (this .getCompiledExpression() == null) {
108: return this .getRaw();
109: }
110: return null;
111: }
112:
113: public Number getNumberValue(ExpressionContext expressionContext)
114: throws Exception {
115: Object res = getValue(expressionContext);
116: if (res instanceof Number) {
117: return (Number) res;
118: }
119: if (res != null) {
120: return Double.valueOf(res.toString());
121: }
122: return null;
123: }
124:
125: public int getIntValue(ExpressionContext expressionContext)
126: throws Exception {
127: Object res = getValue(expressionContext);
128: return res instanceof Number ? ((Number) res).intValue() : 0;
129: }
130:
131: public Object getValue(ExpressionContext expressionContext)
132: throws Exception {
133: if (this .getCompiledExpression() != null) {
134: Object compiled = this .getCompiledExpression();
135: if (compiled instanceof Expression)
136: return ((Expression) compiled)
137: .evaluate(expressionContext);
138: else
139: return compiled;
140: } else
141: return this .getRaw();
142: }
143:
144: public void setLenient(Boolean lenient) {
145: if (this .compiledExpression instanceof Expression)
146: //TODO: hack! bases on particular expression implementation.
147: ((Expression) this .compiledExpression).setProperty(
148: JXPathExpression.LENIENT, lenient);
149: }
150:
151: public static final Iterator EMPTY_ITER = new Iterator() {
152: public boolean hasNext() {
153: return false;
154: }
155:
156: public Object next() {
157: return null;
158: }
159:
160: public void remove() {
161: // EMPTY
162: }
163: };
164:
165: static public final Iterator NULL_ITER = new Iterator() {
166: public boolean hasNext() {
167: return true;
168: }
169:
170: public Object next() {
171: return null;
172: }
173:
174: public void remove() {
175: // EMPTY
176: }
177: };
178: }
|