001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/util/Utils.java,v 1.5 2007/01/15 10:27:31 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.5 $
005: * $Date: 2007/01/15 10:27:31 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author:
039: */
040: package org.mvnforum.util;
041:
042: import java.sql.Timestamp;
043: import java.text.SimpleDateFormat;
044: import java.util.Date;
045: import java.util.Hashtable;
046:
047: import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
048:
049: //==============================================================================
050: // The JavaReference.com Software License, Version 1.0
051: // Copyright (c) 2002-2005 JavaReference.com. All rights reserved.
052: //
053: //
054: // Redistribution and use in source and binary forms, with or without
055: // modification, are permitted provided that the following conditions
056: // are met:
057: //
058: // 1. Redistributions of source code must retain the above copyright notice,
059: // this list of conditions and the following disclaimer.
060: //
061: // 2. Redistributions in binary form must reproduce the above copyright notice,
062: // this list of conditions and the following disclaimer in the documentation
063: // and/or other materials provided with the distribution.
064: //
065: // 3. The end-user documentation included with the redistribution, if any, must
066: // include the following acknowlegement:
067: //
068: // "This product includes software developed by the Javareference.com
069: // (http://www.javareference.com/)."
070: //
071: // Alternately, this acknowlegement may appear in the software itself, if and
072: // wherever such third-party acknowlegements normally appear.
073: //
074: // 4. The names "JavaReference" and "Javareference.com", must not be used to
075: // endorse or promote products derived from this software without prior written
076: // permission. For written permission, please contact webmaster@javareference.com.
077: //
078: // 5. Products derived from this software may not be called "Javareference" nor may
079: // "Javareference" appear in their names without prior written permission of
080: // Javareference.com.
081: //
082: // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
083: // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
084: // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
085: // JAVAREFERENCE.COM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
086: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
087: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
088: // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
089: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
090: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
091: // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
092: //
093: //================================================================================
094: // Software from this site consists of contributions made by various individuals
095: // on behalf of Javareference.com. For more information on Javareference.com,
096: // please see http://www.javareference.com
097: //================================================================================
098:
099: /**
100: * @author anandh
101: */
102: public class Utils {
103:
104: static SimpleDateFormat datetimeFmt = new SimpleDateFormat(
105: "yyyy-MM-dd kk:mm:ss");// TODO why 2 spaces ???
106:
107: static SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
108:
109: static Hashtable str2hex = new Hashtable();
110:
111: static {
112: str2hex.put("0", new Integer(0));
113: str2hex.put("1", new Integer(1));
114: str2hex.put("2", new Integer(2));
115: str2hex.put("3", new Integer(3));
116: str2hex.put("4", new Integer(4));
117: str2hex.put("5", new Integer(5));
118: str2hex.put("6", new Integer(6));
119: str2hex.put("7", new Integer(7));
120: str2hex.put("8", new Integer(8));
121: str2hex.put("9", new Integer(9));
122: str2hex.put("A", new Integer(10));
123: str2hex.put("B", new Integer(11));
124: str2hex.put("C", new Integer(12));
125: str2hex.put("D", new Integer(13));
126: str2hex.put("E", new Integer(14));
127: str2hex.put("F", new Integer(15));
128: str2hex.put("a", new Integer(10));
129: str2hex.put("b", new Integer(11));
130: str2hex.put("c", new Integer(12));
131: str2hex.put("d", new Integer(13));
132: str2hex.put("e", new Integer(14));
133: str2hex.put("f", new Integer(15));
134: }
135:
136: public static String wrapIt(String text) {
137:
138: StringBuffer strbuf = new StringBuffer();
139: //strbuf.append('\'');
140: for (int i = 0; i < text.length(); i++) {
141: if (text.charAt(i) == '\'') {
142: strbuf.append("\'\'");
143: } else {
144: strbuf.append(text.charAt(i));
145: }
146: }
147: //strbuf.append('\'');
148:
149: String result = strbuf.toString();
150: result = DisableHtmlTagFilter.filter(result);
151:
152: // correct smilies
153: // smile
154: result = result.replaceAll("(.*):\\)(.*)", "$1\\[:\\)\\]$2");
155: // sad
156: result = result.replaceAll("(.*):\\((.*)", "$1\\[:\\(\\]$2");
157: // big grin
158: result = result.replaceAll("(.*):D(.*)", "$1\\[:D\\]$2");
159: // laughing
160: result = result
161: .replaceAll("(.*):lol:(.*)", "$1\\[:\\)\\)\\]$2");
162: // crying
163: result = result
164: .replaceAll("(.*):cry:(.*)", "$1\\[:\\(\\(\\]$2");
165: // wink
166: result = result.replaceAll("(.*):wink:(.*)", "$1\\[;\\)\\]$2");
167: // blushing
168: result = result.replaceAll("(.*):oops:(.*)", "$1\\[:\">\\]$2");
169: // tongue
170: result = result.replaceAll("(.*):p(.*)", "$1\\[:p\\]$2");
171: // cool
172: result = result.replaceAll("(.*)8\\)(.*)", "$1\\[B-\\)\\]$2");
173: // confused
174: result = result.replaceAll("(.*):\\?(.*)", "$1\\[:-/\\]$2");
175: // shock
176: result = result.replaceAll("(.*):shock:(.*)", "$1\\[:O\\]$2");
177: // devil
178: result = result.replaceAll("(.*):evil:(.*)", "$1\\[>:\\)\\]$2");
179:
180: // correct BBcodes
181: // url
182: result = result.replaceAll("(.*)\\[url\\](.*)\\[/url\\](.*)",
183: "$1\\[url=$2\\]$2\\[/url\\]$3");
184: // bold
185: result = result
186: .replaceAll(
187: "(.*)\\[b:[0-9,abcdef]*\\](.*)\\[/b:[0-9,abcdef]*\\](.*)",
188: "$1\\[b\\]$2\\[/b\\]$3");
189: // underline
190: result = result
191: .replaceAll(
192: "(.*)\\[u:[0-9,abcdef]*\\](.*)\\[/u:[0-9,abcdef]*\\](.*)",
193: "$1\\[u\\]$2\\[/u\\]$3");
194: // italic
195: result = result
196: .replaceAll(
197: "(.*)\\[i:[0-9,abcdef]*\\](.*)\\[/i:[0-9,abcdef]*\\](.*)",
198: "$1\\[i\\]$2\\[/i\\]$3");
199: // color
200: result = result
201: .replaceAll(
202: "(.*)\\[color=([a-z]*):[0-9,abcdef]*\\](.*)\\[/color:[0-9,abcdef]*\\](.*)",
203: "$1\\[color=$2\\]$3\\[/color\\]$4");
204: //regular quote
205: result = result
206: .replaceAll(
207: "(.*)\\[quote:[0-9,abcdef]*\\](.*)\\[/quote:[0-9,abcdef]*\\](.*)",
208: "$1\\[quote=\\]$2\\[/quote\\]$3");
209: // fancy quote
210: result = result
211: .replaceAll(
212: "(.*)\\[quote:[0-9,abcdef]*=\\\"(.*)\\\"\\](.*)\\[/quote:[0-9,abcdef]*\\](.*)",
213: "$1\\[quote=\\\"$2\\\"\\]$3\\[/quote\\]$4");
214: return result;
215: }
216:
217: public static String DateTimeFromS(long s) {
218: Date d = new Date(s * 1000);
219: return datetimeFmt.format(d);
220: }
221:
222: public static String DateFromS(long s) {
223: Date d = new Date(s * 1000);
224: return dateFmt.format(d);
225: }
226:
227: public static String HexIPtoString(String hexrep) {
228: if (hexrep.length() < 8) {
229: return "0.0.0.0";
230: }
231:
232: if (hexrep.length() > 8) {
233: throw new IllegalArgumentException(
234: "Does not accept hexrep = " + hexrep);
235: }
236:
237: byte[] ipaddr = new byte[8];
238: for (int i = 0; i < ipaddr.length; i++) {
239: String letter = String.valueOf(hexrep.charAt(i));
240: int val = ((Integer) str2hex.get(letter)).intValue();// TODO NPE could occur
241: ipaddr[i] = (byte) val;
242: }
243:
244: int A = 0;
245: A |= ipaddr[0];
246: A = A << 4;
247: A |= ipaddr[1];
248:
249: int B = 0;
250: B |= ipaddr[2];
251: B = B << 4;
252: B |= ipaddr[3];
253:
254: int C = 0;
255: C |= ipaddr[4];
256: C = C << 4;
257: C |= ipaddr[5];
258:
259: int D = 0;
260: D |= ipaddr[6];
261: D = D << 4;
262: D |= ipaddr[7];
263:
264: StringBuffer retValue = new StringBuffer(16);
265: retValue.append(A).append(".").append(B).append(".").append(C)
266: .append(".").append(D);
267: return retValue.toString();
268: }
269:
270: public static String stripPHPBBQuotes(String in) {
271: boolean err = false;
272: StringBuffer endstr = new StringBuffer();
273:
274: while (true) {
275: int firstquote = in.indexOf("[quote:");
276: if (firstquote == -1) {
277: err = true;
278: break;
279: }
280:
281: int firstclosebrace = in.indexOf(']', firstquote);
282: if (firstclosebrace == -1) {
283: err = true;
284: break;
285: }
286:
287: endstr.append(in.substring(0, firstquote));
288: endstr.append("[quote]");
289:
290: int endquote = in.indexOf("[/quote:");
291: if (endquote == -1) {
292: err = true;
293: break;
294: }
295:
296: int endclosebrace = in.indexOf(']', endquote);
297: if (endclosebrace == -1) {
298: err = true;
299: break;
300: }
301:
302: endstr.append(in.substring(firstclosebrace + 1, endquote));
303: endstr.append("[/quote]");
304: endstr.append(in.substring(endclosebrace + 1));
305: }
306:
307: if (err) {
308: return in;
309: } else {
310: return endstr.toString();
311: }
312: }
313:
314: public static String getFormatDate(Date date) {
315: return dateFmt.format(date);
316: }
317:
318: public static Timestamp getTimeStamp(long data) {
319: return new Timestamp(data * 1000);
320: }
321:
322: public static void main(String[] args) {
323: String phpstr = " BOXO[quote:2845990e65=\"suganthan\"]Help me! This is the string[/quote:2845990e65]";
324:
325: System.out.println(Utils.stripPHPBBQuotes(phpstr));
326: }
327: }
|