KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > envops > ToString


1 /*
2  * $Id: ToString.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

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 /**
35  * Converts the specified field to a String, using toString()
36  *
37  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
38  * @version $Rev: 5462 $
39  * @since 2.0
40  */

41 public class ToString extends MethodOperation {
42     
43     public static final String JavaDoc module = ToString.class.getName();
44     
45     ContextAccessor mapAcsr;
46     ContextAccessor fieldAcsr;
47     String JavaDoc format;
48     Integer JavaDoc 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 JavaDoc npStr = element.getAttribute("numeric-padding");
57         if (UtilValidate.isNotEmpty(npStr)) {
58             try {
59                 this.numericPadding = Integer.valueOf(npStr);
60             } catch (Exception JavaDoc 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                 // it seems silly to create a new map, but necessary since whenever
72
// an env field like a Map or List is referenced it should be created, even if empty
73
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 JavaDoc obj = fieldAcsr.get(toMap, methodContext);
79             if (obj != null) {
80                 fieldAcsr.put(toMap, doToString(obj, methodContext), methodContext);
81             }
82         } else {
83             Object JavaDoc 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 JavaDoc doToString(Object JavaDoc obj, MethodContext methodContext) {
93         String JavaDoc outStr = null;
94         try {
95             if (UtilValidate.isNotEmpty(format)) {
96                 outStr = (String JavaDoc) 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 JavaDoc outStrBfr = new StringBuffer JavaDoc(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 JavaDoc rawString() {
117         // TODO: something more than the empty tag
118
return "<to-string field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
119     }
120     public String JavaDoc expandedString(MethodContext methodContext) {
121         // TODO: something more than a stub/dummy
122
return this.rawString();
123     }
124 }
125
Popular Tags