KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > lib > Attachment


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.lib;
19
20 import org.objectweb.jac.core.rtti.ClassRepository;
21 import org.objectweb.jac.core.rtti.FieldItem;
22 import org.objectweb.jac.core.rtti.MetaItem;
23 import org.objectweb.jac.util.MimeTypes;
24
25 /**
26  * This class represents a file attachment. The data field is the
27  * content of the file.
28  */

29 public class Attachment {
30     /**
31      * Creates a new Attachement object. If the mimeType is null, it
32      * will be initialized from the name's extension.
33      */

34     public Attachment(byte[] data, String JavaDoc mimeType, String JavaDoc name) {
35         this.data = data;
36         this.mimeType = mimeType;
37         this.name = name;
38         if (mimeType==null) {
39             guessMimeType();
40         }
41     }
42
43     static MimeTypes mimeTypes;
44
45     public void guessMimeType() {
46         try {
47             if (mimeTypes==null) {
48                 mimeTypes = new MimeTypes();
49                 mimeTypes.readDefaults();
50             }
51             this.mimeType = mimeTypes.getMimeType(name);
52         } catch (Exception JavaDoc e) {
53             e.printStackTrace();
54         }
55     }
56
57     byte[] data;
58     /**
59      * Returns the content of the attachment.
60      * @return the file's content.
61      * @see #setData(byte[])
62      */

63     public byte[] getData() {
64         return data;
65     }
66     /**
67      * Set the content of the file
68      * @param data the content of the file
69      * @see #getData()
70      */

71     public void setData(byte[] data) {
72         this.data = data;
73     }
74
75     String JavaDoc mimeType;
76     /**
77      * @return the mime type of the file (text/plain, text/html,
78      * application/msword, ...)
79      * @see #setMimeType(String)
80      */

81     public String JavaDoc getMimeType() {
82         return mimeType;
83     }
84     /**
85      * Sets the mime type of the file.
86      * @param mimeType the mime type of the file.
87      * @see #getMimeType()
88      */

89     public void setMimeType(String JavaDoc mimeType) {
90         this.mimeType = mimeType;
91     }
92
93     String JavaDoc name;
94     /**
95      * @return the name of the file
96      * @see #setName(String)
97      */

98     public String JavaDoc getName() {
99         return name;
100     }
101     /**
102      * @param name the name of the file
103      * @see #getName()
104      */

105     public void setName(String JavaDoc name) {
106         this.name = name;
107     }
108
109     public static Object JavaDoc getType(FieldItem field, Attachment attachment) {
110         String JavaDoc type = attachment.getMimeType();
111         if (type!=null) {
112             if (type.startsWith("text/")) {
113                 return "text";
114             } else if (type.startsWith("image/")) {
115                 return "image";
116             }
117         }
118         return field.getTypeItem();
119     }
120 }
121
Popular Tags