Coverage report

  %line %branch
com.ozacc.mail.xml.impl.XMLBuilderImpl
67% 
99% 

 1  
 package com.ozacc.mail.xml.impl;
 2  
 
 3  
 import java.io.File;
 4  
 import java.util.Properties;
 5  
 
 6  
 import javax.mail.internet.InternetAddress;
 7  
 import javax.xml.parsers.DocumentBuilder;
 8  
 import javax.xml.parsers.DocumentBuilderFactory;
 9  
 import javax.xml.parsers.FactoryConfigurationError;
 10  
 import javax.xml.parsers.ParserConfigurationException;
 11  
 import javax.xml.transform.OutputKeys;
 12  
 import javax.xml.transform.Transformer;
 13  
 import javax.xml.transform.TransformerException;
 14  
 import javax.xml.transform.TransformerFactory;
 15  
 import javax.xml.transform.dom.DOMSource;
 16  
 import javax.xml.transform.stream.StreamResult;
 17  
 
 18  
 import org.w3c.dom.Document;
 19  
 import org.w3c.dom.Element;
 20  
 
 21  
 import com.ozacc.mail.Mail;
 22  
 import com.ozacc.mail.xml.XMLBuildException;
 23  
 import com.ozacc.mail.xml.XMLBuilder;
 24  
 
 25  
 /**
 26  
  * JDK 1.4°Ê¹ß¤Îɸ½àXML¥é¥¤¥Ö¥é¥ê¤ò»ÈÍѤ·¤Æ¼ÂÁõ¤µ¤?¤¿XMLBuilder¡£
 27  
  * 
 28  
  * @since 1.0
 29  
  * @author Tomohiro Otsuka
 30  
  * @version $Id: XMLBuilderImpl.java,v 1.4.2.1 2005/01/21 22:15:07 otsuka Exp $
 31  
  */
 32  
 public class XMLBuilderImpl implements XMLBuilder {
 33  
 
 34  3
 	private String charset = "UTF-8";
 35  
 
 36  
 	/**
 37  
 	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
 38  
 	 */
 39  6
 	public XMLBuilderImpl() {}
 40  
 
 41  
 	/**
 42  
 	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
 43  
 	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
 44  
 	 * 
 45  
 	 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
 46  
 	 */
 47  
 	public XMLBuilderImpl(String charset) {
 48  0
 		super();
 49  0
 		this.charset = charset;
 50  0
 	}
 51  
 
 52  
 	/**
 53  
 	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤òÊÖ¤·¤Þ¤¹¡£
 54  
 	 * 
 55  
 	 * @return ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
 56  
 	 */
 57  
 	public String getCharset() {
 58  0
 		return charset;
 59  
 	}
 60  
 
 61  
 	/**
 62  
 	 * ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É¤ò»ØÄꤷ¤Þ¤¹¡£¥Ç¥Õ¥©¥?¥È¤ÏUTF-8¡£
 63  
 	 * 
 64  
 	 * @param charset ½ÐÎÏXML¥Õ¥¡¥¤¥?¤Îʸ»ú¥³¡¼¥É
 65  
 	 */
 66  
 	public void setCharset(String charset) {
 67  0
 		this.charset = charset;
 68  0
 	}
 69  
 
 70  
 	/**
 71  
 	 * @see com.ozacc.mail.xml.XMLBuilder#buildDocument(com.ozacc.mail.Mail)
 72  
 	 */
 73  
 	public Document buildDocument(Mail mail) throws XMLBuildException {
 74  3
 		Document doc = createNewDocument();
 75  
 
 76  
 		/*DOMImplementation domImpl = doc.getImplementation();
 77  
 		 DocumentType docType = domImpl.createDocumentType("mail", Mail.DOCTYPE_PUBLIC, Mail.DOCTYPE_SYSTEM);
 78  
 		 doc.appendChild(docType);*/
 79  
 
 80  3
 		Element mailElem = doc.createElement("mail");
 81  
 
 82  
 		// Return-Path
 83  3
 		if (mail.getReturnPath() != null) {
 84  0
 			InternetAddress returnPath = mail.getReturnPath();
 85  0
 			Element returnPathElem = convertInternetAddressIntoElement(returnPath, "returnPath",
 86  0
 					doc);
 87  0
 			mailElem.appendChild(returnPathElem);
 88  
 		}
 89  
 
 90  
 		// From
 91  3
 		if (mail.getFrom() != null) {
 92  3
 			InternetAddress from = mail.getFrom();
 93  3
 			Element fromElem = convertInternetAddressIntoElement(from, "from", doc);
 94  3
 			mailElem.appendChild(fromElem);
 95  
 		}
 96  
 
 97  3
 		if (mail.getTo().length > 0 || mail.getCc().length > 0 || mail.getBcc().length > 0) {
 98  3
 			Element recipientsElem = doc.createElement("recipients");
 99  
 
 100  
 			// To
 101  3
 			if (mail.getTo().length > 0) {
 102  6
 				for (int i = 0; i < mail.getTo().length; i++) {
 103  3
 					InternetAddress to = mail.getTo()[i];
 104  3
 					Element toElem = convertInternetAddressIntoElement(to, "to", doc);
 105  3
 					recipientsElem.appendChild(toElem);
 106  
 				}
 107  
 			}
 108  
 			// Cc
 109  3
 			if (mail.getCc().length > 0) {
 110  0
 				for (int i = 0; i < mail.getCc().length; i++) {
 111  0
 					InternetAddress cc = mail.getCc()[i];
 112  0
 					Element ccElem = convertInternetAddressIntoElement(cc, "cc", doc);
 113  0
 					recipientsElem.appendChild(ccElem);
 114  
 				}
 115  
 			}
 116  
 			// Bcc
 117  3
 			if (mail.getBcc().length > 0) {
 118  0
 				for (int i = 0; i < mail.getBcc().length; i++) {
 119  0
 					InternetAddress bcc = mail.getBcc()[i];
 120  0
 					Element bccElem = convertInternetAddressIntoElement(bcc, "bcc", doc);
 121  0
 					recipientsElem.appendChild(bccElem);
 122  
 				}
 123  
 			}
 124  3
 			mailElem.appendChild(recipientsElem);
 125  
 		}
 126  
 
 127  
 		// Reply-To
 128  3
 		if (mail.getReplyTo() != null) {
 129  0
 			InternetAddress replyTo = mail.getReplyTo();
 130  0
 			Element replyToElem = convertInternetAddressIntoElement(replyTo, "replyTo", doc);
 131  0
 			mailElem.appendChild(replyToElem);
 132  
 		}
 133  
 
 134  
 		// Subject
 135  3
 		if (mail.getSubject() != null) {
 136  3
 			Element subjectElem = doc.createElement("subject");
 137  3
 			subjectElem.appendChild(doc.createTextNode(mail.getSubject()));
 138  3
 			mailElem.appendChild(subjectElem);
 139  
 		}
 140  
 
 141  
 		// Body
 142  3
 		if (mail.getText() != null) {
 143  3
 			Element bodyElem = doc.createElement("body");
 144  3
 			bodyElem.appendChild(doc.createTextNode(mail.getText()));
 145  3
 			mailElem.appendChild(bodyElem);
 146  
 		}
 147  
 
 148  
 		// Html
 149  3
 		if (mail.isHtmlMail()) {
 150  1
 			Element htmlElem = doc.createElement("html");
 151  1
 			htmlElem.appendChild(doc.createCDATASection(mail.getHtmlText()));
 152  1
 			mailElem.appendChild(htmlElem);
 153  
 		}
 154  
 
 155  3
 		doc.appendChild(mailElem);
 156  
 
 157  3
 		return doc;
 158  
 	}
 159  
 
 160  
 	public static Document createNewDocument() throws FactoryConfigurationError {
 161  3
 		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 162  
 		try {
 163  3
 			DocumentBuilder db = dbf.newDocumentBuilder();
 164  3
 			Document doc = db.newDocument();
 165  3
 			return doc;
 166  0
 		} catch (ParserConfigurationException e) {
 167  
 			// never be thrown
 168  0
 			throw new XMLBuildException("", e);
 169  
 		}
 170  
 	}
 171  
 
 172  
 	private Element convertInternetAddressIntoElement(InternetAddress address, String elemName,
 173  
 														Document doc) {
 174  6
 		Element element = doc.createElement(elemName);
 175  6
 		element.setAttribute("email", address.getAddress());
 176  6
 		if (address.getPersonal() != null) {
 177  3
 			element.setAttribute("name", address.getPersonal());
 178  
 		}
 179  6
 		return element;
 180  
 	}
 181  
 
 182  
 	/**
 183  
 	 * »ØÄꤵ¤?¤¿Mail¥¤¥ó¥¹¥¿¥ó¥¹¤«¤éXML¥É¥­¥å¥á¥ó¥È¤òÀ¸À®¤·¡¢
 184  
 	 * »ØÄꤵ¤?¤¿¥Õ¥¡¥¤¥?¤ËÊݸ¤·¤Þ¤¹¡£
 185  
 	 * 
 186  
 	 * ¤³¤Î¥á¥½¥Ã¥ÉÆâÉô¤Ç»ÈÍѤµ¤?¤?TransformerFactory¤¬¥¹¥?¥Ã¥É¥»¡¼¥Õ¤Ç¤Ï¤Ê¤¤¤¿¤á¡¢synchronzed¥á¥½¥Ã¥É¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
 187  
 	 * 
 188  
 	 * @see com.ozacc.mail.xml.XMLBuilder#saveDocument(com.ozacc.mail.Mail, java.io.File)
 189  
 	 * @see TransformerFactory
 190  
 	 */
 191  
 	public synchronized void saveDocument(Mail mail, File destFile) throws XMLBuildException {
 192  2
 		Document doc = buildDocument(mail);
 193  
 
 194  
 		Transformer t;
 195  
 		try {
 196  2
 			t = TransformerFactory.newInstance().newTransformer();
 197  0
 		} catch (Exception e) {
 198  
 			// never be thrown
 199  0
 			throw new XMLBuildException(e.getMessage());
 200  
 		}
 201  2
 		t.setOutputProperties(getOutputProperties());
 202  
 
 203  2
 		DOMSource source = new DOMSource(doc);
 204  2
 		StreamResult result = new StreamResult(destFile);
 205  
 		try {
 206  2
 			t.transform(source, result);
 207  0
 		} catch (TransformerException e) {
 208  0
 			throw new XMLBuildException("XML¥Õ¥¡¥¤¥?¤ÎÊݸ¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e);
 209  
 		}
 210  2
 	}
 211  
 
 212  
 	/**
 213  
 	 * ½ÐÎÏ¥×¥úÁѥƥ£¤òÀ¸À®¡£
 214  
 	 * @return ½ÐÎÏ¥×¥úÁѥƥ£¤òÀßÄꤷ¤¿Properties¥¤¥ó¥¹¥¿¥ó¥¹
 215  
 	 */
 216  
 	private Properties getOutputProperties() {
 217  2
 		Properties p = new Properties();
 218  2
 		p.put(OutputKeys.ENCODING, charset);
 219  2
 		p.put(OutputKeys.INDENT, "yes");
 220  2
 		p.put(OutputKeys.DOCTYPE_PUBLIC, Mail.DOCTYPE_PUBLIC);
 221  2
 		p.put(OutputKeys.DOCTYPE_SYSTEM, Mail.DOCTYPE_SYSTEM);
 222  2
 		return p;
 223  
 	}
 224  
 
 225  
 }

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