Here are the examples of the java api class com.google.caja.util.ContentType taken from open source projects.
1. GuessContentType#guess()
Project: caja
File: GuessContentType.java
File: GuessContentType.java
/** * Tries a number of heuristics to determine the type of content. * <ol> * <li>if mimeType is provided and is recognized, uses it. * <li>if the file path is available and has a recognized extension, uses * it. * <li>if the code is provided then applies some further heuristics; * anything where the first non-whitespace char is {@code '<'} is markup. * </ol> * * @param mimeType null or a string like "text/plain" or * "text/javascript; param=value" * @param path null or the path to the content. Either a URL path * (no trailing query or fragment) or a file system path will work. * @param code null or a representative prefix of the content. * @return a known {@link ContentType} or null if none could be found. */ public static ContentType guess(@Nullable String mimeType, @Nullable String path, @Nullable CharSequence code) { ContentType contentType = null; if (mimeType != null) { contentType = ContentType.fromMimeType(mimeType); } if (contentType == null && path != null) { int dot = path.lastIndexOf('.'); if (dot >= 0) { String ext = path.substring(dot + 1); for (ContentType candidate : ContentType.values()) { if (ext.equals(candidate.ext)) { contentType = candidate; break; } } } } if (contentType == null && code != null) { char ch = '\0'; for (int i = 0, n = code.length(); i < n; ++i) { ch = code.charAt(i); if (!Character.isWhitespace(ch)) { break; } } switch(ch) { case '<': contentType = ContentType.HTML; break; case '@': case '.': case '#': contentType = ContentType.CSS; break; } } if (contentType == null && code != null) { // Try and lex and see what happens. CharProducer cp = CharProducer.Factory.fromString(code, FilePosition.UNKNOWN); boolean sawColon = false; try { CssLexer cssLexer = new CssLexer(cp, DevNullMessageQueue.singleton(), false); contentType = ContentType.CSS; while (cssLexer.hasNext()) { Token<CssTokenType> t = cssLexer.next(); if ("if".equals(t.text) || "while".equals(t.text) || "for".equals(t.text) || "return".equals(t.text) || "?".equals(t.text)) { contentType = ContentType.JS; break; } if (!sawColon && ":".equals(t.text)) { sawColon = true; } } if (!sawColon) { contentType = ContentType.JS; } } catch (ParseException ex) { contentType = ContentType.JS; } } return contentType; }