Package org.sakaiproject.profile2.util
Class ProfileUtils
- java.lang.Object
-
- org.sakaiproject.profile2.util.ProfileUtils
-
public class ProfileUtils extends Object
-
-
Constructor Summary
Constructors Constructor Description ProfileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringcalculateMD5(String s)Calculate an MD5 hash of a stringstatic booleancheckContentTypeForProfileImage(String contentType)Check content type against allowed types. only JPEG,GIF and PNG are support at the momentstatic StringconvertDateForStatus(Date date)Convert a date into a field like "just then, 2 minutes ago, 4 hours ago, yesterday, on sunday, etc"static StringconvertDateToString(Date date, String format)Convert a Date into a String according to format, or, if format is set to null, use DateFormat.MEDIUM.static StringconvertDateToString(Date date, String format, boolean useLocale)Convert a Date into a String according to format, or, if format is set to null, use DateFormat.MEDIUM and also force using locale.static DateconvertStringToDate(String dateStr, String format)Convert a string into a Date object (reverse of abovestatic 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 dimensionsstatic StringcreateEventRef(String ref)Creates a full profile event reference for a given referencestatic StringgenerateUuid()Generate a UUIDstatic StringgetDayName(int day, Locale locale)Get the localised name of the day (ie Monday for en, Maandag for nl)static StringgetInformalFormatForMimeType(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 StringgetSkypeMeURL(String skypeUsername)Returns the SkypeMe URL for the specified Skype username.static LocalegetUserPreferredLocale()Gets the users preferred locale, either from the user's session or Sakai preferences and returns it This depends on Sakai's ResourceLoader.static StringgetUserPreferredOrientation()Gets the users preferred orientation, either from the user's session or Sakai preferences and returns it This depends on Sakai's ResourceLoader.static ObjectgetValueFromMapOrDefault(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 defaultstatic StringprocessHtml(String s)Processes HTML and escapes evils tags like <script>, also converts newlines to proper HTML breaks.static <T> voidremoveDuplicates(List<T> list)Remove duplicates from a list, order is not retained.static <T> voidremoveDuplicatesWithOrder(List<T> list)Remove duplicates from a list, order is retained.static byte[]scaleImage(byte[] imageData, int maxSize, String mimeType)static byte[]scaleImage(InputStream in, int maxSize, String mimeType)Scale an image so it is fit within a give width and height, whilst maintaining its original proportionsstatic StringstripAndCleanHtml(String s)Strips string of HTML, escaping anything that is left to return plain text.static StringstripHtml(String s)Strips string of HTML and returns plain text.static StringstripHtmlFromText(String text, boolean smartSpacing, boolean stripEscapeSequences)Strips html/xml tags from a string and returns the cleaned version.static DatestripYear(Date date)Strip the year from a given date (actually just sets it to 1)static StringtoProperCase(String input)Convert a string to propercase. ie This Is Proper Textstatic Stringtruncate(String s, int maxNumOfChars, boolean isHtml)Trims text to the given maximum number of displayed characters.static StringtruncateAndAbbreviate(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).
-
-
-
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)
-
scaleImage
public static byte[] scaleImage(InputStream in, 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 imagemaxSize- maximum dimension in px
-
convertDateToString
public static String convertDateToString(Date date, String format)
Convert a Date into a String according to format, or, if format is set to null, use DateFormat.MEDIUM. Do not use locale to preserve old behavior- Parameters:
date- date to convertformat- format in SimpleDateFormat syntax. Set to null to force DateFormat.MEDIUM.
-
convertDateToString
public static String convertDateToString(Date date, String format, boolean useLocale)
Convert a Date into a String according to format, or, if format is set to null, use DateFormat.MEDIUM and also force using locale.- Parameters:
date- date to convertformat- format in SimpleDateFormat syntax. Set to null to force DateFormat.MEDIUM and locale.useLocale- Use the users locale, added a default to reduce chance of regression for code that was expecting the previous format when format was not set.
-
convertStringToDate
public static Date convertStringToDate(String dateStr, String format)
Convert a string into a Date object (reverse of above- Parameters:
dateStr- date string to convertformat- 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_WEEKlocale- 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:
-
getUserPreferredOrientation
public static String getUserPreferredOrientation()
Gets the users preferred orientation, 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 splitseparator- 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:
-
stripAndCleanHtml
public static String stripAndCleanHtml(String s)
Strips string of HTML, escaping anything that is left to return plain text.Deals better with poorly formed HTML than just stripHtml and is best for XSS protection, not for storing actual data.
- Parameters:
s- The string to process- Returns:
-
stripHtmlFromText
public static String stripHtmlFromText(String text, boolean smartSpacing, boolean stripEscapeSequences)
Strips html/xml tags from a string and returns the cleaned version.- Parameters:
text- any text (if this is null or empty then the input text is returned unchanged)smartSpacing- if true then try to make the text represent the intent of the html, trims out duplicate spaces, converts block type html into a space, etc., else just removes html tags and leaves all other parts of the string intact, NOTE: false is also slightly fasterstripEscapeSequences- if true, strips out any escape sequences such as ' '- Returns:
- the cleaned string
- See Also:
for alternative mechanism
-
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 stringmaxNumOfChars- 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 stringmaxNumOfChars- 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 imagemimeType- mimetype of image- Returns:
-
-