KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > transform > impl > html > TidyTransformerImpl


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

7
8 package org.exoplatform.services.xml.transform.impl.html;
9
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.util.Properties JavaDoc;
13 import javax.xml.transform.Source JavaDoc;
14 import javax.xml.transform.TransformerException JavaDoc;
15 import javax.xml.transform.stream.StreamResult JavaDoc;
16 import org.w3c.tidy.Tidy;
17
18 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException;
19 import org.exoplatform.services.xml.transform.html.HTMLTransformer;
20 import org.exoplatform.services.xml.transform.impl.TransformerBase;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25
26 /**
27  * Created by The eXo Platform SARL .
28  *
29  * Tidying incoming HTML to XHTML result
30  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
31  * @author <a HREF="mailto:alex.kravchuk@gmail.com">Alexander Kravchuk</a>
32  * @version $Id: TidyTransformerImpl.java 566 2005-01-25 12:50:49Z kravchuk $
33  */

34 public class TidyTransformerImpl extends TransformerBase implements
35         HTMLTransformer {
36     protected Tidy tidy;
37     protected Properties JavaDoc props;
38
39     public TidyTransformerImpl() {
40         super();
41         tidy = new Tidy();
42         initProps();
43     }
44
45     public Properties JavaDoc getOutputProperties() {
46         return this.props;
47     }
48
49     /**
50      * Sets properties for Tidy parser
51      * See Tidy properties
52      */

53     public void setOutputProperties(Properties JavaDoc props) {
54         this.props = props;
55 // tidy.setConfigurationFromProps(props);
56
}
57
58
59     private void initProps() {
60         this.props = new Properties JavaDoc();
61
62         props.setProperty("quiet", "true");
63         props.setProperty("quote-ampersand", "true");
64         props.setProperty("output-xhtml", "true");
65         props.setProperty("show-warnings", "false");
66         props.setProperty("clean", "true");
67
68     }
69
70     public void processNotNativeResult(ByteArrayOutputStream JavaDoc output) throws
71             TransformerException JavaDoc {
72
73         ByteArrayInputStream JavaDoc byteInputStream =
74                 new ByteArrayInputStream JavaDoc(output.toByteArray());
75
76         transformInputStream2Result(byteInputStream, getResult());
77         log.debug("Transform from temp output to "+
78                   getResult().getClass().getName()+" complete");
79     }
80
81     protected void internalTransform(Source JavaDoc source) throws
82             NotSupportedIOTypeException,
83             TransformerException JavaDoc, IllegalStateException JavaDoc {
84         InputStream JavaDoc input = sourceAsInputStream(source);
85
86         try {
87             log.debug(" input available bytes " + input.available());
88             if (input.available() == 0)
89                 return;
90         } catch (IOException JavaDoc ex) {
91             log.error("Error on read Source", ex);
92             new TransformerException JavaDoc("Error on read source",ex);
93         }
94
95
96         //to del begin
97
// ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
98
// transformInputStream2Result(input,new StreamResult(byteOutput));
99
// input = new ByteArrayInputStream(byteOutput.toByteArray());
100
// writeTofile(byteOutput.toByteArray(),"tidy_input");
101
//to del end
102

103         //OutputStream output = null;
104
tidy.setConfigurationFromProps(props);
105
106         if (getResult() instanceof StreamResult JavaDoc) {
107             OutputStream JavaDoc output = ((StreamResult JavaDoc) getResult()).getOutputStream();
108             log.debug("Prepare to write transform result direct to OutputStream");
109             tidy.parse(input, output);
110             log.debug("Tidy parse is complete");
111         } else {
112             ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
113             log.debug("Prepare to write transform result to temp output");
114             tidy.parse(input, output);
115             log.debug("Tidy parse is complete");
116             try {
117                 output.flush();
118             } catch (IOException JavaDoc ex) {
119                 throw new TransformerException JavaDoc(ex);
120             }
121             processNotNativeResult(output);
122         }
123
124     }
125
126 }
127
Popular Tags