PageRenderTime 1941ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/exoportal-v1.0.2/services/xml-processing/impl/src/java/org/exoplatform/services/xml/transform/impl/TransformerBase.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 233 lines | 153 code | 39 blank | 41 comment | 13 complexity | 5dd2f0f963a6eae0cb262695a3532089 MD5 | raw file
  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. package org.exoplatform.services.xml.transform.impl;
  8. import java.io.InputStream;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import javax.xml.transform.TransformerException;
  12. import javax.xml.transform.Result;
  13. import javax.xml.transform.Source;
  14. import javax.xml.transform.sax.SAXResult;
  15. import javax.xml.transform.sax.SAXSource;
  16. import javax.xml.transform.sax.SAXTransformerFactory;
  17. import javax.xml.transform.sax.TransformerHandler;
  18. import javax.xml.transform.stream.StreamResult;
  19. import org.xml.sax.InputSource;
  20. import org.xml.sax.XMLReader;
  21. import org.xml.sax.helpers.XMLReaderFactory;
  22. import org.xml.sax.SAXException;
  23. import org.apache.commons.logging.Log;
  24. import org.exoplatform.services.xml.transform.AbstractTransformer;
  25. import org.exoplatform.services.xml.transform.NotSupportedIOTypeException;
  26. import org.exoplatform.services.log.LogService;
  27. import org.exoplatform.container.PortalContainer;
  28. import org.exoplatform.services.xml.resolving.XMLResolvingService;
  29. /**
  30. * Created by The eXo Platform SARL .
  31. *
  32. * @author <a href="mailto:alex.kravchuk@gmail.com">Alexander Kravchuk</a>
  33. * @version $Id: TransformerBase.java 566 2005-01-25 12:50:49Z kravchuk $
  34. */
  35. public abstract class TransformerBase implements AbstractTransformer {
  36. private Result result = null;
  37. protected Log log;
  38. protected SAXTransformerFactory tSAXFactory;
  39. public XMLResolvingService resolvingService;
  40. public TransformerBase() {
  41. LogService logService = (LogService) PortalContainer.getInstance().
  42. getComponentInstanceOfType(LogService.class);
  43. log = logService.getLog(this.getClass());
  44. log.debug("Current javax.xml.parsers.SAXParserFactory sys property [ " +
  45. System.getProperty("javax.xml.parsers.SAXParserFactory",
  46. "-Not set-") + "]");
  47. tSAXFactory = (SAXTransformerFactory)
  48. SAXTransformerFactory.newInstance();
  49. }
  50. /**
  51. * @todo change. Must no use explicit parser class name
  52. */
  53. static public XMLReader getXMLReader() throws SAXException {
  54. return XMLReaderFactory.
  55. createXMLReader("org.apache.xerces.parsers.SAXParser");
  56. }
  57. /**
  58. * override when need some operation after initialization result
  59. * in transformer
  60. */
  61. protected void afterInitResult(){
  62. log.debug("Result is set");
  63. }
  64. final public void initResult(Result result) throws NotSupportedIOTypeException {
  65. if (!isResultSupported(result)) {
  66. throw new NotSupportedIOTypeException(result);
  67. }
  68. this.result = result;
  69. afterInitResult();
  70. }
  71. protected Result getResult() {
  72. return this.result;
  73. }
  74. protected boolean isSourceSupported(Source source) {
  75. return true;
  76. }
  77. protected boolean isResultSupported(Result result) {
  78. return true;
  79. }
  80. protected abstract void internalTransform(Source src) throws
  81. NotSupportedIOTypeException,
  82. TransformerException, IllegalStateException;
  83. final public void transform(Source source) throws
  84. NotSupportedIOTypeException,
  85. TransformerException, IllegalStateException {
  86. if (!isSourceSupported(source)) {
  87. log.error("source of type "+source.getClass().getName()+" not supported");
  88. throw new NotSupportedIOTypeException(source);
  89. }
  90. if (this.result == null) {
  91. log.error("Result not set");
  92. throw new IllegalStateException(
  93. "Result not specified. See initResult(Result)");
  94. }
  95. internalTransform(source);
  96. }
  97. /**
  98. * Tranform InputStream to specified result, according to type of result
  99. * @param input InputStream
  100. * @param result Result
  101. * @throws TransformerException
  102. */
  103. public void transformInputStream2Result(InputStream input, Result result) throws
  104. TransformerException {
  105. log.debug("Transform InputStream to result of type "+result.getClass().getName());
  106. //StreamResult - write data from InputStream to OutputStream
  107. if (result instanceof StreamResult) {
  108. OutputStream outputStream =
  109. ((StreamResult) result).getOutputStream();
  110. try {
  111. int counter = 0;
  112. while (input.available() > 0) {
  113. byte[] byteArray = new byte[input.available()];
  114. int readBytes = input.read(byteArray);
  115. counter += readBytes;
  116. outputStream.write(byteArray, 0, readBytes);
  117. }
  118. log.debug("Write "+counter+" bytes to ouput stream");
  119. } catch (IOException ex) {
  120. log.error("Error on read/write ",ex);
  121. throw new TransformerException(ex);
  122. }
  123. }
  124. //not StreamResult
  125. else {
  126. XMLReader xmlReader = null;
  127. try {
  128. xmlReader = getXMLReader();
  129. log.debug("xmlReader class is "+xmlReader.getClass().getName());
  130. //set default resolver
  131. if (resolvingService != null) {
  132. xmlReader.setEntityResolver(
  133. resolvingService.getEntityResolver());
  134. log.debug("Set entity resolver");
  135. }
  136. //SAXResult use XMLReader to parce InputStream to SAXEvents
  137. if (result instanceof SAXResult) {
  138. SAXResult saxResult = (SAXResult) result;
  139. xmlReader.setContentHandler(saxResult.getHandler());
  140. log.debug("Parse direct to result");
  141. }
  142. //not StreamResult, not SAXResult - create empty transformation
  143. else {
  144. log.debug("Create empty transformation");
  145. TransformerHandler transformerHandler =
  146. tSAXFactory.newTransformerHandler();
  147. transformerHandler.setResult(result);
  148. xmlReader.setContentHandler(transformerHandler);
  149. log.debug("Parse to result throw empty transformer");
  150. }
  151. xmlReader.parse(new InputSource(input));
  152. log.debug("Parse complete");
  153. } catch (SAXException ex) {
  154. throw new TransformerException(ex);
  155. } catch (IOException ex) {
  156. throw new TransformerException(ex);
  157. }
  158. }
  159. }
  160. /**
  161. * Transform javax.xml.transform.Source to java.io.InputStream
  162. * if can't transform throw exception
  163. * @param source Source
  164. * @return InputStream
  165. * @throws NotSupportedIOTypeException
  166. */
  167. protected InputStream sourceAsInputStream(Source source) throws
  168. NotSupportedIOTypeException {
  169. InputSource inputSource = SAXSource.sourceToInputSource(source);
  170. if (inputSource == null) {
  171. throw new NotSupportedIOTypeException(source);
  172. }
  173. return inputSource.getByteStream();
  174. }
  175. /**
  176. * @todo REMOVE!!!! For debug only!!!
  177. * @deprecated see Warning
  178. */
  179. protected void writeTofile( byte[] bytes,
  180. String postfix){
  181. String POSTFIX = new java.text.SimpleDateFormat("yy-MM-DD_HH-mm-ss_").
  182. format(new java.util.Date());
  183. try {
  184. java.io.FileOutputStream fileLog =
  185. new java.io.FileOutputStream(
  186. "c:/tmp/transf" + POSTFIX+postfix+".xhtml");
  187. fileLog.write(bytes);
  188. fileLog.flush();
  189. fileLog.close();
  190. } catch (java.io.FileNotFoundException ex) {
  191. } catch (IOException ex) {
  192. }
  193. }
  194. }