1 12 package org.displaytag.util; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.apache.commons.lang.StringUtils; 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.displaytag.properties.MediaTypeEnum; 21 22 23 31 public final class MediaUtil 32 { 33 34 37 private static Log log = LogFactory.getLog(MediaUtil.class); 38 39 42 private MediaUtil() 43 { 44 } 45 46 51 public static interface SupportsMedia 52 { 53 54 58 void setSupportedMedia(List media); 59 60 64 List getSupportedMedia(); 65 } 66 67 73 public static void setMedia(SupportsMedia mediaSupporter, String media) 74 { 75 if (mediaSupporter == null) 76 { 77 return; 78 } 79 80 if (StringUtils.isBlank(media) || media.toLowerCase().indexOf("all") > -1) 81 { 82 mediaSupporter.setSupportedMedia(null); 83 return; 84 } 85 List supportedMedia = new ArrayList (); 86 String [] values = StringUtils.split(media); 87 for (int i = 0; i < values.length; i++) 88 { 89 String value = values[i]; 90 if (!StringUtils.isBlank(value)) 91 { 92 MediaTypeEnum type = MediaTypeEnum.fromName(value.toLowerCase()); 93 if (type == null) 94 { 95 log.warn("Unrecognized value for attribute \"media\" value=\"" + value + "\""); 96 } 97 else 98 { 99 supportedMedia.add(type); 100 } 101 } 102 } 103 mediaSupporter.setSupportedMedia(supportedMedia); 104 } 105 106 112 public static boolean availableForMedia(SupportsMedia mediaSupporter, MediaTypeEnum mediaType) 113 { 114 if (mediaSupporter == null) 115 { 116 return false; 117 } 118 119 List supportedMedia = mediaSupporter.getSupportedMedia(); 120 121 if (supportedMedia == null) 122 { 123 return true; 124 } 125 126 return supportedMedia.contains(mediaType); 127 } 128 } 129 | Popular Tags |