KickJava   Java API By Example, From Geeks To Geeks.

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


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.html.HTMLTransformerService;
16 import org.exoplatform.services.xml.transform.html.HTMLTransformer;
17 import org.exoplatform.services.xml.transform.trax.Constants;
18
19 import java.io.FileInputStream JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import org.apache.commons.logging.Log;
24
25 /**
26  * Created by the Exo Development team.
27  */

28 public class TestPipe extends BaseTest {
29
30     private HTMLTransformer htmlTransformer;
31     private TRAXTemplates traxTemplates;
32     private TRAXTransformerService traxService;
33     private Log log;
34
35     public void setUp() throws Exception JavaDoc {
36         log = getLog();
37
38       //html transformer
39
HTMLTransformerService htmlService =
40               (HTMLTransformerService) PortalContainer.getInstance().
41               getComponentInstanceOfType(HTMLTransformerService.class);
42       assertNotNull("htmlService", htmlService);
43       htmlTransformer = htmlService.getTransformer();
44       assertNotNull("get html transformer", htmlTransformer);
45
46       //trax transformer
47
traxService = (TRAXTransformerService) PortalContainer.getInstance().
48               getComponentInstanceOfType(TRAXTransformerService.class);
49
50       //get xsl
51
String JavaDoc XSL_URL = Constants.XSLT_DIR + "/html-url-rewite.xsl";
52       InputStream JavaDoc xslInputStream = Thread.currentThread().
53                                    getContextClassLoader().getResourceAsStream(
54                                            XSL_URL);
55       assertNotNull("empty xsl", xslInputStream);
56       Source JavaDoc xslSource = new StreamSource JavaDoc(xslInputStream);
57       assertNotNull("get xsl source", xslSource);
58       //init transformer
59
traxTemplates = traxService.getTemplates(xslSource);
60       assertNotNull("get trax Templates", traxTemplates);
61   }
62
63   public void testTidyAndXsl() throws Exception JavaDoc {
64       //input
65
FileInputStream JavaDoc inputFileInputStream =
66             new FileInputStream JavaDoc("tmp/rss-out.html");
67       assertTrue("Empty input file",inputFileInputStream.available() > 0);
68
69
70       //output
71
String JavaDoc OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_tydy_xsl.xml";
72       OutputStream JavaDoc outputFileOutputStream =
73                 new FileOutputStream JavaDoc(OUTPUT_FILENAME);
74       TRAXTransformer traxTransformer = traxTemplates.newTransformer();
75
76       //construct pipe
77
htmlTransformer.initResult(
78             traxTransformer.getTransformerAsResult());
79       traxTransformer.initResult(
80             new StreamResult JavaDoc(outputFileOutputStream));
81       htmlTransformer.transform(new StreamSource JavaDoc(inputFileInputStream));
82
83       inputFileInputStream.close();
84
85       //read the output file
86
FileInputStream JavaDoc outputFileInputStream =
87                 new FileInputStream JavaDoc(OUTPUT_FILENAME);
88
89       assertTrue("Output is empty", outputFileInputStream.available() > 0);
90       outputFileInputStream.close();
91   }
92
93   public void testXslAndXsl() throws Exception JavaDoc {
94       //input
95
FileInputStream JavaDoc inputFileInputStream =
96             new FileInputStream JavaDoc("tmp/rss-out.xhtml");
97       assertTrue("Empty input file",inputFileInputStream.available() > 0);
98
99       //output
100
String JavaDoc OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_xsl_xsl.xml";
101       OutputStream JavaDoc outputFileOutputStream =
102                 new FileOutputStream JavaDoc(OUTPUT_FILENAME);
103
104       TRAXTransformer traxTransformer1 = traxTemplates.newTransformer();
105       TRAXTransformer traxTransformer2 = traxTemplates.newTransformer();
106
107       assertNotNull("pipe supported ",
108                     traxTransformer2.getTransformerAsResult());
109
110       //construct pipe
111
traxTransformer1.initResult(
112             traxTransformer2.getTransformerAsResult());
113
114       traxTransformer2.initResult(
115             new StreamResult JavaDoc(outputFileOutputStream));
116
117       traxTransformer1.transform(new StreamSource JavaDoc(inputFileInputStream));
118
119       inputFileInputStream.close();
120       outputFileOutputStream.flush();
121       outputFileOutputStream.close();
122
123       //read the output file
124
FileInputStream JavaDoc outputFileInputStream =
125                 new FileInputStream JavaDoc(OUTPUT_FILENAME);
126
127       assertTrue("Output is empty", outputFileInputStream.available() > 0);
128       //validate output
129
validateXML(outputFileInputStream);
130       outputFileInputStream.close();
131
132   }
133
134   public void testTidyAndXslWithEmptySource() throws Exception JavaDoc {
135
136
137       java.io.ByteArrayOutputStream JavaDoc output =
138                 new java.io.ByteArrayOutputStream JavaDoc();
139       InputStream JavaDoc input = new java.io.ByteArrayInputStream JavaDoc(new byte[]{});
140
141       TRAXTransformer traxTransformer = traxTemplates.newTransformer();
142
143       //construct pipe
144
htmlTransformer.initResult(
145             traxTransformer.getTransformerAsResult());
146       traxTransformer.initResult(
147             new StreamResult JavaDoc(output));
148       htmlTransformer.transform(new StreamSource JavaDoc(input));
149
150       assertTrue("Output is not empty", output.toByteArray().length == 0);
151   }
152
153
154
155 }
156
Popular Tags