KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: StringAppend.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  * Appends 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.2
41  */

42 public class StringAppend extends MethodOperation {
43     
44     public static final String JavaDoc module = StringAppend.class.getName();
45     
46     String JavaDoc string;
47     String JavaDoc prefix;
48     String JavaDoc suffix;
49     ContextAccessor mapAcsr;
50     ContextAccessor fieldAcsr;
51     ContextAccessor argListAcsr;
52
53     public StringAppend(Element element, SimpleMethod simpleMethod) {
54         super(element, simpleMethod);
55         string = element.getAttribute("string");
56         prefix = element.getAttribute("prefix");
57         suffix = element.getAttribute("suffix");
58         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
59         fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
60         argListAcsr = new ContextAccessor(element.getAttribute("arg-list-name"));
61     }
62
63     public boolean exec(MethodContext methodContext) {
64         if (!mapAcsr.isEmpty()) {
65             Map toMap = (Map) mapAcsr.get(methodContext);
66
67             if (toMap == null) {
68                 if (Debug.verboseOn()) Debug.logVerbose("Map not found with name " + mapAcsr + ", creating new map", module);
69                 toMap = new HashMap();
70                 mapAcsr.put(methodContext, toMap);
71             }
72             
73             String JavaDoc oldValue = (String JavaDoc) fieldAcsr.get(toMap, methodContext);
74             fieldAcsr.put(toMap, this.appendString(oldValue, methodContext), methodContext);
75         } else {
76             String JavaDoc oldValue = (String JavaDoc) fieldAcsr.get(methodContext);
77             fieldAcsr.put(methodContext, this.appendString(oldValue, methodContext));
78         }
79
80         return true;
81     }
82     
83     public String JavaDoc appendString(String JavaDoc oldValue, MethodContext methodContext) {
84         String JavaDoc value = methodContext.expandString(string);
85         String JavaDoc prefixValue = methodContext.expandString(prefix);
86         String JavaDoc suffixValue = methodContext.expandString(suffix);
87         
88         if (!argListAcsr.isEmpty()) {
89             List argList = (List) argListAcsr.get(methodContext);
90             if (argList != null && argList.size() > 0) {
91                 value = MessageFormat.format(value, argList.toArray());
92             }
93         }
94
95         StringBuffer JavaDoc newValue = new StringBuffer JavaDoc();
96         if (value != null && value.length() > 0) {
97             if (oldValue == null || oldValue.length() == 0) {
98                 newValue.append(value);
99             } else {
100                 newValue.append(oldValue);
101                 if (prefixValue != null) newValue.append(prefixValue);
102                 newValue.append(value);
103                 if (suffixValue != null) newValue.append(suffixValue);
104             }
105         } else {
106             if (oldValue == null || oldValue.length() == 0) {
107                 newValue.append(oldValue);
108             }
109         }
110         
111         return newValue.toString();
112     }
113
114     public String JavaDoc rawString() {
115         // TODO: something more than the empty tag
116
return "<string-append string=\"" + this.string + "\" prefix=\"" + this.prefix + "\" suffix=\"" + this.suffix + "\" field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
117     }
118     public String JavaDoc expandedString(MethodContext methodContext) {
119         // TODO: something more than a stub/dummy
120
return this.rawString();
121     }
122 }
123
Popular Tags