← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5893: Implemented support for html links in messages. This is useful for linking to reports, resources ...

 

------------------------------------------------------------
revno: 5893
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2012-02-08 17:08:43 +0100
message:
  Implemented support for html links in messages. This is useful for linking to reports, resources etc. Anything starting with http://, https://, www. is detected and turned into html links.
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/interceptor/ContextInterceptor.java
  dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/java/org/hisp/dhis/dashboard/message/action/ReadMessageAction.java
  dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/readMessage.vm


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/TextUtils.java	2012-02-08 16:08:43 +0000
@@ -1,6 +1,8 @@
 package org.hisp.dhis.system.util;
 
 import java.util.Collection;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /*
  * Copyright (c) 2004-2012, University of Oslo
@@ -35,6 +37,45 @@
  */
 public class TextUtils
 {
+    public static final TextUtils INSTANCE = new TextUtils();
+    
+    private static final Pattern LINK_PATTERN = Pattern.compile( "(http://|https://|www\\.).+?($| )" );
+    
+    /**
+     * Substitutes links in the given text with valid HTML mark-up. For instance, 
+     * http://dhis2.org is replaced with <a href="http://dhis2.org";>http://dhis2.org</a>,
+     * and www.dhis2.org is replaced with <a href="http://dhis2.org";>www.dhis2.org</a>.
+     * 
+     * @param text the text to substitute links for.
+     * @return the substituted text.
+     */
+    public static String htmlLinks( String text )
+    {
+        if ( text == null || text.trim().isEmpty() )
+        {
+            return null;
+        }
+        
+        Matcher matcher = LINK_PATTERN.matcher( text );
+
+        StringBuffer buffer = new StringBuffer();
+        
+        while ( matcher.find() )
+        {
+            String match = matcher.group();
+
+            String suffix = match.endsWith( " " ) ? " " : "";
+            
+            String ref = match.trim().startsWith( "www." ) ? "http://"; + match.trim() : match.trim();
+
+            match = "<a href=\"" + ref + "\">" + match.trim() + "</a>" + suffix;
+            
+            matcher.appendReplacement( buffer, match );
+        }
+        
+        return matcher.appendTail( buffer ).toString();
+    }
+    
     /**
      * Gets the sub string of the given string. If the beginIndex is larger than
      * the length of the string, the empty string is returned. If the beginIndex +

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java	2011-12-26 10:07:59 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/TextUtilsTest.java	2012-02-08 16:08:43 +0000
@@ -30,6 +30,7 @@
 import static junit.framework.Assert.assertEquals;
 import static org.hisp.dhis.system.util.TextUtils.subString;
 import static org.hisp.dhis.system.util.TextUtils.trimEnd;
+import static org.hisp.dhis.system.util.TextUtils.htmlLinks;
 
 import org.junit.Test;
 
@@ -42,6 +43,16 @@
     private static final String STRING = "abcdefghij";
     
     @Test
+    public void testHtmlLinks()
+    {
+        assertEquals( "<a href=\"http://dhis2.org\";>http://dhis2.org</a>", htmlLinks( "http://dhis2.org"; ) );
+        assertEquals( "<a href=\"https://dhis2.org\";>https://dhis2.org</a>", htmlLinks( "https://dhis2.org"; ) );
+        assertEquals( "<a href=\"http://www.dhis2.org\";>www.dhis2.org</a>", htmlLinks( "www.dhis2.org" ) );        
+        assertEquals( "Navigate to <a href=\"http://dhis2.org\";>http://dhis2.org</a> or <a href=\"http://www.dhis2.com\";>www.dhis2.com</a> to read more.", 
+            htmlLinks( "Navigate to http://dhis2.org or www.dhis2.com to read more." ) );
+    }
+    
+    @Test
     public void testSubString()
     {
         assertEquals( "abcdefghij", subString( STRING, 0, 10 ) );

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/interceptor/ContextInterceptor.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/interceptor/ContextInterceptor.java	2012-01-22 04:20:13 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/interceptor/ContextInterceptor.java	2012-02-08 16:08:43 +0000
@@ -31,6 +31,7 @@
 import java.util.Map;
 
 import org.hisp.dhis.system.database.DatabaseInfoProvider;
+import org.hisp.dhis.system.util.TextUtils;
 
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.interceptor.Interceptor;
@@ -42,6 +43,7 @@
     implements Interceptor
 {
     private static final String KEY_IN_MEMORY_DATABASE = "inMemoryDatabase";
+    private static final String KEY_TEXT_UTILS = "dhisTextUtils";
     
     private DatabaseInfoProvider databaseInfoProvider;
 
@@ -67,6 +69,7 @@
         Map<String, Object> map = new HashMap<String, Object>();
         
         map.put( KEY_IN_MEMORY_DATABASE, databaseInfoProvider.isInMemory() );
+        map.put( KEY_TEXT_UTILS, TextUtils.INSTANCE );
         
         invocation.getStack().push( map );
         

=== modified file 'dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/java/org/hisp/dhis/dashboard/message/action/ReadMessageAction.java'
--- dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/java/org/hisp/dhis/dashboard/message/action/ReadMessageAction.java	2012-01-13 18:59:01 +0000
+++ dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/java/org/hisp/dhis/dashboard/message/action/ReadMessageAction.java	2012-02-08 16:08:43 +0000
@@ -88,7 +88,7 @@
         throws Exception
     {
         conversation = messageService.getMessageConversation( id );
-        
+                
         if ( conversation.markRead( currentUserService.getCurrentUser() ) )
         {        
             messageService.updateMessageConversation( conversation );

=== modified file 'dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/readMessage.vm'
--- dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/readMessage.vm	2011-12-14 09:17:14 +0000
+++ dhis-2/dhis-web/dhis-web-dashboard-integration/src/main/webapp/dhis-web-dashboard-integration/readMessage.vm	2012-02-08 16:08:43 +0000
@@ -56,7 +56,7 @@
 <span class="bold" style="cursor:pointer" onclick="showSenderInfo( ${message.id}, ${message.sender.id} )">$encoder.htmlEncode( $message.sender.name )</span>&nbsp;
 <span style="color:#606060">$format.formatDate( $message.lastUpdated )</span>
 
-<p>$encoder.htmlEncode( $message.text )</p>
+<p>$dhisTextUtils.htmlLinks( $encoder.htmlEncode( $message.text ) )</p>
 <span id="metaData${message.id}" style="display:none">$!encoder.htmlEncode( $message.metaData )</span>
 </div>
 #end