001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method;
019:
020: import java.util.Map;
021:
022: import org.ofbiz.base.util.collections.FlexibleMapAccessor;
023:
024: /**
025: * Used to flexibly access Map values, supporting the "." (dot) syntax for
026: * accessing sub-map values and the "[]" (square bracket) syntax for accessing
027: * list elements. See individual Map operations for more information.
028: */
029: public class ContextAccessor {
030:
031: protected String name;
032: protected FlexibleMapAccessor fma;
033: protected boolean needsExpand;
034: protected boolean empty;
035:
036: public ContextAccessor(String name) {
037: init(name);
038: }
039:
040: public ContextAccessor(String name, String defaultName) {
041: if (name == null || name.length() == 0) {
042: init(defaultName);
043: } else {
044: init(name);
045: }
046: }
047:
048: protected void init(String name) {
049: this .name = name;
050: if (name == null || name.length() == 0) {
051: empty = true;
052: needsExpand = false;
053: fma = new FlexibleMapAccessor(name);
054: } else {
055: empty = false;
056: int openPos = name.indexOf("${");
057: if (openPos != -1 && name.indexOf("}", openPos) != -1) {
058: fma = null;
059: needsExpand = true;
060: } else {
061: fma = new FlexibleMapAccessor(name);
062: needsExpand = false;
063: }
064: }
065: }
066:
067: public boolean isEmpty() {
068: return this .empty;
069: }
070:
071: /** Based on name get from Map or from List in Map */
072: public Object get(MethodContext methodContext) {
073: if (this .needsExpand) {
074: return methodContext.getEnv(name);
075: } else {
076: return methodContext.getEnv(fma);
077: }
078: }
079:
080: /** Based on name put in Map or from List in Map;
081: * If the brackets for a list are empty the value will be appended to the list,
082: * otherwise the value will be set in the position of the number in the brackets.
083: * If a "+" (plus sign) is included inside the square brackets before the index
084: * number the value will inserted/added at that point instead of set at the point.
085: */
086: public void put(MethodContext methodContext, Object value) {
087: if (this .needsExpand) {
088: methodContext.putEnv(name, value);
089: } else {
090: methodContext.putEnv(fma, value);
091: }
092: }
093:
094: /** Based on name remove from Map or from List in Map */
095: public Object remove(MethodContext methodContext) {
096: if (this .needsExpand) {
097: return methodContext.removeEnv(name);
098: } else {
099: return methodContext.removeEnv(fma);
100: }
101: }
102:
103: /** Based on name get from Map or from List in Map */
104: public Object get(Map theMap, MethodContext methodContext) {
105: if (this .needsExpand) {
106: FlexibleMapAccessor fma = new FlexibleMapAccessor(
107: methodContext.expandString(name));
108: return fma.get(theMap);
109: } else {
110: return fma.get(theMap);
111: }
112: }
113:
114: /** Based on name put in Map or from List in Map;
115: * If the brackets for a list are empty the value will be appended to the list,
116: * otherwise the value will be set in the position of the number in the brackets.
117: * If a "+" (plus sign) is included inside the square brackets before the index
118: * number the value will inserted/added at that point instead of set at the point.
119: */
120: public void put(Map theMap, Object value,
121: MethodContext methodContext) {
122: if (this .needsExpand) {
123: FlexibleMapAccessor fma = new FlexibleMapAccessor(
124: methodContext.expandString(name));
125: fma.put(theMap, value);
126: } else {
127: fma.put(theMap, value);
128: }
129: }
130:
131: /** Based on name remove from Map or from List in Map */
132: public Object remove(Map theMap, MethodContext methodContext) {
133: if (this .needsExpand) {
134: FlexibleMapAccessor fma = new FlexibleMapAccessor(
135: methodContext.expandString(name));
136: return fma.remove(theMap);
137: } else {
138: return fma.remove(theMap);
139: }
140: }
141:
142: /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */
143: public int hashCode() {
144: return this .name.hashCode();
145: }
146:
147: /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */
148: public boolean equals(Object obj) {
149: if (obj instanceof ContextAccessor) {
150: ContextAccessor contextAccessor = (ContextAccessor) obj;
151: if (this .name == null) {
152: return contextAccessor.name == null;
153: }
154: return this .name.equals(contextAccessor.name);
155: } else {
156: String str = (String) obj;
157: if (this .name == null) {
158: return str == null;
159: }
160: return this .name.equals(str);
161: }
162: }
163:
164: /** To be used for a string representation of the accessor, returns the original name. */
165: public String toString() {
166: return this.name;
167: }
168: }
|