KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > utils > FormatUtilities


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2007 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.utils;
13
14 import javax.servlet.ServletException JavaDoc;
15 import org.apache.log4j.Logger ;
16
17 public class FormatUtilities {
18   static Logger log4j = Logger.getLogger(FormatUtilities.class);
19
20   public static String JavaDoc truncate(String JavaDoc s, int i) {
21     if(s == null && s.length() == 0) return "";
22     if(i < s.length()) s = s.substring(0, i) + "...";
23     return s;
24   }
25
26   public static String JavaDoc replaceTildes(String JavaDoc strIni) {
27     //Delete tilde characters
28
return Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( strIni, "á", "a"), "é", "e"), "í", "i"), "ó", "o"), "ú", "u"), "�", "A"), "É", "E"), "�", "I"), "Ó", "O"), "Ú", "U");
29   }
30
31   public static String JavaDoc replace(String JavaDoc strIni) {
32     //delete characters: " ","&",","
33
return Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( Replace.replace( replaceTildes(strIni), "-",""), "/", ""), "#", ""), " ", ""), "&", ""), ",", ""), "(", ""), ")", "");
34   }
35
36   public static String JavaDoc replaceJS(String JavaDoc strIni) {
37     return replaceJS(strIni, true);
38   }
39
40   public static String JavaDoc replaceJS(String JavaDoc strIni, boolean isUnderQuotes) {
41     return Replace.replace( Replace.replace(Replace.replace(Replace.replace(strIni, "'", (isUnderQuotes?"\\'":"&#039;")), "\"", "\\\""), "\n", "\\n"), "\r", "");
42   }
43
44   public static String JavaDoc sha1Base64(String JavaDoc text) throws ServletException JavaDoc {
45     if (text==null || text.trim().equals("")) return "";
46     String JavaDoc result = text;
47     result = CryptoSHA1BASE64.encriptar(text);
48     return result;
49   }
50
51   public static String JavaDoc encryptDecrypt(String JavaDoc text, boolean encrypt) throws ServletException JavaDoc {
52     if (text==null || text.trim().equals("")) return "";
53     String JavaDoc result = text;
54     if (encrypt) result = CryptoUtility.encrypt(text);
55     else result = CryptoUtility.decrypt(text);
56     return result;
57   }
58 }
59
Popular Tags