001: //--------------------------------------------------------------------------
002: // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package ognl;
032:
033: /**
034: * @author Luke Blanshard (blanshlu@netscape.net)
035: * @author Drew Davidson (drew@ognl.org)
036: */
037: class ASTProperty extends SimpleNode {
038: private boolean indexedAccess = false;
039:
040: public ASTProperty(int id) {
041: super (id);
042: }
043:
044: public ASTProperty(OgnlParser p, int id) {
045: super (p, id);
046: }
047:
048: public void setIndexedAccess(boolean value) {
049: indexedAccess = value;
050: }
051:
052: /**
053: Returns true iff this property is itself an index reference.
054: */
055: public boolean isIndexedAccess() {
056: return indexedAccess;
057: }
058:
059: /**
060: Returns true if this property is described by an IndexedPropertyDescriptor
061: and that if followed by an index specifier it will call the index get/set
062: methods rather than go through property accessors.
063: */
064: public int getIndexedPropertyType(OgnlContext context, Object source)
065: throws OgnlException {
066: if (!isIndexedAccess()) {
067: Object property = getProperty(context, source);
068:
069: if (property instanceof String) {
070: return OgnlRuntime.getIndexedPropertyType(context,
071: (source == null) ? null : source.getClass(),
072: (String) property);
073: }
074: }
075: return OgnlRuntime.INDEXED_PROPERTY_NONE;
076: }
077:
078: public Object getProperty(OgnlContext context, Object source)
079: throws OgnlException {
080: return children[0].getValue(context, context.getRoot());
081: }
082:
083: protected Object getValueBody(OgnlContext context, Object source)
084: throws OgnlException {
085: Object result, property = getProperty(context, source);
086: Node indexSibling;
087:
088: result = OgnlRuntime.getProperty(context, source, property);
089: if (result == null) {
090: result = OgnlRuntime.getNullHandler(
091: OgnlRuntime.getTargetClass(source))
092: .nullPropertyValue(context, source, property);
093: }
094: return result;
095: }
096:
097: protected void setValueBody(OgnlContext context, Object target,
098: Object value) throws OgnlException {
099: OgnlRuntime.setProperty(context, target, getProperty(context,
100: target), value);
101: }
102:
103: public boolean isNodeSimpleProperty(OgnlContext context)
104: throws OgnlException {
105: return (children != null) && (children.length == 1)
106: && ((SimpleNode) children[0]).isConstant(context);
107: }
108:
109: public String toString() {
110: String result;
111:
112: if (isIndexedAccess()) {
113: result = "[" + children[0] + "]";
114: } else {
115: result = ((ASTConst) children[0]).getValue().toString();
116: }
117: return result;
118: }
119: }
|