001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.servlets;
017:
018: import java.text.MessageFormat;
019: import java.util.LinkedHashMap;
020: import org.apache.commons.lang.StringUtils;
021: import org.jamwiki.Environment;
022: import org.jamwiki.WikiMessage;
023: import org.jamwiki.utils.LinkUtil;
024: import org.jamwiki.utils.NamespaceHandler;
025: import org.jamwiki.utils.WikiLink;
026: import org.jamwiki.utils.WikiLogger;
027:
028: /**
029: * The <code>WikiPageInfo</code> class provides an object containing common
030: * data used for generating wiki page display.
031: */
032: public class WikiPageInfo {
033:
034: private static final WikiLogger logger = WikiLogger
035: .getLogger(WikiPageInfo.class.getName());
036: protected static final String JSP_TOPIC = "topic.jsp";
037: private boolean admin = false;
038: private String contentJsp = JSP_TOPIC;
039: private WikiMessage pageTitle = null;
040: private String redirectName = null;
041: private boolean special = false;
042: private LinkedHashMap tabMenu = new LinkedHashMap();
043: private String topicName = "";
044: private LinkedHashMap userMenu = new LinkedHashMap();
045:
046: /**
047: *
048: */
049: protected WikiPageInfo() {
050: }
051:
052: /**
053: * Reset all parameters of the current <code>WikiPageInfo</code> object
054: * to default values.
055: */
056: protected void reset() {
057: this .admin = false;
058: this .contentJsp = JSP_TOPIC;
059: this .pageTitle = null;
060: this .redirectName = null;
061: this .special = false;
062: this .tabMenu = new LinkedHashMap();
063: this .topicName = "";
064: this .userMenu = new LinkedHashMap();
065: }
066:
067: /**
068: * If a page is a part of the admin tool then this method will return
069: * <code>true</code>.
070: *
071: * @return <code>true</code> if a page is part of the admin tool,
072: * <code>false</code> otherwise.
073: */
074: public boolean getAdmin() {
075: return this .admin;
076: }
077:
078: /**
079: * Set a flag indicating whether or not the page being displayed is a part
080: * of the admin tool.
081: *
082: * @param admin <code>true</code> if a page is part of the admin tool,
083: * <code>false</code> otherwise.
084: */
085: public void setAdmin(boolean admin) {
086: this .admin = admin;
087: }
088:
089: /**
090: * Retrieve the name of the JSP page that will be used to display the
091: * results of this page request.
092: *
093: * @return The name of the JSP page that will be used to display the
094: * results of the page request.
095: */
096: public String getContentJsp() {
097: return this .contentJsp;
098: }
099:
100: /**
101: * Set the JSP page that will display the results of this page request.
102: * If no value is specified then the default is to display the request
103: * using the topic display JSP.
104: *
105: * @param contentJsp The JSP page that should be used to display the
106: * results of the page request.
107: */
108: public void setContentJsp(String contentJsp) {
109: this .contentJsp = contentJsp;
110: }
111:
112: /**
113: * Return a description for the current page that can be used in an HTML
114: * meta tag.
115: *
116: * @return A description for the current page that can be used in an HTML
117: * meta tag.
118: */
119: public String getMetaDescription() {
120: String pattern = Environment
121: .getValue(Environment.PROP_BASE_META_DESCRIPTION);
122: if (StringUtils.isBlank(pattern)) {
123: return "";
124: }
125: MessageFormat formatter = new MessageFormat(pattern);
126: Object params[] = new Object[1];
127: params[0] = (this .topicName == null) ? "" : this .topicName;
128: return formatter.format(params);
129: }
130:
131: /**
132: * Return the title for the current page.
133: *
134: * @return The title for the current page.
135: */
136: public WikiMessage getPageTitle() {
137: return this .pageTitle;
138: }
139:
140: /**
141: * Set the title for the current page.
142: *
143: * @param pageTitle A <code>WikiMessage</code> object that contains a
144: * translatable page title value.
145: */
146: public void setPageTitle(WikiMessage pageTitle) {
147: this .pageTitle = pageTitle;
148: }
149:
150: /**
151: * If printable pages should open in a new window then this method will
152: * return the HTML target "_blank", otherwise this method returns an
153: * empty String.
154: *
155: * @return The HTML target "_blank" if printable pages should open in a
156: * new window, otherwise an empty String.
157: */
158: public String getPrintTarget() {
159: return (Environment
160: .getBooleanValue(Environment.PROP_PRINT_NEW_WINDOW)) ? "_blank"
161: : "";
162: }
163:
164: /**
165: * If the topic currently being displayed is the result of a redirect from
166: * another topic, return the name of the topic that is being redirected
167: * from.
168: *
169: * @return The name of the topic being redirected from, or
170: * <code>null</code> if the current page is not the result of a redirect.
171: */
172: public String getRedirectName() {
173: return this .redirectName;
174: }
175:
176: /**
177: * If the topic currently being displayed is the result of a redirect from
178: * another topic, set the name of the topic that is being redirected
179: * from.
180: *
181: * @param redirectName The name of the topic being redirected from, or
182: * <code>null</code> if the current page is not the result of a redirect.
183: */
184: public void setRedirectName(String redirectName) {
185: this .redirectName = redirectName;
186: }
187:
188: /**
189: * Return the base title used with RSS feeds.
190: *
191: * @return The base title used with RSS feeds.
192: */
193: public String getRSSTitle() {
194: return Environment.getValue("rss-title");
195: }
196:
197: /**
198: * Return a flag indicating whether or not the current page is a "Special:"
199: * page, as opposed to a standard topic.
200: *
201: * @return <code>true</code> if the current page is a "Special:" page,
202: * <code>false</code> otherwise.
203: */
204: public boolean getSpecial() {
205: return this .special;
206: }
207:
208: /**
209: * Set a flag indicating whether or not the current page is a "Special:"
210: * page, as opposed to a standard topic.
211: *
212: * @param special Set to <code>true</code> if the current page is a
213: * "Special:" page, <code>false</code> otherwise.
214: */
215: public void setSpecial(boolean special) {
216: this .special = special;
217: }
218:
219: /**
220: * Return the namespace of the topic displayed by the current page. The
221: * namespace is the part of topic name, up to the colon. For regular
222: * articles the namespace is an empty string.
223: *
224: * The namespace cannot be set directly, only the topic name can be set.
225: *
226: * @return The wiki namespace of this page, or an empty string for pages
227: * in the main namespace.
228: * @see #getPagename
229: * @see #getTopicName
230: */
231: public String getNamespace() {
232: WikiLink wikiLink = LinkUtil.parseWikiLink(this .getTopicName());
233: return wikiLink.getNamespace();
234: }
235:
236: /**
237: * Return the name of the page, i.e. the name of the topic displayed by
238: * the current page, without the namespace.
239: *
240: * @return Name of the page.
241: */
242: public String getPagename() {
243: WikiLink wikiLink = LinkUtil.parseWikiLink(this .getTopicName());
244: return wikiLink.getArticle();
245: }
246:
247: /**
248: * Return a LinkedHashMap containing the topic and text for all links
249: * that should appear for the tab menu.
250: *
251: * @return A LinkedHashMap containing the topic and text for all links
252: * that should appear for the tab menu.
253: */
254: public LinkedHashMap getTabMenu() {
255: return this .tabMenu;
256: }
257:
258: /**
259: * Set a LinkedHashMap containing the topic and text for all links
260: * that should appear for the tab menu.
261: *
262: * @param tabMenu A LinkedHashMap containing the topic and text for all
263: * links that should appear for the tab menu.
264: */
265: public void setTabMenu(LinkedHashMap tabMenu) {
266: this .tabMenu = tabMenu;
267: }
268:
269: /**
270: * Return the name of the topic being displayed by the current page.
271: *
272: * @return The name of the topic being displayed by the current page.
273: */
274: public String getTopicName() {
275: return this .topicName;
276: }
277:
278: /**
279: * Set the name of the topic being displayed by the current page.
280: *
281: * @param topicName The name of the topic being displayed by the current
282: * page.
283: */
284: public void setTopicName(String topicName) {
285: this .topicName = topicName;
286: }
287:
288: /**
289: * Return a LinkedHashMap containing the topic and text for all links
290: * that should appear for the user menu.
291: *
292: * @return A LinkedHashMap containing the topic and text for all links
293: * that should appear for the user menu.
294: */
295: public LinkedHashMap getUserMenu() {
296: return this .userMenu;
297: }
298:
299: /**
300: * Set a LinkedHashMap containing the topic and text for all links
301: * that should appear for the user menu.
302: *
303: * @param userMenu A LinkedHashMap containing the topic and text for all
304: * links that should appear for the user menu.
305: */
306: public void setUserMenu(LinkedHashMap userMenu) {
307: this .userMenu = userMenu;
308: }
309:
310: /**
311: * If the page currently being viewed is a user page or a user comments
312: * page return <code>true</code>
313: *
314: * @return <code>true</code> if the page currently being viewed is a
315: * user page, otherwise <code>false</code>.
316: */
317: public boolean isUserPage() {
318: WikiLink wikiLink = LinkUtil.parseWikiLink(this .getTopicName());
319: if (wikiLink.getNamespace().equals(
320: NamespaceHandler.NAMESPACE_USER)) {
321: return true;
322: }
323: if (wikiLink.getNamespace().equals(
324: NamespaceHandler.NAMESPACE_USER_COMMENTS)) {
325: return true;
326: }
327: return false;
328: }
329: }
|