some needed date-converters
[mir.git] / source / mir / misc / StringUtil.java
index 2b70011..dddaf65 100755 (executable)
@@ -33,6 +33,22 @@ public final class StringUtil {
   }
 
   /**
+   * Wandelt Calendar in einen 12-ziffrigen String um (yyyymmddhhmm)
+   * @param theDate
+   * @return 12-ziffriger String (yyyymmdd)
+   */
+
+  public static final String date2webdbDateTime (GregorianCalendar theDate) {
+    StringBuffer webdbDate = new StringBuffer();
+    webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
+    webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
+    webdbDate.append(pad2(theDate.get(Calendar.DATE)));
+    webdbDate.append(pad2(theDate.get(Calendar.HOUR)));
+    webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
+    return  webdbDate.toString();
+  }
+
+  /**
    * wandelt Calendar in dd.mm.yyyy / hh.mm um
    * @param theDate
    * @return String mit (dd.mm.yyyy / hh.mm um)
@@ -99,6 +115,57 @@ public final class StringUtil {
     return returnDate.toString();
   }
 
+  /**
+   * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+   * to yyyy
+   */
+  public static String dateToYear (String date) {
+    StringBuffer returnDate = new StringBuffer();
+    if (date!=null) {
+
+      returnDate.append(date.substring(0,4));
+    }
+    return returnDate.toString();
+  }
+
+  /**
+   * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+   * to [m]m
+   */
+  public static String dateToMonth (String date) {
+    StringBuffer returnDate = new StringBuffer();
+    if (date!=null) {
+                 if (!date.substring(5,6).equalsIgnoreCase("0")) returnDate.append(date.substring(5,7));
+      else returnDate.append(date.substring(6,7));
+    }
+    return returnDate.toString();
+  }
+
+  /**
+   * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+   * to [d]d
+   */
+  public static String dateToDayOfMonth (String date) {
+    StringBuffer returnDate = new StringBuffer();
+    if (date!=null) {
+                 if (!date.substring(8,9).equalsIgnoreCase("0")) returnDate.append(date.substring(8,10));
+      else returnDate.append(date.substring(9,10));
+    }
+    return returnDate.toString();
+  }
+
+  /**
+   * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+   * to hh:mm
+   */
+  public static String dateToTime (String date) {
+    StringBuffer returnDate = new StringBuffer();
+    if (date!=null) {
+      returnDate.append(date.substring(11,16));
+    }
+    return returnDate.toString();
+  }
+
 
   /**
    * schließt einen String in Anführungsszeichen ein, falls er Leerzeichen o.ä. enthält
@@ -225,6 +292,24 @@ public final class StringUtil {
     return buf.toString();
   }
 
+  /**
+   * Ersetzt in String <code>s</code> das Regexp <code>pattern</code> durch <code>substitute</code>
+   * @param s
+   * @param pattern
+   * @param substitute
+   * @return String mit den Ersetzungen
+   */
+  public static String regexpReplace(String haystack, String pattern, String substitute) {
+    try {
+      RE regex = new RE(pattern);
+      return regex.substituteAll(haystack,substitute);
+    } catch(REException ex){
+      return null;
+    }
+  }
+
+
+
 
   /**
    * Fügt einen Separator an den Pfad an
@@ -256,6 +341,17 @@ public final class StringUtil {
   }
 
   /**
+   * Checks to see if the path is absolute by looking for a leading file
+   * separater
+   * @todo deal with windows drive letters.
+   * @param path
+   * @return
+   */
+  public static boolean isAbsolutePath (String path) {
+    return  path.startsWith(File.separator);
+  }
+
+  /**
    * Löscht Slash am Anfang des Strings
    * @param path
    * @return
@@ -421,10 +517,10 @@ public final class StringUtil {
           continue;
         }
         //has to be the last
-        if( s.indexOf( "&", i ) == i ) {
-          buf.append( "&#38;" ); i += 0;
-          continue;
-        }
+        //if( s.indexOf( "&", i ) == i ) {
+        //  buf.append( "&#38;" ); i += 0;
+        //  continue;
+        //}
       }
       // convert umlauts an other special charakters
       switch( s.charAt(i) ) {
@@ -743,8 +839,8 @@ public final class StringUtil {
     return Math.min(i, j);
   }
 
-  /**
-   * Diese Routine macht aus links in reinem text browsbare links
+   /**
+   * This routine makes html links (href) out of text browseable urls
    * @param text
    * @return Konvertierter String
    */
@@ -838,8 +934,10 @@ public final class StringUtil {
    */
   public static String createURLLinks(String haystack) {
     try {
+
       //dieser Ausdruck brauch dringend fachliche Beratung
-      RE regex = new RE("((https://)|(http://)|(ftp://))+([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/([^ \t\r\n<>]+[^ \t\r\n.,<>])");
+      //RE regex = new RE("((https://)|(http://)|(ftp://))+([a-zA-Z0-9_-]+).([a-zA-Z0-9_:-]+)/([^ \t\r\n<>)]+[^ \t\r\n,<>])");
+      RE regex = new RE("((https://)|(http://)|(ftp://)){1}([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/?([^ \t\r\n<>\\)\\]]+[^ \t\r\n.,<>\\)\\]])");
       return regex.substituteAll(haystack,"<a href=\"$0\">$0</a>");
     } catch(REException ex){
       return null;
@@ -847,21 +945,70 @@ public final class StringUtil {
   }
 
   /**
-   *  createURLLinks wandelt text im url-format
-   *  in einen klickbaren link um
-   *  nur sinnvoll, wenn text nicht im html-format eingegeben
+   * this routine takes text in url format and makes
+   * a clickaeble "<href>" link removing any "illegal" html tags
+   * @param haystack, the url
+   * @param title, the href link text
+   * @param imagRoot, the place to find icons
+   * @param extImage, the url of the icon to show next to the link
+   * @return a String containing the url
    */
-  public static String createURLLinks(String haystack,String imageRoot,String extImage,String intImage) {
+  public static String createURLLinks(String haystack, String title, String imageRoot,String extImage) {
     try {
       //dieser Ausdruck brauch dringend fachliche Beratung
-      RE regex = new RE("((https://)|(http://)|(ftp://))+([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/?([^ \t\r\n<>]+[^ \t\r\n.,<>])");
-      return regex.substituteAll(haystack,"<img src=\""+imageRoot+"/"+extImage+"\" border=\"0\"/>&#160;<a href=\"$0\">$0</a>");
+      RE regex = new RE("((https://)|(http://)|(ftp://)){1}([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/?([^ \t\r\n<>\\)\\]]+[^ \t\r\n.,<>\\)\\]])");
+
+      if (title == null) {
+        return regex.substituteAll(haystack,"<img src=\""+imageRoot+"/"+extImage+"\" border=\"0\"/>&#160;<a href=\"$0\">$0</a>");
+      } else {
+        title = removeHTMLTags(title);
+        return regex.substituteAll(haystack,"<img src=\""+imageRoot+"/"+extImage+"\" border=\"0\"/>&#160;<a href=\"$0\">"+title+"</a>");
+      }
     } catch(REException ex){
       return null;
     }
   }
 
   /**
+   * this routine takes text in url format and makes
+   * a clickaeble "<href>" link removing any "illegal" html tags
+   * @param haystack, the url
+   * @param imageRoot, the place to find icons
+   * @param extImage, the url of the icon to show next to the link
+   * @param intImage, unused
+   * @return a String containing the url
+   */
+  public static String createURLLinks(String haystack, String title, String imageRoot,String extImage,String intImage) {
+    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) {
+    try {
+      //dieser Ausdruck brauch dringend fachliche Beratung
+      RE regex = new RE("((https://)|(http://)|(ftp://))+([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/?([^ \t\r\n<>]+[^ \t\r\n.,<>])");
+      if (title != null) {
+        title = removeHTMLTags(title);
+        return regex.substituteAll(haystack,"<img hspace=\"10\" vspace=\"6\" align=\"left\" src=\"$0\" width=\""+width+"\" height=\""+height+"\" alt=\""+title+"\"/>&#160;<br><i>"+title+"</i>");
+      } else {
+        return regex.substituteAll(haystack,"<img hspace=\"10\" vspace=\"6\" align=\"left\" src=\"$0\" width=\""+width+"\" height=\""+height+"\" alt=\"\"/>&#160;");
+      }
+    } catch(REException ex){
+      return null;
+    }
+  }
+
+
+
+   /**
    *  deleteForbiddenTags
    *  this method deletes all <script>, <body> and <head>-tags
    */
@@ -878,7 +1025,7 @@ public final class StringUtil {
       return null;
     }
   }
-  
+
     /**
      * this method deletes all html tags
      *
@@ -908,24 +1055,24 @@ try {
       String approvedTags="a|img|h1|h2|h3|h4|h5|h6|br|b|i|strong|p";
       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";
       String approvedProtocols="rtsp|http|ftp|https|freenet|mailto";
-      
+
       // kill all the bad tags that have attributes
       String s = "<\\s*/?\\s*(?!(("+approvedTags+")\\s))\\w+\\s[^>]*>";
       RE regex = new RE(s,RE.REG_ICASE);
       haystack = regex.substituteAll(haystack,"");
-      
+
       // kill all the bad tags that are attributeless
       regex = new RE("<\\s*/?\\s*(?!(("+approvedTags+")\\s*>))\\w+\\s*>",RE.REG_ICASE);
       haystack = regex.substituteAll(haystack,"");
-      
+
       // kill all the tags which have a javascript attribute like onLoad
       regex = new RE("<[^>]*("+badAttributes+")[^>]*>",RE.REG_ICASE);
       haystack = regex.substituteAll(haystack,"");
-      
+
       // kill all the tags which include a url to an unacceptable protocol
       regex = new RE("<\\s*a\\s+[^>]*href=(?!(\'|\")?("+approvedProtocols+"))[^>]*>",RE.REG_ICASE);
       haystack = regex.substituteAll(haystack,"");
-      
+
       return haystack;
     } catch(REException ex){
       ex.printStackTrace();
@@ -956,7 +1103,7 @@ try {
     content=convertNewline2Break(content);
     content=convertNewline2P(content);
     content=createMailLinks(content,producerDocRoot,mailImage);
-    content=createURLLinks(content,producerDocRoot,extImage,intImage);
+    content=createURLLinks(content,null,producerDocRoot,extImage,intImage);
     return content;
   }