1 package org.apache.slide.projector; 2 3 import java.util.Iterator ; 4 import java.util.List ; 5 6 public class ContentType { 7 public final static String XML = "text/xml"; 8 public final static String HTML = "text/html"; 9 public final static String PLAIN_TEXT = "text/plain"; 10 public final static String ANY_TEXT = "text/*"; 11 public final static String DYNAMIC = "*"; 12 13 public static String determineContentType(String 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 content) { 24 if (determineContentType(content) == PLAIN_TEXT ) { 25 return false; 26 } 27 return true; 28 } 29 30 public static boolean matches(String requiredContentType, String 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 []allowedContentTypes, String 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 getContentTypesAsString(List contentTypes) { 44 StringBuffer buffer = new StringBuffer (256); 45 boolean first = true; 46 for ( Iterator 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 getContentTypesAsString(String [] contentTypes) { 57 StringBuffer buffer = new StringBuffer (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 |