View Javadoc
1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package tools.gsf.facade.runtag.xlat;
18  
19  import COM.FutureTense.Interfaces.ICS;
20  import tools.gsf.facade.runtag.AbstractTagRunner;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * Wrapper around the XLAT.LOOKUP xml tag
27   *
28   * @author Mike Field
29   * @since August 15, 2008
30   */
31  public final class Lookup extends AbstractTagRunner {
32      // Default Constructor
33      public Lookup() {
34          super("XLAT.LOOKUP");
35      }
36  
37      /**
38       * Sets key to the value of <code>s</code>
39       *
40       * @param s The key of the db entry to return
41       */
42      public void setKey(String s) {
43          // validate first
44          if (s == null || s.length() == 0) {
45              throw new IllegalArgumentException("Invalid key string: " + s);
46          }
47          this.set("KEY", s);
48      }
49  
50      /**
51       * Sets varname to the value of <code>s</code>
52       *
53       * @param s The varname to output to.
54       */
55      public void setVarname(String s) {
56          // validate first
57          if (s == null || s.length() == 0) {
58              throw new IllegalArgumentException("Invalid varname string: " + s);
59          }
60          this.set("VARNAME", s);
61      }
62  
63      /**
64       * Sets locale to the value of <code>s</code>
65       *
66       * @param s The locale of the db entry
67       */
68      public void setLocale(String s) {
69          // validate first
70          if (s == null || s.length() == 0) {
71              throw new IllegalArgumentException("Invalid locale string: " + s);
72          }
73          this.set("LOCALE", s);
74      }
75  
76      /**
77       * Sets encode to the value of <code>s</code>
78       *
79       * @param s true or false (case sensitive)
80       */
81      public void setEncode(String s) {
82          // validate first
83          if (s == null || s.length() == 0 || !s.equals("true") && !s.equals("false")) {
84              throw new IllegalArgumentException("Invalid encode string: " + s);
85          }
86          this.set("ENCODE", s);
87      }
88  
89      /**
90       * Sets escape to the value of <code>s</code>
91       *
92       * @param s true or false (case sensitive)
93       */
94      public void setEscape(String s) {
95          // validate first
96          if (s == null || s.length() == 0 || !s.equals("true") && !s.equals("false")) {
97              throw new IllegalArgumentException("Invalid escape string: " + s);
98          }
99          this.set("ESCAPE", s);
100     }
101 
102     /**
103      * Sets evalall to the value of <code>s</code>
104      *
105      * @param s true or false (case sensitive)
106      */
107     public void setEvalAll(String s) {
108         // validate first
109         if (s == null || s.length() == 0 || !s.equals("true") && !s.equals("false")) {
110             throw new IllegalArgumentException("Invalid evalall string: " + s);
111         }
112         this.set("EVALALL", s);
113     }
114 
115     /**
116      * Sets the name and value of a custome argument to
117      * <code>argname=argvalue</code>
118      *
119      * @param argname  The name of the argument
120      * @param argvalue The name of the value
121      */
122     public void setArgument(String argname, String argvalue) {
123         // validate first
124         if (argname == null || argname.length() == 0) {
125             throw new IllegalArgumentException("Invalid argname string: " + argname);
126         }
127         if (argvalue == null || argvalue.length() == 0) {
128             throw new IllegalArgumentException("Invalid argvalue string: " + argvalue);
129         }
130         this.set(argname, argvalue);
131     }
132 
133     protected void preExecute(ICS ics) {
134         // work around a bug in the tag where a variable needs to exist for the
135         // mapping to work
136         super.preExecute(ics);
137 
138         List<String> newVars = new ArrayList<String>();
139         for (Object oKey : list.keySet()) {
140             String sKey = (String) oKey;
141             if (ics.GetVar(sKey) == null) {
142                 newVars.add(sKey);
143                 ics.SetVar(sKey, list.getValString(sKey));
144             }
145         }
146         ics.SetObj("NewVars", newVars);
147     }
148 
149     @SuppressWarnings("unchecked")
150     protected void postExecute(ICS ics) {
151         super.postExecute(ics);
152         List<String> newVars = (List<String>) ics.GetObj("NewVars");
153         for (String toRemove : newVars) {
154             ics.RemoveVar(toRemove);
155         }
156         ics.SetObj("NewVars", null);
157     }
158 
159     /**
160      * Utility function for returning a simpel lookup string, with an option for
161      * encoding. Note that this function is not appropriate when variable
162      * substitution is required or when escaping is required. However, it is a
163      * convenient shortcut for most cases.
164      *
165      * @param ics    ICS context
166      * @param key    i18n key
167      * @param locale locale into which to look up the translation
168      * @param encode to encode or not
169      * @return the correct string
170      */
171     public static String lookup(ICS ics, String key, String locale, boolean encode) {
172         Lookup l = new Lookup();
173         l.setKey(key);
174         l.setEncode(encode ? "true" : "false");
175         l.setLocale(locale);
176         String var = ics.genID(true);
177         l.setVarname(var);
178         String result = null;
179         try {
180             l.execute(ics);
181             result = ics.GetVar(var);
182         } finally {
183             ics.RemoveVar(var);
184         }
185         return result;
186     }
187 
188 }