KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > operation > Convert


1 /*
2  * $Id: Convert.java 6009 2005-10-24 06:44:29Z 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.operation;
25
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.GeneralException;
32 import org.ofbiz.base.util.ObjectType;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  * Convert the current field from the in-map and place it in the out-map
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @version $Rev: 6009 $
40  * @since 2.0
41  */

42 public class Convert extends SimpleMapOperation {
43     public static final String JavaDoc module = Convert.class.getName();
44     
45     String JavaDoc toField;
46     String JavaDoc type;
47     boolean replace = true;
48     boolean setIfNull = true;
49     String JavaDoc format;
50
51     public Convert(Element JavaDoc element, SimpleMapProcess simpleMapProcess) {
52         super(element, simpleMapProcess);
53         this.toField = element.getAttribute("to-field");
54         if (this.toField == null || this.toField.length() == 0) {
55             this.toField = this.fieldName;
56         }
57
58         type = element.getAttribute("type");
59         // if anything but false it will be true
60
replace = !"false".equals(element.getAttribute("replace"));
61         // if anything but false it will be true
62
setIfNull = !"false".equals(element.getAttribute("set-if-null"));
63
64         format = element.getAttribute("format");
65     }
66
67     public void exec(Map JavaDoc inMap, Map JavaDoc results, List JavaDoc messages, Locale JavaDoc locale, ClassLoader JavaDoc loader) {
68         Object JavaDoc fieldObject = inMap.get(fieldName);
69
70         if (fieldObject == null) {
71             if (setIfNull && (replace || !results.containsKey(toField)))
72                 results.put(toField, null);
73             return;
74         }
75
76         // if an incoming string is empty,
77
// set to null if setIfNull is true, otherwise do nothing, ie treat as if null
78
if (fieldObject instanceof java.lang.String JavaDoc) {
79             if (((String JavaDoc) fieldObject).length() == 0) {
80                 if (setIfNull && (replace || !results.containsKey(toField)))
81                     results.put(toField, null);
82                 return;
83             }
84         }
85
86         Object JavaDoc convertedObject = null;
87
88         try {
89             convertedObject = ObjectType.simpleTypeConvert(fieldObject, type, format, locale);
90         } catch (GeneralException e) {
91             addMessage(messages, loader, locale);
92             Debug.logError(e, "Error in convert simple-map-processor operation: " + e.toString(), module);
93             return;
94         }
95
96         if (convertedObject == null)
97             return;
98
99         if (replace) {
100             results.put(toField, convertedObject);
101             // if (Debug.infoOn()) Debug.logInfo("[SimpleMapProcessor.Converted.exec] Put converted value \"" + convertedObject + "\" in field \"" + toField + "\"", module);
102
} else {
103             if (results.containsKey(toField)) {// do nothing
104
} else {
105                 results.put(toField, convertedObject);
106                 // if (Debug.infoOn()) Debug.logInfo("[SimpleMapProcessor.Converted.exec] Put converted value \"" + convertedObject + "\" in field \"" + toField + "\"", module);
107
}
108         }
109     }
110 }
111
Popular Tags