001: package com.mvnforum.jaxb.util;
002:
003: import java.sql.Timestamp;
004: import java.text.ParseException;
005: import java.text.SimpleDateFormat;
006: import java.util.Date;
007: import java.util.Hashtable;
008:
009: import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
010:
011: public class ImportExportUtil {
012:
013: static SimpleDateFormat datetimefmt = new SimpleDateFormat(
014: "yyyy-MM-dd kk:mm:ss");
015:
016: static SimpleDateFormat datefmt = new SimpleDateFormat("yyyy-MM-dd");
017:
018: static Hashtable str2hex = new Hashtable();
019: static {
020: str2hex.put("0", new Integer(0));
021: str2hex.put("1", new Integer(1));
022: str2hex.put("2", new Integer(2));
023: str2hex.put("3", new Integer(3));
024: str2hex.put("4", new Integer(4));
025: str2hex.put("5", new Integer(5));
026: str2hex.put("6", new Integer(6));
027: str2hex.put("7", new Integer(7));
028: str2hex.put("8", new Integer(8));
029: str2hex.put("9", new Integer(9));
030: str2hex.put("A", new Integer(10));
031: str2hex.put("B", new Integer(11));
032: str2hex.put("C", new Integer(12));
033: str2hex.put("D", new Integer(13));
034: str2hex.put("E", new Integer(14));
035: str2hex.put("F", new Integer(15));
036: str2hex.put("a", new Integer(10));
037: str2hex.put("b", new Integer(11));
038: str2hex.put("c", new Integer(12));
039: str2hex.put("d", new Integer(13));
040: str2hex.put("e", new Integer(14));
041: str2hex.put("f", new Integer(15));
042: }
043:
044: /*public static String wrapit(String text)
045: {
046: StringBuffer strbuf = new StringBuffer();
047: strbuf.append('\'');
048: for (int i=0; i<text.length(); i++)
049: {
050: if (text.charAt(i) == '\'' )
051: strbuf.append("\\\'");
052: else
053: strbuf.append(text.charAt(i));
054: }
055: strbuf.append('\'');
056:
057: return strbuf.toString();
058: }*/
059:
060: public static String wrapit(String text) {
061: StringBuffer strbuf = new StringBuffer();
062: //strbuf.append('\'');
063: for (int i = 0; i < text.length(); i++) {
064: if (text.charAt(i) == '\'')
065: strbuf.append("\'\'");
066: else
067: strbuf.append(text.charAt(i));
068: }
069: //strbuf.append('\'');
070:
071: String result = strbuf.toString();
072: result = DisableHtmlTagFilter.filter(result);
073: // correct smilies
074: // smile
075: result = result.replaceAll("(.*):\\)(.*)", "$1\\[:\\)\\]$2");
076: // sad
077: result = result.replaceAll("(.*):\\((.*)", "$1\\[:\\(\\]$2");
078: // big grin
079: result = result.replaceAll("(.*):D(.*)", "$1\\[:D\\]$2");
080: // laughing
081: result = result
082: .replaceAll("(.*):lol:(.*)", "$1\\[:\\)\\)\\]$2");
083: // crying
084: result = result
085: .replaceAll("(.*):cry:(.*)", "$1\\[:\\(\\(\\]$2");
086: // wink
087: result = result.replaceAll("(.*):wink:(.*)", "$1\\[;\\)\\]$2");
088: // blushing
089: result = result.replaceAll("(.*):oops:(.*)", "$1\\[:\">\\]$2");
090: // tongue
091: result = result.replaceAll("(.*):p(.*)", "$1\\[:p\\]$2");
092: // cool
093: result = result.replaceAll("(.*)8\\)(.*)", "$1\\[B-\\)\\]$2");
094: // confused
095: result = result.replaceAll("(.*):\\?(.*)", "$1\\[:-/\\]$2");
096: // shock
097: result = result.replaceAll("(.*):shock:(.*)", "$1\\[:O\\]$2");
098: // devil
099: result = result.replaceAll("(.*):evil:(.*)", "$1\\[>:\\)\\]$2");
100:
101: // correct BBcodes
102: // url
103: result = result.replaceAll("(.*)\\[url\\](.*)\\[/url\\](.*)",
104: "$1\\[url=$2\\]$2\\[/url\\]$3");
105: // bold
106: result = result
107: .replaceAll(
108: "(.*)\\[b:[0-9,abcdef]*\\](.*)\\[/b:[0-9,abcdef]*\\](.*)",
109: "$1\\[b\\]$2\\[/b\\]$3");
110: // underline
111: result = result
112: .replaceAll(
113: "(.*)\\[u:[0-9,abcdef]*\\](.*)\\[/u:[0-9,abcdef]*\\](.*)",
114: "$1\\[u\\]$2\\[/u\\]$3");
115: // italic
116: result = result
117: .replaceAll(
118: "(.*)\\[i:[0-9,abcdef]*\\](.*)\\[/i:[0-9,abcdef]*\\](.*)",
119: "$1\\[i\\]$2\\[/i\\]$3");
120: // color
121: result = result
122: .replaceAll(
123: "(.*)\\[color=([a-z]*):[0-9,abcdef]*\\](.*)\\[/color:[0-9,abcdef]*\\](.*)",
124: "$1\\[color=$2\\]$3\\[/color\\]$4");
125: //regular quote
126: result = result
127: .replaceAll(
128: "(.*)\\[quote:[0-9,abcdef]*\\](.*)\\[/quote:[0-9,abcdef]*\\](.*)",
129: "$1\\[quote=\\]$2\\[/quote\\]$3");
130: // fancy quote
131: result = result
132: .replaceAll(
133: "(.*)\\[quote:[0-9,abcdef]*=\\\"(.*)\\\"\\](.*)\\[/quote:[0-9,abcdef]*\\](.*)",
134: "$1\\[quote=\\\"$2\\\"\\]$3\\[/quote\\]$4");
135: return result;
136: }
137:
138: public static String HexIPtoString(String hexrep) {
139: if (hexrep.length() < 8)
140: return "0.0.0.0";
141:
142: byte[] ipaddr = new byte[8];
143: for (int i = 0; i < ipaddr.length; i++) {
144: String letter = "" + hexrep.charAt(i);
145: int val = ((Integer) str2hex.get(letter)).intValue();
146: ipaddr[i] = (byte) val;
147: }
148:
149: String strrep = "";
150: int A = 0;
151: A |= ipaddr[0];
152: A = A << 4;
153: A |= ipaddr[1];
154: int B = 0;
155: B |= ipaddr[2];
156: B = B << 4;
157: B |= ipaddr[3];
158: int C = 0;
159: C |= ipaddr[4];
160: C = C << 4;
161: C |= ipaddr[5];
162: int D = 0;
163: D |= ipaddr[6];
164: D = D << 4;
165: D |= ipaddr[7];
166:
167: return "" + A + "." + B + "." + C + "." + D;
168: }
169:
170: public static String stripPHPBBQuotes(String in) {
171: boolean err = false;
172: StringBuffer endstr = new StringBuffer();
173:
174: do {
175: int firstquote = in.indexOf("[quote:");
176: if (firstquote == -1) {
177: err = true;
178: break;
179: }
180:
181: int firstclosebrace = in.indexOf(']', firstquote);
182: if (firstclosebrace == -1) {
183: err = true;
184: break;
185: }
186:
187: endstr.append(in.substring(0, firstquote));
188: endstr.append("[quote]");
189:
190: int endquote = in.indexOf("[/quote:");
191: if (endquote == -1) {
192: err = true;
193: break;
194: }
195:
196: int endclosebrace = in.indexOf(']', endquote);
197: if (endclosebrace == -1) {
198: err = true;
199: break;
200: }
201:
202: endstr.append(in.substring(firstclosebrace + 1, endquote));
203: endstr.append("[/quote]");
204: endstr.append(in.substring(endclosebrace + 1));
205: } while (false);
206:
207: if (err)
208: return in;
209: else
210: return endstr.toString();
211: }
212:
213: public static String dateTimeFormat(long s) {
214: Date d = new Date(s * 1000);
215: return datetimefmt.format(d);
216: }
217:
218: public static String dateFormat(long s) {
219: Date d = new Date(s * 1000);
220: return datefmt.format(d);
221: }
222:
223: public static String dateTimeFormat(Timestamp timestamp) {
224: return datetimefmt.format(timestamp);
225: }
226:
227: public static String dateFormat(Timestamp timestamp) {
228: return datefmt.format(timestamp);
229: }
230:
231: public static String getFormatDate(Date date) {
232: return datefmt.format(date);
233: }
234:
235: public static Timestamp getTimeStamp(long data) {
236: return new Timestamp(data * 1000);
237: }
238:
239: public static Timestamp string2TimeStamp(String value)
240: throws ParseException {
241: Date d = datetimefmt.parse(value);
242: return new Timestamp(d.getTime());
243: }
244:
245: public static java.sql.Date string2Date(String value)
246: throws ParseException {
247: return new java.sql.Date(datetimefmt.parse(value).getTime());
248:
249: }
250: }
|