KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > ContentType


1 /*
2  * ContentType.java
3  * Created on 17. maj 2003, 00:39
4  *
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS"
12  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
13  * License for the specific language governing rights and limitations
14  * under the License.
15  *
16  * The Original Code is Eudora Mail Import Filter plugin for Columba.
17  *
18  * The Initial Developer of the Original Code is Karl Peder Olesen.
19  * Portions created by Karl Peder Olesen are Copyright (C) 2003
20  *
21  * All Rights Reserved.
22  *
23  */

24 package org.columba.mail;
25
26 import java.util.StringTokenizer JavaDoc;
27
28 /**
29  * Class representing a Content-Type header with type, subType and parameters.
30  * Main use is to parse a Content-Type header (without "Content-Type: ") to
31  * get type, subType and boundary.
32  *
33  * @author Karl Peder Olesen
34  * @version 1.0
35  */

36 class ContentType
37 {
38     
39     private String JavaDoc mType;
40     private String JavaDoc mSubType;
41     private String JavaDoc mParameters;
42     private String JavaDoc mBoundary; // the boundary parameter
43

44
45     /** Creates new instance and sets content type from a string (header minus "Content-Type: " */
46     public ContentType(String JavaDoc input)
47     {
48         
49         String JavaDoc s = input.trim();
50         mType = s.substring(0, s.indexOf('/'));
51         mSubType = (new StringTokenizer JavaDoc(s.substring(s.indexOf('/') + 1))).nextToken();
52         mParameters = s.substring(s.indexOf('/') + mSubType.length() + 1); // parameters ~ rest of string
53
int pos = s.indexOf("boundary=");
54         pos = pos + 9;
55         if (s.indexOf(';', pos) == -1)
56             mBoundary = s.substring(pos).trim();
57         else
58             mBoundary = s.substring(pos, s.indexOf(';', pos)).trim();
59         if (mBoundary.startsWith("\""))
60             mBoundary = mBoundary.substring(1, mBoundary.length()-1); // strip "'s
61
}
62     
63     public String JavaDoc getType() { return mType; }
64     
65     public String JavaDoc getSubType() { return mSubType; }
66     
67     public String JavaDoc getBoundary() { return mBoundary; }
68  
69     /** outputs the Content-Type definition (minus "Content-Type: ") */
70     public String JavaDoc toString()
71     {
72         return mType + "/" + mSubType + mParameters;
73     }
74 }
75
Popular Tags