=== 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 http://dhis2.org, + * and www.dhis2.org is replaced with www.dhis2.org. + * + * @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 = "" + match.trim() + "" + 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( "http://dhis2.org", htmlLinks( "http://dhis2.org" ) ); + assertEquals( "https://dhis2.org", htmlLinks( "https://dhis2.org" ) ); + assertEquals( "www.dhis2.org", htmlLinks( "www.dhis2.org" ) ); + assertEquals( "Navigate to http://dhis2.org or www.dhis2.com 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 map = new HashMap(); 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 @@ $encoder.htmlEncode( $message.sender.name )  $format.formatDate( $message.lastUpdated ) -

$encoder.htmlEncode( $message.text )

+

$dhisTextUtils.htmlLinks( $encoder.htmlEncode( $message.text ) )

#end