1 7 package org.exoplatform.services.xml.transform.impl; 8 9 import java.io.InputStream ; 10 import java.io.IOException ; 11 import java.io.OutputStream ; 12 13 import javax.xml.transform.TransformerException ; 14 import javax.xml.transform.Result ; 15 import javax.xml.transform.Source ; 16 import javax.xml.transform.sax.SAXResult ; 17 import javax.xml.transform.sax.SAXSource ; 18 import javax.xml.transform.sax.SAXTransformerFactory ; 19 import javax.xml.transform.sax.TransformerHandler ; 20 import javax.xml.transform.stream.StreamResult ; 21 import org.xml.sax.InputSource ; 22 import org.xml.sax.XMLReader ; 23 import org.xml.sax.helpers.XMLReaderFactory ; 24 import org.xml.sax.SAXException ; 25 26 import org.apache.commons.logging.Log; 27 28 import org.exoplatform.services.xml.transform.AbstractTransformer; 29 import org.exoplatform.services.xml.transform.NotSupportedIOTypeException; 30 import org.exoplatform.services.log.LogService; 31 import org.exoplatform.container.PortalContainer; 32 import org.exoplatform.services.xml.resolving.XMLResolvingService; 33 34 35 36 42 public abstract class TransformerBase implements AbstractTransformer { 43 private Result result = null; 44 45 protected Log log; 46 protected SAXTransformerFactory tSAXFactory; 47 48 public XMLResolvingService resolvingService; 49 50 public TransformerBase() { 51 LogService logService = (LogService) PortalContainer.getInstance(). 52 getComponentInstanceOfType(LogService.class); 53 log = logService.getLog(this.getClass()); 54 55 log.debug("Current javax.xml.parsers.SAXParserFactory sys property [ " + 56 System.getProperty("javax.xml.parsers.SAXParserFactory", 57 "-Not set-") + "]"); 58 59 tSAXFactory = (SAXTransformerFactory ) 60 SAXTransformerFactory.newInstance(); 61 } 62 63 66 static public XMLReader getXMLReader() throws SAXException { 67 return XMLReaderFactory. 68 createXMLReader("org.apache.xerces.parsers.SAXParser"); 69 } 70 71 75 protected void afterInitResult(){ 76 log.debug("Result is set"); 77 } 78 79 final public void initResult(Result result) throws NotSupportedIOTypeException { 80 if (!isResultSupported(result)) { 81 throw new NotSupportedIOTypeException(result); 82 } 83 this.result = result; 84 afterInitResult(); 85 } 86 87 protected Result getResult() { 88 return this.result; 89 } 90 91 protected boolean isSourceSupported(Source source) { 92 return true; 93 } 94 95 protected boolean isResultSupported(Result result) { 96 return true; 97 } 98 99 protected abstract void internalTransform(Source src) throws 100 NotSupportedIOTypeException, 101 TransformerException , IllegalStateException ; 102 103 104 final public void transform(Source source) throws 105 NotSupportedIOTypeException, 106 TransformerException , IllegalStateException { 107 108 if (!isSourceSupported(source)) { 109 log.error("source of type "+source.getClass().getName()+" not supported"); 110 throw new NotSupportedIOTypeException(source); 111 } 112 113 if (this.result == null) { 114 log.error("Result not set"); 115 throw new IllegalStateException ( 116 "Result not specified. See initResult(Result)"); 117 } 118 119 internalTransform(source); 120 } 121 122 128 129 public void transformInputStream2Result(InputStream input, Result result) throws 130 TransformerException { 131 log.debug("Transform InputStream to result of type "+result.getClass().getName()); 132 133 if (result instanceof StreamResult ) { 135 OutputStream outputStream = 136 ((StreamResult ) result).getOutputStream(); 137 try { 138 int counter = 0; 139 while (input.available() > 0) { 140 byte[] byteArray = new byte[input.available()]; 141 int readBytes = input.read(byteArray); 142 counter += readBytes; 143 outputStream.write(byteArray, 0, readBytes); 144 } 145 log.debug("Write "+counter+" bytes to ouput stream"); 146 } catch (IOException ex) { 147 log.error("Error on read/write ",ex); 148 throw new TransformerException (ex); 149 } 150 } 151 else { 153 XMLReader xmlReader = null; 154 try { 155 xmlReader = getXMLReader(); 156 log.debug("xmlReader class is "+xmlReader.getClass().getName()); 157 158 if (resolvingService != null) { 160 xmlReader.setEntityResolver( 161 resolvingService.getEntityResolver()); 162 log.debug("Set entity resolver"); 163 } 164 165 if (result instanceof SAXResult ) { 167 SAXResult saxResult = (SAXResult ) result; 168 xmlReader.setContentHandler(saxResult.getHandler()); 169 log.debug("Parse direct to result"); 170 } 171 172 else { 174 log.debug("Create empty transformation"); 175 TransformerHandler transformerHandler = 176 tSAXFactory.newTransformerHandler(); 177 transformerHandler.setResult(result); 178 xmlReader.setContentHandler(transformerHandler); 179 log.debug("Parse to result throw empty transformer"); 180 } 181 xmlReader.parse(new InputSource (input)); 182 log.debug("Parse complete"); 183 } catch (SAXException ex) { 184 throw new TransformerException (ex); 185 } catch (IOException ex) { 186 throw new TransformerException (ex); 187 } 188 } 189 } 190 191 198 protected InputStream sourceAsInputStream(Source source) throws 199 NotSupportedIOTypeException { 200 InputSource inputSource = SAXSource.sourceToInputSource(source); 201 if (inputSource == null) { 202 throw new NotSupportedIOTypeException(source); 203 } 204 return inputSource.getByteStream(); 205 } 206 207 211 protected void writeTofile( byte[] bytes, 212 String postfix){ 213 String POSTFIX = new java.text.SimpleDateFormat ("yy-MM-DD_HH-mm-ss_"). 214 format(new java.util.Date ()); 215 try { 216 217 java.io.FileOutputStream fileLog = 218 new java.io.FileOutputStream ( 219 "c:/tmp/transf" + POSTFIX+postfix+".xhtml"); 220 fileLog.write(bytes); 221 fileLog.flush(); 222 fileLog.close(); 223 224 } catch (java.io.FileNotFoundException ex) { 225 } catch (IOException ex) { 226 } 227 } 228 229 } 230 231 232 233 234 | Popular Tags |