001: package vqwiki.utils;
002:
003: import vqwiki.WikiBase;
004: import vqwiki.Environment;
005:
006: import javax.servlet.http.HttpServletRequest;
007: import java.net.URLDecoder;
008: import java.net.URLEncoder;
009: import java.text.DecimalFormat;
010: import java.io.UnsupportedEncodingException;
011:
012: import org.apache.log4j.Logger;
013:
014: /*
015: Very Quick Wiki - WikiWikiWeb clone
016: Copyright (C) 2001-2002 Gareth Cronin
017:
018: This program is free software; you can redistribute it and/or modify
019: it under the terms of the latest version of the GNU Lesser General
020: Public License as published by the Free Software Foundation;
021:
022: This program is distributed in the hope that it will be useful,
023: but WITHOUT ANY WARRANTY; without even the implied warranty of
024: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
025: GNU Lesser General Public License for more details.
026:
027: You should have received a copy of the GNU Lesser General Public License
028: along with this program (gpl.txt); if not, write to the Free Software
029: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
030: */
031:
032: public class JSPUtils {
033:
034: /** Logger */
035: public static final Logger logger = Logger
036: .getLogger(JSPUtils.class);
037: protected static DecimalFormat decFormat = new DecimalFormat("0.00");
038:
039: /**
040: *
041: */
042: public JSPUtils() {
043: }
044:
045: /**
046: * This caused problems - encoding without a charset is not well-defined
047: * behaviour, so we'll look for a default encoding. (coljac)
048: */
049: public static String encodeURL(String url) {
050: try {
051: if (Environment.getInstance().getForceEncoding() != null) {
052: return URLEncoder.encode(url, Environment.getInstance()
053: .getForceEncoding());
054: } else {
055: return url;
056: }
057: } catch (UnsupportedEncodingException e) {
058: e.printStackTrace();
059: return url;
060: }
061: }
062:
063: /**
064: *
065: */
066: public static String encodeURL(String url, String charset) {
067: try {
068: return URLEncoder.encode(url, charset);
069: } catch (java.io.UnsupportedEncodingException ex) {
070: logger.error("unknown char set: " + charset, ex);
071: return null;
072: }
073: }
074:
075: /**
076: *
077: */
078: public static String decodeURL(String url) {
079: try {
080: return URLDecoder.decode(url, Environment.getInstance()
081: .getStringSetting(
082: Environment.PROPERTY_FILE_ENCODING));
083: } catch (UnsupportedEncodingException e) {
084: logger.error("unknown char set in environment", e);
085: try {
086: return URLDecoder.decode(url, "UTF-8");
087: } catch (UnsupportedEncodingException e1) {
088: logger.fatal("unknown charset in catch-method: ", e1);
089: return null;
090: }
091: }
092: }
093:
094: /**
095: *
096: */
097: public static String decodeURL(String url, String charset) {
098: try {
099: return URLDecoder.decode(url, charset);
100: } catch (java.io.UnsupportedEncodingException ex) {
101: logger.error("unknown char set: " + charset, ex);
102: return null;
103: }
104: }
105:
106: /**
107: *
108: */
109: public static String decimalFormat(double number) {
110: return decFormat.format(number);
111: }
112:
113: /**
114: *
115: */
116: public static String convertToHTML(char character) {
117: switch (character) {
118: case ('<'):
119: return "<";
120: case ('>'):
121: return ">";
122: case ('&'):
123: return "&";
124: }
125: return String.valueOf(character);
126: }
127:
128: /**
129: *
130: */
131: public static boolean containsSpecial(String text) {
132: if (text.indexOf('<') >= 0) {
133: return true;
134: } else if (text.indexOf('>') >= 0) {
135: return true;
136: } else if (text.indexOf('&') >= 0) {
137: return true;
138: }
139: return false;
140: }
141:
142: /**
143: * Create the root path for a specific WIKI without the server name.
144: * This is useful for local redirection or local URL's (relative URL's to the server).
145: * @param request The HttpServletRequest
146: * @param virtualWiki The name of the current virtual Wiki
147: * @return the root path for this viki
148: */
149: public static String createLocalRootPath(
150: HttpServletRequest request, String virtualWiki) {
151: String contextPath = "";
152: contextPath += request.getContextPath();
153: if (virtualWiki == null || virtualWiki.length() < 1) {
154: virtualWiki = WikiBase.DEFAULT_VWIKI;
155: }
156: return contextPath + "/" + virtualWiki + "/";
157: }
158:
159: /**
160: * Create the root path for a specific WIKI with a specific server
161: * @param request The HttpServletRequest
162: * @param virtualWiki The name of the current virtual Wiki
163: * @param server the specific server given for the path.
164: * If it is set to "null" or an empty string, it will take
165: * the servername from the given request.
166: * @return the root path for this viki
167: */
168: public static String createRootPath(HttpServletRequest request,
169: String virtualWiki, String server) {
170: String contextPath = "";
171: String contextScheme = request.getScheme();
172:
173: if (server == null || server.trim().equals("")) {
174: contextPath = contextScheme + "://"
175: + request.getServerName();
176: } else {
177: contextPath = contextScheme + "://" + server;
178: }
179: if ((contextScheme.equals("http") && request.getServerPort() != 80)
180: || (contextScheme.equals("https") && request
181: .getServerPort() != 443)) {
182: contextPath += ":" + request.getServerPort();
183: }
184: contextPath += request.getContextPath();
185: if (virtualWiki == null || virtualWiki.length() < 1) {
186: virtualWiki = WikiBase.DEFAULT_VWIKI;
187: }
188: return contextPath + "/" + virtualWiki + "/";
189: }
190:
191: /**
192: *
193: */
194: public static String createRedirectURL(HttpServletRequest request,
195: String url) {
196: String rootPath = JSPUtils.createLocalRootPath(request,
197: (String) request.getAttribute("virtualWiki"));
198: StringBuffer buffer = new StringBuffer();
199: buffer.append(rootPath);
200: buffer.append(url);
201: return buffer.toString();
202: }
203: }
|