ViewCommon.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 8k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * This file creation date: 02/04/2004 - 20:31:35
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.view.forum.common;
  44. import java.io.UnsupportedEncodingException;
  45. import java.net.URI;
  46. import java.net.URLEncoder;
  47. import java.text.SimpleDateFormat;
  48. import java.util.Date;
  49. import org.apache.commons.lang.StringUtils;
  50. import net.jforum.JForumExecutionContext;
  51. import net.jforum.context.RequestContext;
  52. import net.jforum.entities.User;
  53. import net.jforum.exceptions.ForumException;
  54. import net.jforum.util.preferences.ConfigKeys;
  55. import net.jforum.util.preferences.SystemGlobals;
  56. import net.jforum.util.preferences.TemplateKeys;
  57. import freemarker.template.SimpleHash;
  58. /**
  59.  * @author Rafael Steil
  60.  * @version $Id: ViewCommon.java,v 1.32 2007/09/20 16:07:08 rafaelsteil Exp $
  61.  */
  62. public final class ViewCommon
  63. {
  64. /**
  65.  * Prepared the user context to use data pagination. 
  66.  * The following variables are set to the context:
  67.  * <p>
  68.  *  <ul>
  69.  *  <li> <i>totalPages</i> - total number of pages
  70.  *  <li> <i>recordsPerPage</i> - how many records will be shown on each page
  71.  *  <li> <i>totalRecords</i> - number of records fount
  72.  *  <li> <i>thisPage</i> - the current page being shown
  73.  *  <li> <i>start</i> - 
  74.  *  </ul>
  75.  * </p>
  76.  * @param start int
  77.  * @param totalRecords  int
  78.  * @param recordsPerPage int
  79.  */
  80. public static void contextToPagination(int start, int totalRecords, int recordsPerPage)
  81. {
  82. SimpleHash context = JForumExecutionContext.getTemplateContext();
  83. context.put("totalPages", new Double(Math.ceil((double) totalRecords / (double) recordsPerPage)));
  84. context.put("recordsPerPage", new Integer(recordsPerPage));
  85. context.put("totalRecords", new Integer(totalRecords));
  86. context.put("thisPage", new Double(Math.ceil((double) (start + 1) / (double) recordsPerPage)));
  87. context.put("start", new Integer(start));
  88. }
  89. /**
  90.  * Prepares the template context to show the login page, using the current URI as return path.
  91.  * @return TemplateKeys.USER_LOGIN
  92.  */
  93. public static String contextToLogin() 
  94. {
  95. RequestContext request = JForumExecutionContext.getRequest();
  96. String uri = request.getRequestURI();
  97. String query = request.getQueryString();
  98. String returnPath = query == null ? uri : uri + "?" + query;
  99. return contextToLogin(returnPath);
  100. }
  101. /**
  102.  * Prepares the template context to show the login page, using "returnPath" as return path
  103.  * @param returnPath the URI to use as return path
  104.  * @return TemplateKeys.USER_LOGIN
  105.  */
  106. public static String contextToLogin(String returnPath)
  107. {
  108. JForumExecutionContext.getTemplateContext().put("returnPath", returnPath);
  109. if (ConfigKeys.TYPE_SSO.equals(SystemGlobals.getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
  110. String redirect = SystemGlobals.getValue(ConfigKeys.SSO_REDIRECT);
  111. if (!StringUtils.isEmpty(redirect)) {
  112. URI redirectUri = URI.create(redirect);
  113. if (!redirectUri.isAbsolute()) {
  114. throw new ForumException("SSO redirect URL should start with a scheme");
  115. }
  116. try {
  117. returnPath = URLEncoder.encode( ViewCommon.getForumLink() + returnPath, "UTF-8");
  118. }
  119. catch (UnsupportedEncodingException e) {}
  120. if (redirect.indexOf('?') == -1) {
  121. redirect += "?";
  122. }
  123. else {
  124. redirect += "&";
  125. }
  126. redirect += "returnUrl=" + returnPath;
  127. JForumExecutionContext.setRedirect(redirect);
  128. }
  129. }
  130. return TemplateKeys.USER_LOGIN;
  131. }
  132. /**
  133.  * Returns the initial page to start fetching records from.
  134.  *   
  135.  * @return The initial page number
  136.  */
  137. public static int getStartPage()
  138. {
  139. String s = JForumExecutionContext.getRequest().getParameter("start");
  140. int start;
  141. if (StringUtils.isEmpty(s)) {
  142. start = 0;
  143. }
  144. else {
  145. start = Integer.parseInt(s);
  146. if (start < 0) {
  147. start = 0;
  148. }
  149. }
  150. return start;
  151. }
  152. /**
  153.  * Gets the forum base link.
  154.  * The returned link has a trailing slash
  155.  * @return The forum link, with the trailing slash
  156.  */
  157. public static String getForumLink()
  158. {
  159. String forumLink = SystemGlobals.getValue(ConfigKeys.FORUM_LINK);
  160. if (forumLink.charAt(forumLink.length() - 1) != '/') {
  161. forumLink += "/";
  162. }
  163. return forumLink;
  164. }
  165. public static String toUtf8String(String s)
  166. {
  167. StringBuffer sb = new StringBuffer();
  168. for (int i = 0; i < s.length(); i++) {
  169. char c = s.charAt(i);
  170. if ((c >= 0) && (c <= 255)) {
  171. sb.append(c);
  172. }
  173. else {
  174. byte[] b;
  175. try {
  176. b = Character.toString(c).getBytes("utf-8");
  177. }
  178. catch (Exception ex) {
  179. System.out.println(ex);
  180. b = new byte[0];
  181. }
  182. for (int j = 0; j < b.length; j++) {
  183. int k = b[j];
  184. if (k < 0) {
  185. k += 256;
  186. }
  187. sb.append('%').append(Integer.toHexString(k).toUpperCase());
  188. }
  189. }
  190. }
  191. return sb.toString();
  192. }
  193. /**
  194.  * Formats a date using the pattern defined in the configuration file.
  195.  * The key is the value of {@link net.jforum.util.preferences.ConfigKeys.DATE_TIME_FORMAT}
  196.  * @param date the date to format
  197.  * @return the string with the formated date
  198.  */
  199. public static String formatDate(Date date) 
  200. {
  201. SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
  202. return df.format(date);
  203. }
  204. /**
  205.  * Escapes &lt; by &amp;lt; and &gt; by &amp;gt;
  206.  * @param contents the string to parse
  207.  * @return the new string
  208.  */
  209. public static String espaceHtml(String contents)
  210. {
  211. StringBuffer sb = new StringBuffer(contents);
  212. replaceAll(sb, "<", "&lt");
  213. replaceAll(sb, ">", "&gt;");
  214. return sb.toString();
  215. }
  216. /**
  217.  * Replaces some string with another value
  218.  * @param sb the StrinbBuilder with the contents to work on
  219.  * @param what the string to be replaced
  220.  * @param with the new value
  221.  * @return the new string
  222.  */
  223. public static String replaceAll(StringBuffer sb, String what, String with)
  224. {
  225. int pos = sb.indexOf(what);
  226. while (pos > -1) {
  227. sb.replace(pos, pos + what.length(), with);
  228. pos = sb.indexOf(what);
  229. }
  230. return sb.toString();
  231. }
  232. /**
  233.  * @see #replaceAll(StringBuffer, String, String)
  234.      * @param contents String
  235.      * @param what String
  236.      * @param with String
  237.      * @return String
  238.  */
  239. public static String replaceAll(String contents, String what, String with)
  240. {
  241. return replaceAll(new StringBuffer(contents), what, with);
  242. }
  243. /**
  244.  * Parse the user's signature, to make it proper to visualization
  245.  * @param u the user instance
  246.  */
  247. public static void prepareUserSignature(User u)
  248. {
  249. if (u.getSignature() != null) {
  250. StringBuffer sb = new StringBuffer(u.getSignature());
  251. replaceAll(sb, "n", "<br />");
  252. u.setSignature(sb.toString());
  253. u.setSignature(PostCommon.prepareTextForDisplayExceptCodeTag(u.getSignature(), true, true));
  254. }
  255. }
  256. }