KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > infra > Utils


1 package org.javabb.infra;
2
3 import java.security.MessageDigest JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Random JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 /*
12  * Copyright 2004 JavaFree.org
13  *
14  * Licensed under the Apache License, Version 2.0 (the "License");
15  * you may not use this file except in compliance with the License.
16  * You may obtain a copy of the License at
17  *
18  * http://www.apache.org/licenses/LICENSE-2.0
19  *
20  * Unless required by applicable law or agreed to in writing, software
21  * distributed under the License is distributed on an "AS IS" BASIS,
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23  * See the License for the specific language governing permissions and
24  * limitations under the License.
25  */

26
27 /**
28  * $Id: Utils.java,v 1.15.4.3.2.2.2.2 2006/04/17 17:47:19 daltoncamargo Exp $
29  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
30  * @author Ronald Tetsuo Miura
31  */

32 public class Utils {
33     private static final Log LOG = LogFactory.getLog(Utils.class);
34
35     private static final Random JavaDoc RANDOM = new Random JavaDoc();
36
37     /**
38      * Encodes a string
39      * @param str
40      * @return md5
41      */

42     public static String JavaDoc encrypt(String JavaDoc str) {
43         String JavaDoc sign = null;
44
45         try {
46             MessageDigest JavaDoc md = MessageDigest.getInstance("MD5");
47             md.update(str.getBytes());
48
49             byte[] hash = md.digest();
50             StringBuffer JavaDoc hexString = new StringBuffer JavaDoc();
51
52             for (int i = 0; i < hash.length; i++) {
53                 if ((0xff & hash[i]) < 0x10) {
54                     hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
55                 } else {
56                     hexString.append(Integer.toHexString(0xFF & hash[i]));
57                 }
58             }
59
60             sign = hexString.toString();
61         } catch (Exception JavaDoc e) {
62             LOG.error(e.getMessage(), e);
63         }
64         return sign;
65     }
66
67     /**
68      * Obtém um número randômico
69      * @return número randomizado
70      */

71     public static String JavaDoc randomNumber() {
72         if (System.currentTimeMillis() % 10000 == 0) {
73             RANDOM.setSeed(System.currentTimeMillis());
74         }
75         return String.valueOf(RANDOM.nextLong());
76     }
77
78     /**
79      * Valida o WebSite digitado, e insere por padrão o protocolo http em frente.
80      * @param ws endereço para padronizar
81      * @return endereço padronizado
82      */

83     public static String JavaDoc validateWebSite(String JavaDoc ws) {
84         if ((ws != null) && (ws.length() > 0)) {
85             ws = ws.replaceAll("http://", "");
86             ws = "http://" + ws;
87
88             return ws;
89         }
90         return "";
91     }
92
93     /**
94      * @param text
95      * @return verified text
96      */

97     public static String JavaDoc verifyURLs(String JavaDoc text) {
98         final String JavaDoc regex = "([\\s\\n\\r\\t\\.,]|^)((?:http://|https://|ftp://)(?:\\S*))";
99         final String JavaDoc replacement = "$1[url]$2[/url]";
100
101         // Verificação de URLs no post
102
try {
103             text = text.replaceAll(regex, replacement);
104         } catch (RuntimeException JavaDoc e) {
105             LOG.error(e.getMessage(), e);
106         }
107
108         final String JavaDoc wwwregex = "([\\s\\n\\r\\t\\.,]|^)(www\\.\\S*)";
109         final String JavaDoc wwwreplacement = "$1[url=\"http://$2\"]$2[/url]";
110
111         // Verificação de URLs no post
112
try {
113             text = text.replaceAll(wwwregex, wwwreplacement);
114         } catch (RuntimeException JavaDoc e) {
115             LOG.error(e.getMessage(), e);
116         }
117
118         final String JavaDoc emailregex = "([\\s\\n\\r\\t\\.,]|^)([\\w-]+(?:\\.[\\w-]+)*@[\\w-]+\\.[\\w-]+(?:\\.[\\w-]+)*)";
119         final String JavaDoc emailreplacement = "$1[url=\"mailto:$2\"]$2[/url]";
120
121         // Verificação de emails no post
122
try {
123             text = text.replaceAll(emailregex, emailreplacement);
124         } catch (RuntimeException JavaDoc e) {
125             LOG.error(e.getMessage(), e);
126         }
127
128         return text;
129     }
130
131     /**
132      * @param texto
133      * @return text
134      */

135     public static String JavaDoc replaceHTML(String JavaDoc texto) {
136         return texto.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
137     }
138
139     /**
140      * @param userName
141      * @return code
142      */

143     public static String JavaDoc getCodeUser(String JavaDoc userName) {
144         String JavaDoc codeUser = userName + System.currentTimeMillis() + Utils.randomNumber();
145         return Utils.encrypt(codeUser);
146     }
147
148     /**
149      * @param string
150      * @return
151      */

152     public static String JavaDoc avoidNull(String JavaDoc string) {
153
154         return string != null ? string : "";
155     }
156
157     /**
158      * @author Dalton Camargo
159      * Get all String positions, similiar indexOf
160      * @param text - Text to be parsed
161      * @param key - Key to be found
162      * @return List<Integer> with all positions
163      */

164     public static List JavaDoc indexOf(String JavaDoc text, String JavaDoc key){
165         if(text == null || key == null){
166             return null;
167         }
168         List JavaDoc lst = new ArrayList JavaDoc();
169         int i = 0;
170         while(true){
171             int occur = text.indexOf(key, ((i>text.length())?text.length():i));
172             if(occur == -1){
173                 break;
174             } else {
175                 lst.add(new Integer JavaDoc(occur));
176                 i= occur + 1;
177             }
178         }
179         return lst;
180     }
181     
182     
183     public static boolean isBetween(int index, List JavaDoc initCodePos, List JavaDoc finalCodePos){
184         for(int y=0; y<initCodePos.size(); y++){
185             int init = ((Integer JavaDoc) initCodePos.get(y)).intValue();
186             int limit = ((Integer JavaDoc) finalCodePos.get(y)).intValue();
187             LOG.info("init:" + init + " limit:" + limit+ " index:" + index);
188             if(index > init && index < limit){
189                 return true;
190             }
191         }
192         return false;
193     }
194
195     
196     public static void main(String JavaDoc[] args){
197         //System.out.println(Utils.encrypt("dalton"));
198

199         System.out.println(Utils.indexOf("dalt:)on ca:)m..:)", ":)"));
200         
201     }
202 }
203
Popular Tags