001package org.hl7.fhir.utilities;
002
003import java.io.*;
004import java.util.Map;
005
006import javax.xml.transform.Transformer;
007import javax.xml.transform.TransformerException;
008import javax.xml.transform.TransformerFactory;
009import javax.xml.transform.URIResolver;
010import javax.xml.transform.stream.StreamResult;
011import javax.xml.transform.stream.StreamSource;
012
013import net.sf.saxon.TransformerFactoryImpl;
014
015/**
016 * Utiltities for working with Saxon
017 */
018public class SaxonUtilities {
019
020  /*
021   * Note: this functionality was removed from Utilities.java in order to prevent users of that
022   * class from needing to import unneeded libraries (sound, saxon, etc.)
023   */
024
025  public static byte[] saxonTransform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException  {
026    TransformerFactory f = new net.sf.saxon.TransformerFactoryImpl();
027    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
028    StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
029    f.setURIResolver(new ZipURIResolver(files));
030    Transformer t = f.newTransformer(xsrc);
031 
032    t.setURIResolver(new ZipURIResolver(files));
033    StreamSource src = new StreamSource(new ByteArrayInputStream(source));
034    ByteArrayOutputStream out = new ByteArrayOutputStream();
035    StreamResult res = new StreamResult(out);
036    t.transform(src, res);
037    return out.toByteArray();    
038  }
039  
040  public static byte[] transform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException  {
041    TransformerFactory f = TransformerFactory.newInstance();
042    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
043    StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
044    f.setURIResolver(new ZipURIResolver(files));
045    Transformer t = f.newTransformer(xsrc);
046
047    t.setURIResolver(new ZipURIResolver(files));
048    StreamSource src = new StreamSource(new ByteArrayInputStream(source));
049    ByteArrayOutputStream out = new ByteArrayOutputStream();
050    StreamResult res = new StreamResult(out);
051    t.transform(src, res);
052    return out.toByteArray();    
053  }
054
055  public static String saxonTransform(String source, String xslt) throws TransformerException, FileNotFoundException  {
056    TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
057    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
058    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
059    Transformer t = f.newTransformer(xsrc);
060    StreamSource src = new StreamSource(new FileInputStream(source));
061    StreamResult res = new StreamResult(new ByteArrayOutputStream());
062    t.transform(src, res);
063    return res.getOutputStream().toString();   
064  }
065
066  public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException  {
067   saxonTransform(xsltDir, source, xslt, dest, alt, null);
068  }
069
070  public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map<String, String> params) throws FileNotFoundException, TransformerException  {
071    TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
072    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
073    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
074    f.setURIResolver(new MyURIResolver(xsltDir, alt));
075    Transformer t = f.newTransformer(xsrc);
076      if (params != null) {
077         for (Map.Entry<String, String> entry : params.entrySet()) {
078            t.setParameter(entry.getKey(), entry.getValue());
079         }
080   }
081    
082    t.setURIResolver(new MyURIResolver(xsltDir, alt));
083    StreamSource src = new StreamSource(new FileInputStream(source));
084    StreamResult res = new StreamResult(new FileOutputStream(dest));
085    t.transform(src, res);    
086  }
087  
088  public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException  {
089
090    TransformerFactory f = TransformerFactory.newInstance();
091    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
092    f.setURIResolver(new MyURIResolver(xsltDir, alt));
093    Transformer t = f.newTransformer(xsrc);
094
095    t.setURIResolver(new MyURIResolver(xsltDir, alt));
096    StreamSource src = new StreamSource(new FileInputStream(source));
097    StreamResult res = new StreamResult(new FileOutputStream(dest));
098    t.transform(src, res);
099    
100  }
101  
102}