001package org.hl7.fhir.utilities;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033import net.sf.saxon.TransformerFactoryImpl;
034
035import javax.xml.transform.Transformer;
036import javax.xml.transform.TransformerException;
037import javax.xml.transform.TransformerFactory;
038import javax.xml.transform.URIResolver;
039import javax.xml.transform.stream.StreamResult;
040import javax.xml.transform.stream.StreamSource;
041import java.io.*;
042import java.util.Map;
043
044/**
045 * Utilities for working with XML
046 * <p>
047 * This is separate from {@link Utilities} in order to avoid introducing a mandatory
048 * dependency on Saxon for anyone using just the structures
049 * </p>
050 */
051public class XsltUtilities {
052
053  public static byte[] saxonTransform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException {
054    TransformerFactory f = new net.sf.saxon.TransformerFactoryImpl();
055    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
056    StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
057    f.setURIResolver(new ZipURIResolver(files));
058    Transformer t = f.newTransformer(xsrc);
059
060    t.setURIResolver(new ZipURIResolver(files));
061    StreamSource src = new StreamSource(new ByteArrayInputStream(source));
062    ByteArrayOutputStream out = new ByteArrayOutputStream();
063    StreamResult res = new StreamResult(out);
064    t.transform(src, res);
065    return out.toByteArray();
066  }
067
068  public static byte[] transform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException {
069    TransformerFactory f = TransformerFactory.newInstance();
070    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
071    StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
072    f.setURIResolver(new ZipURIResolver(files));
073    Transformer t = f.newTransformer(xsrc);
074
075    t.setURIResolver(new ZipURIResolver(files));
076    StreamSource src = new StreamSource(new ByteArrayInputStream(source));
077    ByteArrayOutputStream out = new ByteArrayOutputStream();
078    StreamResult res = new StreamResult(out);
079    t.transform(src, res);
080    return out.toByteArray();
081  }
082
083  public static String saxonTransform(String source, String xslt) throws TransformerException, FileNotFoundException {
084    TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
085    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
086    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
087    Transformer t = f.newTransformer(xsrc);
088    StreamSource src = new StreamSource(new FileInputStream(source));
089    StreamResult res = new StreamResult(new ByteArrayOutputStream());
090    t.transform(src, res);
091    return res.getOutputStream().toString();
092  }
093
094  public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
095    saxonTransform(xsltDir, source, xslt, dest, alt, null);
096  }
097
098  public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map<String, String> params) throws FileNotFoundException, TransformerException {
099    TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
100    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
101    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
102    f.setURIResolver(new MyURIResolver(xsltDir, alt));
103    Transformer t = f.newTransformer(xsrc);
104    if (params != null) {
105      for (Map.Entry<String, String> entry : params.entrySet()) {
106        t.setParameter(entry.getKey(), entry.getValue());
107      }
108    }
109
110    t.setURIResolver(new MyURIResolver(xsltDir, alt));
111    StreamSource src = new StreamSource(new FileInputStream(source));
112    StreamResult res = new StreamResult(new FileOutputStream(dest));
113    t.transform(src, res);
114  }
115
116  public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
117
118    TransformerFactory f = TransformerFactory.newInstance();
119    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
120    f.setURIResolver(new MyURIResolver(xsltDir, alt));
121    Transformer t = f.newTransformer(xsrc);
122
123    t.setURIResolver(new MyURIResolver(xsltDir, alt));
124    StreamSource src = new StreamSource(new FileInputStream(source));
125    StreamResult res = new StreamResult(new FileOutputStream(dest));
126    t.transform(src, res);
127
128  }
129
130}