KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: StringToField.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.text.*;
27 import java.util.*;
28
29 import org.w3c.dom.*;
30
31 import org.ofbiz.base.util.*;
32 import org.ofbiz.minilang.*;
33 import org.ofbiz.minilang.method.*;
34
35 /**
36  * Copies the specified String to a field
37  *
38  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
39  * @version $Rev: 5462 $
40  * @since 2.0
41  */

42 public class StringToField extends MethodOperation {
43     
44     public static final String JavaDoc module = StringToField.class.getName();
45     
46     String JavaDoc string;
47     ContextAccessor mapAcsr;
48     ContextAccessor fieldAcsr;
49     ContextAccessor argListAcsr;
50     String JavaDoc messageFieldName;
51
52     public StringToField(Element element, SimpleMethod simpleMethod) {
53         super(element, simpleMethod);
54         string = element.getAttribute("string");
55         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
56         fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
57         argListAcsr = new ContextAccessor(element.getAttribute("arg-list-name"));
58         messageFieldName = element.getAttribute("message-field-name");
59     }
60
61     public boolean exec(MethodContext methodContext) {
62         String JavaDoc valueStr = methodContext.expandString(string);
63         
64         if (!argListAcsr.isEmpty()) {
65             List argList = (List) argListAcsr.get(methodContext);
66             if (argList != null && argList.size() > 0) {
67                 valueStr = MessageFormat.format(valueStr, argList.toArray());
68             }
69         }
70
71         Object JavaDoc value;
72         if (this.messageFieldName != null && this.messageFieldName.length() > 0) {
73             value = new MessageString(valueStr, this.messageFieldName, true);
74         } else {
75             value = valueStr;
76         }
77         
78         if (!mapAcsr.isEmpty()) {
79             Map toMap = (Map) mapAcsr.get(methodContext);
80
81             if (toMap == null) {
82                 if (Debug.verboseOn()) Debug.logVerbose("Map not found with name " + mapAcsr + ", creating new map", module);
83                 toMap = new HashMap();
84                 mapAcsr.put(methodContext, toMap);
85             }
86             fieldAcsr.put(toMap, value, methodContext);
87         } else {
88             fieldAcsr.put(methodContext, value);
89         }
90
91         return true;
92     }
93
94     public String JavaDoc rawString() {
95         // TODO: something more than the empty tag
96
return "<string-to-field string=\"" + this.string + "\" field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
97     }
98     public String JavaDoc expandedString(MethodContext methodContext) {
99         // TODO: something more than a stub/dummy
100
return this.rawString();
101     }
102 }
103
Popular Tags