print out tokens in IMAP
[org.ibex.mail.git] / src / org / ibex / mail / ContentType.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import static org.ibex.mail.MailException.*;
7 import org.ibex.crypto.*;
8 import org.ibex.util.*;
9 import org.ibex.mail.protocol.*;
10 import org.ibex.io.*;
11 import org.ibex.js.*;
12 import java.util.*;
13 import java.net.*;
14 import java.io.*;
15
16 // multipart/mixed       -- default
17 // multipart/parallel    -- order of components does not matter
18 // multipart/alternative -- same data, different versions
19 // multipart/digest      -- default content-type of components is message/rfc822
20 // message/rfc822        -- FIXME
21 // message/partial       -- not supported; see RFC 2046, section 5.2.2
22 // message/external-body -- not supported; see RFC 2046, section 5.2.3
23 // FIXME charsets  US-ASCII, ISO-8559-X, 
24 public class ContentType extends org.ibex.js.JSReflection {
25     public final String    type;
26     public final String    subtype;
27     public final String    description;
28     public final String    id;
29     public final String    transferEncoding;
30     public final String    charset;
31     public final boolean   composite;
32     public final boolean   alternative;
33     public final Hash      parameters = new Hash();
34     public ContentType(String header, String description, String id, String transferEncoding) {
35         this.id = id;
36         this.description = description;
37         this.transferEncoding = transferEncoding;
38         if (header == null) { type="text"; subtype="plain"; charset="us-ascii"; alternative=false; composite=false; return; }
39         header = header.trim();
40         if (header.indexOf('/') == -1) {
41             Log.warn(this, "content-type lacks a forward slash: \""+header+"\"");
42             header = "text/plain";
43         }
44         type = header.substring(0, header.indexOf('/')).toLowerCase();
45         header = header.substring(header.indexOf('/') + 1);
46         subtype = (header.indexOf(';') == -1) ? header.toLowerCase() : header.substring(0, header.indexOf(';')).toLowerCase();
47         composite   = type != null && (type.equals("message") || type.equals("multipart"));
48         alternative = composite && subtype.equals("alternative");
49         charset     = parameters.get("charset") == null ? "us-ascii" : parameters.get("charset").toString();
50         if (header.indexOf(';') == -1) return;
51         StringTokenizer st = new StringTokenizer(header.substring(header.indexOf(';') + 1), ";");
52         while(st.hasMoreTokens()) {
53             String key = st.nextToken().trim();
54             if (key.indexOf('=') == -1)
55                 throw new Malformed("content-type parameter lacks an equals sign: \""+key+"\"");
56             String val = key.substring(key.indexOf('=')+1).trim();
57             if (val.startsWith("\"") && val.endsWith("\"")) val = val.substring(1, val.length() - 2);
58             key = key.substring(0, key.indexOf('=')+1).toLowerCase();
59             parameters.put(key, val);
60         }
61     }
62 }