KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > transform > TestXsl


1 /**
2  * Copyright 2001-2005 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.xml.transform;
6
7 import javax.xml.transform.stream.StreamResult JavaDoc;
8 import javax.xml.transform.stream.StreamSource JavaDoc;
9 import javax.xml.transform.Source JavaDoc;
10
11 import org.exoplatform.container.PortalContainer;
12 import org.exoplatform.services.xml.transform.trax.TRAXTransformer;
13 import org.exoplatform.services.xml.transform.trax.TRAXTransformerService;
14 import org.exoplatform.services.xml.transform.trax.TRAXTemplates;
15 import org.exoplatform.services.xml.transform.trax.Constants;
16
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 import org.apache.commons.logging.Log;
24
25 /**
26  * Created by the Exo Development team.
27  */

28 public class TestXsl extends BaseTest {
29     private TRAXTransformerService traxService;
30     private Log log;
31
32     public void setUp() throws Exception JavaDoc {
33         log = getLog();
34         traxService =
35                 (TRAXTransformerService) PortalContainer.getInstance().
36                  getComponentInstanceOfType(TRAXTransformerService.class);
37         assertNotNull("traxService", traxService);
38 // dateFormat = new SimpleDateFormat(DATE_PATTERN);
39

40
41     }
42
43
44   public void testSimpleXslt() throws Exception JavaDoc {
45
46
47       String JavaDoc OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_xsl.xml";
48
49       FileInputStream JavaDoc inputFileInputStream =
50                 new FileInputStream JavaDoc("tmp/rss-out.xhtml");
51       assertTrue("Empty input file",inputFileInputStream.available() > 0);
52
53       //output file
54
OutputStream JavaDoc outputFileOutputStream =
55                 new FileOutputStream JavaDoc(OUTPUT_FILENAME);
56
57
58       //get xsl
59
String JavaDoc XSL_URL = Constants.XSLT_DIR+"/html-url-rewite.xsl";
60       InputStream JavaDoc xslInputStream = Thread.currentThread().
61                    getContextClassLoader().getResourceAsStream(XSL_URL);
62       assertNotNull("empty xsl",xslInputStream);
63       Source JavaDoc xslSource = new StreamSource JavaDoc(xslInputStream);
64       assertNotNull("get xsl source",xslSource);
65
66       //init transformer
67
TRAXTransformer traxTransformer = traxService.getTransformer(xslSource);
68       assertNotNull("get transformer",traxTransformer);
69
70       traxTransformer.initResult(new StreamResult JavaDoc(outputFileOutputStream));
71       traxTransformer.transform(new StreamSource JavaDoc(inputFileInputStream));
72
73       inputFileInputStream.close();
74       outputFileOutputStream.close();
75
76       //read the output file
77
FileInputStream JavaDoc outputFileInputStream =
78                 new FileInputStream JavaDoc(OUTPUT_FILENAME);
79
80       assertTrue("Output is empty", outputFileInputStream.available() > 0);
81       outputFileInputStream.close();
82
83
84   }
85
86   public void testXsltUseTemplates() throws Exception JavaDoc {
87     //input
88
FileInputStream JavaDoc inputFileInputStream =
89               new FileInputStream JavaDoc("tmp/rss-out.xhtml");
90     assertTrue("Empty input file",inputFileInputStream.available() > 0);
91
92     //output
93
ByteArrayOutputStream JavaDoc byteOutputStream = new ByteArrayOutputStream JavaDoc();
94
95     //get xsl
96
String JavaDoc XSL_URL = Constants.XSLT_DIR+"/html-url-rewite.xsl";
97     InputStream JavaDoc xslInputStream = Thread.currentThread().
98                  getContextClassLoader().getResourceAsStream(XSL_URL);
99     assertNotNull("empty xsl",xslInputStream);
100     Source JavaDoc xslSource = new StreamSource JavaDoc(xslInputStream);
101     assertNotNull("get xsl source",xslSource);
102
103     //init templates
104
TRAXTemplates traxTemplates = traxService.getTemplates(xslSource);
105     assertNotNull("get templates",traxTemplates);
106
107     //get transformer
108
TRAXTransformer traxTransformer = traxTemplates.newTransformer();
109     assertNotNull("get transformer",traxTransformer);
110
111     //transform
112
traxTransformer.initResult(new StreamResult JavaDoc(byteOutputStream));
113     traxTransformer.transform(new StreamSource JavaDoc(inputFileInputStream));
114     inputFileInputStream.close();
115
116     assertTrue("Output is empty", byteOutputStream.size() > 0);
117
118     //other transformer from same templates
119

120     TRAXTransformer traxOtherTransformer = traxTemplates.newTransformer();
121     assertNotNull("get Other transformer",traxOtherTransformer);
122
123     FileInputStream JavaDoc inputOtherFileInputStream =
124             new FileInputStream JavaDoc("tmp/rss-out.xhtml");
125     assertTrue("Empty input other file",
126                inputOtherFileInputStream.available() > 0);
127
128     ByteArrayOutputStream JavaDoc byteOtherOutputStream = new ByteArrayOutputStream JavaDoc();
129
130     traxOtherTransformer.initResult(new StreamResult JavaDoc(byteOtherOutputStream));
131     traxOtherTransformer.transform(new StreamSource JavaDoc(inputOtherFileInputStream));
132     inputOtherFileInputStream.close();
133     assertTrue("Output other is empty", byteOutputStream.size() > 0);
134
135 }
136
137
138
139
140 }
141
Popular Tags