001: package org.apache.velocity.runtime.parser.node;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.velocity.runtime.RuntimeLogger;
023: import org.apache.velocity.runtime.log.Log;
024: import org.apache.velocity.runtime.log.RuntimeLoggerLog;
025: import org.apache.velocity.util.introspection.Introspector;
026:
027: /**
028: * Handles discovery and valuation of a
029: * boolean object property, of the
030: * form public boolean is<property> when executed.
031: *
032: * We do this separately as to preserve the current
033: * quasi-broken semantics of get<as is property>
034: * get< flip 1st char> get("property") and now followed
035: * by is<Property>
036: *
037: * @author <a href="geirm@apache.org">Geir Magnusson Jr.</a>
038: * @version $Id: BooleanPropertyExecutor.java 463298 2006-10-12 16:10:32Z henning $
039: */
040: public class BooleanPropertyExecutor extends PropertyExecutor {
041: /**
042: * @param log
043: * @param introspector
044: * @param clazz
045: * @param property
046: */
047: public BooleanPropertyExecutor(final Log log,
048: final Introspector introspector, final Class clazz,
049: final String property) {
050: super (log, introspector, clazz, property);
051: }
052:
053: /**
054: * @param rlog
055: * @param introspector
056: * @param clazz
057: * @param property
058: * @deprecated RuntimeLogger is deprecated. Use the other constructor.
059: */
060: public BooleanPropertyExecutor(final RuntimeLogger rlog,
061: final Introspector introspector, final Class clazz,
062: final String property) {
063: super (new RuntimeLoggerLog(rlog), introspector, clazz, property);
064: }
065:
066: protected void discover(final Class clazz, final String property) {
067: try {
068: Object[] params = {};
069:
070: StringBuffer sb = new StringBuffer("is");
071: sb.append(property);
072:
073: setMethod(getIntrospector().getMethod(clazz, sb.toString(),
074: params));
075:
076: if (!isAlive()) {
077: /*
078: * now the convenience, flip the 1st character
079: */
080:
081: char c = sb.charAt(2);
082:
083: if (Character.isLowerCase(c)) {
084: sb.setCharAt(2, Character.toUpperCase(c));
085: } else {
086: sb.setCharAt(2, Character.toLowerCase(c));
087: }
088:
089: setMethod(getIntrospector().getMethod(clazz,
090: sb.toString(), params));
091: }
092:
093: if (isAlive()) {
094: if (getMethod().getReturnType() != Boolean.TYPE) {
095: setMethod(null); // That case is rare but not unknown
096: }
097: }
098: }
099: /**
100: * pass through application level runtime exceptions
101: */
102: catch (RuntimeException e) {
103: throw e;
104: } catch (Exception e) {
105: log.error("While looking for boolean property getter for '"
106: + property + "':", e);
107: }
108: }
109: }
|