1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.servlet.StringServletResponse;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.taglib.util.IncludeTag;
29  
30  import java.io.IOException;
31  
32  import java.util.Locale;
33  
34  import javax.servlet.RequestDispatcher;
35  import javax.servlet.ServletContext;
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.jsp.JspException;
40  
41  /**
42   * <a href="LanguageTag.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class LanguageTag extends IncludeTag {
48  
49      public static final int LIST_ICON = 0;
50  
51      public static final int LIST_LONG_TEXT = 1;
52  
53      public static final int LIST_SHORT_TEXT = 2;
54  
55      public static final int SELECT_BOX = 3;
56  
57      public static void doTag(
58              ServletContext ctx, HttpServletRequest req, HttpServletResponse res)
59          throws IOException, ServletException {
60  
61          doTag(
62              _PAGE, _FORM_NAME, _FORM_ACTION, _NAME, null, _DISPLAY_STYLE, ctx,
63              req, res);
64      }
65  
66      public static void doTag(
67              String formName, String formAction, String name,
68              String[] languageIds, int displayStyle, ServletContext ctx,
69              HttpServletRequest req, HttpServletResponse res)
70          throws IOException, ServletException {
71  
72          doTag(
73              _PAGE, formName, formAction, name, languageIds, displayStyle, ctx,
74              req, res);
75      }
76  
77      public static void doTag(
78              String page, String formName, String formAction, String name,
79              String[] languageIds, int displayStyle, ServletContext ctx,
80              HttpServletRequest req, HttpServletResponse res)
81          throws IOException, ServletException {
82  
83          req.setAttribute("liferay-ui:language:formName", formName);
84          req.setAttribute("liferay-ui:language:formAction", formAction);
85          req.setAttribute("liferay-ui:language:name", name);
86  
87          Locale[] locales = null;
88  
89          if ((languageIds == null) || (languageIds.length == 0)) {
90              locales = LanguageUtil.getAvailableLocales();
91          }
92          else {
93              locales = LocaleUtil.fromLanguageIds(languageIds);
94          }
95  
96          req.setAttribute("liferay-ui:language:locales", locales);
97  
98          req.setAttribute(
99              "liferay-ui:language:displayStyle", String.valueOf(displayStyle));
100 
101         RequestDispatcher rd = ctx.getRequestDispatcher(page);
102 
103         rd.include(req, res);
104     }
105 
106     public int doEndTag() throws JspException {
107         try {
108             ServletContext ctx = getServletContext();
109             HttpServletRequest req = getServletRequest();
110             StringServletResponse res = getServletResponse();
111 
112             doTag(
113                 _formName, _formAction, _name, _languageIds, _displayStyle, ctx,
114                 req, res);
115 
116             pageContext.getOut().print(res.getString());
117 
118             return EVAL_PAGE;
119         }
120         catch (Exception e) {
121             throw new JspException(e);
122         }
123     }
124 
125     public void setFormName(String formName) {
126         _formName = formName;
127     }
128 
129     public void setFormAction(String formAction) {
130         _formAction = formAction;
131     }
132 
133     public void setName(String name) {
134         _name = name;
135     }
136 
137     public void setLanguageIds(String[] languageIds) {
138         _languageIds = languageIds;
139     }
140 
141     public void setDisplayStyle(int displayStyle) {
142         _displayStyle = displayStyle;
143     }
144 
145     protected String getDefaultPage() {
146         return _PAGE;
147     }
148 
149     private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
150 
151     private static final String _FORM_NAME = "fm";
152 
153     private static final String _FORM_ACTION = null;
154 
155     private static final String _NAME = "languageId";
156 
157     private static final int _DISPLAY_STYLE = 0;
158 
159     private String _formName = _FORM_NAME;
160     private String _formAction = _FORM_ACTION;
161     private String _name = _NAME;
162     private String[] _languageIds;
163     private int _displayStyle = _DISPLAY_STYLE;
164 
165 }