KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mvnforum > util > Utils


1 package org.mvnforum.util;
2 import java.sql.Timestamp JavaDoc;
3 import java.text.SimpleDateFormat JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
7
8 //==============================================================================
9
// The JavaReference.com Software License, Version 1.0
10
// Copyright (c) 2002-2005 JavaReference.com. All rights reserved.
11
//
12
//
13
// Redistribution and use in source and binary forms, with or without
14
// modification, are permitted provided that the following conditions
15
// are met:
16
//
17
// 1. Redistributions of source code must retain the above copyright notice,
18
// this list of conditions and the following disclaimer.
19
//
20
// 2. Redistributions in binary form must reproduce the above copyright notice,
21
// this list of conditions and the following disclaimer in the documentation
22
// and/or other materials provided with the distribution.
23
//
24
// 3. The end-user documentation included with the redistribution, if any, must
25
// include the following acknowlegement:
26
//
27
// "This product includes software developed by the Javareference.com
28
// (http://www.javareference.com/)."
29
//
30
// Alternately, this acknowlegement may appear in the software itself, if and
31
// wherever such third-party acknowlegements normally appear.
32
//
33
// 4. The names "JavaReference" and "Javareference.com", must not be used to
34
// endorse or promote products derived from this software without prior written
35
// permission. For written permission, please contact webmaster@javareference.com.
36
//
37
// 5. Products derived from this software may not be called "Javareference" nor may
38
// "Javareference" appear in their names without prior written permission of
39
// Javareference.com.
40
//
41
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
42
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
44
// JAVAREFERENCE.COM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
45
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
50
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
//
52
//================================================================================
53
// Software from this site consists of contributions made by various individuals
54
// on behalf of Javareference.com. For more information on Javareference.com,
55
// please see http://www.javareference.com
56
//================================================================================
57

58 /**
59  * @author anandh
60  */

61 public class Utils {
62
63     static SimpleDateFormat JavaDoc datetimefmt = new SimpleDateFormat JavaDoc("yyyy-MM-dd kk:mm:ss");
64
65     static SimpleDateFormat JavaDoc datefmt = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
66
67     static Hashtable JavaDoc str2hex = new Hashtable JavaDoc();
68     static {
69         str2hex.put("0", new Integer JavaDoc(0));
70         str2hex.put("1", new Integer JavaDoc(1));
71         str2hex.put("2", new Integer JavaDoc(2));
72         str2hex.put("3", new Integer JavaDoc(3));
73         str2hex.put("4", new Integer JavaDoc(4));
74         str2hex.put("5", new Integer JavaDoc(5));
75         str2hex.put("6", new Integer JavaDoc(6));
76         str2hex.put("7", new Integer JavaDoc(7));
77         str2hex.put("8", new Integer JavaDoc(8));
78         str2hex.put("9", new Integer JavaDoc(9));
79         str2hex.put("A", new Integer JavaDoc(10));
80         str2hex.put("B", new Integer JavaDoc(11));
81         str2hex.put("C", new Integer JavaDoc(12));
82         str2hex.put("D", new Integer JavaDoc(13));
83         str2hex.put("E", new Integer JavaDoc(14));
84         str2hex.put("F", new Integer JavaDoc(15));
85         str2hex.put("a", new Integer JavaDoc(10));
86         str2hex.put("b", new Integer JavaDoc(11));
87         str2hex.put("c", new Integer JavaDoc(12));
88         str2hex.put("d", new Integer JavaDoc(13));
89         str2hex.put("e", new Integer JavaDoc(14));
90         str2hex.put("f", new Integer JavaDoc(15));
91     }
92
93     /*public static String wrapit(String text)
94      {
95      StringBuffer strbuf = new StringBuffer();
96      strbuf.append('\'');
97      for(int i=0; i<text.length(); i++)
98      {
99      if(text.charAt(i) == '\'' )
100      strbuf.append("\\\'");
101      else
102      strbuf.append(text.charAt(i));
103      }
104      strbuf.append('\'');
105
106      return strbuf.toString();
107      }*/

108
109     public static String JavaDoc wrapit(String JavaDoc text) {
110         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
111         //strbuf.append('\'');
112
for (int i = 0; i < text.length(); i++) {
113             if (text.charAt(i) == '\'')
114                 strbuf.append("\'\'");
115             else
116                 strbuf.append(text.charAt(i));
117         }
118         //strbuf.append('\'');
119

120         String JavaDoc result = strbuf.toString();
121         result = DisableHtmlTagFilter.filter(result);
122         // correct smilies
123
// smile
124
result = result.replaceAll("(.*):\\)(.*)", "$1\\[:\\)\\]$2");
125         // sad
126
result = result.replaceAll("(.*):\\((.*)", "$1\\[:\\(\\]$2");
127         // big grin
128
result = result.replaceAll("(.*):D(.*)", "$1\\[:D\\]$2");
129         // laughing
130
result = result.replaceAll("(.*):lol:(.*)", "$1\\[:\\)\\)\\]$2");
131         // crying
132
result = result.replaceAll("(.*):cry:(.*)", "$1\\[:\\(\\(\\]$2");
133         // wink
134
result = result.replaceAll("(.*):wink:(.*)", "$1\\[;\\)\\]$2");
135         // blushing
136
result = result.replaceAll("(.*):oops:(.*)", "$1\\[:\">\\]$2");
137         // tongue
138
result = result.replaceAll("(.*):p(.*)", "$1\\[:p\\]$2");
139         // cool
140
result = result.replaceAll("(.*)8\\)(.*)", "$1\\[B-\\)\\]$2");
141         // confused
142
result = result.replaceAll("(.*):\\?(.*)", "$1\\[:-/\\]$2");
143         // shock
144
result = result.replaceAll("(.*):shock:(.*)", "$1\\[:O\\]$2");
145         // devil
146
result = result.replaceAll("(.*):evil:(.*)", "$1\\[>:\\)\\]$2");
147
148         // correct BBcodes
149
// url
150
result = result.replaceAll("(.*)\\[url\\](.*)\\[/url\\](.*)", "$1\\[url=$2\\]$2\\[/url\\]$3");
151         // bold
152
result = result.replaceAll("(.*)\\[b:[0-9,abcdef]*\\](.*)\\[/b:[0-9,abcdef]*\\](.*)", "$1\\[b\\]$2\\[/b\\]$3");
153         // underline
154
result = result.replaceAll("(.*)\\[u:[0-9,abcdef]*\\](.*)\\[/u:[0-9,abcdef]*\\](.*)", "$1\\[u\\]$2\\[/u\\]$3");
155         // italic
156
result = result.replaceAll("(.*)\\[i:[0-9,abcdef]*\\](.*)\\[/i:[0-9,abcdef]*\\](.*)", "$1\\[i\\]$2\\[/i\\]$3");
157         // color
158
result = result.replaceAll("(.*)\\[color=([a-z]*):[0-9,abcdef]*\\](.*)\\[/color:[0-9,abcdef]*\\](.*)",
159                 "$1\\[color=$2\\]$3\\[/color\\]$4");
160         //regular quote
161
result = result.replaceAll("(.*)\\[quote:[0-9,abcdef]*\\](.*)\\[/quote:[0-9,abcdef]*\\](.*)", "$1\\[quote=\\]$2\\[/quote\\]$3");
162         // fancy quote
163
result = result.replaceAll("(.*)\\[quote:[0-9,abcdef]*=\\\"(.*)\\\"\\](.*)\\[/quote:[0-9,abcdef]*\\](.*)", "$1\\[quote=\\\"$2\\\"\\]$3\\[/quote\\]$4");
164         return result;
165     }
166
167     public static String JavaDoc DateTimeFromS(long s) {
168         Date JavaDoc d = new Date JavaDoc(s * 1000);
169         return datetimefmt.format(d);
170     }
171
172     public static String JavaDoc DateFromS(long s) {
173         Date JavaDoc d = new Date JavaDoc(s * 1000);
174         return datefmt.format(d);
175     }
176
177     public static String JavaDoc HexIPtoString(String JavaDoc hexrep) {
178         if (hexrep.length() < 8)
179             return "0.0.0.0";
180
181         byte[] ipaddr = new byte[8];
182         for (int i = 0; i < ipaddr.length; i++) {
183             String JavaDoc letter = "" + hexrep.charAt(i);
184             int val = ((Integer JavaDoc) str2hex.get(letter)).intValue();
185             ipaddr[i] = (byte) val;
186         }
187
188         String JavaDoc strrep = "";
189         int A = 0;
190         A |= ipaddr[0];
191         A = A << 4;
192         A |= ipaddr[1];
193         int B = 0;
194         B |= ipaddr[2];
195         B = B << 4;
196         B |= ipaddr[3];
197         int C = 0;
198         C |= ipaddr[4];
199         C = C << 4;
200         C |= ipaddr[5];
201         int D = 0;
202         D |= ipaddr[6];
203         D = D << 4;
204         D |= ipaddr[7];
205
206         return "" + A + "." + B + "." + C + "." + D;
207     }
208
209     public static String JavaDoc stripPHPBBQuotes(String JavaDoc in) {
210         boolean err = false;
211         StringBuffer JavaDoc endstr = new StringBuffer JavaDoc();
212
213         do {
214             int firstquote = in.indexOf("[quote:");
215             if (firstquote == -1) {
216                 err = true;
217                 break;
218             }
219
220             int firstclosebrace = in.indexOf(']', firstquote);
221             if (firstclosebrace == -1) {
222                 err = true;
223                 break;
224             }
225
226             endstr.append(in.substring(0, firstquote));
227             endstr.append("[quote]");
228
229             int endquote = in.indexOf("[/quote:");
230             if (endquote == -1) {
231                 err = true;
232                 break;
233             }
234
235             int endclosebrace = in.indexOf(']', endquote);
236             if (endclosebrace == -1) {
237                 err = true;
238                 break;
239             }
240
241             endstr.append(in.substring(firstclosebrace + 1, endquote));
242             endstr.append("[/quote]");
243             endstr.append(in.substring(endclosebrace + 1));
244         } while (false);
245
246         if (err)
247             return in;
248         else
249             return endstr.toString();
250     }
251     
252     public static String JavaDoc getFormatDate (Date JavaDoc date) {
253         return datefmt.format(date);
254     }
255     
256     public static Timestamp JavaDoc getTimeStamp (long data) {
257         return new Timestamp JavaDoc(data*1000);
258     }
259
260     public static void main(String JavaDoc[] args) {
261         String JavaDoc phpstr = " BOXO[quote:2845990e65=\"suganthan\"]Help me! This is the string[/quote:2845990e65]";
262
263         System.out.println(Utils.stripPHPBBQuotes(phpstr));
264     }
265 }
266
Popular Tags