/projects/jboss-5.1.0/varia/src/main/org/jboss/varia/deployment/convertor/XslTransformer.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 133 lines · 62 code · 14 blank · 57 comment · 6 complexity · d56417cbe1bb53c8d95438d36d2163e5 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.varia.deployment.convertor;
  23. import java.net.URL;
  24. import java.util.Properties;
  25. import java.util.Enumeration;
  26. import java.io.InputStream;
  27. import java.io.OutputStream;
  28. import java.io.IOException;
  29. import javax.xml.transform.TransformerFactory;
  30. import javax.xml.transform.Transformer;
  31. import javax.xml.transform.Templates;
  32. import javax.xml.transform.TransformerException;
  33. import javax.xml.transform.stream.StreamSource;
  34. import javax.xml.transform.stream.StreamResult;
  35. /**
  36. * XslTransformer is a utility class for XSL transformations.
  37. *
  38. * @author <a href="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
  39. */
  40. public class XslTransformer
  41. {
  42. // Attributes --------------------------------------------------------
  43. /**
  44. The system property that determines which Factory implementation
  45. to create is named "javax.xml.transform.TransformerFactory".
  46. If the property is not defined, a platform default is used.
  47. An implementation of the TransformerFactory class is NOT guaranteed
  48. to be thread safe.
  49. */
  50. private static TransformerFactory transformerFactory =
  51. TransformerFactory.newInstance();
  52. // Public static methods ---------------------------------------------
  53. /**
  54. Applies transformation.
  55. Pre-compiled stylesheet should be used in a thread-safe manner grabbing
  56. a new transformer before completing the transformation.
  57. */
  58. public static synchronized void applyTransformation(InputStream srcIs,
  59. OutputStream destOs,
  60. InputStream templateIs,
  61. Properties outputProps)
  62. throws TransformerException, IOException
  63. {
  64. StreamSource source = new StreamSource(srcIs);
  65. StreamResult result = new StreamResult(destOs);
  66. StreamSource template = new StreamSource(templateIs);
  67. /*
  68. Pre-compile the stylesheet. This Templates object may be used
  69. concurrently across multiple threads. Creating a Templates object
  70. allows the TransformerFactory to do detailed performance optimization
  71. of transformation instructions, without penalizing runtime
  72. transformation.
  73. */
  74. Templates templates = transformerFactory.newTemplates( template );
  75. Transformer transformer = templates.newTransformer();
  76. if( outputProps != null )
  77. {
  78. transformer.setOutputProperties( outputProps );
  79. }
  80. transformer.transform( source, result );
  81. }
  82. /**
  83. * Applies template <code>templateIs</code> to xml source
  84. * <code>srcIs</code> with output properties <code>outputProps</code>
  85. * and parameters <code>xslParams</code>.
  86. * The resulting xml is written to <code>destOs</code>
  87. */
  88. public static synchronized void applyTransformation(InputStream srcIs,
  89. OutputStream destOs,
  90. InputStream templateIs,
  91. Properties outputProps,
  92. Properties xslParams)
  93. throws TransformerException, IOException
  94. {
  95. StreamSource source = new StreamSource( srcIs );
  96. StreamResult result = new StreamResult( destOs );
  97. StreamSource template = new StreamSource( templateIs );
  98. Templates templates = transformerFactory.newTemplates( template );
  99. // set output properties
  100. Transformer transformer = templates.newTransformer();
  101. if(outputProps != null)
  102. {
  103. transformer.setOutputProperties(outputProps);
  104. }
  105. // set xsl parameters
  106. if(xslParams != null)
  107. {
  108. // note, xslParams.keys() will not work properly,
  109. // because it will not return the keys for default properties.
  110. Enumeration keys = xslParams.propertyNames();
  111. while( keys.hasMoreElements() )
  112. {
  113. String key = (String)keys.nextElement();
  114. transformer.setParameter(key, xslParams.getProperty(key));
  115. }
  116. }
  117. transformer.transform( source, result );
  118. }
  119. }