The default implementation of PageCreator. Designed to be extended for easier handling of new content types. Just check the content type in createPage() and call super(createPage()) if your custom type isn't found. There are also protected createXXXXPage()</co
| 79 | * @author Antoni Reus |
| 80 | */ |
| 81 | public class DefaultPageCreator implements PageCreator, Serializable { |
| 82 | |
| 83 | private static final byte[] MARKER_UTF8 = {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}; |
| 84 | private static final byte[] MARKER_UTF16BE = {(byte) 0xfe, (byte) 0xff}; |
| 85 | private static final byte[] MARKER_UTF16LE = {(byte) 0xff, (byte) 0xfe}; |
| 86 | |
| 87 | /** |
| 88 | * See <a href="http://tools.ietf.org/html/draft-abarth-mime-sniff-05"> |
| 89 | * http://tools.ietf.org/html/draft-abarth-mime-sniff-05</a> |
| 90 | */ |
| 91 | private static final String[] HTML_PATTERNS = {"!DOCTYPE HTML", "HTML", "HEAD", "SCRIPT", |
| 92 | "IFRAME", "H1", "DIV", "FONT", "TABLE", "A", "STYLE", "TITLE", "B", "BODY", "BR", "P", "!--" }; |
| 93 | |
| 94 | private static final HTMLParser HTML_PARSER = new HtmlUnitNekoHtmlParser(); |
| 95 | |
| 96 | /** |
| 97 | * The different supported page types. |
| 98 | */ |
| 99 | public enum PageType { |
| 100 | /** html. */ |
| 101 | HTML, |
| 102 | /** javascript. */ |
| 103 | JAVASCRIPT, |
| 104 | /** xml. */ |
| 105 | XML, |
| 106 | /** text. */ |
| 107 | TEXT, |
| 108 | /** unknown. */ |
| 109 | UNKNOWN |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Determines the kind of page to create from the content type. |
| 114 | * @param contentType the content type to evaluate |
| 115 | * @return "xml", "html", "javascript", "text" or "unknown" |
| 116 | */ |
| 117 | public static PageType determinePageType(final String contentType) { |
| 118 | if (null == contentType) { |
| 119 | return PageType.UNKNOWN; |
| 120 | } |
| 121 | |
| 122 | final String contentTypeLC = StringUtils.toRootLowerCase(contentType); |
| 123 | |
| 124 | if (MimeType.isJavascriptMimeType(contentTypeLC)) { |
| 125 | return PageType.JAVASCRIPT; |
| 126 | } |
| 127 | switch (contentTypeLC) { |
| 128 | case MimeType.TEXT_HTML: |
| 129 | case "image/svg+xml": |
| 130 | return PageType.HTML; |
| 131 | |
| 132 | case MimeType.TEXT_XML: |
| 133 | case MimeType.APPLICATION_XML: |
| 134 | case "text/vnd.wap.wml": |
| 135 | return PageType.XML; |
| 136 | |
| 137 | default: |
| 138 | if (contentTypeLC.endsWith("+xml")) { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…