KickJava   Java API By Example, From Geeks To Geeks.

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


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.jsp.JspException JavaDoc;
28 import org.meshcms.core.*;
29 import org.meshcms.util.*;
30
31 public class LangMenu extends AbstractTag {
32   private String JavaDoc separator = " ";
33   private String JavaDoc pre;
34   private String JavaDoc post;
35
36   public void setSeparator(String JavaDoc separator) {
37     if (separator != null) {
38       this.separator = separator;
39     }
40   }
41
42   public String JavaDoc getSeparator() {
43     return separator;
44   }
45
46   public void writeTag() throws IOException, JspException JavaDoc {
47     SiteMap siteMap = webSite.getSiteMap();
48
49     if (pagePath.isRoot() || siteMap.getPageInfo(pagePath) == null) {
50       return;
51     }
52
53     List langList = siteMap.getLangList();
54
55     if (langList.size() > 1) {
56       Iterator iter = langList.iterator();
57       boolean putSeparator = false;
58       Writer w = getOut();
59
60       if (pre != null) {
61        w.write(pre);
62       }
63
64       while (iter.hasNext()) {
65         if (putSeparator) {
66           w.write(separator);
67         }
68
69         putSeparator = true;
70         SiteMap.CodeLocalePair lang = (SiteMap.CodeLocalePair) iter.next();
71         String JavaDoc langCode = lang.getCode();
72         String JavaDoc localeName = Utils.encodeHTML(lang.getName());
73         String JavaDoc link = null;
74         String JavaDoc msg = null;
75
76         if (!langCode.equalsIgnoreCase(pagePath.getElementAt(0))) {
77           Path path = siteMap.getServedPath(pagePath.replace(0, langCode));
78
79           if (!webSite.getFile(path).isFile()) {
80             if (userInfo != null && userInfo.canWrite(webSite, path)) {
81               PageInfo ppi = siteMap.getParentPageInfo(pagePath);
82
83               if (ppi != null && ppi.getLevel() > 0) {
84                 Path pPath = ppi.getPath().replace(0, langCode);
85
86                 if (siteMap.getPageInfo(pPath) != null) {
87                   if (msg == null) {
88                     ResourceBundle bundle =
89                         ResourceBundle.getBundle("org/meshcms/webui/Locales",
90                         WebUtils.getPageLocale(pageContext));
91                     msg = Utils.replace(bundle.getString("confirmTranslation"),
92                         '\'', "\\'");
93                   }
94                   link = "javascript:if (confirm('" + msg +"')) location.href='" +
95                       afp + "/createpage.jsp?popup=false&newdir=false&fullpath=" +
96                       path + "';";
97                 }
98               }
99             }
100
101             if (link == null) {
102               path = new Path(langCode);
103             }
104           }
105
106           if (link == null) {
107             link = cp + webSite.getLink(path);
108           }
109         }
110
111         if (link == null) {
112           w.write(localeName);
113         } else {
114           w.write("<a HREF=\"" + link + "\">" + localeName + "</a>");
115         }
116       }
117
118       if (post != null) {
119         w.write(post);
120       }
121     }
122   }
123
124   public String JavaDoc getPre() {
125     return pre;
126   }
127
128   public void setPre(String JavaDoc pre) {
129     this.pre = pre;
130   }
131
132   public String JavaDoc getPost() {
133     return post;
134   }
135
136   public void setPost(String JavaDoc post) {
137     this.post = post;
138   }
139 }
140
Popular Tags