001: /*
002: * $Id: FieldObject.java,v 1.1 2003/08/17 06:06:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 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:
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030: import org.ofbiz.minilang.*;
031:
032: /**
033: * A type of MethodObject that represents an Object value in a certain location
034: *
035: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
036: * @version $Revision: 1.1 $
037: * @since 2.0
038: */
039: public class FieldObject extends MethodObject {
040:
041: public static final String module = FieldObject.class.getName();
042:
043: ContextAccessor fieldAcsr;
044: ContextAccessor mapAcsr;
045: String type;
046:
047: public FieldObject(Element element, SimpleMethod simpleMethod) {
048: super (element, simpleMethod);
049: fieldAcsr = new ContextAccessor(element
050: .getAttribute("field-name"));
051: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
052: type = element.getAttribute("type");
053: if (UtilValidate.isEmpty(type)) {
054: type = "String";
055: }
056: }
057:
058: /** Get the name for the type of the object */
059: public String getTypeName() {
060: return type;
061: }
062:
063: public Class getTypeClass(ClassLoader loader) {
064: try {
065: return ObjectType.loadClass(type, loader);
066: } catch (ClassNotFoundException e) {
067: Debug.logError(e, "Could not find class for type: " + type,
068: module);
069: return null;
070: }
071: }
072:
073: public Object getObject(MethodContext methodContext) {
074: Object fieldVal = null;
075:
076: if (!mapAcsr.isEmpty()) {
077: Map fromMap = (Map) mapAcsr.get(methodContext);
078: if (fromMap == null) {
079: Debug
080: .logWarning(
081: "Map not found with name "
082: + mapAcsr
083: + ", not getting Object value, returning null.",
084: module);
085: return null;
086: }
087: fieldVal = fieldAcsr.get(fromMap, methodContext);
088: } else {
089: // no map name, try the env
090: fieldVal = fieldAcsr.get(methodContext);
091: }
092:
093: if (fieldVal == null) {
094: if (Debug.infoOn())
095: Debug
096: .logInfo(
097: "Field value not found with name "
098: + fieldAcsr
099: + " in Map with name "
100: + mapAcsr
101: + ", not getting Object value, returning null.",
102: module);
103: return null;
104: }
105:
106: return fieldVal;
107: }
108: }
|