Coverage report

  %line %branch
com.ozacc.mail.xml.impl.JDomXMLBuilder
69% 
99% 

 1  
 package com.ozacc.mail.xml.impl;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileOutputStream;
 5  
 import java.io.IOException;
 6  
 
 7  
 import javax.mail.internet.InternetAddress;
 8  
 
 9  
 import org.jdom.CDATA;
 10  
 import org.jdom.DocType;
 11  
 import org.jdom.Document;
 12  
 import org.jdom.Element;
 13  
 import org.jdom.JDOMException;
 14  
 import org.jdom.output.DOMOutputter;
 15  
 import org.jdom.output.Format;
 16  
 import org.jdom.output.XMLOutputter;
 17  
 
 18  
 import com.ozacc.mail.Mail;
 19  
 import com.ozacc.mail.xml.XMLBuildException;
 20  
 import com.ozacc.mail.xml.XMLBuilder;
 21  
 
 22  
 /**
 23  
  * XMLBuilder¤Î¼ÂÁõ¥¯¥é¥¹¡£
 24  
  * 
 25  
  * @since 1.0
 26  
  * @author Tomohiro Otsuka
 27  
  * @version $Id: JDomXMLBuilder.java,v 1.6 2004/09/18 00:39:17 otsuka Exp $
 28  
  */
 29  
 public class JDomXMLBuilder implements XMLBuilder {
 30  
 
 31  
 	public static final String DOCTYPE_PUBLIC = "-//OZACC//DTD MAIL//EN";
 32  
 
 33  
 	public static final String DOCTYPE_SYSTEM = "http://www.ozacc.com/library/dtd/ozacc-mail.dtd";
 34  
 
 35  3
 	private String charset = "UTF-8";
 36  
 
 37  
 	/**
 38  
 	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
 39  
 	 */
 40  6
 	public JDomXMLBuilder() {}
 41  
 
 42  
 	/**
 43  
 	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
 44  
 	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
 45  
 	 * 
 46  
 	 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
 47  
 	 */
 48  
 	public JDomXMLBuilder(String charset) {
 49  0
 		this();
 50  0
 		setCharset(charset);
 51  0
 	}
 52  
 
 53  
 	/**
 54  
 	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
 55  
 	 * 
 56  
 	 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
 57  
 	 */
 58  
 	public void setCharset(String charset) {
 59  0
 		this.charset = charset;
 60  0
 	}
 61  
 
 62  
 	/**
 63  
 	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
 64  
 	 * 
 65  
 	 * @return ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
 66  
 	 */
 67  
 	public String getCharset() {
 68  0
 		return charset;
 69  
 	}
 70  
 
 71  
 	/**
 72  
 	 * @see com.ozacc.mail.xml.XMLBuilder#buildDocument(com.ozacc.mail.Mail)
 73  
 	 */
 74  
 	public org.w3c.dom.Document buildDocument(Mail mail) throws XMLBuildException {
 75  1
 		Document doc = buildJDomDocument(mail);
 76  1
 		DOMOutputter outputter = new DOMOutputter();
 77  
 		try {
 78  1
 			return outputter.output(doc);
 79  0
 		} catch (JDOMException e) {
 80  0
 			throw new XMLBuildException("DOM Document¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
 81  
 		}
 82  
 	}
 83  
 
 84  
 	/**
 85  
 	 * »ØÄꤵ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤«¤éJDOM¥É¥­¥å¥á¥ó¥È¤òÀ¸À®¤·¤Þ¤¹¡£
 86  
 	 * 
 87  
 	 * @return À¸À®¤µ¤?¤¿JDOM¥É¥­¥å¥á¥ó¥È
 88  
 	 */
 89  
 	public Document buildJDomDocument(Mail mail) {
 90  
 
 91  3
 		Element mailElem = new Element("mail");
 92  
 
 93  
 		// Return-Path
 94  3
 		if (mail.getReturnPath() != null) {
 95  0
 			InternetAddress returnPath = mail.getReturnPath();
 96  0
 			Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath");
 97  0
 			mailElem.addContent(returnPathElem);
 98  
 		}
 99  
 
 100  
 		// From
 101  3
 		if (mail.getFrom() != null) {
 102  3
 			InternetAddress from = mail.getFrom();
 103  3
 			Element fromElem = convertInternetAddressIntoElement(from, "from");
 104  3
 			mailElem.addContent(fromElem);
 105  
 		}
 106  
 
 107  3
 		if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) {
 108  3
 			Element recipientsElem = new Element("recipients");
 109  
 
 110  
 			// To
 111  3
 			if (mail.getTo().length > 0) {
 112  6
 				for (int i = 0; i < mail.getTo().length; i++) {
 113  3
 					InternetAddress to = mail.getTo()[i];
 114  3
 					Element toElem = convertInternetAddressIntoElement(to, "to");
 115  3
 					recipientsElem.addContent(toElem);
 116  
 				}
 117  
 			}
 118  
 			// Cc
 119  3
 			if (mail.getCc().length > 0) {
 120  0
 				for (int i = 0; i < mail.getCc().length; i++) {
 121  0
 					InternetAddress cc = mail.getCc()[i];
 122  0
 					Element ccElem = convertInternetAddressIntoElement(cc, "cc");
 123  0
 					recipientsElem.addContent(ccElem);
 124  
 				}
 125  
 			}
 126  
 			// Bcc
 127  3
 			if (mail.getBcc().length > 0) {
 128  0
 				for (int i = 0; i < mail.getBcc().length; i++) {
 129  0
 					InternetAddress bcc = mail.getBcc()[i];
 130  0
 					Element bccElem = convertInternetAddressIntoElement(bcc, "bcc");
 131  0
 					recipientsElem.addContent(bccElem);
 132  
 				}
 133  
 			}
 134  3
 			mailElem.addContent(recipientsElem);
 135  
 		}
 136  
 
 137  
 		// Reply-To
 138  3
 		if (mail.getReplyTo() != null) {
 139  0
 			InternetAddress replyTo = mail.getReplyTo();
 140  0
 			Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo");
 141  0
 			mailElem.addContent(replyToElem);
 142  
 		}
 143  
 
 144  
 		// Subject
 145  3
 		if (mail.getSubject() != null) {
 146  3
 			Element subjectElem = new Element("subject");
 147  3
 			subjectElem.setText(mail.getSubject());
 148  3
 			mailElem.addContent(subjectElem);
 149  
 		}
 150  
 
 151  
 		// Body
 152  3
 		if (mail.getText() != null) {
 153  3
 			Element textElem = new Element("body");
 154  3
 			textElem.setText(mail.getText());
 155  3
 			mailElem.addContent(textElem);
 156  
 		}
 157  
 
 158  
 		// Html
 159  3
 		if (mail.isHtmlMail()) {
 160  1
 			Element htmlElem = new Element("html");
 161  1
 			htmlElem.setContent(new CDATA(mail.getHtmlText()));
 162  1
 			mailElem.addContent(htmlElem);
 163  
 		}
 164  
 
 165  3
 		Document doc = new Document(mailElem);
 166  3
 		DocType docType = new DocType("mail", DOCTYPE_PUBLIC, DOCTYPE_SYSTEM);
 167  3
 		doc.setDocType(docType);
 168  3
 		return doc;
 169  
 	}
 170  
 
 171  
 	/**
 172  
 	 * 
 173  
 	 * @param address
 174  
 	 * @param elemName
 175  
 	 * @return
 176  
 	 */
 177  
 	private Element convertInternetAddressIntoElement(InternetAddress address, String elemName) {
 178  6
 		Element element = new Element(elemName);
 179  6
 		element.setAttribute("email", address.getAddress());
 180  6
 		if (address.getPersonal() != null) {
 181  3
 			element.setAttribute("name", address.getPersonal());
 182  
 		}
 183  6
 		return element;
 184  
 	}
 185  
 
 186  
 	/**
 187  
 	 * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)
 188  
 	 */
 189  
 	public void saveDocument(Mail mail, File destFile) throws XMLBuildException {
 190  
 		// JDOM Document¤òÀ¸À®
 191  2
 		Document doc = buildJDomDocument(mail);
 192  
 
 193  
 		// ¥Õ¥¡¥¤¥?½ÐÎÏ
 194  
 		try {
 195  2
 			FileOutputStream fos = new FileOutputStream(destFile);
 196  2
 			XMLOutputter outputter = getXMLOutputter();
 197  2
 			outputter.output(doc, fos);
 198  2
 			fos.close();
 199  0
 		} catch (IOException e) {
 200  0
 			throw new XMLBuildException("DOM Document¤Î¥Õ¥¡¥¤¥?½ÐÎϤ˼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
 201  
 		}
 202  2
 	}
 203  
 
 204  
 	public XMLOutputter getXMLOutputter() {
 205  2
 		Format format = Format.getPrettyFormat();
 206  2
 		format.setEncoding(charset);
 207  2
 		XMLOutputter outputter = new XMLOutputter(format);
 208  2
 		return outputter;
 209  
 	}
 210  
 }

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