KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > ContextAccessor


1 /*
2  * $Id: ContextAccessor.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method;
25
26 import java.util.Map JavaDoc;
27
28 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
29
30 /**
31  * Used to flexibly access Map values, supporting the "." (dot) syntax for
32  * accessing sub-map values and the "[]" (square bracket) syntax for accessing
33  * list elements. See individual Map operations for more information.
34  *
35  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
36  * @version $Rev: 5462 $
37  * @since 2.1
38  */

39 public class ContextAccessor {
40
41     protected String JavaDoc name;
42     protected FlexibleMapAccessor fma;
43     protected boolean needsExpand;
44     protected boolean empty;
45
46     public ContextAccessor(String JavaDoc name) {
47         init(name);
48     }
49     
50     public ContextAccessor(String JavaDoc name, String JavaDoc defaultName) {
51         if (name == null || name.length() == 0) {
52             init(defaultName);
53         } else {
54             init(name);
55         }
56     }
57     
58     protected void init(String JavaDoc name) {
59         this.name = name;
60         if (name == null || name.length() == 0) {
61             empty = true;
62             needsExpand = false;
63             fma = new FlexibleMapAccessor(name);
64         } else {
65             empty = false;
66             int openPos = name.indexOf("${");
67             if (openPos != -1 && name.indexOf("}", openPos) != -1) {
68                 fma = null;
69                 needsExpand = true;
70             } else {
71                 fma = new FlexibleMapAccessor(name);
72                 needsExpand = false;
73             }
74         }
75     }
76     
77     public boolean isEmpty() {
78         return this.empty;
79     }
80     
81     /** Based on name get from Map or from List in Map */
82     public Object JavaDoc get(MethodContext methodContext) {
83         if (this.needsExpand) {
84             return methodContext.getEnv(name);
85         } else {
86             return methodContext.getEnv(fma);
87         }
88     }
89     
90     /** Based on name put in Map or from List in Map;
91      * If the brackets for a list are empty the value will be appended to the list,
92      * otherwise the value will be set in the position of the number in the brackets.
93      * If a "+" (plus sign) is included inside the square brackets before the index
94      * number the value will inserted/added at that point instead of set at the point.
95      */

96     public void put(MethodContext methodContext, Object JavaDoc value) {
97         if (this.needsExpand) {
98             methodContext.putEnv(name, value);
99         } else {
100             methodContext.putEnv(fma, value);
101         }
102     }
103     
104     /** Based on name remove from Map or from List in Map */
105     public Object JavaDoc remove(MethodContext methodContext) {
106         if (this.needsExpand) {
107             return methodContext.removeEnv(name);
108         } else {
109             return methodContext.removeEnv(fma);
110         }
111     }
112     
113     /** Based on name get from Map or from List in Map */
114     public Object JavaDoc get(Map JavaDoc theMap, MethodContext methodContext) {
115         if (this.needsExpand) {
116             FlexibleMapAccessor fma = new FlexibleMapAccessor(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 JavaDoc theMap, Object JavaDoc value, MethodContext methodContext) {
130         if (this.needsExpand) {
131             FlexibleMapAccessor fma = new FlexibleMapAccessor(methodContext.expandString(name));
132             fma.put(theMap, value);
133         } else {
134             fma.put(theMap, value);
135         }
136     }
137     
138     /** Based on name remove from Map or from List in Map */
139     public Object JavaDoc remove(Map JavaDoc theMap, MethodContext methodContext) {
140         if (this.needsExpand) {
141             FlexibleMapAccessor fma = new FlexibleMapAccessor(methodContext.expandString(name));
142             return fma.remove(theMap);
143         } else {
144             return fma.remove(theMap);
145         }
146     }
147     
148     /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */
149     public int hashCode() {
150         return this.name.hashCode();
151     }
152
153     /** The equals and hashCode methods are imnplemented just case this object is ever accidently used as a Map key */
154     public boolean equals(Object JavaDoc obj) {
155         if (obj instanceof ContextAccessor) {
156             ContextAccessor contextAccessor = (ContextAccessor) obj;
157             if (this.name == null) {
158                 return contextAccessor.name == null;
159             }
160             return this.name.equals(contextAccessor.name);
161         } else {
162             String JavaDoc str = (String JavaDoc) obj;
163             if (this.name == null) {
164                 return str == null;
165             }
166             return this.name.equals(str);
167         }
168     }
169
170     /** To be used for a string representation of the accessor, returns the original name. */
171     public String JavaDoc toString() {
172         return this.name;
173     }
174 }
175
Popular Tags