1 24 package org.ofbiz.minilang.method.envops; 25 26 import java.util.*; 27 28 import org.w3c.dom.*; 29 30 import org.ofbiz.base.util.*; 31 import org.ofbiz.minilang.*; 32 import org.ofbiz.minilang.method.*; 33 34 41 public class ToString extends MethodOperation { 42 43 public static final String module = ToString.class.getName(); 44 45 ContextAccessor mapAcsr; 46 ContextAccessor fieldAcsr; 47 String format; 48 Integer numericPadding; 49 50 public ToString(Element element, SimpleMethod simpleMethod) { 51 super(element, simpleMethod); 52 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 53 fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 54 format = element.getAttribute("format"); 55 56 String npStr = element.getAttribute("numeric-padding"); 57 if (UtilValidate.isNotEmpty(npStr)) { 58 try { 59 this.numericPadding = Integer.valueOf(npStr); 60 } catch (Exception e) { 61 Debug.logError(e, "Error parsing numeric-padding attribute value on the to-string element", module); 62 } 63 } 64 } 65 66 public boolean exec(MethodContext methodContext) { 67 if (!mapAcsr.isEmpty()) { 68 Map toMap = (Map) mapAcsr.get(methodContext); 69 70 if (toMap == null) { 71 if (Debug.verboseOn()) Debug.logVerbose("Map not found with name " + mapAcsr + ", creating new map", module); 74 toMap = new HashMap(); 75 mapAcsr.put(methodContext, toMap); 76 } 77 78 Object obj = fieldAcsr.get(toMap, methodContext); 79 if (obj != null) { 80 fieldAcsr.put(toMap, doToString(obj, methodContext), methodContext); 81 } 82 } else { 83 Object obj = fieldAcsr.get(methodContext); 84 if (obj != null) { 85 fieldAcsr.put(methodContext, doToString(obj, methodContext)); 86 } 87 } 88 89 return true; 90 } 91 92 public String doToString(Object obj, MethodContext methodContext) { 93 String outStr = null; 94 try { 95 if (UtilValidate.isNotEmpty(format)) { 96 outStr = (String ) ObjectType.simpleTypeConvert(obj, "java.lang.String", format, methodContext.getLocale()); 97 } else { 98 outStr = obj.toString(); 99 } 100 } catch (GeneralException e) { 101 Debug.logError(e, "", module); 102 outStr = obj.toString(); 103 } 104 105 if (this.numericPadding != null) { 106 StringBuffer outStrBfr = new StringBuffer (outStr); 107 while (this.numericPadding.intValue() > outStrBfr.length()) { 108 outStrBfr.insert(0, '0'); 109 } 110 outStr = outStrBfr.toString(); 111 } 112 113 return outStr; 114 } 115 116 public String rawString() { 117 return "<to-string field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 119 } 120 public String expandedString(MethodContext methodContext) { 121 return this.rawString(); 123 } 124 } 125 | Popular Tags |