调用方:
//以URL形式获取到连接到邮件文本xsl文件的URL(绝对路径)
URL stylesheetUrl = MailTransformer.class
.getResource("/jp/co/。。。/sourcing/mail/cbmnMailBody.xsl");
。。。
// メールのタイトル
String subject = mailTransformer.getSubject(stylesheetUrl);
-》从指定的XSL文件中,将指定节点的值读取出来。(20090511追加)
<!-- -->
基础服务提供方:
public String getSubject(URL stylesheetUrl) throws IOException, XPathExpressionException, ParserConfigurationException, SAXException {
String ret = null;
InputStream stylesheetInputStream = null;
try {
// Opens a connection to this URL and returns an InputStream for reading from that connection.
// 执行完这句话之后,相当于一根管道接到了stylesheetUrl指向的文件上(网络文件),准备从该文件中读取数据
stylesheetInputStream = stylesheetUrl.openStream();
// 将输入流作为参数传入,从中读取出Subject给程序
ret = getSubject(stylesheetInputStream);
} finally {
IOUtils.closeQuietly(stylesheetInputStream);
}
return StringUtils.trim(ret);
}
-》根据传入的URL参数,将subject返回给调用方。(20090511追加)
邮件标题实际处理方法:
public String getSubject(InputStream stylesheetInputStream) throws ParserConfigurationException, XPathExpressionException, SAXException, IOException {
// Create a new input source with a byte stream.
InputSource source = new InputSource(stylesheetInputStream);
// Obtain a new instance of a DocumentBuilderFactory.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Specifies that the parser produced by this code will provide support for XML namespaces.
factory.setNamespaceAware(true);
// Creates a new instance of a DocumentBuilder using the currently configured parameters.
DocumentBuilder builder = factory.newDocumentBuilder();
// Parse the content of the given input source as an XML document and return a new DOM Document object.
Document doc = builder.parse(source);
// Get a new XPathFactory instance using the default object model, DEFAULT_OBJECT_MODEL_URI, the W3C DOM.
XPathFactory pathFactory = XPathFactory.newInstance();
// Return a new XPath using the underlying object model determined when the XPathFactory was instantiated.
XPath xpath = pathFactory.newXPath();
// Establish a namespace context.
xpath.setNamespaceContext(new NamespaceContextProvider("xsl", "http://www.w3.org/1999/XSL/Transform"));
// Compile an XPath expression for later evaluation.
XPathExpression subjectPathExpression = xpath.compile("//xsl:stylesheet/xsl:template");
// Evaluate the compiled XPath expression in the context of the specified InputSource and return the result as the specified type.
// 此时就将xsl文件的固定内容读出来了
NodeList nodeList = (NodeList) subjectPathExpression.evaluate(doc, XPathConstants.NODESET);
Node node;
String ret = null;
// 对根节点(可能有多个)进行处理
// 此例中,为“<xsl:template match="/">”和“<xsl:template match="Subject">”
for (int i = 0, length = nodeList.getLength(); i < length; i++) {
node = nodeList.item(i);
if ("Subject".equals(node.getAttributes().getNamedItem("match").getNodeValue())) {
// 找到节点名称为“Subject”的,将其内容(比如“結果通知”)都出来。
ret = node.getTextContent();
break;
}
}
return ret;
}
-》上述这个方法就是具体的处理过程,从指定的xsl文件(输入流)中,将指定节点的值读取出来,返回回去。(20090511追加)
XSL文件的内容(相关部分)为下:
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:space="preserve">
<xsl:output method="text"/>
<xsl:variable name="strTitleName" select="root/title"/>
<xsl:template match="/">
。。。。。。
===============================================
結果通知
===============================================
。。。。。。
</xsl:template>
<xsl:template match="Subject">
結果通知
</xsl:template>
</xsl:stylesheet>
-》和想读取节点subject相关的部分。(20090511追加)
<!-- --><!-- --> <!-- -->
这样一圈下来后,就能将 XSL文件中写好的邮件 Subject( 結果通知 )读取到程序中来了。