KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > util > MediaUtil


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.util;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
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 /**
24  * This class provides services for configuring and determining the list of media types an instance of
25  * <code>SupportsMedia</code> supports. (Background: ColumnTag, FooterTag and CaptionTag can be configured to support
26  * a set of media types. This class factors the logic for setting and determining the media instances these objects
27  * support)
28  * @author Jorge L. Barroso
29  * @version $Revision$ ($Author$)
30  */

31 public final class MediaUtil
32 {
33
34     /**
35      * logger.
36      */

37     private static Log log = LogFactory.getLog(MediaUtil.class);
38
39     /**
40      * Don't instantiate MediaUtil.
41      */

42     private MediaUtil()
43     {
44     }
45
46     /**
47      * Defines a type of object that can support a list of media types.
48      * @author Jorge L. Barroso
49      * @version $Revision$ ($Author$)
50      */

51     public static interface SupportsMedia
52     {
53
54         /**
55          * Configure the list of media types this object will support.
56          * @param media The list of media types this object will support.
57          */

58         void setSupportedMedia(List JavaDoc media);
59
60         /**
61          * Obtain the list of media types this object supports.
62          * @return The list of media types this object supports.
63          */

64         List JavaDoc getSupportedMedia();
65     }
66
67     /**
68      * Configures the media supported by an object that implements <code>SupportsMedia</code>. (Background: factored
69      * from ColumnTag)
70      * @param mediaSupporter The <code>SupportsMedia</code> instance being configured to support a list of media.
71      * @param media The media being configured on the given <code>SupportsMedia</code> instance.
72      */

73     public static void setMedia(SupportsMedia mediaSupporter, String JavaDoc 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 JavaDoc supportedMedia = new ArrayList JavaDoc();
86         String JavaDoc[] values = StringUtils.split(media);
87         for (int i = 0; i < values.length; i++)
88         {
89             String JavaDoc 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     /**
107      * Is this media supporter configured for the media type? (Background: Factored from ColumnTag)
108      * @param mediaSupporter An object that supports various media.
109      * @param mediaType The currentMedia type
110      * @return true if the media supporter should be displayed for this request
111      */

112     public static boolean availableForMedia(SupportsMedia mediaSupporter, MediaTypeEnum mediaType)
113     {
114         if (mediaSupporter == null)
115         {
116             return false;
117         }
118
119         List JavaDoc supportedMedia = mediaSupporter.getSupportedMedia();
120
121         if (supportedMedia == null)
122         {
123             return true;
124         }
125
126         return supportedMedia.contains(mediaType);
127     }
128 }
129
Popular Tags