View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   //CabaWeb
3   //Copyright (C) 2004  Thomas Vogt <Thomas.Vogt@TVC-Software.com>
4   //
5   //This library is free software; you can redistribute it and/or
6   //modify it under the terms of the GNU Lesser General Public
7   //License as published by the Free Software Foundation; either
8   //version 2.1 of the License, or (at your option) any later version.
9   //
10  //This library is distributed in the hope that it will be useful,
11  //but WITHOUT ANY WARRANTY; without even the implied warranty of
12  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  //Lesser General Public License for more details.
14  //
15  //You should have received a copy of the GNU Lesser General Public
16  //License along with this library; if not, write to the Free Software
17  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package org.fhw.cabaweb.tools;
21  
22  import java.io.IOException;
23  import java.io.UnsupportedEncodingException;
24  import java.net.URLDecoder;
25  import java.net.URLEncoder;
26  import java.util.Properties;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.fhw.cabaweb.calculation.threads.Lock;
31  
32  /***
33   * Klasse um verschiedene mit Strings zusammenh&auml;ngende Funktionen zusammenzufassen
34   *
35   * @author  <a href="mailto:thomas.vogt@tvc-software.com">Thomas Vogt</a>
36   * @version Version 1.0 06.07.2004
37   */
38  public final class StringUtilities
39  {
40      /*** Commons Logging Instanz */
41      private static Log log = LogFactory.getLog("org.fhw.cabaweb.tools.StringUtilities");
42  
43      /***
44       * Standardkonstruktor der nicht zug&auml;nglich sein soll, da dies eine Utility Klasse ist
45       */
46      protected StringUtilities()
47      {
48          throw new UnsupportedOperationException(); // Aufrufe aus eventuellen SubKlassen verhindern
49      }
50  
51      /***
52       * Gibt true zur&uuml;ck wenn es sich um eine g&uuuml;ltige eMail Adresse handelt
53       * (Bedingung mindestens ein "@" Zeichen und ein Punkt nach dem "@" in der eMail Adresse.
54       *
55       * @param   emailAddress String mit der zu pr&uuml;fenden eMail Adresse
56       * @return  true wenn korrekt und false wenn nicht
57       */
58      public static final boolean isValidEmailAddress(String emailAddress)
59      {
60          emailAddress = emailAddress.trim();
61  
62          boolean isValid = false;
63  
64          if (emailAddress != null && emailAddress.length() > 3 && emailAddress.indexOf("@") != -1 && emailAddress.indexOf(".") != -1 && emailAddress.indexOf("@") < emailAddress.lastIndexOf(".") && emailAddress.indexOf("@") > 0 && emailAddress.indexOf("@") == emailAddress.lastIndexOf("@") && emailAddress.lastIndexOf(".") + 2 <= emailAddress.length())
65          {
66              isValid = true;
67          }
68  
69          return isValid;
70      }
71  
72      /***
73       * Konvertiert den in UTF-8 codierten String in ISO-8859-1 um.
74       *
75       * @param   utf8string In UTF-8 codierter String
76       * @return  Der ISO-8859-1 String
77       */
78      public static final String convertUTF8String(String utf8string)
79      {
80          Properties properties = StringUtilities.getPropertiesFromFile("cabaweb.properties");
81  
82          String convert = properties.getProperty("ConvertUTF8");
83  
84          String from = null;
85          String to = utf8string;
86  
87          if(convert.compareTo("true") == 0 || convert.compareTo("on") == 0 || convert.compareTo("yes") == 0)
88          {
89              try
90              {
91  
92                  from = URLEncoder.encode(utf8string);
93                  to = URLDecoder.decode(from, "UTF8");
94              }
95              catch (UnsupportedEncodingException e)
96              {
97                  log.error(e.getMessage(), e);
98              }
99          }
100         
101         return to;
102     }
103 
104     /***
105      * Formatiert den gegebenen String so um, dass die hier geprüften Zeichen
106      * als HTML Code Zeichen ausgegeben werden.
107      *
108      * @param   eingabe Der umzuwandelnde String
109      * @return  Der konvertierte String
110      */
111     public static final String toHTMLString(String eingabe)
112     {
113         StringBuffer out = new StringBuffer();
114         char[] eingabechars = eingabe.toCharArray();
115 
116         for (int i = 0; i < eingabechars.length; i++)
117         {
118             char c = eingabechars[i];
119 
120             if (c == '\'')
121             {
122                 out.append("&#39;");
123             }
124             else if (c == '\"')
125             {
126                 out.append("&quot;");
127             }
128             else if (c == '&')
129             {
130                 out.append("&amp;");
131             }
132             else if (c == 'ä')
133             {
134                 out.append("&auml;");
135             }
136             else if (c == 'ö')
137             {
138                 out.append("&ouml;");
139             }
140             else if (c == 'ü')
141             {
142                 out.append("&uuml;");
143             }
144             else if (c == 'Ä')
145             {
146                 out.append("&Auml;");
147             }
148             else if (c == 'Ö')
149             {
150                 out.append("&Ouml;");
151             }
152             else if (c == 'Ü')
153             {
154                 out.append("&Uuml;");
155             }
156             else if (c == '<')
157             {
158                 out.append("&lt;");
159             }
160             else if (c == '>')
161             {
162                 out.append("&gt;");
163             }
164             else if (c == 'ß')
165             {
166                 out.append("&szlig;");
167             }
168             else if (c == '€')
169             {
170                 out.append("&#8364;");
171             }
172             else if (c == '\n')
173             {
174                 out.append("<BR/>\n");
175             }
176             else
177             {
178                 out.append(c);
179             }
180         }
181 
182         return out.toString();
183     }
184 
185     public final static Properties getPropertiesFromFile(String propertiesFile)
186     {
187         Properties properties = new Properties();
188         Lock lock = new Lock();
189 
190         try
191         {
192             properties.load(lock.getClass().getClassLoader().getResourceAsStream(propertiesFile));
193         }
194         catch (IOException e)
195         {
196             log.error(e.getLocalizedMessage(), e);
197         }
198 
199         return properties;
200     }
201 }