KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > taglib > SetLocale


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.taglib;
24
25 import java.io.*;
26 import java.util.*;
27 import javax.servlet.http.*;
28 import javax.servlet.jsp.*;
29 import org.meshcms.core.*;
30 import org.meshcms.util.*;
31
32 /**
33  * Writes the context path as returned by
34  * <code>HttpServletRequest.getContextPath()</code>.
35  */

36 public final class SetLocale extends AbstractTag {
37   public static final String JavaDoc REDIRECT_ATTRIBUTE = "meshcms-redirect";
38
39   private String JavaDoc value;
40   private String JavaDoc defaultValue;
41   private String JavaDoc redirectRoot;
42
43   public void writeTag() throws IOException {
44     if (Utils.isTrue(redirectRoot) &&
45         webSite.getSiteMap().getPathInMenu(pagePath).isRoot()) {
46       if (setRedirectToLanguage(request,
47           (HttpServletResponse) pageContext.getResponse())) {
48         return;
49       }
50     }
51
52     Locale locale = null;
53
54     if (value != null) {
55       locale = Utils.getLocale(value);
56     } else if (!pagePath.isRoot()) {
57       for (int i = pagePath.getElementCount() - 1; locale == null && i >= 0; i--) {
58         locale = Utils.getLocale(pagePath.getElementAt(i));
59       }
60     }
61
62     if (locale == null) {
63       locale = Utils.getLocale(defaultValue);
64     }
65
66     if (locale != null) {
67       pageContext.setAttribute(HitFilter.LOCALE_ATTRIBUTE, locale,
68           PageContext.REQUEST_SCOPE);
69     }
70   }
71
72   public String JavaDoc getValue() {
73     return value;
74   }
75
76   public void setValue(String JavaDoc value) {
77     this.value = value;
78   }
79
80   public String JavaDoc getDefaultValue() {
81     return defaultValue;
82   }
83
84   public void setDefaultValue(String JavaDoc defaultValue) {
85     this.defaultValue = defaultValue;
86   }
87
88   public String JavaDoc getRedirectRoot() {
89     return redirectRoot;
90   }
91
92   public void setRedirectRoot(String JavaDoc redirectRoot) {
93     this.redirectRoot = redirectRoot;
94   }
95
96   public static boolean setRedirectToLanguage(HttpServletRequest request,
97       HttpServletResponse response) throws IOException {
98     if (response.isCommitted()) {
99       return false;
100     }
101
102     WebSite webSite = (WebSite) request.getAttribute(HitFilter.WEBSITE_ATTRIBUTE);
103     List available = webSite.getSiteMap().getLangList();
104     String JavaDoc[] accepted = WebUtils.getAcceptedLanguages(request);
105     SiteMap.CodeLocalePair chosen = null;
106
107     if (available != null && available.size() > 0) {
108       for (int i = 0; chosen == null && i < accepted.length; i++) {
109         Iterator iter = available.iterator();
110
111         while (chosen == null && iter.hasNext()) {
112           SiteMap.CodeLocalePair clp = (SiteMap.CodeLocalePair) iter.next();
113
114           if (clp.getCode().equalsIgnoreCase(accepted[i])) {
115             chosen = clp;
116           }
117         }
118       }
119
120       if (chosen == null) {
121         chosen = (SiteMap.CodeLocalePair) available.get(0);
122       }
123
124       WebUtils.setBlockCache(request);
125       request.setAttribute(REDIRECT_ATTRIBUTE, request.getContextPath() + '/' +
126           chosen.getCode() + '/');
127       return true;
128     }
129
130     return false;
131   }
132 }
133
Popular Tags