1 7 8 package org.exoplatform.services.xml.transform.impl.html; 9 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 import java.util.Properties ; 13 import javax.xml.transform.Source ; 14 import javax.xml.transform.TransformerException ; 15 import javax.xml.transform.stream.StreamResult ; 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 ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 25 26 34 public class TidyTransformerImpl extends TransformerBase implements 35 HTMLTransformer { 36 protected Tidy tidy; 37 protected Properties props; 38 39 public TidyTransformerImpl() { 40 super(); 41 tidy = new Tidy(); 42 initProps(); 43 } 44 45 public Properties getOutputProperties() { 46 return this.props; 47 } 48 49 53 public void setOutputProperties(Properties props) { 54 this.props = props; 55 } 57 58 59 private void initProps() { 60 this.props = new Properties (); 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 output) throws 71 TransformerException { 72 73 ByteArrayInputStream byteInputStream = 74 new ByteArrayInputStream (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 source) throws 82 NotSupportedIOTypeException, 83 TransformerException , IllegalStateException { 84 InputStream input = sourceAsInputStream(source); 85 86 try { 87 log.debug(" input available bytes " + input.available()); 88 if (input.available() == 0) 89 return; 90 } catch (IOException ex) { 91 log.error("Error on read Source", ex); 92 new TransformerException ("Error on read source",ex); 93 } 94 95 96 103 tidy.setConfigurationFromProps(props); 105 106 if (getResult() instanceof StreamResult ) { 107 OutputStream output = ((StreamResult ) 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 output = new ByteArrayOutputStream (); 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 ex) { 119 throw new TransformerException (ex); 120 } 121 processNotNativeResult(output); 122 } 123 124 } 125 126 } 127 | Popular Tags |