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