Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ContentTypeUtil |
|
| 3.0;3 |
1 | /* | |
2 | * Copyright 2004-2011 the Seasar Foundation and the Others. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package org.seasar.teeda.core.util; | |
17 | ||
18 | import org.seasar.framework.util.StringUtil; | |
19 | import org.seasar.teeda.core.JsfConstants; | |
20 | ||
21 | /** | |
22 | * @author shot | |
23 | * @author yone | |
24 | */ | |
25 | 0 | public class ContentTypeUtil { |
26 | ||
27 | public static String getDefaultContentType() { | |
28 | 2 | return JsfConstants.HTML_CONTENT_TYPE; |
29 | } | |
30 | ||
31 | public static String getContentType(String contentTypeList) { | |
32 | 12 | if (contentTypeList == null) { |
33 | 0 | return getDefaultContentType(); |
34 | } | |
35 | 12 | String[] strs = StringUtil.split(contentTypeList, ","); |
36 | 12 | String[] contentTypes = removeSemiColon(strs); |
37 | 23 | for (int i = 0; i < contentTypes.length; i++) { |
38 | 18 | if (isHtmlContentType(contentTypes[i].trim())) { |
39 | 7 | return JsfConstants.HTML_CONTENT_TYPE; |
40 | } | |
41 | } | |
42 | 8 | for (int i = 0; i < contentTypes.length; i++) { |
43 | 6 | if (isXmlContentType(contentTypes[i].trim())) { |
44 | 3 | return JsfConstants.XHTML_CONTENT_TYPE; |
45 | } | |
46 | } | |
47 | 2 | return getDefaultContentType(); |
48 | } | |
49 | ||
50 | public static String[] removeSemiColon(String[] contentTypes) { | |
51 | 39 | for (int i = 0; i < contentTypes.length; i++) { |
52 | 25 | String type = contentTypes[i]; |
53 | 25 | int index = type.indexOf(";"); |
54 | 25 | if (index != -1) { |
55 | 2 | type = type.substring(0, index); |
56 | 2 | contentTypes[i] = type; |
57 | } | |
58 | } | |
59 | 14 | return contentTypes; |
60 | } | |
61 | ||
62 | public static boolean isHtmlContentType(String type) { | |
63 | 22 | return type.indexOf(JsfConstants.HTML_CONTENT_TYPE) != -1 |
64 | || type.equals(JsfConstants.ANY_CONTENT_TYPE); | |
65 | } | |
66 | ||
67 | public static boolean isXmlContentType(String type) { | |
68 | 10 | return type.indexOf(JsfConstants.XHTML_CONTENT_TYPE) != -1 |
69 | || type.indexOf(JsfConstants.APPLICATION_XML_CONTENT_TYPE) != -1 | |
70 | || type.indexOf(JsfConstants.TEXT_XML_CONTENT_TYPE) != -1; | |
71 | } | |
72 | } |