KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > plugins > mode > ContentTypes


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.server.plugins.mode;
10
11 import java.util.Collection JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.jboss.portal.common.metadata.MetaData;
20 import org.jboss.portal.server.metadata.ContentTypeMetaData;
21 import org.jboss.portal.server.metadata.ContentTypesMetaData;
22 import org.jboss.portal.server.plugins.PluginService;
23
24 /**
25  * This object holds the content type and mode capabilities.
26  *
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.3 $
29  */

30 public class ContentTypes
31    extends PluginService
32 {
33
34    private final Map JavaDoc contentTypeToModes = new HashMap JavaDoc();
35    private final Map JavaDoc typeToPorltetModes = new HashMap JavaDoc(); // Including *
36

37    private Set JavaDoc allContentTypes = new HashSet JavaDoc();
38    private Set JavaDoc allModes = new HashSet JavaDoc();
39    private Set JavaDoc immutableAllContentTypes = Collections.unmodifiableSet(allContentTypes);
40    private Set JavaDoc immutableAllModes = Collections.unmodifiableSet(allModes);
41    private ContentTypesMetaData metaData;
42
43    // case-insensitive
44
// CONTENT TYPE = TYPE / SUBTYPE
45
// SUBTYPE = any char except a TSPECIALS or SPACE or CTLS (??)
46
// TSPECIALS ( ) < > @ , ; : \ " / [ ] ? . =
47

48    public ContentTypes()
49    {
50    }
51
52    public void start() throws Exception JavaDoc
53    {
54       // Process all the supported content types
55
for (Iterator JavaDoc i = this.metaData.iterator();i.hasNext();)
56       {
57          ContentTypeMetaData contentTypeMD = (ContentTypeMetaData)i.next();
58
59          // Get the content type
60
String JavaDoc contentType = contentTypeMD.getContentType().toLowerCase();
61
62          // Add the content type to the view mode
63
// because each content type must handle this view
64
add(contentType, Mode.VIEW);
65
66          // Then process each mode
67
Set JavaDoc modes = contentTypeMD.getModes();
68          for (Iterator JavaDoc j = modes.iterator();j.hasNext();)
69          {
70             Mode mode = (Mode)j.next();
71             add(contentType, mode);
72          }
73       }
74    }
75
76    public void setMetaData(MetaData metaData)
77    {
78       this.metaData = (ContentTypesMetaData)metaData;
79    }
80
81    public MetaData getMetaData()
82    {
83       return metaData;
84    }
85
86    private void add(String JavaDoc contentType, Mode mode)
87    {
88       //
89
if ("*".equals(contentType) || "*/*".equals(contentType))
90       {
91          Set JavaDoc modes = (Set JavaDoc)typeToPorltetModes.get("*");
92          if (modes == null)
93          {
94             modes = new HashSet JavaDoc();
95             typeToPorltetModes.put("*", modes);
96          }
97          modes.add(mode);
98          return;
99       }
100
101       //
102
int index = contentType.indexOf('/');
103       if (index == -1)
104       {
105          return;
106       }
107
108       // Get type and subtype
109
String JavaDoc type = contentType.substring(0, index);
110       String JavaDoc subtype = contentType.substring(index + 1);
111
112       //
113
if ("*".equals(subtype))
114       {
115          Set JavaDoc modes = (Set JavaDoc)typeToPorltetModes.get(type);
116          if (modes == null)
117          {
118             modes = new HashSet JavaDoc();
119             typeToPorltetModes.put(type, modes);
120          }
121          modes.add(mode);
122          return;
123       }
124
125       //
126
Set JavaDoc modes = (Set JavaDoc)contentTypeToModes.get(contentType);
127       if (modes == null)
128       {
129          modes = new HashSet JavaDoc();
130          contentTypeToModes.put(contentType, modes);
131       }
132       modes.add(mode);
133    }
134
135    /**
136     * Return all modes.
137     */

138    public Collection JavaDoc getModes()
139    {
140       return immutableAllModes;
141    }
142
143    /**
144     * Return all content types.
145     */

146    public Collection JavaDoc getContentTypes()
147    {
148       return immutableAllContentTypes;
149    }
150
151    /**
152     * Return true if the mode is supported
153     *
154     * @throws IllegalArgumentException if the mode is null
155     */

156    public boolean isModeSupported(Mode mode)
157    {
158       if (mode == null)
159       {
160          throw new IllegalArgumentException JavaDoc("Mode must not be null");
161       }
162       return allModes.contains(mode);
163    }
164
165    /**
166     * Return all the supported modes for this content type.
167     *
168     * @throws IllegalArgumentException if the content type is null
169     */

170    public Set JavaDoc getSupportedModes(String JavaDoc contentType)
171    {
172       if (contentType == null)
173       {
174          throw new IllegalArgumentException JavaDoc("Content type must not be null");
175       }
176
177       int index = contentType.indexOf('/');
178       if (index == -1)
179       {
180          // Not valid content type so return empty set
181
return Collections.EMPTY_SET;
182       }
183
184       //
185
Set JavaDoc set = new HashSet JavaDoc();
186
187       //
188
Set JavaDoc contentTypeModes = (Set JavaDoc)contentTypeToModes.get(contentType.toLowerCase());
189       if (contentTypeModes != null)
190       {
191          set.addAll(contentTypeModes);
192       }
193
194       //
195
Set JavaDoc typeModes = (Set JavaDoc)typeToPorltetModes.get(contentType.substring(0, index).toLowerCase());
196       if (typeModes != null)
197       {
198          set.addAll(typeModes);
199       }
200
201       Set JavaDoc wildcardModes = (Set JavaDoc)typeToPorltetModes.get("*");
202       if (wildcardModes != null)
203       {
204          set.addAll(wildcardModes);
205       }
206
207       return set;
208    }
209
210    /**
211     * Return true if the content type is supported.
212     *
213     * @throws IllegalArgumentException if the content type is null
214     */

215    public boolean isContentTypeSupported(String JavaDoc contentType)
216    {
217       if (contentType == null)
218       {
219          throw new IllegalArgumentException JavaDoc("Content type must not be null");
220       }
221
222       int index = contentType.indexOf('/');
223       if (index == -1)
224       {
225          // Not valid content type so return false
226
return false;
227       }
228
229       // Try the content type, * or */*
230
if (allContentTypes.contains(contentType) |
231           allContentTypes.contains(contentType.toLowerCase()) |
232           allContentTypes.contains("*") |
233           allContentTypes.contains("*/*"))
234       {
235          return true;
236       }
237
238       // Try xxx/*
239
String JavaDoc wildcard = contentType.substring(0, index) + "/*";
240       return allContentTypes.contains(wildcard) | allContentTypes.contains(wildcard.toLowerCase());
241    }
242
243    /**
244     * Return true if the given content type is supported for the given mode.
245     *
246     * @throws IllegalArgumentException if the content type or the mode is null
247     */

248    public boolean isSupported(Mode mode, String JavaDoc contentType)
249    {
250       if (mode == null)
251       {
252          throw new IllegalArgumentException JavaDoc("Mode must not be null");
253       }
254       if (contentType == null)
255       {
256          throw new IllegalArgumentException JavaDoc("Content type must not be null");
257       }
258
259       int index = contentType.indexOf('/');
260       if (index == -1)
261       {
262          // Not valid content type so return false
263
return false;
264       }
265
266       // Try full name matching
267
Set JavaDoc contentTypeModes = (Set JavaDoc)contentTypeToModes.get(contentType);
268       if (contentTypeModes != null && contentTypeModes.contains(mode))
269       {
270          return true;
271       }
272
273       // Try full name lowercase matching
274
Set JavaDoc lowercaseContentTypeModes = (Set JavaDoc)contentTypeToModes.get(contentType.toLowerCase());
275       if (lowercaseContentTypeModes != null && lowercaseContentTypeModes.contains(mode))
276       {
277          return true;
278       }
279
280       // Try type matching
281
String JavaDoc type = contentType.substring(0, index);
282       Set JavaDoc typeMode = (Set JavaDoc)typeToPorltetModes.get(type);
283       if (typeMode != null && typeMode.contains(mode))
284       {
285          return true;
286       }
287
288       // Try type matching
289
Set JavaDoc lowercaseTypeMode = (Set JavaDoc)typeToPorltetModes.get(type.toLowerCase());
290       if (lowercaseTypeMode != null && lowercaseTypeMode.contains(mode))
291       {
292          return true;
293       }
294
295       // Try wildcard matching
296
Set JavaDoc wildcardMode = (Set JavaDoc)typeToPorltetModes.get("*");
297       return wildcardMode != null && wildcardMode.contains(mode);
298    }
299 }
300
Popular Tags