KickJava   Java API By Example, From Geeks To Geeks.

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


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.sax.TransformerHandler JavaDoc;
10 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
11 import javax.xml.transform.sax.SAXResult JavaDoc;
12
13 import org.exoplatform.container.PortalContainer;
14
15 import org.exoplatform.services.xml.transform.html.HTMLTransformer;
16 import org.exoplatform.services.xml.transform.html.HTMLTransformerService;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.OutputStream JavaDoc;
20
21 import java.util.Properties JavaDoc;
22
23 import org.exoplatform.services.log.LogService;
24 import org.exoplatform.container.RootContainer;
25 import org.apache.commons.logging.Log;
26
27
28 /**
29  * Created by the Exo Development team.
30  */

31 public class TestTidy extends BaseTest {
32     private HTMLTransformer htmlTransformer;
33
34   private Log log;
35
36   public void setUp() throws Exception JavaDoc {
37       log = getLog();
38         HTMLTransformerService htmlService =
39                 (HTMLTransformerService) PortalContainer.getInstance().
40                 getComponentInstanceOfType(HTMLTransformerService.class);
41         assertNotNull("htmlService", htmlService);
42         htmlTransformer = htmlService.getTransformer();
43
44     }
45
46
47   public void testTidy() throws Exception JavaDoc {
48     try {
49       String JavaDoc OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_xhtml.xhtml";
50
51       FileInputStream JavaDoc inputFileInputStream =
52                 new FileInputStream JavaDoc("tmp/rss-out.html");
53
54       //output file
55
OutputStream JavaDoc outputFileOutputStream =
56                 new FileOutputStream JavaDoc(OUTPUT_FILENAME);
57
58       htmlTransformer.initResult(new StreamResult JavaDoc(outputFileOutputStream));
59       htmlTransformer.transform(new StreamSource JavaDoc(inputFileInputStream));
60
61       outputFileOutputStream.close();
62
63       //read the output file
64
FileInputStream JavaDoc outputFileInputStream =
65                 new FileInputStream JavaDoc(OUTPUT_FILENAME);
66
67       assertTrue("Output is empty", outputFileInputStream.available() > 0);
68
69       //validate output xml
70
validateXML(outputFileInputStream);
71
72     } catch (Exception JavaDoc e) {
73       fail("testTidy() ERROR: " + e.toString());
74     }
75   }
76
77   public void testSAXResultType() throws Exception JavaDoc {
78
79       FileInputStream JavaDoc inputFileInputStream =
80               new FileInputStream JavaDoc("tmp/rss-out.html");
81       assertTrue("Empty input file",inputFileInputStream.available() > 0);
82
83       //create empty transformation
84
TransformerHandler JavaDoc transformHandler = //a copy of the source to the result
85
((SAXTransformerFactory JavaDoc) SAXTransformerFactory.newInstance()).
86               newTransformerHandler();
87
88       String JavaDoc OUTPUT_FILENAME = "tmp/rss-out_"+getTimeStamp()+"_html2sax.xhtml";
89       OutputStream JavaDoc output = new FileOutputStream JavaDoc(OUTPUT_FILENAME);
90
91       transformHandler.setResult(new StreamResult JavaDoc(output));
92
93       SAXResult JavaDoc saxResult = new SAXResult JavaDoc(transformHandler);
94       htmlTransformer.initResult(saxResult);
95       htmlTransformer.transform(new StreamSource JavaDoc(inputFileInputStream));
96
97
98       output.flush();
99       output.close();
100       //read the output file
101
FileInputStream JavaDoc outputFileInputStream =
102                 new FileInputStream JavaDoc(OUTPUT_FILENAME);
103       assertTrue("Output is empty", outputFileInputStream.available() > 0);
104       //validate output xml
105
validateXML(outputFileInputStream);
106   }
107
108
109   public void testProps() throws Exception JavaDoc {
110       try {
111           Properties JavaDoc props = htmlTransformer.getOutputProperties();
112
113           assertEquals(props.getProperty("quiet"), "true");
114           props.setProperty("quiet", "false");
115
116           htmlTransformer.setOutputProperties(props);
117           assertEquals(htmlTransformer.getOutputProperties().getProperty(
118                   "quiet"), "false");
119
120       } catch (Exception JavaDoc e) {
121           fail("testProps() ERROR: " + e.toString());
122       }
123
124   }
125
126
127 }
128
Popular Tags