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: import java.util.*;
034:
035: /**
036: * @author Luke Blanshard (blanshlu@netscape.net)
037: * @author Drew Davidson (drew@ognl.org)
038: */
039: class ASTMap extends SimpleNode {
040: private static Class DEFAULT_MAP_CLASS;
041: private String className;
042:
043: static {
044: /* Try to get LinkedHashMap; if older JDK than 1.4 use HashMap */
045: try {
046: DEFAULT_MAP_CLASS = Class
047: .forName("java.util.LinkedHashMap");
048: } catch (ClassNotFoundException ex) {
049: DEFAULT_MAP_CLASS = HashMap.class;
050: }
051: }
052:
053: public ASTMap(int id) {
054: super (id);
055: }
056:
057: public ASTMap(OgnlParser p, int id) {
058: super (p, id);
059: }
060:
061: protected void setClassName(String value) {
062: className = value;
063: }
064:
065: protected Object getValueBody(OgnlContext context, Object source)
066: throws OgnlException {
067: Map answer;
068:
069: if (className == null) {
070: try {
071: answer = (Map) DEFAULT_MAP_CLASS.newInstance();
072: } catch (Exception ex) {
073: /* This should never happen */
074: throw new OgnlException("Default Map class '"
075: + DEFAULT_MAP_CLASS.getName()
076: + "' instantiation error", ex);
077: }
078: } else {
079: try {
080: answer = (Map) OgnlRuntime.classForName(context,
081: className).newInstance();
082: } catch (Exception ex) {
083: throw new OgnlException("Map implementor '" + className
084: + "' not found", ex);
085: }
086: }
087:
088: for (int i = 0; i < jjtGetNumChildren(); ++i) {
089: ASTKeyValue kv = (ASTKeyValue) children[i];
090: Node k = kv.getKey(), v = kv.getValue();
091:
092: answer.put(k.getValue(context, source), (v == null) ? null
093: : v.getValue(context, source));
094: }
095: return answer;
096: }
097:
098: public String toString() {
099: String result = "#";
100:
101: if (className != null) {
102: result = result + "@" + className + "@";
103: }
104: result = result + "{ ";
105: for (int i = 0; i < jjtGetNumChildren(); ++i) {
106: ASTKeyValue kv = (ASTKeyValue) children[i];
107:
108: if (i > 0) {
109: result = result + ", ";
110: }
111: result = result + kv.getKey() + " : " + kv.getValue();
112: }
113: return result + " }";
114: }
115: }
|