Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. //Setup FOP
  2. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
  3. //Setup Transformer
  4. Source xsltSrc = new StreamSource(new File(xslPath));
  5. Transformer transformer = tFactory.newTransformer(xsltSrc);
  6.  
  7. //Make sure the XSL transformation's result is piped through to FOP
  8. Result res = new SAXResult(fop.getDefaultHandler());
  9. //Setup input
  10. Source src = new StreamSource(new File(xmlPath));
  11. //Start the transformation and rendering process
  12. transformer.transform(src, res);
  13.  
  14. ClassLoader cl = this.getClass().getClassLoader();
  15. String systemID = "resources/xslt/myfile.xslt";
  16. InputStream in = cl.getResourceAsStream(systemID);
  17. URL url = cl.getResource(systemID);
  18. Source source = new StreamSource(in);
  19. source.setSystemId(url.toExternalForm());
  20. transformer = tFactory.newTransformer(source);
  21.  
  22. // construct a Source that reads from an InputStream
  23. Source mySrc = new StreamSource(anInputStream);
  24. // specify a system ID (a String) so the
  25. // Source can resolve relative URLs
  26. // that are encountered in XSLT stylesheets
  27. mySrc.setSystemId(aSystemId);
  28.  
  29. TransformerFactory transFact = TransformerFactory.newInstance();
  30. StreamSource xsltSource = new StreamSource(xsl);
  31.  
  32. // XXX for 'xsl:import' to load other xsls from class path
  33. transFact.setURIResolver(new ClasspathResourceURIResolver());
  34. Templates cachedXSLT = transFact.newTemplates(xsltSource);
  35. Transformer transformer = cachedXSLT.newTransformer();
  36.  
  37.  
  38. class ClasspathResourceURIResolver implements URIResolver {
  39. @Override
  40. public Source resolve(String href, String base) throws TransformerException {
  41. return new StreamSource(XXX.getClassLoader().getResourceAsStream(href));
  42. }
  43. }
  44.  
  45. <xsl:import href="META-INF/companybusinesscredit/imported.xsl"/>
  46.  
  47. JarfileResolver jarfileResolver = new JarfileResolver();
  48. transformer.setURIResolver(jarfileResolver);
  49.  
  50.  
  51. public class JarfileResolver implements URIResolver
  52. {
  53. public Source resolve(String fileName, String base) throws TransformerException
  54. {
  55. URL url = getClass().getClassLoader().getResource(fileName);
  56. StreamSource jarFileSS = new StreamSource();
  57.  
  58. try
  59. {
  60. InputStream jarfileIS = url.openStream();
  61. jarFileSS.setInputStream(jarfileIS);
  62. }
  63. catch(IOException ioExp)
  64. {
  65. throw new TransformerException(ioExp);
  66. }
  67. return jarFileSS;
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement