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