org.sakaiproject.profile2.util
Class ProfileUtils

java.lang.Object
  extended by org.sakaiproject.profile2.util.ProfileUtils

public class ProfileUtils
extends Object


Constructor Summary
ProfileUtils()
           
 
Method Summary
static String calculateMD5(String s)
          Calculate an MD5 hash of a string
static boolean checkContentTypeForProfileImage(String contentType)
          Check content type against allowed types.
static String convertDateForStatus(Date date)
          Convert a date into a field like "just then, 2 minutes ago, 4 hours ago, yesterday, on sunday, etc"
static String convertDateToString(Date date, String format)
          Convert a Date into a String according to format
static Date convertStringToDate(String dateStr, String format)
          Convert a string into a Date object (reverse of above
static byte[] createAvatar(byte[] imageData, String mimeType)
          Creates a square avatar image by taking a segment out of the centre of the original image and resizing to the appropriate dimensions
static String createEventRef(String ref)
          Creates a full profile event reference for a given reference
static String generateUuid()
          Generate a UUID
static String getDayName(int day, Locale locale)
          Get the localised name of the day (ie Monday for en, Maandag for nl)
static String getInformalFormatForMimeType(String mimeType)
          Helper to get the informal format name that is used by ImageIO.
static List<String> getListFromString(String str, char separator)
          Method to chop a String into it's parts based on the separator and return as a List.
static String getSkypeMeURL(String skypeUsername)
          Returns the SkypeMe URL for the specified Skype username.
static Locale getUserPreferredLocale()
          Gets the users preferred locale, either from the user's session or Sakai preferences and returns it This depends on Sakai's ResourceLoader.
static Object getValueFromMapOrDefault(Map<?,?> map, Object key, Object defaultValue)
          Method for getting a value from a map based on the given key, but if it does not exist, use the given default
static String processHtml(String s)
          Processes HTML and escapes evils tags like <script>, also converts newlines to proper HTML breaks.
static
<T> void
removeDuplicates(List<T> list)
          Remove duplicates from a list, order is not retained.
static
<T> void
removeDuplicatesWithOrder(List<T> list)
          Remove duplicates from a list, order is retained.
static byte[] scaleImage(byte[] imageData, int maxSize, String mimeType)
          Scale an image so it is fit within a give width and height, whilst maintaining its original proportions
static String stripHtml(String s)
          Strips string of HTML and returns plain text.
static Date stripYear(Date date)
          Strip the year from a given date (actually just sets it to 1)
static String toProperCase(String input)
          Convert a string to propercase.
static String truncate(String s, int maxNumOfChars, boolean isHtml)
          Trims text to the given maximum number of displayed characters.
static String truncateAndAbbreviate(String s, int maxNumOfChars, boolean isHtml)
          Trims and abbreviates text to the given maximum number of displayed characters (less 3 characters, in case "..." must be appended).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProfileUtils

public ProfileUtils()
Method Detail

checkContentTypeForProfileImage

public static boolean checkContentTypeForProfileImage(String contentType)
Check content type against allowed types. only JPEG,GIF and PNG are support at the moment

Parameters:
contentType - string of the content type determined by some image parser

getInformalFormatForMimeType

public static String getInformalFormatForMimeType(String mimeType)
Helper to get the informal format name that is used by ImageIO. We have access to the mimetype so we can map them.

If no valid mapping is found, it will default to "jpg".

Parameters:
mimeType - the mimetype of the original image, eg image/jpeg

scaleImage

public static byte[] scaleImage(byte[] imageData,
                                int maxSize,
                                String mimeType)
Scale an image so it is fit within a give width and height, whilst maintaining its original proportions

Parameters:
imageData - bytes of the original image
maxSize - maximum dimension in px

convertDateToString

public static String convertDateToString(Date date,
                                         String format)
Convert a Date into a String according to format

Parameters:
date - date to convert
format - format in SimpleDateFormat syntax

convertStringToDate

public static Date convertStringToDate(String dateStr,
                                       String format)
Convert a string into a Date object (reverse of above

Parameters:
dateStr - date string to convert
format - format of the input date in SimpleDateFormat syntax

stripYear

public static Date stripYear(Date date)
Strip the year from a given date (actually just sets it to 1)

Parameters:
date - original date
Returns:

getDayName

public static String getDayName(int day,
                                Locale locale)
Get the localised name of the day (ie Monday for en, Maandag for nl)

Parameters:
day - int according to Calendar.DAY_OF_WEEK
locale - locale to render dayname in
Returns:

toProperCase

public static String toProperCase(String input)
Convert a string to propercase. ie This Is Proper Text

Parameters:
input - string to be formatted
Returns:

convertDateForStatus

public static String convertDateForStatus(Date date)
Convert a date into a field like "just then, 2 minutes ago, 4 hours ago, yesterday, on sunday, etc"

Parameters:
date - date to convert

getUserPreferredLocale

public static Locale getUserPreferredLocale()
Gets the users preferred locale, either from the user's session or Sakai preferences and returns it This depends on Sakai's ResourceLoader.

Returns:

createEventRef

public static String createEventRef(String ref)
Creates a full profile event reference for a given reference

Parameters:
ref -
Returns:

getValueFromMapOrDefault

public static Object getValueFromMapOrDefault(Map<?,?> map,
                                              Object key,
                                              Object defaultValue)
Method for getting a value from a map based on the given key, but if it does not exist, use the given default

Parameters:
map -
key -
defaultValue -
Returns:

getListFromString

public static List<String> getListFromString(String str,
                                             char separator)
Method to chop a String into it's parts based on the separator and return as a List. Useful for multi valued Sakai properties

Parameters:
str - the String to split
separator - separator character
Returns:

processHtml

public static String processHtml(String s)
Processes HTML and escapes evils tags like <script>, also converts newlines to proper HTML breaks.

Parameters:
s -
Returns:

stripHtml

public static String stripHtml(String s)
Strips string of HTML and returns plain text.

Parameters:
s -
Returns:

truncate

public static String truncate(String s,
                              int maxNumOfChars,
                              boolean isHtml)
Trims text to the given maximum number of displayed characters. Supports HTML and preserves formatting.

Parameters:
s - the string
maxNumOfChars - num chars to keep. If HTML, it's the number of content chars, ignoring tags.
isHtml - is the string HTML?
Returns:

truncateAndAbbreviate

public static String truncateAndAbbreviate(String s,
                                           int maxNumOfChars,
                                           boolean isHtml)
Trims and abbreviates text to the given maximum number of displayed characters (less 3 characters, in case "..." must be appended). Supports HTML and preserves formatting.

Parameters:
s - the string
maxNumOfChars - num chars to keep. If HTML, it's the number of content chars, ignoring tags.
isHtml - is the string HTML?
Returns:

generateUuid

public static String generateUuid()
Generate a UUID

Returns:

getSkypeMeURL

public static String getSkypeMeURL(String skypeUsername)
Returns the SkypeMe URL for the specified Skype username.

Parameters:
skypeUsername -
Returns:
the SkypeMe URL for the specified Skype username.

removeDuplicates

public static <T> void removeDuplicates(List<T> list)
Remove duplicates from a list, order is not retained.

Parameters:
list - list of objects to clean

removeDuplicatesWithOrder

public static <T> void removeDuplicatesWithOrder(List<T> list)
Remove duplicates from a list, order is retained.

Parameters:
list - list of objects to clean

calculateMD5

public static String calculateMD5(String s)
Calculate an MD5 hash of a string

Parameters:
s - String to hash
Returns:
MD5 hash as a String

createAvatar

public static byte[] createAvatar(byte[] imageData,
                                  String mimeType)
Creates a square avatar image by taking a segment out of the centre of the original image and resizing to the appropriate dimensions

Parameters:
imageData - original bytes of the image
mimeType - mimetype of image
Returns:


Copyright © 2008-2012 The Sakai Foundation. All Rights Reserved.