View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
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  package org.apache.jetspeed.services.jsp.tags;
17  
18  // Servlet API
19  import javax.servlet.jsp.JspException;
20  import javax.servlet.jsp.PageContext;
21  import javax.servlet.jsp.tagext.TagSupport;
22  
23  // Turbine Classes
24  import org.apache.turbine.services.jsp.JspService;
25  
26  // ECS support
27  import org.apache.ecs.ConcreteElement;
28  import org.apache.ecs.StringElement;
29  
30  // Jetspeed support
31  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
32  import org.apache.jetspeed.services.logging.JetspeedLogger;
33  import org.apache.jetspeed.services.rundata.JetspeedRunData;
34  import org.apache.jetspeed.services.customlocalization.CustomLocalizationTool;
35  import org.apache.jetspeed.services.customlocalization.CustomLocalization;
36  
37  /***
38   * Supporting class for the localization (l10n) tag.
39   * Returns a localized text for string specified in the "key" parameter.
40   * Use "alt" parameter to specify a default value if "key" is not translated.
41   *
42   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a>
43   * @version $Id: JetspeedL10NTag.java,v 1.6 2004/02/23 03:59:40 jford Exp $
44   */
45  public class JetspeedL10NTag extends TagSupport
46  {
47      /***
48       * Static initialization of the logger for this class
49       */    
50      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedL10NTag.class.getName());
51      
52      private CustomLocalizationTool tool = null;
53  
54      private String key = null;
55      private String alt = null;
56  
57      public void setKey(String value)
58      {
59          this.key = value;
60      }
61  
62      public String getKey()
63      {
64          return this.key;
65      }
66  
67      public void setAlt(String value)
68      {
69          this.alt = value;
70      }
71  
72      public String getAlt()
73      {
74          return this.alt;
75      }
76  
77      /***
78       * Method called when the tag is encountered to send attributes to the
79       * output stream
80       *
81       * @return SKIP_BODY, as it is intended to be a single tag.
82       */
83      public int doStartTag() throws JspException
84      {
85          JetspeedRunData data = (JetspeedRunData) pageContext.getAttribute(JspService.RUNDATA, PageContext.REQUEST_SCOPE);
86  
87          try
88          {
89              if (this.tool == null)
90              {
91                  this.tool = new CustomLocalizationTool();
92                  this.tool.init(data);
93              }
94              ConcreteElement result = null;
95              String def = this.alt != null && this.alt.trim().length() > 0 ? this.alt : this.key;
96  
97              try 
98              {
99                  String translation = this.tool.get(key, CustomLocalization.getLocale(data));
100 								
101                 translation = translation == null ? def : translation;
102                 result = new StringElement(translation);            
103             }
104             catch (Exception oe)
105             {
106                 result = new StringElement(def);
107                 logger.error("Exception", oe);
108             }
109 
110             // Output the result
111             if (result != null)
112             {
113                 pageContext.getOut().print(result);
114             }
115 
116         }
117         catch (Exception e)
118         {
119             String message = "Error processing key '" + this.key + "'.";
120             logger.error(message, e);
121             try
122             {
123                 data.getOut().print("Error translating key '" + this.key + "'. See log for more information.");
124             }
125             catch (java.io.IOException ioe)
126             {
127             }
128         }
129         return EVAL_BODY_INCLUDE;
130     }
131 }