001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005:
006: package com.opensymphony.xwork.util;
007:
008: import java.util.HashMap;
009: import java.util.Map;
010:
011: /**
012: * Manages variables in the OgnlContext and returns values
013: * to be used by the application.
014: *
015: * @author Gabe
016: */
017: public class OgnlContextState {
018:
019: public static final String CURRENT_PROPERTY_PATH = "current.property.path";
020: public static final String FULL_PROPERTY_PATH = "current.property.path";
021: private static final String GETTING_BY_KEY_PROPERTY = "xwork.getting.by.key.property";
022:
023: private static final String SET_MAP_KEY = "set.map.key";
024:
025: public static boolean isCreatingNullObjects(Map context) {
026: //TODO
027: return getBooleanProperty(
028: InstantiatingNullHandler.CREATE_NULL_OBJECTS, context);
029: }
030:
031: public static void setCreatingNullObjects(Map context,
032: boolean creatingNullObjects) {
033: setBooleanValue(InstantiatingNullHandler.CREATE_NULL_OBJECTS,
034: context, creatingNullObjects);
035: }
036:
037: public static boolean isGettingByKeyProperty(Map context) {
038: return getBooleanProperty(GETTING_BY_KEY_PROPERTY, context);
039: }
040:
041: public static void setDenyMethodExecution(Map context,
042: boolean denyMethodExecution) {
043: setBooleanValue(XWorkMethodAccessor.DENY_METHOD_EXECUTION,
044: context, denyMethodExecution);
045: }
046:
047: public static boolean isDenyMethodExecution(Map context) {
048: return getBooleanProperty(
049: XWorkMethodAccessor.DENY_METHOD_EXECUTION, context);
050: }
051:
052: public static void setGettingByKeyProperty(Map context,
053: boolean gettingByKeyProperty) {
054: setBooleanValue(GETTING_BY_KEY_PROPERTY, context,
055: gettingByKeyProperty);
056: }
057:
058: public static boolean isReportingConversionErrors(Map context) {
059: return getBooleanProperty(
060: XWorkConverter.REPORT_CONVERSION_ERRORS, context);
061: }
062:
063: public static void setReportingConversionErrors(Map context,
064: boolean reportingErrors) {
065: setBooleanValue(XWorkConverter.REPORT_CONVERSION_ERRORS,
066: context, reportingErrors);
067: }
068:
069: public static Class getLastBeanClassAccessed(Map context) {
070: return (Class) context
071: .get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED);
072: }
073:
074: public static void setLastBeanPropertyAccessed(Map context,
075: String property) {
076: context.put(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED,
077: property);
078: }
079:
080: public static String getLastBeanPropertyAccessed(Map context) {
081: return (String) context
082: .get(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED);
083: }
084:
085: public static void setLastBeanClassAccessed(Map context, Class clazz) {
086: context.put(XWorkConverter.LAST_BEAN_CLASS_ACCESSED, clazz);
087: }
088:
089: /**
090: * Gets the current property path but not completely.
091: * It does not use the [ and ] used in some representations
092: * of Maps and Lists. The reason for this is that the current
093: * property path is only currently used for caching purposes
094: * so there is no real reason to have an exact replica.
095: *
096: * <p/>So if the real path is myProp.myMap['myKey'] this would
097: * return myProp.myMap.myKey.
098: *
099: * @param context
100: */
101: public static String getCurrentPropertyPath(Map context) {
102: return (String) context.get(CURRENT_PROPERTY_PATH);
103: }
104:
105: public static String getFullPropertyPath(Map context) {
106: return (String) context.get(FULL_PROPERTY_PATH);
107: }
108:
109: public static void setFullPropertyPath(Map context, String path) {
110: context.put(FULL_PROPERTY_PATH, path);
111:
112: }
113:
114: public static void updateCurrentPropertyPath(Map context,
115: Object name) {
116: String currentPath = getCurrentPropertyPath(context);
117: if (name != null) {
118: if (currentPath != null) {
119: currentPath = currentPath + "." + name.toString();
120: } else {
121: currentPath = name.toString();
122: }
123: context.put(CURRENT_PROPERTY_PATH, currentPath);
124: }
125: }
126:
127: public static void setSetMap(Map context, Map setMap, String path) {
128: Map mapOfSetMaps = (Map) context.get(SET_MAP_KEY);
129: if (mapOfSetMaps == null) {
130: mapOfSetMaps = new HashMap();
131: context.put(SET_MAP_KEY, mapOfSetMaps);
132: }
133: mapOfSetMaps.put(path, setMap);
134: }
135:
136: public static Map getSetMap(Map context, String path) {
137: Map mapOfSetMaps = (Map) context.get(SET_MAP_KEY);
138: if (mapOfSetMaps == null) {
139: return null;
140: }
141: return (Map) mapOfSetMaps.get(path);
142: }
143:
144: private static boolean getBooleanProperty(String property,
145: Map context) {
146: Boolean myBool = (Boolean) context.get(property);
147: return (myBool == null) ? false : myBool.booleanValue();
148: }
149:
150: private static void setBooleanValue(String property, Map context,
151: boolean value) {
152: context.put(property, new Boolean(value));
153: }
154:
155: /**
156: *
157: */
158: public static void clearCurrentPropertyPath(Map context) {
159: context.put(CURRENT_PROPERTY_PATH, null);
160:
161: }
162:
163: public static void clear(Map context) {
164: context.put(XWorkConverter.LAST_BEAN_CLASS_ACCESSED, null);
165: context.put(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED, null);
166:
167: context.put(CURRENT_PROPERTY_PATH, null);
168: context.put(FULL_PROPERTY_PATH, null);
169:
170: }
171:
172: }
|