001 /* 002 Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.lib; 019 020 import org.objectweb.jac.core.rtti.ClassRepository; 021 import org.objectweb.jac.core.rtti.FieldItem; 022 import org.objectweb.jac.core.rtti.MetaItem; 023 import org.objectweb.jac.util.MimeTypes; 024 025 /** 026 * This class represents a file attachment. The data field is the 027 * content of the file. 028 */ 029 public class Attachment { 030 /** 031 * Creates a new Attachement object. If the mimeType is null, it 032 * will be initialized from the name's extension. 033 */ 034 public Attachment(byte[] data, String mimeType, String name) { 035 this.data = data; 036 this.mimeType = mimeType; 037 this.name = name; 038 if (mimeType==null) { 039 guessMimeType(); 040 } 041 } 042 043 static MimeTypes mimeTypes; 044 045 public void guessMimeType() { 046 try { 047 if (mimeTypes==null) { 048 mimeTypes = new MimeTypes(); 049 mimeTypes.readDefaults(); 050 } 051 this.mimeType = mimeTypes.getMimeType(name); 052 } catch (Exception e) { 053 e.printStackTrace(); 054 } 055 } 056 057 byte[] data; 058 /** 059 * Returns the content of the attachment. 060 * @return the file's content. 061 * @see #setData(byte[]) 062 */ 063 public byte[] getData() { 064 return data; 065 } 066 /** 067 * Set the content of the file 068 * @param data the content of the file 069 * @see #getData() 070 */ 071 public void setData(byte[] data) { 072 this.data = data; 073 } 074 075 String mimeType; 076 /** 077 * @return the mime type of the file (text/plain, text/html, 078 * application/msword, ...) 079 * @see #setMimeType(String) 080 */ 081 public String getMimeType() { 082 return mimeType; 083 } 084 /** 085 * Sets the mime type of the file. 086 * @param mimeType the mime type of the file. 087 * @see #getMimeType() 088 */ 089 public void setMimeType(String mimeType) { 090 this.mimeType = mimeType; 091 } 092 093 String name; 094 /** 095 * @return the name of the file 096 * @see #setName(String) 097 */ 098 public String getName() { 099 return name; 100 } 101 /** 102 * @param name the name of the file 103 * @see #getName() 104 */ 105 public void setName(String name) { 106 this.name = name; 107 } 108 109 public static Object getType(FieldItem field, Attachment attachment) { 110 String 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 }