KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > geinuke > util > TextUtil


1
2
3  /*
4  -- GeiNuke --
5 Copyright (c) 2005 by Roberto Sidoti [geinuke@users.sourceforge.net]
6  http://www.hostingjava.it/-geinuke/
7
8 This file is part of GeiNuke.
9
10     GeiNuke is free software; you can redistribute it and/or modify
11     it under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 2 of the License, or
13     (at your option) any later version.
14
15     GeiNuke is distributed in the hope that it will be useful,
16     but WITHOUT ANY WARRANTY; without even the implied warranty of
17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     GNU General Public License for more details.
19
20     You should have received a copy of the GNU General Public License
21     along with GeiNuke; if not, write to the Free Software
22     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */

24 package com.geinuke.util;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.security.MessageDigest JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.regex.Matcher JavaDoc;
30 import java.util.regex.Pattern JavaDoc;
31
32 import com.geinuke.vo.ModuleDBVO;
33
34 import sun.misc.BASE64Encoder;
35 public class TextUtil {
36     
37     
38     
39     public static String JavaDoc[] badChars={"-","'","\"",";","'",">","<","(",")"};
40     
41     
42     private static Pattern JavaDoc ampPt = Pattern.compile("&");
43
44     private static Pattern JavaDoc quotPt = Pattern.compile("\"");
45
46     private static Pattern JavaDoc ltPt = Pattern.compile("<");
47
48     private static Pattern JavaDoc gtPt = Pattern.compile(">");
49     private static Pattern JavaDoc wikiLink = Pattern.compile("gikiPage-");
50
51     public static String JavaDoc normHTML(String JavaDoc inputText) {
52         String JavaDoc tmp = ampPt.matcher(inputText).replaceAll("&amp;");
53         tmp = quotPt.matcher(tmp).replaceAll("&quot;");
54         tmp = ltPt.matcher(tmp).replaceAll("&lt;");
55         tmp = gtPt.matcher(tmp).replaceAll("&gt;");
56         return tmp;
57     }
58     
59     public static String JavaDoc removeHTML(String JavaDoc inputText) {
60         String JavaDoc tmp = "<([^<>]*)>";
61         String JavaDoc res=null;
62         res=inputText.replaceAll(tmp,"");
63         return res;
64     }
65     
66     public static String JavaDoc trunc(String JavaDoc it,int max) {
67         if(it!=null && it.length()>max){
68             it=it.substring(0,max-1);
69         }
70         return it;
71     }
72     
73     
74     public static String JavaDoc getString(BufferedReader JavaDoc in) throws Exception JavaDoc{
75         String JavaDoc res=null,aux=null;
76         res="";
77         while( (aux=in.readLine())!=null )
78             res=res+aux+"\n";
79         return res;
80     }
81     
82     public static String JavaDoc getModuleNameByLinkModule(String JavaDoc li) throws Exception JavaDoc{
83         String JavaDoc res=null;
84         res=li.substring(ModuleDBVO.LINK_MODULE.length());
85         int pos=res.indexOf(".");
86         res=res.substring(0,pos);
87         
88         
89         return res;
90     }
91     
92     public static String JavaDoc normIfWin(String JavaDoc c) throws Exception JavaDoc{
93         c=c.replace('\\','/');
94         c=c.replaceAll("C:","");
95         return c;
96     }
97     
98     public static String JavaDoc solveWikiLink(String JavaDoc inputText) {
99         String JavaDoc tmp = wikiLink.matcher(inputText).replaceAll("Giki.jhtm?op=showA&name=");
100         
101         return tmp;
102     }
103
104         
105     public static String JavaDoc replaceAmp(String JavaDoc input) {
106         if(input==null)
107             return null;
108         else{
109             Matcher JavaDoc matcher = ampPt.matcher(input);
110             return matcher.replaceAll("&amp;");
111         }
112     }
113
114     public static String JavaDoc replaceTags(String JavaDoc input) {
115
116         if(input==null)
117             return null;
118         else{
119             Matcher JavaDoc matcher = ltPt.matcher(input);
120             input = matcher.replaceAll("&lt;");
121             matcher = gtPt.matcher(input);
122             return matcher.replaceAll("&gt;");
123         }
124     }
125     
126     
127     
128     public static String JavaDoc encr(String JavaDoc passw)throws Exception JavaDoc{
129         String JavaDoc crypted=passw;
130         return crypted;
131     }
132     
133     public static String JavaDoc encr1(String JavaDoc passw)throws Exception JavaDoc{
134         MessageDigest JavaDoc md = null;
135         md = MessageDigest.getInstance("SHA-1"); //step 2
136
md.update(passw.getBytes("UTF-8")); //step 3
137
byte raw[] = md.digest(); //step 4
138
String JavaDoc crypted = (new BASE64Encoder()).encode(raw); //step 5
139
return crypted;
140     }
141     
142     public static boolean hasChars(String JavaDoc target,String JavaDoc []chars){
143         boolean flag=false;
144         for(int i=0;i<chars.length && !flag;i++){
145             if(target.indexOf(chars[i])>-1)
146                 flag=true;
147         }
148         return flag;
149     }
150     
151     public static String JavaDoc[] tokenize(String JavaDoc source,String JavaDoc chars){
152         String JavaDoc [] res=null;
153         StringTokenizer JavaDoc st=new StringTokenizer JavaDoc(source,chars);
154         res=new String JavaDoc[st.countTokens()];
155         int ind=0;
156         while(st.hasMoreTokens()){
157             res[ind++]=st.nextToken();
158         }
159         return res;
160     }
161     
162     public static boolean isInteger(String JavaDoc s){
163         boolean flag=true;
164         try{
165             Integer.parseInt(s);
166         }catch(Exception JavaDoc e){
167             flag=false;
168         }
169         return flag;
170     }
171     
172     public static String JavaDoc convert(String JavaDoc []chars){
173         String JavaDoc res=null;
174         res="";
175         for(int i=0;i<chars.length;i++){
176             res+=chars[i];
177         }
178         return res;
179     }
180     
181     public static int splitAt(String JavaDoc date,int pos){
182         int aux=-1;
183         String JavaDoc [] ar=date.split("_");
184         aux = Integer.parseInt( ar[pos] );
185         return aux;
186     }
187     
188     public static boolean isEmpty(String JavaDoc s){
189         boolean b=false;
190         b=(s==null) || s.trim().equals("");
191         return b;
192     }
193     
194     public static String JavaDoc normString(String JavaDoc s){
195         
196         boolean b=(s==null) || s.trim().equals("");
197         if(b)
198             return "";
199         else
200             return s.trim();
201         
202     }
203     
204     
205 }
206
Popular Tags