001: /*
002: * $Id: ContextAccessor.java,v 1.1 2003/08/17 06:06:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method;
025:
026: import java.util.*;
027: import org.ofbiz.base.util.*;
028:
029: /**
030: * Used to flexibly access Map values, supporting the "." (dot) syntax for
031: * accessing sub-map values and the "[]" (square bracket) syntax for accessing
032: * list elements. See individual Map operations for more information.
033: *
034: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
035: * @version $Revision: 1.1 $
036: * @since 2.1
037: */
038: public class ContextAccessor {
039:
040: protected String name;
041: protected FlexibleMapAccessor fma;
042: protected boolean needsExpand;
043: protected boolean empty;
044:
045: public ContextAccessor(String name) {
046: init(name);
047: }
048:
049: public ContextAccessor(String name, String defaultName) {
050: if (name == null || name.length() == 0) {
051: init(defaultName);
052: } else {
053: init(name);
054: }
055: }
056:
057: protected void init(String name) {
058: this .name = name;
059: if (name == null || name.length() == 0) {
060: empty = true;
061: needsExpand = false;
062: fma = new FlexibleMapAccessor(name);
063: } else {
064: empty = false;
065: int openPos = name.indexOf("${");
066: if (openPos != -1 && name.indexOf("}", openPos) != -1) {
067: fma = null;
068: needsExpand = true;
069: } else {
070: fma = new FlexibleMapAccessor(name);
071: needsExpand = false;
072: }
073: }
074: }
075:
076: public boolean isEmpty() {
077: return this .empty;
078: }
079:
080: /** Based on name get from Map or from List in Map */
081: public Object get(MethodContext methodContext) {
082: if (this .needsExpand) {
083: return methodContext.getEnv(name);
084: } else {
085: return methodContext.getEnv(fma);
086: }
087: }
088:
089: /** Based on name put in Map or from List in Map;
090: * If the brackets for a list are empty the value will be appended to the list,
091: * otherwise the value will be set in the position of the number in the brackets.
092: * If a "+" (plus sign) is included inside the square brackets before the index
093: * number the value will inserted/added at that point instead of set at the point.
094: */
095: public void put(MethodContext methodContext, Object value) {
096: if (this .needsExpand) {
097: methodContext.putEnv(name, value);
098: } else {
099: methodContext.putEnv(fma, value);
100: }
101: }
102:
103: /** Based on name remove from Map or from List in Map */
104: public Object remove(MethodContext methodContext) {
105: if (this .needsExpand) {
106: return methodContext.removeEnv(name);
107: } else {
108: return methodContext.removeEnv(fma);
109: }
110: }
111:
112: /** Based on name get from Map or from List in Map */
113: public Object get(Map theMap, MethodContext methodContext) {
114: if (this .needsExpand) {
115: FlexibleMapAccessor fma = new FlexibleMapAccessor(
116: methodContext.expandString(name));
117: return fma.get(theMap);
118: } else {
119: return fma.get(theMap);
120: }
121: }
122:
123: /** Based on name put in Map or from List in Map;
124: * If the brackets for a list are empty the value will be appended to the list,
125: * otherwise the value will be set in the position of the number in the brackets.
126: * If a "+" (plus sign) is included inside the square brackets before the index
127: * number the value will inserted/added at that point instead of set at the point.
128: */
129: public void put(Map theMap, Object value,
130: MethodContext methodContext) {
131: if (this .needsExpand) {
132: FlexibleMapAccessor fma = new FlexibleMapAccessor(
133: methodContext.expandString(name));
134: fma.put(theMap, value);
135: } else {
136: fma.put(theMap, value);
137: }
138: }
139:
140: /** Based on name remove from Map or from List in Map */
141: public Object remove(Map theMap, MethodContext methodContext) {
142: if (this .needsExpand) {
143: FlexibleMapAccessor fma = new FlexibleMapAccessor(
144: methodContext.expandString(name));
145: return fma.remove(theMap);
146: } else {
147: return fma.remove(theMap);
148: }
149: }
150:
151: /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */
152: public int hashCode() {
153: return this .name.hashCode();
154: }
155:
156: /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */
157: public boolean equals(Object obj) {
158: if (obj instanceof ContextAccessor) {
159: ContextAccessor contextAccessor = (ContextAccessor) obj;
160: if (this .name == null) {
161: return contextAccessor.name == null;
162: }
163: return this .name.equals(contextAccessor.name);
164: } else {
165: String str = (String) obj;
166: if (this .name == null) {
167: return str == null;
168: }
169: return this .name.equals(str);
170: }
171: }
172:
173: /** To be used for a string representation of the accessor, returns the original name. */
174: public String toString() {
175: return this.name;
176: }
177: }
|