View Javadoc

1   /*
2    * Copyright 2007-2009 Medsea Business Solutions S.L.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package eu.medsea.mimeutil;
17  
18  import eu.medsea.util.EncodingGuesser;
19  
20  /**
21   * This class can be used to represent a mime type for a text file.
22   * This should only be returned by MimeDetector(s) that use magic number
23   * type matching. It allows for an encoding to be associated to a text type
24   * mime type such as text/plain.
25   *
26   * @author Steven McArdle
27   *
28   */
29  public class TextMimeType extends MimeType {
30  
31  	private static final long serialVersionUID = -4798584119063522367L;
32  
33  	// The default encoding is always set Unknown
34  	private String encoding = "Unknown";
35  
36  	/**
37  	 * Construct a TextMimeType from a string representation of a MimeType and
38  	 * an encoding that should be one of the known encodings.
39  	 *
40  	 * @param mimeType
41  	 * @param encoding
42  	 *
43  	 * @see #getKnownEncodings()
44  	 * @see #addKnownEncoding(String)
45  	 */
46  	public TextMimeType(final String mimeType, final String encoding) {
47  		super(mimeType);
48  		this.encoding = getValidEncoding(encoding);
49  	}
50  
51  	public TextMimeType(final MimeType mimeType, final String encoding) {
52  		super(mimeType);
53  		this.encoding = getValidEncoding(encoding);
54  	}
55  
56  	public TextMimeType(final MimeType mimeType) {
57  		super(mimeType);
58  		// We don't change the encoding
59  	}
60  
61  	public void setMimeType(MimeType mimeType) {
62  		mediaType = mimeType.mediaType;
63  		subType = mimeType.subType;
64  	}
65  
66  	/**
67  	 * Get the encoding currently set for this TextMimeType.
68  	 * @return the encoding as a string
69  	 * @see #getKnownEncodings()
70  	 * @see #setEncoding(String)
71  	 */
72  	public String getEncoding() {
73  		return encoding;
74  	}
75  
76  	public void setEncoding(String encoding) {
77  		this.encoding = encoding;
78  	}
79  
80  	public String toString() {
81  		return super.toString() + ";charset=" + getEncoding();
82  	}
83  
84  	/**
85  	 * Utility method to see if the passed in encoding is known to this class.
86  	 *
87  	 * @param encoding
88  	 * @return true if encoding passed in is one of the known encodings else false
89  	 * @see #getKnownEncodings()
90  	 */
91  	private boolean isKnownEncoding(String encoding) {
92  		return EncodingGuesser.isKnownEncoding(encoding);
93  	}
94  
95  	private String getValidEncoding(String encoding) {
96  		if(isKnownEncoding(encoding)) {
97  			return encoding;
98  		} else {
99  			return "Unknown";
100 		}
101 	}
102 
103 	public void setMediaType(String mediaType) {
104 		this.mediaType = mediaType;
105 	}
106 
107 	public void setSubType(String subType) {
108 		this.subType = subType;
109 	}
110 }
111