Source Code Cross Referenced for ViewCommon.java in  » Forum » JForum-2.1.8 » net » jforum » view » forum » common » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Forum » JForum 2.1.8 » net.jforum.view.forum.common 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) JForum Team
003:         * All rights reserved.
004:         * 
005:         * Redistribution and use in source and binary forms, 
006:         * with or without modification, are permitted provided 
007:         * that the following conditions are met:
008:         * 
009:         * 1) Redistributions of source code must retain the above 
010:         * copyright notice, this list of conditions and the 
011:         * following  disclaimer.
012:         * 2)  Redistributions in binary form must reproduce the 
013:         * above copyright notice, this list of conditions and 
014:         * the following disclaimer in the documentation and/or 
015:         * other materials provided with the distribution.
016:         * 3) Neither the name of "Rafael Steil" nor 
017:         * the names of its contributors may be used to endorse 
018:         * or promote products derived from this software without 
019:         * specific prior written permission.
020:         * 
021:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
022:         * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
023:         * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
024:         * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
025:         * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
026:         * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
027:         * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
028:         * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
029:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
030:         * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
031:         * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
032:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
033:         * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
034:         * IN CONTRACT, STRICT LIABILITY, OR TORT 
035:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
036:         * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
037:         * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038:         * 
039:         * This file creation date: 02/04/2004 - 20:31:35
040:         * The JForum Project
041:         * http://www.jforum.net
042:         */
043:        package net.jforum.view.forum.common;
044:
045:        import java.io.UnsupportedEncodingException;
046:        import java.net.URI;
047:        import java.net.URLEncoder;
048:        import java.text.SimpleDateFormat;
049:        import java.util.Date;
050:
051:        import org.apache.commons.lang.StringUtils;
052:
053:        import net.jforum.JForumExecutionContext;
054:        import net.jforum.context.RequestContext;
055:        import net.jforum.entities.User;
056:        import net.jforum.exceptions.ForumException;
057:        import net.jforum.util.preferences.ConfigKeys;
058:        import net.jforum.util.preferences.SystemGlobals;
059:        import net.jforum.util.preferences.TemplateKeys;
060:        import freemarker.template.SimpleHash;
061:
062:        /**
063:         * @author Rafael Steil
064:         * @version $Id: ViewCommon.java,v 1.32 2007/09/20 16:07:08 rafaelsteil Exp $
065:         */
066:        public final class ViewCommon {
067:            /**
068:             * Prepared the user context to use data pagination. 
069:             * The following variables are set to the context:
070:             * <p>
071:             * 	<ul>
072:             * 		<li> <i>totalPages</i> - total number of pages
073:             * 		<li> <i>recordsPerPage</i> - how many records will be shown on each page
074:             * 		<li> <i>totalRecords</i> - number of records fount
075:             * 		<li> <i>thisPage</i> - the current page being shown
076:             * 		<li> <i>start</i> - 
077:             * 	</ul>
078:             * </p>
079:             * @param start int
080:             * @param totalRecords  int
081:             * @param recordsPerPage int
082:             */
083:            public static void contextToPagination(int start, int totalRecords,
084:                    int recordsPerPage) {
085:                SimpleHash context = JForumExecutionContext
086:                        .getTemplateContext();
087:
088:                context
089:                        .put("totalPages", new Double(Math
090:                                .ceil((double) totalRecords
091:                                        / (double) recordsPerPage)));
092:                context.put("recordsPerPage", new Integer(recordsPerPage));
093:                context.put("totalRecords", new Integer(totalRecords));
094:                context.put("thisPage", new Double(Math
095:                        .ceil((double) (start + 1) / (double) recordsPerPage)));
096:                context.put("start", new Integer(start));
097:            }
098:
099:            /**
100:             * Prepares the template context to show the login page, using the current URI as return path.
101:             * @return TemplateKeys.USER_LOGIN
102:             */
103:            public static String contextToLogin() {
104:                RequestContext request = JForumExecutionContext.getRequest();
105:
106:                String uri = request.getRequestURI();
107:                String query = request.getQueryString();
108:                String returnPath = query == null ? uri : uri + "?" + query;
109:
110:                return contextToLogin(returnPath);
111:            }
112:
113:            /**
114:             * Prepares the template context to show the login page, using "returnPath" as return path
115:             * @param returnPath the URI to use as return path
116:             * @return TemplateKeys.USER_LOGIN
117:             */
118:            public static String contextToLogin(String returnPath) {
119:                JForumExecutionContext.getTemplateContext().put("returnPath",
120:                        returnPath);
121:
122:                if (ConfigKeys.TYPE_SSO.equals(SystemGlobals
123:                        .getValue(ConfigKeys.AUTHENTICATION_TYPE))) {
124:                    String redirect = SystemGlobals
125:                            .getValue(ConfigKeys.SSO_REDIRECT);
126:
127:                    if (!StringUtils.isEmpty(redirect)) {
128:                        URI redirectUri = URI.create(redirect);
129:
130:                        if (!redirectUri.isAbsolute()) {
131:                            throw new ForumException(
132:                                    "SSO redirect URL should start with a scheme");
133:                        }
134:
135:                        try {
136:                            returnPath = URLEncoder.encode(ViewCommon
137:                                    .getForumLink()
138:                                    + returnPath, "UTF-8");
139:                        } catch (UnsupportedEncodingException e) {
140:                        }
141:
142:                        if (redirect.indexOf('?') == -1) {
143:                            redirect += "?";
144:                        } else {
145:                            redirect += "&";
146:                        }
147:
148:                        redirect += "returnUrl=" + returnPath;
149:
150:                        JForumExecutionContext.setRedirect(redirect);
151:                    }
152:                }
153:
154:                return TemplateKeys.USER_LOGIN;
155:            }
156:
157:            /**
158:             * Returns the initial page to start fetching records from.
159:             *   
160:             * @return The initial page number
161:             */
162:            public static int getStartPage() {
163:                String s = JForumExecutionContext.getRequest().getParameter(
164:                        "start");
165:                int start;
166:
167:                if (StringUtils.isEmpty(s)) {
168:                    start = 0;
169:                } else {
170:                    start = Integer.parseInt(s);
171:
172:                    if (start < 0) {
173:                        start = 0;
174:                    }
175:                }
176:
177:                return start;
178:            }
179:
180:            /**
181:             * Gets the forum base link.
182:             * The returned link has a trailing slash
183:             * @return The forum link, with the trailing slash
184:             */
185:            public static String getForumLink() {
186:                String forumLink = SystemGlobals
187:                        .getValue(ConfigKeys.FORUM_LINK);
188:
189:                if (forumLink.charAt(forumLink.length() - 1) != '/') {
190:                    forumLink += "/";
191:                }
192:
193:                return forumLink;
194:            }
195:
196:            public static String toUtf8String(String s) {
197:                StringBuffer sb = new StringBuffer();
198:
199:                for (int i = 0; i < s.length(); i++) {
200:                    char c = s.charAt(i);
201:
202:                    if ((c >= 0) && (c <= 255)) {
203:                        sb.append(c);
204:                    } else {
205:                        byte[] b;
206:
207:                        try {
208:                            b = Character.toString(c).getBytes("utf-8");
209:                        } catch (Exception ex) {
210:                            System.out.println(ex);
211:                            b = new byte[0];
212:                        }
213:
214:                        for (int j = 0; j < b.length; j++) {
215:                            int k = b[j];
216:
217:                            if (k < 0) {
218:                                k += 256;
219:                            }
220:
221:                            sb.append('%').append(
222:                                    Integer.toHexString(k).toUpperCase());
223:                        }
224:                    }
225:                }
226:
227:                return sb.toString();
228:            }
229:
230:            /**
231:             * Formats a date using the pattern defined in the configuration file.
232:             * The key is the value of {@link net.jforum.util.preferences.ConfigKeys.DATE_TIME_FORMAT}
233:             * @param date the date to format
234:             * @return the string with the formated date
235:             */
236:            public static String formatDate(Date date) {
237:                SimpleDateFormat df = new SimpleDateFormat(SystemGlobals
238:                        .getValue(ConfigKeys.DATE_TIME_FORMAT));
239:                return df.format(date);
240:            }
241:
242:            /**
243:             * Escapes &lt; by &amp;lt; and &gt; by &amp;gt;
244:             * @param contents the string to parse
245:             * @return the new string
246:             */
247:            public static String espaceHtml(String contents) {
248:                StringBuffer sb = new StringBuffer(contents);
249:
250:                replaceAll(sb, "<", "&lt");
251:                replaceAll(sb, ">", "&gt;");
252:
253:                return sb.toString();
254:            }
255:
256:            /**
257:             * Replaces some string with another value
258:             * @param sb the StrinbBuilder with the contents to work on
259:             * @param what the string to be replaced
260:             * @param with the new value
261:             * @return the new string
262:             */
263:            public static String replaceAll(StringBuffer sb, String what,
264:                    String with) {
265:                int pos = sb.indexOf(what);
266:
267:                while (pos > -1) {
268:                    sb.replace(pos, pos + what.length(), with);
269:                    pos = sb.indexOf(what);
270:                }
271:
272:                return sb.toString();
273:            }
274:
275:            /**
276:             * @see #replaceAll(StringBuffer, String, String)
277:             * @param contents String
278:             * @param what String
279:             * @param with String
280:             * @return String
281:             */
282:            public static String replaceAll(String contents, String what,
283:                    String with) {
284:                return replaceAll(new StringBuffer(contents), what, with);
285:            }
286:
287:            /**
288:             * Parse the user's signature, to make it proper to visualization
289:             * @param u the user instance
290:             */
291:            public static void prepareUserSignature(User u) {
292:                if (u.getSignature() != null) {
293:                    StringBuffer sb = new StringBuffer(u.getSignature());
294:
295:                    replaceAll(sb, "\n", "<br />");
296:
297:                    u.setSignature(sb.toString());
298:                    u.setSignature(PostCommon
299:                            .prepareTextForDisplayExceptCodeTag(u
300:                                    .getSignature(), true, true));
301:                }
302:            }
303:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.