01: /*
02: * Created on May 24, 2004
03: *
04: * TODO To change the template for this generated file go to
05: * Window - Preferences - Java - Code Generation - Code and Comments
06: */
07: package de.schlund.pfixxml.util;
08:
09: import java.io.File;
10: import java.io.StringWriter;
11:
12: import javax.xml.transform.Result;
13: import javax.xml.transform.Templates;
14: import javax.xml.transform.TransformerException;
15: import javax.xml.transform.dom.DOMResult;
16: import javax.xml.transform.stream.StreamResult;
17:
18: import junit.framework.TestCase;
19:
20: import org.w3c.dom.Attr;
21: import org.w3c.dom.Document;
22: import org.w3c.dom.Element;
23: import org.w3c.dom.NodeList;
24:
25: import com.icl.saxon.om.AbstractNode;
26: import com.icl.saxon.om.NodeInfo;
27:
28: import de.schlund.pfixxml.resources.ResourceUtil;
29:
30: public class XsltTest extends TestCase {
31:
32: protected XsltVersion getXsltVersion() {
33: return XsltVersion.XSLT1;
34: }
35:
36: //-- make sure we have several bug fixes
37:
38: public void testBugfix962737_IncorrectNsPrefix() throws Exception {
39: Document doc = transform("incorrectnsprefix");
40: NodeList lst = doc.getDocumentElement().getElementsByTagNameNS(
41: "http://www.w3.org/1999/XSL/Transform", "message");
42: assertEquals(1, lst.getLength());
43: assertEquals("alias:message", ((AbstractNode) lst.item(0))
44: .getDisplayName());
45: }
46:
47: //-- test extensions
48:
49: public void testExtension() throws Exception {
50: Document doc = transform("extension");
51: Element hello = (Element) doc.getDocumentElement()
52: .getElementsByTagName("hello").item(0);
53: assertEquals("foo", hello.getAttribute("attr"));
54: }
55:
56: public void testUriEncodingWithHtmlOutput() throws Exception {
57: StreamResult result;
58: StringWriter writer;
59: Document doc;
60:
61: // Xml.serialize does *not* encode urls, thus, I have to use Saxon's stream serialization
62: writer = new StringWriter();
63: result = new StreamResult(writer);
64: transform("html", result);
65: doc = Xml.parseString(getXsltVersion(), writer.getBuffer()
66: .toString());
67: assertEquals("m%C3%BCller", ((Attr) XPath.selectNode(doc,
68: "/html/a/@href")).getValue());
69: }
70:
71: //-- helper code
72:
73: private Document transform(String name) throws Exception {
74: DOMResult result;
75:
76: result = new DOMResult();
77: transform(name, result);
78: return (Document) result.getNode();
79: }
80:
81: private void transform(String name, Result result) throws Exception {
82: final String PREFIX = "tests/junit/de/schlund/pfixxml/util/"; // TODO: windows
83: final String xml = name + ".xml";
84: final String xsl = name + ".xsl";
85: Document doc;
86: Templates trafo;
87:
88: doc = Xml.parse(getXsltVersion(), new File(PREFIX + xml));
89: trafo = Xslt.loadTemplates(getXsltVersion(), ResourceUtil
90: .getFileResource("file://"
91: + (new File(PREFIX + xsl)).getAbsolutePath()));
92: Xslt.transform(doc, trafo, null, result);
93: }
94:
95: public static NodeInfo toDocumentExtension(String str)
96: throws TransformerException {
97: return (NodeInfo) Xml.parseString(XsltVersion.XSLT1, str);
98: }
99: }
|