Coverage report

  %line %branch
com.ozacc.mail.impl.AbstractXMLMailBuilder
92% 
94% 

 1  
 package com.ozacc.mail.impl;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.IOException;
 5  
 import java.io.InputStream;
 6  
 import java.util.HashMap;
 7  
 import java.util.Map;
 8  
 
 9  
 import javax.xml.parsers.DocumentBuilder;
 10  
 import javax.xml.parsers.DocumentBuilderFactory;
 11  
 import javax.xml.parsers.FactoryConfigurationError;
 12  
 import javax.xml.parsers.ParserConfigurationException;
 13  
 
 14  
 import org.w3c.dom.Document;
 15  
 import org.w3c.dom.Element;
 16  
 import org.w3c.dom.Node;
 17  
 import org.w3c.dom.NodeList;
 18  
 import org.xml.sax.SAXException;
 19  
 
 20  
 import com.ozacc.mail.Mail;
 21  
 
 22  
 /**
 23  
  * XMLMailBuilder¼ÂÁõ¤¬·Ñ¾µ¤¹¤?´ðÄ?¥¯¥é¥¹¡£
 24  
  * 
 25  
  * @since 1.1
 26  
  * 
 27  
  * @author Tomohiro Otsuka
 28  
  * @version $Id: AbstractXMLMailBuilder.java,v 1.4.2.2 2004/11/25 08:00:49 otsuka Exp $
 29  
  */
 30  
 public abstract class AbstractXMLMailBuilder {
 31  
 
 32  
 	protected Map documentBuilderCache;
 33  
 
 34  
 	/**
 35  
 	 * ¥³¥ó¥¹¥È¥é¥¯¥¿¡£
 36  
 	 */
 37  30
 	public AbstractXMLMailBuilder() {
 38  30
 		documentBuilderCache = new HashMap();
 39  30
 	}
 40  
 
 41  
 	/**
 42  
 	 * »ØÄꤵ¤?¤¿XML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
 43  
 	 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢XML¤Î¥³¥á¥ó¥È¤òº?½?¤·¤Þ¤»¤ó¡£
 44  
 	 * 
 45  
 	 * @param file XML¥Õ¥¡¥¤¥?
 46  
 	 * @return DOM Document
 47  
 	 * @throws IOException
 48  
 	 * @throws SAXException
 49  
 	 */
 50  
 	protected synchronized Document getDocumentFromFile(File file, boolean ignoreComment)
 51  
 																							throws SAXException,
 52  
 																							IOException {
 53  24
 		DocumentBuilder db = createDocumentBuilder(ignoreComment);
 54  24
 		return db.parse(file);
 55  
 	}
 56  
 
 57  
 	/**
 58  
 	 * »ØÄꤵ¤?¤¿XML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
 59  
 	 * XML¤Î¥³¥á¥ó¥È¤ä²?¹Ô¤Ïº?½?¤µ¤?¤Þ¤¹¡£
 60  
 	 * 
 61  
 	 * @param file XML¥Õ¥¡¥¤¥?
 62  
 	 * @return DOM Document
 63  
 	 * @throws IOException
 64  
 	 * @throws SAXException
 65  
 	 */
 66  
 	protected Document getDocumentFromFile(File file) throws SAXException, IOException {
 67  23
 		return getDocumentFromFile(file, true);
 68  
 	}
 69  
 
 70  
 	/**
 71  
 	 * DocumentBuilder¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
 72  
 	 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢¥³¥á¥ó¥È¤òº?½?¤·¤Ê¤¤¤è¤¦¤ËÀßÄꤵ¤?¤¿DocumentBuilder¤òÀ¸À®¤·¤Þ¤¹¡£
 73  
 	 * 
 74  
 	 * @param ignoreComment
 75  
 	 * @return DocumentBuilder
 76  
 	 * @throws FactoryConfigurationError 
 77  
 	 */
 78  
 	protected DocumentBuilder createDocumentBuilder(boolean ignoreComment)
 79  
 																			throws FactoryConfigurationError {
 80  41
 		Boolean dbKey = Boolean.valueOf(ignoreComment);
 81  41
 		DocumentBuilder db = (DocumentBuilder)documentBuilderCache.get(dbKey);
 82  41
 		if (db == null) {
 83  31
 			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 84  31
 			dbf.setIgnoringComments(ignoreComment);
 85  31
 			dbf.setCoalescing(ignoreComment);
 86  31
 			dbf.setIgnoringElementContentWhitespace(true);
 87  31
 			dbf.setValidating(true);
 88  
 			try {
 89  31
 				db = dbf.newDocumentBuilder();
 90  31
 				db.setEntityResolver(new DTDEntityResolver());
 91  31
 				documentBuilderCache.put(dbKey, db);
 92  0
 			} catch (ParserConfigurationException e) {
 93  
 				// never be thrown
 94  0
 				throw new RuntimeException(e);
 95  
 			}
 96  
 		}
 97  41
 		return db;
 98  
 	}
 99  
 
 100  
 	/**
 101  
 	 * DocumentBuilder¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Þ¤¹¡£
 102  
 	 * ¤³¤ÎDocumentBuilder¤ò»ÈÍѤ·¤ÆÀ¸À®¤µ¤?¤?DOM Document¤Ç¤Ï¡¢¸µ¤ÎXML¥Ç¡¼¥¿¤Ë¤¢¤?¥³¥á¥ó¥È¤Ïº?½?¤µ¤?¤Þ¤¹¡£
 103  
 	 * 
 104  
 	 * @return DocumentBuilder
 105  
 	 * @throws FactoryConfigurationError 
 106  
 	 */
 107  
 	protected DocumentBuilder createDocumentBuilder() throws FactoryConfigurationError {
 108  5
 		return createDocumentBuilder(true);
 109  
 	}
 110  
 
 111  
 	/**
 112  
 	 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¤ÎXML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
 113  
 	 * ignoreComment¤¬»ØÄꤵ¤?¤Æ¤¤¤?¾?¹ç¤Ï¡¢XML¤Î¥³¥á¥ó¥È¤òº?½?¤·¤Þ¤»¤ó¡£
 114  
 	 * 
 115  
 	 * @param ignoreComment
 116  
 	 * @param classPath
 117  
 	 * @return DOM Document
 118  
 	 * @throws IOException
 119  
 	 * @throws SAXException
 120  
 	 */
 121  
 	protected synchronized Document getDocumentFromClassPath(String classPath, boolean ignoreComment)
 122  
 																										throws SAXException,
 123  
 																										IOException {
 124  12
 		InputStream is = getClass().getResourceAsStream(classPath);
 125  12
 		DocumentBuilder db = createDocumentBuilder(ignoreComment);
 126  
 		try {
 127  12
 			return db.parse(is);
 128  0
 		} finally {
 129  12
 			if (is != null) {
 130  12
 				is.close();
 131  
 			}
 132  12
 		}
 133  
 	}
 134  
 
 135  
 	/**
 136  
 	 * »ØÄꤵ¤?¤¿¥¯¥é¥¹¥Ñ¥¹¤ÎXML¥Õ¥¡¥¤¥?¤òÆÉ¤ß¹?¤ß¡¢DOM Document¤òÀ¸À®¤·¤Þ¤¹¡£
 137  
 	 * XML¤Î¥³¥á¥ó¥È¤ä²?¹Ô¤Ïº?½?¤µ¤?¤Þ¤¹¡£
 138  
 	 * 
 139  
 	 * @param classPath
 140  
 	 * @return DOM Document
 141  
 	 * @throws IOException
 142  
 	 * @throws SAXException
 143  
 	 */
 144  
 	protected Document getDocumentFromClassPath(String classPath) throws SAXException, IOException {
 145  9
 		return getDocumentFromClassPath(classPath, true);
 146  
 	}
 147  
 
 148  
 	/**
 149  
 	 * @param root
 150  
 	 * @param mail 
 151  
 	 */
 152  
 	protected void setReplyTo(Element root, Mail mail) {
 153  36
 		NodeList nodes = root.getElementsByTagName("replyTo");
 154  36
 		Element replyTo = (Element)nodes.item(0);
 155  36
 		if (replyTo != null && replyTo.getAttribute("email").length() > 0) {
 156  28
 			mail.setReplyTo(replyTo.getAttribute("email"));
 157  
 		}
 158  36
 	}
 159  
 
 160  
 	/**
 161  
 	 * @param root
 162  
 	 * @param mail 
 163  
 	 */
 164  
 	protected void setText(Element root, Mail mail) {
 165  36
 		NodeList nodes = root.getElementsByTagName("body");
 166  36
 		Element bodyElem = (Element)nodes.item(0);
 167  36
 		if (bodyElem == null) {
 168  0
 			return;
 169  
 		}
 170  36
 		String body = bodyElem.getFirstChild().getNodeValue();
 171  36
 		mail.setText(body.trim());
 172  36
 	}
 173  
 
 174  
 	/**
 175  
 	 * HTMLËÜʸ¤ò¥»¥Ã¥È¡£
 176  
 	 * 
 177  
 	 * @param root
 178  
 	 * @param mail
 179  
 	 */
 180  
 	protected void setHtml(Element root, Mail mail) {
 181  36
 		NodeList nodes = root.getElementsByTagName("html");
 182  36
 		Element htmlElem = (Element)nodes.item(0);
 183  36
 		if (htmlElem == null) {
 184  8
 			return;
 185  
 		}
 186  28
 		String html = htmlElem.getFirstChild().getNodeValue();
 187  28
 		mail.setHtmlText(html.trim());
 188  28
 	}
 189  
 
 190  
 	/**
 191  
 	 * @param root
 192  
 	 * @param mail 
 193  
 	 */
 194  
 	protected void setSubject(Element root, Mail mail) {
 195  36
 		NodeList nodes = root.getElementsByTagName("subject");
 196  36
 		Element subjectElem = (Element)nodes.item(0);
 197  36
 		if (subjectElem == null) {
 198  0
 			return;
 199  
 		}
 200  36
 		String subject = subjectElem.getFirstChild().getNodeValue();
 201  36
 		mail.setSubject(subject.trim());
 202  36
 	}
 203  
 
 204  
 	/**
 205  
 	 * @param root
 206  
 	 * @param mail 
 207  
 	 */
 208  
 	protected void setRecipients(Element root, Mail mail) {
 209  36
 		NodeList nodes = root.getElementsByTagName("recipients");
 210  36
 		Element recipientsElem = (Element)nodes.item(0);
 211  36
 		if (recipientsElem == null) {
 212  1
 			return;
 213  
 		}
 214  
 
 215  35
 		NodeList recipientElemList = recipientsElem.getChildNodes();
 216  182
 		for (int i = 0, max = recipientElemList.getLength(); i < max; i++) {
 217  147
 			Node node = recipientElemList.item(i);
 218  147
 			if (node.getNodeType() != Node.ELEMENT_NODE) {
 219  0
 				continue;
 220  
 			}
 221  147
 			Element e = (Element)node;
 222  147
 			if ("to".equals(e.getNodeName())) { // to
 223  63
 				if (e.getAttribute("email").length() > 0) {
 224  63
 					if (e.getAttribute("name").length() > 0) {
 225  35
 						mail.addTo(e.getAttribute("email"), e.getAttribute("name"));
 226  
 					} else {
 227  28
 						mail.addTo(e.getAttribute("email"));
 228  
 					}
 229  
 				}
 230  84
 			} else if ("cc".equals(e.getNodeName())) { // cc
 231  56
 				if (e.getAttribute("email").length() > 0) {
 232  56
 					if (e.getAttribute("name").length() > 0) {
 233  28
 						mail.addCc(e.getAttribute("email"), e.getAttribute("name"));
 234  
 					} else {
 235  28
 						mail.addCc(e.getAttribute("email"));
 236  
 					}
 237  
 				}
 238  
 			} else {
 239  28
 				if (e.getAttribute("email").length() > 0) { // bcc
 240  28
 					mail.addBcc(e.getAttribute("email"));
 241  
 				}
 242  
 			}
 243  
 		}
 244  35
 	}
 245  
 
 246  
 	/**
 247  
 	 * @param root
 248  
 	 * @param mail 
 249  
 	 */
 250  
 	protected void setReturnPath(Element root, Mail mail) {
 251  36
 		NodeList nodes = root.getElementsByTagName("returnPath");
 252  36
 		Element returnPath = (Element)nodes.item(0);
 253  36
 		if (returnPath != null && returnPath.getAttribute("email").length() > 0) {
 254  28
 			mail.setReturnPath(returnPath.getAttribute("email"));
 255  
 		}
 256  36
 	}
 257  
 
 258  
 	/**
 259  
 	 * @param root
 260  
 	 * @param mail 
 261  
 	 */
 262  
 	protected void setFrom(Element root, Mail mail) {
 263  36
 		NodeList nodes = root.getElementsByTagName("from");
 264  36
 		Element from = (Element)nodes.item(0);
 265  36
 		if (from != null && from.getAttribute("email").length() > 0) {
 266  35
 			if (from.getAttribute("name").length() > 0) {
 267  35
 				mail.setFrom(from.getAttribute("email"), from.getAttribute("name"));
 268  
 			} else {
 269  0
 				mail.setFrom(from.getAttribute("email"));
 270  
 			}
 271  
 		}
 272  36
 	}
 273  
 
 274  
 }

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