2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
37 import java.text.NumberFormat;
41 * Statische Hilfsmethoden zur Stringbehandlung
43 * @version $Revision: 1.23.2.1 $ $Date: 2002/09/01 21:31:40 $
44 * @author $Author: mh $
46 * $Log: StringUtil.java,v $
47 * Revision 1.23.2.1 2002/09/01 21:31:40 mh
50 * Revision 1.23 2002/06/28 20:39:37 mh
51 * added numberformat helper. make webdbDate2readableDate use webdb_create instead. make the order and appearance of it more consistent. cvs macros. and finally code tidying
55 public final class StringUtil {
57 private static RE re_newline2br, re_brbr2p, re_mail, re_url, re_tags;
59 private StringUtil() { } // this avoids contruction
64 re_newline2br = new RE("(\r?\n){1}");
65 re_brbr2p = new RE("(<br>\r?\n<br>){1,}");
66 re_mail = new RE("([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_-]+).([a-zA-Z0-9_.-]+)");
67 re_url = new RE("((https://)|(http://)|(ftp://)){1}([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/?([^ \t\r\n<>\\)\\]]+[^ \t\r\n.,<>\\)\\]])");
68 re_tags = new RE("<[^>]*>",RE.REG_ICASE);
70 catch (REException e){
71 System.err.println("FATAL: StringUtil: could not precompile REGEX: "+e.toString());
76 * Formats a number with the specified minimum and maximum number of digits.
78 public static synchronized String zeroPaddingNumber(long value, int minDigits,
81 NumberFormat numberFormat = NumberFormat.getInstance();
82 numberFormat.setMinimumIntegerDigits(minDigits);
83 numberFormat.setMaximumIntegerDigits(maxDigits);
84 return numberFormat.format(value);
88 * Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd)
90 * @return 8-ziffriger String (yyyymmdd)
93 public static final String date2webdbDate (GregorianCalendar theDate) {
94 StringBuffer webdbDate = new StringBuffer();
95 webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
96 webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
97 webdbDate.append(pad2(theDate.get(Calendar.DATE)));
98 return webdbDate.toString();
102 * Wandelt Calendar in einen 12-ziffrigen String um (yyyymmddhhmm)
104 * @return 12-ziffriger String (yyyymmdd)
107 public static final String date2webdbDateTime (GregorianCalendar theDate) {
108 StringBuffer webdbDate = new StringBuffer();
109 webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
110 webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
111 webdbDate.append(pad2(theDate.get(Calendar.DATE)));
112 webdbDate.append(pad2(theDate.get(Calendar.HOUR)));
113 webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
114 return webdbDate.toString();
118 * Return a http://www.w3.org/TR/NOTE-datetime formatted date (yyyy-mm-ddThh:mm:ssTZ)
120 * @return w3approved datetime
123 public static final String date2w3DateTime (GregorianCalendar theDate) {
124 StringBuffer webdbDate = new StringBuffer();
125 webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
126 webdbDate.append("-");
127 webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
128 webdbDate.append("-");
129 webdbDate.append(pad2(theDate.get(Calendar.DATE)));
130 webdbDate.append("T");
131 webdbDate.append(pad2(theDate.get(Calendar.HOUR)));
132 webdbDate.append(":");
133 webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
134 webdbDate.append(":");
135 webdbDate.append(pad2(theDate.get(Calendar.SECOND)));
136 //assumes you are an hour-multiple away from UTC....
137 int offset=(theDate.get(Calendar.ZONE_OFFSET)/(60*60*1000));
139 webdbDate.append("-");
142 webdbDate.append("+");
144 webdbDate.append(pad2(Math.abs(offset)));
145 webdbDate.append(":00");
146 return webdbDate.toString();
150 * wandelt Calendar in dd.mm.yyyy / hh.mm um
152 * @return String mit (dd.mm.yyyy / hh.mm um)
154 public static String date2readableDateTime (GregorianCalendar theDate) {
155 String readable = "";
157 readable += pad2(theDate.get(Calendar.DATE));
158 readable += "." + pad2(theDate.get(Calendar.MONTH) + 1);
159 readable += "." + String.valueOf(theDate.get(Calendar.YEAR));
160 hour = theDate.get(Calendar.HOUR);
161 if (theDate.get(Calendar.AM_PM) == Calendar.PM)
163 readable += " / " + pad2(hour);
164 readable += ":" + pad2(theDate.get(Calendar.MINUTE));
169 * wandelt eine Datum in einen 8-buchstabigen String, der durch <code>/</code>
173 * @return String mit <code>/yyyy/mm/dd</code>
175 public static final String webdbDate2path (String webdbDate) {
176 StringBuffer path = new StringBuffer();
177 path.append("/").append(webdbDate.substring(0, 4));
178 path.append("/").append(webdbDate.substring(4, 6));
181 //path.append("/").append(webdbDate.substring(6, 8));
182 return path.toString();
186 * wandelt Calendar in dd.mm.yyyy um
189 * @return String mit <code>yyyy.mm.dd</code>
191 public static final String webdbDate2readableDate (String webdbDate) {
193 date += webdbDate.substring(0, 4);
194 date += "-" + webdbDate.substring(5, 7);
195 date += "-"+webdbDate.substring(8, 10);
201 * converts string from format: yyyy-mm-dd__hh:mm:ss.d
202 * to dd.mm.yyyy hh:mm
204 public static String dateToReadableDate(String date) {
205 StringBuffer returnDate = new StringBuffer();
208 returnDate.append(date.substring(8,10)).append('.');
209 returnDate.append(date.substring(5,7)).append('.');
210 returnDate.append(date.substring(0,4)).append(' ');
211 returnDate.append(date.substring(11,16));
213 return returnDate.toString();
217 * converts string from format: yyyy-mm-dd__hh:mm:ss.dddddd+TZ
218 * to yyyy-mm-ddThh:mm:ss+TZ:00 (w3 format for Dublin Core)
220 public static String webdbdateToDCDate(String date) {
221 StringBuffer returnDate = new StringBuffer();
223 returnDate.append(date.substring(0,10));
224 returnDate.append("T");
225 returnDate.append(date.substring(11,19));
226 //String tzInfo=date.substring(26,29);
227 //if (tzInfo.equals("+00")){
228 //UTC gets a special code in w3 dates
229 // returnDate.append("Z");
232 //need to see what a newfoundland postgres
233 //timestamp looks like before making this robust
234 // returnDate.append(tzInfo);
235 // returnDate.append(":00");
239 return returnDate.toString();
244 * converts string from format: yyyy-mm-dd__hh:mm:ss.d
247 public static String dateToYear (String date) {
248 StringBuffer returnDate = new StringBuffer();
251 returnDate.append(date.substring(0,4));
253 return returnDate.toString();
257 * converts string from format: yyyy-mm-dd__hh:mm:ss.d
260 public static String dateToMonth (String date) {
261 StringBuffer returnDate = new StringBuffer();
263 if (!date.substring(5,6).equalsIgnoreCase("0")) returnDate.append(date.substring(5,7));
264 else returnDate.append(date.substring(6,7));
266 return returnDate.toString();
270 * converts string from format: yyyy-mm-dd__hh:mm:ss.d
273 public static String dateToDayOfMonth (String date) {
274 StringBuffer returnDate = new StringBuffer();
276 if (!date.substring(8,9).equalsIgnoreCase("0")) returnDate.append(date.substring(8,10));
277 else returnDate.append(date.substring(9,10));
279 return returnDate.toString();
283 * converts string from format: yyyy-mm-dd__hh:mm:ss.d
286 public static String dateToTime (String date) {
287 StringBuffer returnDate = new StringBuffer();
289 returnDate.append(date.substring(11,16));
291 return returnDate.toString();
295 * Splits the provided CSV text into a list. stolen wholesale from
296 * from Jakarta Turbine StrinUtils.java -mh
298 * @param text The CSV list of values to split apart.
299 * @param separator The separator character.
300 * @return The list of values.
302 public static String[] split(String text, String separator)
304 StringTokenizer st = new StringTokenizer(text, separator);
305 String[] values = new String[st.countTokens()];
307 while (st.hasMoreTokens())
309 values[pos++] = st.nextToken();
315 * Joins the elements of the provided array into a single string
316 * containing a list of CSV elements. Stolen wholesale from Jakarta
317 * Turbine StringUtils.java. -mh
319 * @param list The list of values to join together.
320 * @param separator The separator character.
321 * @return The CSV text.
323 public static String join(String[] list, String separator)
325 StringBuffer csv = new StringBuffer();
326 for (int i = 0; i < list.length; i++)
330 csv.append(separator);
334 return csv.toString();
339 * schließt einen String in Anführungsszeichen ein, falls er Leerzeichen o.ä. enthält
341 * @return gequoteter String
343 public static String quoteIfNecessary(String s) {
344 for (int i = 0; i < s.length(); i++)
345 if (!(Character.isLetterOrDigit(s.charAt(i)) || s.charAt(i) == '.'))
346 return quote(s, '"');
351 * schließt <code>s</code> in <code>'</code> ein und setzt Backslashes vor
352 * "gefährliche" Zeichen innerhalb des Strings
353 * Quotes special SQL-characters in <code>s</code>
355 * @return geqoteter String
357 public static String quote(String s)
359 //String s2 = quote(s, '\'');
360 //Quickhack ÊÊ Ê Ê Ê Ê Ê Ê
361 //Because of '?-Bug in Postgresql-JDBC-Driver
362 StringBuffer temp = new StringBuffer();
363 for(int i=0;i<s.length();i++){
364 if(s.charAt(i)=='\''){
365 temp.append("'");
367 temp.append(s.charAt(i));
370 String s2 = temp.toString();
373 s2 = quote(s2, '\"');
378 * schließt <code>s</code> in <code>'</code> ein und setzt Backslashes vor
379 * "gefährliche" Zeichen innerhalb des Strings
381 * @param s String, der gequoted werden soll
382 * @param quoteChar zu quotendes Zeichen
383 * @return gequoteter String
385 public static String quote(String s, char quoteChar)
387 StringBuffer buf = new StringBuffer(s.length());
389 while (pos < s.length()) {
390 int i = s.indexOf(quoteChar, pos);
391 if (i < 0) i = s.length();
392 buf.append(s.substring(pos, i));
394 if (pos < s.length()) {
396 buf.append(quoteChar);
400 return buf.toString();
404 * replaces dangerous characters in <code>s</code>
408 public static String unquote(String s)
411 StringBuffer buf = new StringBuffer(s.length());
413 String searchString = "\\"+quoteChar;
414 while (pos < s.length()) {
415 int i = s.indexOf(searchString, pos);
416 if (i < 0) i = s.length();
417 buf.append(s.substring(pos, i));
420 return buf.toString();
424 * Wandelet String in byte[] um.
426 * @return byte[] des String
429 public static byte[] stringToBytes(String s) {
430 String crlf = System.getProperty("line.separator");
431 if (!crlf.equals("\n"))
432 s = replace(s, "\n", crlf);
433 // byte[] buf = new byte[s.length()];
434 byte[] buf = s.getBytes();
439 * Ersetzt in String <code>s</code> das <code>pattern</code> durch <code>substitute</code>
443 * @return String mit den Ersetzungen
445 public static String replace(String s, String pattern, String substitute) {
446 int i = 0, pLen = pattern.length(), sLen = substitute.length();
447 StringBuffer buf = new StringBuffer(s.length());
449 int j = s.indexOf(pattern, i);
451 buf.append(s.substring(i));
454 buf.append(s.substring(i, j));
455 buf.append(substitute);
459 return buf.toString();
463 * Ersetzt in String <code>s</code> das Regexp <code>pattern</code> durch <code>substitute</code>
467 * @return String mit den Ersetzungen
469 public static String regexpReplace(String haystack, String pattern, String substitute) {
471 RE regex = new RE(pattern);
472 return regex.substituteAll(haystack,substitute);
473 } catch(REException ex){
482 * Fügt einen Separator an den Pfad an
484 * @return Pfad mit Separator am Ende
486 public static final String addSeparator (String path) {
487 return path.length() == 0 || path.endsWith(File.separator) ? path : path
488 + File.separatorChar;
492 * Fügt ein <code>/</code> ans ende des Strings and
494 * @return Pfad mit <code>/</code> am Ende
496 public static final String addSlash (String path) {
497 return path.length() == 0 || path.endsWith("/") ? path : path + '/';
501 * Löscht <code>/</code> am Ende des Strings, falls vorhanden
503 * @return String ohne <code>/</code> am Ende
505 public static final String removeSlash (String path) {
506 return path.length() > 1 && path.endsWith("/") ? path.substring(0, path.length()
511 * Checks to see if the path is absolute by looking for a leading file
516 public static boolean isAbsolutePath (String path) {
517 return path.startsWith(File.separator);
521 * Löscht Slash am Anfang des Strings
525 public static String removeFirstSlash (String path) {
526 return path.startsWith("/") ? path.substring(1) : path;
530 * formatiert eine Zahl (0-99) zweistellig (z.B. 5 -> 05)
531 * @return zwistellige Zahl
533 public static String pad2 (int number) {
534 return number < 10 ? "0" + number : String.valueOf(number);
538 * formatiert eine Zahl (0-999) dreistellig (z.B. 7 -> 007)
540 * @return 3-stellige Zahl
542 public static String pad3 (int number) {
543 return number < 10 ? "00" + number : number < 100 ? "0" + number : String.valueOf(number);
547 * Konvertiert Unix-Linefeeds in Win-Linefeeds
549 * @return Konvertierter String
551 public static String unixLineFeedsToWin(String s) {
554 i = s.indexOf('\n', i+1);
556 if ((i == 0 || s.charAt(i-1) != '\r') &&
557 (i == s.length()-1 || s.charAt(i+1) != '\r')) {
558 s = s.substring(0, i)+'\r'+s.substring(i);
567 * verwandelt einen String in eine gültige Url, konvertiert Sonderzeichen
568 * und Spaces werden zu Underscores
570 * @return gültige Url
572 public static String convert2url(String s) {
574 StringBuffer buf = new StringBuffer();
575 for(int i = 0; i < s.length(); i++ ) {
576 switch( s.charAt( i ) ) {
578 buf.append( "oe" ); break;
580 buf.append( "ae" ); break;
582 buf.append( "ue" ); break;
584 buf.append( "a" ); break;
587 buf.append( "_" ); break;
589 if( buf.charAt( buf.length() - 1 ) != '_' ) {
594 buf.append( s.charAt( i ) );
597 return buf.toString();
601 public static String decodeHTMLinTags(String s){
602 StringBuffer buffer = new StringBuffer();
603 boolean start = false;
604 boolean stop = false;
609 for(int i=0;i<s.length();i++){
610 if(s.charAt(i)=='<'){
613 } else if(s.charAt(i)=='>'){
618 buffer.append(s.substring(temp,startIndex));
619 buffer.append(replaceQuot(s.substring(startIndex,stopIndex+1)));
620 i= temp= stopIndex+1;
626 buffer.append(s.substring(stopIndex+1));
627 return buffer.toString();
633 public static String replaceQuot(String s) {
634 StringBuffer buffer = new StringBuffer();
635 for(int j = 0; j < s.length();j++){
636 if(s.charAt(j)=='&'){
637 if(s.indexOf( """,j) == j) {
638 buffer.append( "\"" );
642 buffer.append(s.charAt(j));
645 return buffer.toString();
648 /** wandelt Quotes in Sonderzeichen um
651 public static String decodeHtml(String s) {
652 StringBuffer buf = new StringBuffer();
653 for(int i=0;i < s.length(); i++ ) {
654 if( s.indexOf( "ö", i ) == i ) {
655 buf.append( "ö" ); i += 5;
658 if( s.indexOf( "ä", i ) == i ) {
659 buf.append( "ä" ); i += 5;
662 if( s.indexOf( "ü", i ) == i ) {
663 buf.append( "ü" ); i += 5;
666 if( s.indexOf( "Ö", i ) == i ) {
667 buf.append( "Ö" ); i += 5;
670 if( s.indexOf( "Ä", i ) == i ) {
671 buf.append( "Ä" ); i += 5;
674 if( s.indexOf( "Ü", i ) == i ) {
675 buf.append( "Ü" ); i += 5;
678 if( s.indexOf( "ß", i ) == i ) {
679 buf.append( "ß" ); i += 6;
682 if( s.indexOf( """, i ) == i ) {
683 buf.append( "\"" ); i += 5;
686 buf.append( s.charAt(i) );
688 return buf.toString();
693 * schnellere Variante der String.toLowerCase()-Routine
695 * @return String in Kleinbuchsten
697 public static String toLowerCase(String s) {
699 char[] a = new char[l];
700 for (int i = 0; i < l; i++)
701 a[i] = Character.toLowerCase(s.charAt(i));
702 return new String(a);
706 * Findet <code>element</code> im String-Array <code>array</code>
709 * @return Fundstelle als int oder -1
711 public static int indexOf(String[] array, String element) {
713 for (int i = 0; i < array.length; i++)
714 if (array[i].equals(element))
720 * Testet auf Vorkommen von <code>element</code> in <code>array</code>
721 * @param array String-Array
723 * @return true wenn <code>element</code> vorkommt, sonst false
725 public static boolean contains(String[] array, String element) {
726 return indexOf(array, element) >= 0;
730 * Ermittelt CRC-Prüfsumme von String <code>s</code>
732 * @return CRC-Prüfsumme
734 public static int getCRC(String s) {
736 char val[] = s.toCharArray();
737 int len = val.length;
739 for (int i = 0 ; i < len; i++) {
741 h = (((h >> 30) | (h << 1)) ^ (val[i]+i));
744 return (h << 8) | (len & 0xff);
748 * Liefert Default-Wert def zurück, wenn String <code>s</code>
753 * @return geparster int aus s oder def
755 public static int parseInt(String s, int def) {
756 if (s == null) return def;
758 return Integer.parseInt(s);
759 } catch (NumberFormatException e) {
765 * Liefert Defaultwert def zurück, wenn s nicht zu einem float geparsed werden kann.
768 * @return geparster float oder def
770 public static float parseFloat(String s, float def) {
771 if (s == null) return def;
773 return new Float(s).floatValue();
774 } catch (NumberFormatException e) {
780 * Findet Ende eines Satzes in String <code>text</code>
783 * @return index des Satzendes, oder -1
785 public static int findEndOfSentence(String text, int startIndex) {
787 int i = text.indexOf('.', startIndex);
788 if (i < 0) return -1;
789 if (i > 0 && !Character.isDigit(text.charAt(i-1)) &&
790 (i+1 >= text.length()
791 || text.charAt(i+1) == ' '
792 || text.charAt(i+1) == '\n'
793 || text.charAt(i+1) == '\t'))
800 * Findet Wortende in String <code>text</code> ab <code>startIndex</code>
803 * @return Index des Wortendes, oder -1
805 public static int findEndOfWord(String text, int startIndex) {
806 int i = text.indexOf(' ', startIndex),
807 j = text.indexOf('\n', startIndex);
808 if (i < 0) i = text.length();
809 if (j < 0) j = text.length();
810 return Math.min(i, j);
815 * convertNewline2P ist eine regex-routine zum umwandeln von 2 oder mehr newlines (\n)
816 * in den html-tag <p>
817 * nur sinnvoll, wenn text nicht im html-format eingegeben
819 public static String convertNewline2P(String haystack) {
820 return re_brbr2p.substituteAll(haystack,"\n</p><p>");
824 * convertNewline2Break ist eine regex-routine zum umwandeln von 1 newline (\n)
825 * in den html-tag <br>
826 * nur sinnvoll, wenn text nicht im html-format eingegeben
828 public static String convertNewline2Break(String haystack) {
829 return re_newline2br.substituteAll(haystack,"$0<br />");
833 * createMailLinks wandelt text im email-adressenformat
834 * in einen klickbaren link um
835 * nur sinnvoll, wenn text nicht im html-format eingegeben
837 public static String createMailLinks(String haystack) {
838 return re_mail.substituteAll(haystack,"<a href=\"mailto:$0\">$0</a>");
843 * createMailLinks wandelt text im email-adressenformat
844 * in einen klickbaren link um
845 * nur sinnvoll, wenn text nicht im html-format eingegeben
847 public static String createMailLinks(String haystack, String imageRoot, String mailImage) {
848 return re_mail.substituteAll(haystack,"<img src=\""+imageRoot+"/"+mailImage+"\" border=\"0\"/> <a href=\"mailto:$0\">$0</a>");
853 * createURLLinks wandelt text im url-format
854 * in einen klickbaren link um
855 * nur sinnvoll, wenn text nicht im html-format eingegeben
857 public static String createURLLinks(String haystack) {
858 return re_url.substituteAll(haystack,"<a href=\"$0\">$0</a>");
862 * this routine takes text in url format and makes
863 * a clickaeble "<href>" link removing any "illegal" html tags
864 * @param haystack, the url
865 * @param title, the href link text
866 * @param imagRoot, the place to find icons
867 * @param extImage, the url of the icon to show next to the link
868 * @return a String containing the url
870 public static String createURLLinks(String haystack, String title, String imageRoot,String extImage) {
872 return re_url.substituteAll(haystack,"<img src=\""+imageRoot+"/"+extImage+"\" border=\"0\"/> <a href=\"$0\">$0</a>");
874 title = removeHTMLTags(title);
875 return re_url.substituteAll(haystack,"<img src=\""+imageRoot+"/"+extImage+"\" border=\"0\"/> <a href=\"$0\">"+title+"</a>");
880 * this routine takes text in url format and makes
881 * a clickaeble "<href>" link removing any "illegal" html tags
882 * @param haystack, the url
883 * @param imageRoot, the place to find icons
884 * @param extImage, the url of the icon to show next to the link
885 * @param intImage, unused
886 * @return a String containing the url
888 public static String createURLLinks(String haystack, String title, String imageRoot,String extImage,String intImage) {
889 return createURLLinks(haystack, title, imageRoot, extImage);
893 * deleteForbiddenTags
894 * this method deletes all <script>, <body> and <head>-tags
896 public static final String deleteForbiddenTags(String haystack) {
898 RE regex = new RE("<[ \t\r\n](.*?)script(.*?)/script(.*?)>",RE.REG_ICASE);
899 haystack = regex.substituteAll(haystack,"");
900 regex = new RE("<head>(.*?)</head>");
901 haystack = regex.substituteAll(haystack,"");
902 regex = new RE("<[ \t\r\n/]*body(.*?)>");
903 haystack = regex.substituteAll(haystack,"");
905 } catch(REException ex){
911 * this method deletes all html tags
913 public static final String removeHTMLTags(String haystack){
914 return re_tags.substituteAll(haystack,"");
919 * this method deletes all but the approved tags html tags
920 * it also deletes approved tags which contain malicious-looking attributes and doesn't work at all
922 public static String approveHTMLTags(String haystack){
924 String approvedTags="a|img|h1|h2|h3|h4|h5|h6|br|b|i|strong|p";
925 String badAttributes="onAbort|onBlur|onChange|onClick|onDblClick|onDragDrop|onError|onFocus|onKeyDown|onKeyPress|onKeyUp|onLoad|onMouseDown|onMouseMove|onMouseOut|onMouseOver|onMouseUp|onMove|onReset|onResize|onSelect|onSubmit|onUnload";
926 String approvedProtocols="rtsp|http|ftp|https|freenet|mailto";
928 // kill all the bad tags that have attributes
929 String s = "<\\s*/?\\s*(?!(("+approvedTags+")\\s))\\w+\\s[^>]*>";
930 RE regex = new RE(s,RE.REG_ICASE);
931 haystack = regex.substituteAll(haystack,"");
933 // kill all the bad tags that are attributeless
934 regex = new RE("<\\s*/?\\s*(?!(("+approvedTags+")\\s*>))\\w+\\s*>",RE.REG_ICASE);
935 haystack = regex.substituteAll(haystack,"");
937 // kill all the tags which have a javascript attribute like onLoad
938 regex = new RE("<[^>]*("+badAttributes+")[^>]*>",RE.REG_ICASE);
939 haystack = regex.substituteAll(haystack,"");
941 // kill all the tags which include a url to an unacceptable protocol
942 regex = new RE("<\\s*a\\s+[^>]*href=(?!(\'|\")?("+approvedProtocols+"))[^>]*>",RE.REG_ICASE);
943 haystack = regex.substituteAll(haystack,"");
946 } catch(REException ex){
947 ex.printStackTrace();
954 * createHTML ruft alle regex-methoden zum unwandeln eines nicht
955 * htmlcodierten string auf und returnt einen htmlcodierten String
957 public static String createHTML(String content){
958 content=convertNewline2Break(content);
959 content=convertNewline2P(content);
960 content=createMailLinks(content);
961 content=createURLLinks(content);
967 * createHTML ruft alle regex-methoden zum unwandeln eines nicht
968 * htmlcodierten string auf und returnt einen htmlcodierten String
970 public static String createHTML(String content,String producerDocRoot,String mailImage,String extImage,String intImage){
971 content=convertNewline2Break(content);
972 content=convertNewline2P(content);
973 content=createMailLinks(content,producerDocRoot,mailImage);
974 content=createURLLinks(content,null,producerDocRoot,extImage,intImage);