KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > otherops > PropertyToField


1 /*
2  * $Id: PropertyToField.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.otherops;
25
26 import java.text.*;
27 import java.util.*;
28
29 import org.w3c.dom.*;
30 import org.ofbiz.base.util.*;
31 import org.ofbiz.minilang.*;
32 import org.ofbiz.minilang.method.*;
33
34 /**
35  * Copies an properties file property value to a field
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 PropertyToField extends MethodOperation {
42     
43     public static final String JavaDoc module = PropertyToField.class.getName();
44     
45     String JavaDoc resource;
46     String JavaDoc property;
47     ContextAccessor mapAcsr;
48     ContextAccessor fieldAcsr;
49     String JavaDoc defaultVal;
50     boolean noLocale;
51     ContextAccessor argListAcsr;
52
53     public PropertyToField(Element element, SimpleMethod simpleMethod) {
54         super(element, simpleMethod);
55         resource = element.getAttribute("resource");
56         property = element.getAttribute("property");
57         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
58         fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
59         defaultVal = element.getAttribute("default");
60         // defaults to false, ie anything but true is false
61
noLocale = "true".equals(element.getAttribute("no-locale"));
62         argListAcsr = new ContextAccessor(element.getAttribute("arg-list-name"));
63     }
64
65     public boolean exec(MethodContext methodContext) {
66         String JavaDoc resource = methodContext.expandString(this.resource);
67         String JavaDoc property = methodContext.expandString(this.property);
68         
69         String JavaDoc value = null;
70         if (noLocale) {
71             value = UtilProperties.getPropertyValue(resource, property);
72         } else {
73             value = UtilProperties.getMessage(resource, property, methodContext.getLocale());
74         }
75         if (value == null || value.length() == 0) {
76             value = defaultVal;
77         }
78         
79         // note that expanding the value string here will handle defaultValue and the string from
80
// the properties file; if we decide later that we don't want the string from the properties
81
// file to be expanded we should just expand the defaultValue at the beginning of this method.
82
value = methodContext.expandString(value);
83
84         if (!argListAcsr.isEmpty()) {
85             List argList = (List) argListAcsr.get(methodContext);
86             if (argList != null && argList.size() > 0) {
87                 value = MessageFormat.format(value, argList.toArray());
88             }
89         }
90
91         if (!mapAcsr.isEmpty()) {
92             Map toMap = (Map) mapAcsr.get(methodContext);
93
94             if (toMap == null) {
95                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", creating new map", module);
96                 toMap = new HashMap();
97                 mapAcsr.put(methodContext, toMap);
98             }
99             fieldAcsr.put(toMap, value, methodContext);
100         } else {
101             fieldAcsr.put(methodContext, value);
102         }
103
104         return true;
105     }
106
107     public String JavaDoc rawString() {
108         // TODO: add all attributes and other info
109
return "<property-to-field field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
110     }
111     public String JavaDoc expandedString(MethodContext methodContext) {
112         // TODO: something more than a stub/dummy
113
return this.rawString();
114     }
115 }
116
Popular Tags