KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > mapping > DefaultPageLocationResolver


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.pages.mapping;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.riotfamily.common.web.util.PathCompleter;
32 import org.riotfamily.common.web.util.ServletUtils;
33 import org.riotfamily.pages.Page;
34 import org.riotfamily.pages.PageLocation;
35 import org.springframework.util.StringUtils;
36
37 /**
38  * @author Felix Gnass [fgnass at neteye dot de]
39  * @author Jan-Frederic Linde [jfl at neteye dot de]
40  * @since 6.5
41  */

42 public class DefaultPageLocationResolver implements PageLocationResolver {
43
44     private Collection JavaDoc locales;
45
46     private Locale JavaDoc fixedLocale = null;
47
48     private PathCompleter pathCompleter;
49
50     public DefaultPageLocationResolver(PathCompleter pathCompleter) {
51         this.pathCompleter = pathCompleter;
52     }
53
54     public void setLocales(Collection JavaDoc locales) {
55         this.locales = locales;
56         if (locales != null && locales.size() == 1) {
57             fixedLocale = (Locale JavaDoc) locales.iterator().next();
58         }
59     }
60
61     protected boolean localesInPath() {
62         return locales != null && locales.size() > 1;
63     }
64
65     public PageLocation getPageLocation(HttpServletRequest JavaDoc request) {
66         String JavaDoc path = ServletUtils.getPathWithoutServletMapping(request);
67         if (path.length() > 1 && path.endsWith("/")) {
68             path = path.substring(0, path.length() - 1);
69         }
70         Locale JavaDoc locale = null;
71         if (localesInPath()) {
72             if (path.length() > 1) {
73                 int i = path.indexOf('/', 1);
74                 if (i > 1) {
75                     String JavaDoc localeString = path.substring(1, i);
76                     locale = StringUtils.parseLocaleString(localeString);
77                     path = path.substring(i);
78                 }
79             }
80         }
81         else {
82             locale = fixedLocale;
83         }
84         return new PageLocation(null, path, locale);
85     }
86
87     public String JavaDoc getUrl(Page page) {
88         PageLocation location = new PageLocation(page);
89         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
90         if (localesInPath() && location.getLocale() != null) {
91             url.append('/');
92             url.append(location.getLocale().toString().toLowerCase());
93         }
94         url.append(location.getPath());
95         if (!page.isFolder() || pathCompleter.isPrefixMapping()) {
96             pathCompleter.addServletMapping(url);
97         }
98         return url.toString();
99     }
100 }
101
Popular Tags