KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > ContentType


1 package org.apache.slide.projector;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 public class ContentType {
7     public final static String JavaDoc XML = "text/xml";
8     public final static String JavaDoc HTML = "text/html";
9     public final static String JavaDoc PLAIN_TEXT = "text/plain";
10     public final static String JavaDoc ANY_TEXT = "text/*";
11     public final static String JavaDoc DYNAMIC = "*";
12
13     public static String JavaDoc determineContentType(String JavaDoc content) {
14         if (content.indexOf("<html") != -1 ) {
15             return HTML;
16         } else if (content.startsWith("<?xml")) {
17             return XML;
18         } else {
19             return PLAIN_TEXT;
20         }
21     }
22
23     public static boolean determineIsDocument(String JavaDoc content) {
24         if (determineContentType(content) == PLAIN_TEXT ) {
25             return false;
26         }
27         return true;
28     }
29
30     public static boolean matches(String JavaDoc requiredContentType, String JavaDoc givenContentType) {
31         if ( requiredContentType.equals(givenContentType) ) return true;
32         if ( requiredContentType.endsWith("*") && givenContentType.startsWith(requiredContentType.substring(0, requiredContentType.length()-1)) ) return true;
33         return false;
34     }
35
36     public static boolean matches(String JavaDoc []allowedContentTypes, String JavaDoc givenContentType) {
37         for ( int i = 0; i < allowedContentTypes.length; i++ ) {
38             if ( matches(allowedContentTypes[i], givenContentType ) ) return true;
39         }
40         return false;
41     }
42
43     public static String JavaDoc getContentTypesAsString(List JavaDoc contentTypes) {
44         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(256);
45         boolean first = true;
46         for ( Iterator JavaDoc i = contentTypes.iterator(); i.hasNext(); ) {
47             if ( !first ) {
48                 buffer.append(", ");
49             }
50             first = false;
51             buffer.append(i.next());
52         }
53         return buffer.toString();
54     }
55
56     public static String JavaDoc getContentTypesAsString(String JavaDoc[] contentTypes) {
57         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(256);
58         for ( int i = 0; i < contentTypes.length; i++ ) {
59             if ( i > 0 ) {
60                 buffer.append(", ");
61             }
62             buffer.append(contentTypes[i]);
63         }
64         return buffer.toString();
65     }
66 }
Popular Tags