/*
- * put your module comment here
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with the com.oreilly.servlet library, any library
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of
+ * the above that use the same license as the above), and distribute linked
+ * combinations including the two. You must obey the GNU General Public
+ * License in all respects for all of the code used other than the above
+ * mentioned libraries. If you modify this file, you may extend this exception
+ * to your version of the file, but you are not obligated to do so. If you do
+ * not wish to do so, delete this exception statement from your version.
*/
-
package mir.misc;
import java.io.*;
import java.lang.*;
import java.util.*;
+import java.text.NumberFormat;
import gnu.regexp.*;
/**
* Statische Hilfsmethoden zur Stringbehandlung
*
- * @version 29.6.99
- * @author RK
+ * @version $Id: StringUtil.java,v 1.23.2.3 2002/12/09 06:23:38 mh Exp $
+ * @author rk, mir-coders group
+ *
*/
+
public final class StringUtil {
private static RE re_newline2br, re_brbr2p, re_mail, re_url, re_tags;
//precompile regex
re_newline2br = new RE("(\r?\n){1}");
re_brbr2p = new RE("(<br>\r?\n<br>){1,}");
- re_mail = new RE("([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_-]+).([a-zA-Z0-9_.-]+)");
+ re_mail = new RE("([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_-]+)\\.([a-zA-Z0-9_.-]+)");
re_url = new RE("((https://)|(http://)|(ftp://)){1}([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/?([^ \t\r\n<>\\)\\]]+[^ \t\r\n.,<>\\)\\]])");
re_tags = new RE("<[^>]*>",RE.REG_ICASE);
}
}
}
+ /**
+ * Formats a number with the specified minimum and maximum number of digits.
+ **/
+ public static synchronized String zeroPaddingNumber(long value, int minDigits,
+ int maxDigits)
+ {
+ NumberFormat numberFormat = NumberFormat.getInstance();
+ numberFormat.setMinimumIntegerDigits(minDigits);
+ numberFormat.setMaximumIntegerDigits(maxDigits);
+ return numberFormat.format(value);
+ }
/**
* Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd)
}
/**
+ * Return a http://www.w3.org/TR/NOTE-datetime formatted date (yyyy-mm-ddThh:mm:ssTZ)
+ * @param theDate
+ * @return w3approved datetime
+ */
+
+ public static final String date2w3DateTime (GregorianCalendar theDate) {
+ StringBuffer webdbDate = new StringBuffer();
+ webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
+ webdbDate.append("-");
+ webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
+ webdbDate.append("-");
+ webdbDate.append(pad2(theDate.get(Calendar.DATE)));
+ webdbDate.append("T");
+ webdbDate.append(pad2(theDate.get(Calendar.HOUR)));
+ webdbDate.append(":");
+ webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
+ webdbDate.append(":");
+ webdbDate.append(pad2(theDate.get(Calendar.SECOND)));
+ //assumes you are an hour-multiple away from UTC....
+ int offset=(theDate.get(Calendar.ZONE_OFFSET)/(60*60*1000));
+ if (offset < 0){
+ webdbDate.append("-");
+ }
+ else{
+ webdbDate.append("+");
+ }
+ webdbDate.append(pad2(Math.abs(offset)));
+ webdbDate.append(":00");
+ return webdbDate.toString();
+ }
+
+ /**
* wandelt Calendar in dd.mm.yyyy / hh.mm um
* @param theDate
* @return String mit (dd.mm.yyyy / hh.mm um)
* wandelt Calendar in dd.mm.yyyy um
*
* @param theDate
- * @return String mit <code>dd.mm.yyyy</code>
+ * @return String mit <code>yyyy.mm.dd</code>
*/
public static final String webdbDate2readableDate (String webdbDate) {
String date = "";
- date += webdbDate.substring(6, 8);
- date += "." + webdbDate.substring(4, 6);
- date += "." + webdbDate.substring(0, 4);
+ date += webdbDate.substring(0, 4);
+ date += "-" + webdbDate.substring(5, 7);
+ date += "-"+webdbDate.substring(8, 10);
return date;
}
}
return returnDate.toString();
}
+
+ /**
+ * converts string from format: yyyy-mm-dd__hh:mm:ss.dddddd+TZ
+ * to yyyy-mm-ddThh:mm:ss+TZ:00 (w3 format for Dublin Core)
+ */
+ public static String webdbdateToDCDate(String date) {
+ StringBuffer returnDate = new StringBuffer();
+ if (date!=null) {
+ returnDate.append(date.substring(0,10));
+ returnDate.append("T");
+ returnDate.append(date.substring(11,19));
+ //String tzInfo=date.substring(26,29);
+ //if (tzInfo.equals("+00")){
+ //UTC gets a special code in w3 dates
+ // returnDate.append("Z");
+ //}
+ //else{
+ //need to see what a newfoundland postgres
+ //timestamp looks like before making this robust
+ // returnDate.append(tzInfo);
+ // returnDate.append(":00");
+ //}
+
+ }
+ return returnDate.toString();
+ }
+
/**
* converts string from format: yyyy-mm-dd__hh:mm:ss.d
}
/**
- * Splits the provided CSV text into a list. stolen wholesale from
+ * Splits the provided CSV text into a list. stolen wholesale from
* from Jakarta Turbine StrinUtils.java -mh
*
* @param text The CSV list of values to split apart.
public static String quote(String s)
{
//String s2 = quote(s, '\'');
-
- //Quickhack
+ //Quickhack ÊÊ Ê Ê Ê Ê Ê Ê
//Because of '?-Bug in Postgresql-JDBC-Driver
StringBuffer temp = new StringBuffer();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='\''){
- temp.append("´");
+ temp.append("'");
} else {
temp.append(s.charAt(i));
}
}
String s2 = temp.toString();
- //Quickhack end
-
+ //end Quickhack
+
s2 = quote(s2, '\"');
return s2;
}
return buf.toString();
}
- /**
- * wandelt Sonderzeichen in Quotes um
- *
- * @return Kovertierter String
- */
- public static String encodeHtml(String s) {
- StringBuffer buf = new StringBuffer();
- for(int i=0;i < s.length(); i++ ) {
-
- /** @todo looks inefficient, to ask for index of every char, in
- * case of failure it runs to the end.*/
- if (s.charAt(i)=='&') {
- // convert html to xml-parsable representation
- if( s.indexOf( "ö", i ) == i ) {
- buf.append( "ö" ); i += 5;
- continue;
- }
- if( s.indexOf( "ä", i ) == i ) {
- buf.append( "ä" ); i += 5;
- continue;
- }
- if( s.indexOf( "ü", i ) == i ) {
- buf.append( "ü" ); i += 5;
- continue;
- }
- if( s.indexOf( "Ö", i ) == i ) {
- buf.append( "Ö" ); i += 5;
- continue;
- }
- if( s.indexOf( "Ä", i ) == i ) {
- buf.append( "Ä" ); i += 5;
- continue;
- }
- if( s.indexOf( "Ü", i ) == i ) {
- buf.append( "Ü" ); i += 5;
- continue;
- }
- if( s.indexOf( "ß", i ) == i ) {
- buf.append( "ß" ); i += 6;
- continue;
- }
-
- /** @todo should only escape outside of tags */
-
- if( s.indexOf( """, i ) == i ) {
- buf.append( "ß" ); i += 5;
- continue;
- }
- if( s.indexOf( "–", i ) == i ) {
- buf.append( "–" ); i += 6;
- continue;
- }
- if( s.indexOf( "—", i ) == i ) {
- buf.append( "—" ); i += 6;
- continue;
- }
- if( s.indexOf( "“", i ) == i ) {
- buf.append( "“" ); i += 6;
- continue;
- }
- if( s.indexOf( "”", i ) == i ) {
- buf.append( "”" ); i += 6;
- continue;
- }
- if( s.indexOf( "„", i ) == i ) {
- buf.append( "„" ); i += 6;
- continue;
- }
-
- //looks pretty stupid
- if( s.indexOf( "<", i ) == i ) {
- buf.append( "<" ); i += 3;
- continue;
- }
- if( s.indexOf( ">", i ) == i ) {
- buf.append( ">" ); i += 3;
- continue;
- }
- if( s.indexOf( "´", i ) == i ) {
- buf.append( "´" ); i += 6;
- continue;
- }
- if( s.indexOf( " ", i ) == i ) {
- buf.append( " " ); i += 5;
- continue;
- }
- //has to be the last
- //if( s.indexOf( "&", i ) == i ) {
- // buf.append( "&" ); i += 0;
- // continue;
- //}
- }
- // convert umlauts an other special charakters
- switch( s.charAt(i) ) {
- case 'ö': buf.append( "ö" ); break;
- case 'ä': buf.append( "ä" ); break;
- case 'ü': buf.append( "ü" ); break;
- case 'Ö': buf.append( "Ö" ); break;
- case 'Ä': buf.append( "Ä" ); break;
- case 'Ü': buf.append( "Ü" ); break;
- case 'ß': buf.append( "ß" ); break;
- case 'é': buf.append( "é" ); break;
- case 'è': buf.append( "è" ); break;
- case 'á': buf.append( "á" ); break;
- case 'à': buf.append( "à" ); break;
- case 'â': buf.append( "â" ); break;
- case 'ã': buf.append( "ã" ); break;
- case '¬': buf.append( "¬" ); break;
- case '¹': buf.append( "¹" ); break;
- case '²': buf.append( "²" ); break;
- case '³': buf.append( "³" ); break;
- case '¼': buf.append( "¼" ); break;
- case '½': buf.append( "½" ); break;
- case '¾': buf.append( "¾" ); break;
- case '¶': buf.append( "¶" ); break;
- case 'æ': buf.append( "æ" ); break;
- case 'ð': buf.append( "ð" ); break;
- case '|': buf.append( "¦" ); break;
- case '·': buf.append( "·" ); break;
- case '°': buf.append( "°" ); break;
- case '§': buf.append( "§" ); break;
- case 'ø': buf.append( "ø" ); break;
- case 'ç': buf.append( "ç" ); break;
- case '¤': buf.append( "¤" ); break;
- case 'ª': buf.append( "ª" ); break;
- case 'Ç': buf.append( "Ç" ); break;
- case 'Ã': buf.append( "Ã" ); break;
- case 'Â': buf.append( "Â" ); break;
- case 'Æ': buf.append( "Æ" ); break;
- case '©': buf.append( "©" ); break;
- case '®': buf.append( "®" ); break;
- case '¥': buf.append( "¥" ); break;
- case 'Þ': buf.append( "þ" ); break;
- case '¯': buf.append( "¯" ); break;
- case 'Ð': buf.append( "Ð" ); break;
- case 'º': buf.append( "º" ); break;
- case '¡': buf.append( "¡" ); break;
- case '£': buf.append( "£" ); break;
- case '±': buf.append( "±" ); break;
- case '¿': buf.append( "¿" ); break;
- case 'Ø': buf.append( "Ø" ); break;
- case 'Á': buf.append( "À" ); break;
- case 'À': buf.append( "Á" ); break;
- case 'É': buf.append( "È" ); break;
- case 'È': buf.append( "É" ); break;
- case 'ù': buf.append( "ú" ); break;
- case 'ñ': buf.append( "ñ" ); break;
- case 'Ñ': buf.append( "Ñ" ); break;
- case 'µ': buf.append( "µ" ); break;
- case 'Í': buf.append( "Ì" ); break;
- case 'Ì': buf.append( "Í" ); break;
- case 'í': buf.append( "ì" ); break;
- case 'ì': buf.append( "í" ); break;
- case 'î': buf.append( "î" ); break;
- case 'Î': buf.append( "Î" ); break;
- case 'ó': buf.append( "ó" ); break;
- case 'Ó': buf.append( "Ò" ); break;
- case 'ò': buf.append( "Î" ); break;
- case 'Ò': buf.append( "Ó" ); break;
- case 'ô': buf.append( "ô" ); break;
- case 'Ô': buf.append( "Ô" ); break;
- case 'õ': buf.append( "õ" ); break;
- case 'Õ': buf.append( "Õ" ); break;
- case 'ý': buf.append( "ý" ); break;
- case 'Ý': buf.append( "Ý" ); break;
- case 'û': buf.append( "û" ); break;
- case 'Û': buf.append( "Û" ); break;
- case 'ú': buf.append( "ù" ); break;
- case 'Ú': buf.append( "Ù" ); break;
- case 'Ù': buf.append( "Ú" ); break;
- case 'Ê': buf.append( "Ê" ); break;
- case 'ê': buf.append( "ê" ); break;
- case 'å': buf.append( "å" ); break;
- case 'Å': buf.append( "Å" ); break;
- case 'ë': buf.append( "ë" ); break;
- case 'Ë': buf.append( "Ë" ); break;
- case 'ÿ': buf.append( "ÿ" ); break;
- case 'ï': buf.append( "ï" ); break;
- case 'Ï': buf.append( "Ï" ); break;
- case '«': buf.append( "«" ); break;
- case '»': buf.append( "»" ); break;
- case '\'': buf.append( "´" ); break;
- case '\"': buf.append( """ ); break;
- //case '\u8211': buf.append( "–" ); break;
- //case '\u8212': buf.append( "—" ); break;
- //case '\u8220': buf.append( "“" ); break;
- //case '\u8221': buf.append( "”" ); break;
- //case '\u8222': buf.append( "„" ); break;
- //case '\"': buf.append( """ ); break;
- default: buf.append( s.charAt(i) );
- }
-
- }
- return buf.toString();
- }
-
-
- public static String decodeHTMLinTags(String s){
- StringBuffer buffer = new StringBuffer();
- boolean start = false;
- boolean stop = false;
- int startIndex = 0;
- int stopIndex = 0;
- int temp = 0;
-
- for(int i=0;i<s.length();i++){
- if(s.charAt(i)=='<'){
- start = true;
- startIndex = i;
- } else if(s.charAt(i)=='>'){
- stop = true;
- stopIndex = i;
-
- if(start && stop){
- buffer.append(s.substring(temp,startIndex));
- buffer.append(replaceQuot(s.substring(startIndex,stopIndex+1)));
- i= temp= stopIndex+1;
- start= stop= false;
- }
- }
- }
- if(stopIndex>0){
- buffer.append(s.substring(stopIndex+1));
- return buffer.toString();
- } else {
- return s;
- }
- }
-
public static String replaceQuot(String s) {
StringBuffer buffer = new StringBuffer();
for(int j = 0; j < s.length();j++){
return buffer.toString();
}
- /** wandelt Quotes in Sonderzeichen um
- */
- /**
- public static String decodeHtml(String s) {
- StringBuffer buf = new StringBuffer();
- for(int i=0;i < s.length(); i++ ) {
- if( s.indexOf( "ö", i ) == i ) {
- buf.append( "ö" ); i += 5;
- continue;
- }
- if( s.indexOf( "ä", i ) == i ) {
- buf.append( "ä" ); i += 5;
- continue;
- }
- if( s.indexOf( "ü", i ) == i ) {
- buf.append( "ü" ); i += 5;
- continue;
- }
- if( s.indexOf( "Ö", i ) == i ) {
- buf.append( "Ö" ); i += 5;
- continue;
- }
- if( s.indexOf( "Ä", i ) == i ) {
- buf.append( "Ä" ); i += 5;
- continue;
- }
- if( s.indexOf( "Ü", i ) == i ) {
- buf.append( "Ü" ); i += 5;
- continue;
- }
- if( s.indexOf( "ß", i ) == i ) {
- buf.append( "ß" ); i += 6;
- continue;
- }
- if( s.indexOf( """, i ) == i ) {
- buf.append( "\"" ); i += 5;
- continue;
- }
- buf.append( s.charAt(i) );
- }
- return buf.toString();
- }
- */
-
/**
* schnellere Variante der String.toLowerCase()-Routine
*
* nur sinnvoll, wenn text nicht im html-format eingegeben
*/
public static String convertNewline2Break(String haystack) {
- return re_newline2br.substituteAll(haystack,"$0<br>");
+ return re_newline2br.substituteAll(haystack,"$0<br />");
}
/**
return createURLLinks(haystack, title, imageRoot, extImage);
}
- /**
- * this routine takes text in url format and makes
- * an image link removing any "illegal" html tags
- * @param haystack, the url
- * @param title, the image alt text, can be null
- * @param height, height of the image
- * @param width, width of the image
- * @return a String containing the url
- */
- public static String createIMGLinks(String haystack, String title,
- String height,String width) {
- String wh="";
- if ( (height != null) && (width != null) )
- {
- wh = "width=\""+width+"\" height=\""+height+"\"";
- }
- if (title != null) {
- title = removeHTMLTags(title);
- return re_url.substituteAll(haystack,
- "<img hspace=\"10\" vspace=\"6\" "+
- "align=\"left\" src=\"$0\" "+wh+
- " alt=\""+title+"\"/> <br><i>"+
- title+"</i>");
- } else {
- return re_url.substituteAll(haystack,
- "<img hspace=\"10\" vspace=\"6\" "+
- "align=\"left\" src=\"$0\" "+wh+
- " alt=\"\"/> ");
- }
- }
-
-
/**
* deleteForbiddenTags
* this method deletes all <script>, <body> and <head>-tags