001: /*
002: * $Id: ToString.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.envops;
025:
026: import java.util.*;
027:
028: import org.w3c.dom.*;
029:
030: import org.ofbiz.base.util.*;
031: import org.ofbiz.minilang.*;
032: import org.ofbiz.minilang.method.*;
033:
034: /**
035: * Converts the specified field to a String, using toString()
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.1 $
039: * @since 2.0
040: */
041: public class ToString extends MethodOperation {
042:
043: public static final String module = ToString.class.getName();
044:
045: ContextAccessor mapAcsr;
046: ContextAccessor fieldAcsr;
047: String format;
048: Integer numericPadding;
049:
050: public ToString(Element element, SimpleMethod simpleMethod) {
051: super (element, simpleMethod);
052: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
053: fieldAcsr = new ContextAccessor(element
054: .getAttribute("field-name"));
055: format = element.getAttribute("format");
056:
057: String npStr = element.getAttribute("numeric-padding");
058: if (UtilValidate.isNotEmpty(npStr)) {
059: try {
060: this .numericPadding = Integer.valueOf(npStr);
061: } catch (Exception e) {
062: Debug
063: .logError(
064: e,
065: "Error parsing numeric-padding attribute value on the to-string element",
066: module);
067: }
068: }
069: }
070:
071: public boolean exec(MethodContext methodContext) {
072: if (!mapAcsr.isEmpty()) {
073: Map toMap = (Map) mapAcsr.get(methodContext);
074:
075: if (toMap == null) {
076: // it seems silly to create a new map, but necessary since whenever
077: // an env field like a Map or List is referenced it should be created, even if empty
078: if (Debug.verboseOn())
079: Debug.logVerbose("Map not found with name "
080: + mapAcsr + ", creating new map", module);
081: toMap = new HashMap();
082: mapAcsr.put(methodContext, toMap);
083: }
084:
085: Object obj = fieldAcsr.get(toMap, methodContext);
086: if (obj != null) {
087: fieldAcsr.put(toMap, doToString(obj, methodContext),
088: methodContext);
089: }
090: } else {
091: Object obj = fieldAcsr.get(methodContext);
092: if (obj != null) {
093: fieldAcsr.put(methodContext, doToString(obj,
094: methodContext));
095: }
096: }
097:
098: return true;
099: }
100:
101: public String doToString(Object obj, MethodContext methodContext) {
102: String outStr = null;
103: try {
104: if (UtilValidate.isNotEmpty(format)) {
105: outStr = (String) ObjectType.simpleTypeConvert(obj,
106: "java.lang.String", format, methodContext
107: .getLocale());
108: } else {
109: outStr = obj.toString();
110: }
111: } catch (GeneralException e) {
112: Debug.logError(e, "", module);
113: outStr = obj.toString();
114: }
115:
116: if (this .numericPadding != null) {
117: StringBuffer outStrBfr = new StringBuffer(outStr);
118: while (this .numericPadding.intValue() > outStrBfr.length()) {
119: outStrBfr.insert(0, '0');
120: }
121: outStr = outStrBfr.toString();
122: }
123:
124: return outStr;
125: }
126: }
|