Coverage report

  %line %branch
com.ozacc.mail.fetch.impl.sk_jp.MultipartUtility
0% 
0% 

 1  
 /*
 2  
  * @(#) $Id: MultipartUtility.java,v 1.1.2.2 2004/10/24 10:26:50 otsuka Exp $
 3  
  * $Revision: 1.1.2.2 $
 4  
  * Copyright (c) 2000 Shin Kinoshita All Rights Reserved.
 5  
  */
 6  
 package com.ozacc.mail.fetch.impl.sk_jp;
 7  
 
 8  
 import java.io.IOException;
 9  
 import java.io.UnsupportedEncodingException;
 10  
 
 11  
 import javax.activation.DataHandler;
 12  
 import javax.mail.MessagingException;
 13  
 import javax.mail.Multipart;
 14  
 import javax.mail.Part;
 15  
 import javax.mail.internet.ContentType;
 16  
 import javax.mail.internet.MimeBodyPart;
 17  
 import javax.mail.internet.MimeMultipart;
 18  
 
 19  
 /**
 20  
  * ¥á¥Ã¥»¡¼¥¸¥Ü¥Ç¥£¤ò¼è¤?½Ð¤¹¼?ÃʤòÄ󶡤¹¤?static¥á¥½¥Ã¥É¤Î¥»¥Ã¥È¤Ç¤¹¡£
 21  
  * <p>
 22  
  * </p>
 23  
  * @version $Revision: 1.1.2.2 $ $Date: 2004/10/24 10:26:50 $
 24  
  * @author Shin
 25  
  * @author Tomohiro Otsuka
 26  
  */
 27  0
 public class MultipartUtility {
 28  
 
 29  
 	private static final String JIS_CHARSET = "ISO-2022-JP";
 30  
 
 31  
 	/**
 32  
 	 * »ØÄ?¥Ñ¡¼¥È¤Î¥Ü¥Ç¥£¤òÊÖ¤·¤Þ¤¹¡£
 33  
 	 * <P>
 34  
 	 * Part#getContent()¤ÎÂå¤?¤ê¤Ç¤¹¡£
 35  
 	 * MIME¤Ë½àµò¤·¤Ê¤¤Content-Type:¤ÎÊäÀµ¤ò¹Ô¤¤¤Þ¤¹¡£
 36  
 	 * charset»ØÄ꤬¤Ê¤¤¾?¹ç¤Ï"ISO-2022-JP"¤òÊ䤤¤Þ¤¹¡£
 37  
 	 * </P><P>
 38  
 	 * ¥Ñ¡¼¥È¤¬UTF-7¤Î¾?¹ç¤âÀµ¾?¤ËÆâÍÆ¤ò¼èÆÀ½ÐÍè¤Þ¤¹¡£
 39  
 	 * </P>
 40  
 	 */
 41  
 	public static Object getContent(Part part) throws MessagingException, IOException {
 42  0
 		return getContent(part, JIS_CHARSET);
 43  
 	}
 44  
 
 45  0
 	private static CorrectedContentTypeDataSource correctedDataSource = new CorrectedContentTypeDataSourceUTF7Support();
 46  
 
 47  0
 	private static DataHandler correctedDataHandler = new DataHandler(correctedDataSource);
 48  
 
 49  
 	/**
 50  
 	 * »ØÄ?¥Ñ¡¼¥È¤Î¥Ü¥Ç¥£¤òÊÖ¤·¤Þ¤¹¡£
 51  
 	 * <P>
 52  
 	 * MIME¤Ë½àµò¤·¤Ê¤¤Content-Type:¤ÎÊäÀµ¤ò¹Ô¤¤¤Þ¤¹¡£
 53  
 	 * charset»ØÄ꤬¤Ê¤¤¾?¹ç¤Ïcharset¤Ç»ØÄꤵ¤?¤¿¤â¤Î¤ÇÊ䤤¤Þ¤¹¡£
 54  
 	 * </P><P>
 55  
 	 * ¥Ñ¡¼¥È¤¬UTF-7¤Î¾?¹ç¤âÀµ¾?¤ËÆâÍÆ¤ò¼èÆÀ½ÐÍè¤Þ¤¹¡£
 56  
 	 * </P>
 57  
 	 */
 58  
 	public static Object getContent(Part part, String charset) throws MessagingException,
 59  
 																IOException {
 60  0
 		synchronized (correctedDataSource) {
 61  
 
 62  0
 			correctedDataSource.setPart(part);
 63  
 			try {
 64  0
 				correctedDataSource.setDefaultCharset(charset);
 65  0
 				return correctedDataHandler.getContent();
 66  0
 			} catch (UnsupportedEncodingException e) {
 67  
 				/*
 68  
 				 * ÉÔÀµ¤Êʸ»ú¥³¡¼¥É¤¬charset¤Ë¥»¥Ã¥È¤µ¤?Îã³°¤¬¥¹¥ú½¼¤µ¤?¤¿¾?¹ç¤Ë
 69  
 				 * JIS_CHARSET¤Ëʸ»ú¥³¡¼¥É¤òÃÖ¤­´¹¤¨¡¢ºÆÅ٥ۥǥ£¤Î¼èÆÀ¤ò»ûÀߤޤ¹¡£
 70  
 				 * 
 71  
 				 * by otsuka
 72  
 				 */
 73  0
 				correctedDataSource.setForceCharset(JIS_CHARSET);
 74  0
 				return correctedDataHandler.getContent();
 75  
 			}
 76  
 
 77  
 		}
 78  
 	}
 79  
 
 80  
 	/**
 81  
 	 * »ØÄ?¥Ñ¡¼¥ÈÇÛ²¼¤ÇºÇ½é¤Ë¸«¤Ä¤±¤¿¥Æ¥­¥¹¥È¥Ñ¡¼¥È¤Î¥Ü¥Ç¥£¤òÊÖ¤·¤Þ¤¹¡£
 82  
 	 * process()¤ò¸Æ¤Ó½Ð¤·¤Æ·?²Ì¤òÊÖ¤¹¤À¤±¤Îconvenience method¤Ç¤¹¡£
 83  
 	 */
 84  
 	public static String getFirstPlainText(Part part) throws MessagingException {
 85  0
 		FirstPlainPartExtractor h = new FirstPlainPartExtractor();
 86  0
 		process(part, h);
 87  0
 		return h.getText();
 88  
 	}
 89  
 
 90  
 	/**
 91  
 	 * »ØÄ?¥Ñ¡¼¥ÈÇÛ²¼¤Îinline¤Ê¥Æ¥­¥¹¥È¥Ñ¡¼¥È¤ò½¸¤á¤ÆÉ½¼¨ÍѤΥܥǥ£¤òÊÖ¤·¤Þ¤¹¡£
 92  
 	 * process()¤ò¸Æ¤Ó½Ð¤·¤Æ·?²Ì¤òÊÖ¤¹¤À¤±¤Îconvenience method¤Ç¤¹¡£
 93  
 	 */
 94  
 	public static String getPlainText(Part part) throws MessagingException {
 95  0
 		PlainPartExtractor h = new PlainPartExtractor();
 96  0
 		process(part, h);
 97  0
 		return h.getText();
 98  
 	}
 99  
 
 100  
 	/**
 101  
 	 * »ØÄ?¥Ñ¡¼¥ÈÇÛ²¼¤Î³Æ¥Ñ¡¼¥È¤ò½èÍ?¤·¤Þ¤¹¡£
 102  
 	 * <P>
 103  
 	 * ¤¹¤Ù¤Æ¤ÎPart¤ËÂФ·¤ÆPartHandler¤¬¸Æ¤Ó½Ð¤µ¤?¤Þ¤¹¡£<BR>
 104  
 	 * </P>
 105  
 	 */
 106  
 	public static void process(Part part, PartHandler handler) throws MessagingException {
 107  0
 		process(part, handler, null);
 108  0
 	}
 109  
 
 110  
 	private static boolean process(Part part, PartHandler handler, ContentType context)
 111  
 																						throws MessagingException {
 112  
 		try {
 113  0
 			if (part.isMimeType("multipart/*")) {
 114  0
 				Multipart mp = (Multipart)part.getContent();
 115  0
 				ContentType cType = new ContentType(part.getContentType());
 116  0
 				for (int i = 0; i < mp.getCount(); i++) {
 117  0
 					if (!process(mp.getBodyPart(i), handler, cType)) {
 118  0
 						return false;
 119  
 					}
 120  
 				}
 121  0
 				return true;
 122  
 			}
 123  0
 			return handler.processPart(part, context);
 124  0
 		} catch (IOException e) {
 125  0
 			throw new MessagingException("Got exception \nin " + part + "\n", e);
 126  
 		}
 127  
 	}
 128  
 
 129  
 	/**
 130  
 	 * »ØÄêpart¤ËbodyPart¤òÄɲä·¤Þ¤¹¡£
 131  
 	 * part¤¬¥Þ¥?¥Á¥Ñ¡¼¤È¥³¥ó¥Æ¥Ê¤Î¾?¹ç¤Ï¤½¤Î¥³¥ó¥Æ¥Ê¤ËbodyPart¤òÄɲä·¤Þ¤¹¡£
 132  
 	 * ¤½¤¦¤Ç¤Ê¤¤¾?¹ç¤Ïpart¤Î¥Ü¥Ç¥£¤È¤·¤Æmultipart/mixed¤Î¥³¥ó¥Æ¥Ê¤òÀßÄꤷ¡¢
 133  
 	 * ¸µ¤Îpart¤Î¥Ü¥Ç¥£¤ÈbodyPart¤Î¥Ü¥Ç¥£¤ò¤½¤Î¥³¥ó¥Æ¥Ê¤ËÄɲä·¤Þ¤¹¡£
 134  
 	 */
 135  
 	public static void addBodyPart(Part part, MimeBodyPart bodyPart) throws MessagingException,
 136  
 																	IOException {
 137  0
 		if (part.isMimeType("multipart/*")) {
 138  0
 			((MimeMultipart)part.getContent()).addBodyPart(bodyPart);
 139  0
 			return;
 140  
 		}
 141  
 		// ²¾
 142  0
 		MimeMultipart mp = new MimeMultipart("mixed");
 143  0
 		MimeBodyPart original = new MimeBodyPart();
 144  0
 		original.setContent(part.getContent(), part.getContentType());
 145  0
 		mp.addBodyPart(original);
 146  0
 		mp.addBodyPart(bodyPart);
 147  0
 		part.setContent(mp);
 148  0
 	}
 149  
 
 150  
 	/**
 151  
 	 * part¤Î¥Ä¥ê¡¼¹½Â¤¤ò¥À¥ó¥×¤¹¤?¥Ç¥Ð¥Ã¥°Íѥ᥽¥Ã¥É¤Ç¤¹¡£
 152  
 	 */
 153  
 	public static void dump(Part part) {
 154  0
 		dump(part, 0);
 155  0
 	}
 156  
 
 157  
 	private static void dump(Part part, int layer) {
 158  0
 		for (int i = 0; i < layer; i++) {
 159  0
 			System.out.print("    ");
 160  
 		}
 161  
 		try {
 162  0
 			System.out.println(part.getClass() + ":" + part.getContentType());
 163  0
 			if (part.isMimeType("multipart/*")) {
 164  0
 				MimeMultipart mp = (MimeMultipart)part.getContent();
 165  0
 				for (int i = 0; i < mp.getCount(); i++) {
 166  0
 					dump(mp.getBodyPart(i), layer + 1);
 167  
 				}
 168  
 			}
 169  0
 		} catch (Exception e) {
 170  0
 			e.printStackTrace();
 171  
 		}
 172  0
 	}
 173  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.