001package org.hl7.fhir.utilities;
002
003/*-
004 * #%L
005 * org.hl7.fhir.utilities
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import net.sf.saxon.TransformerFactoryImpl;
024
025import javax.xml.transform.Transformer;
026import javax.xml.transform.TransformerException;
027import javax.xml.transform.TransformerFactory;
028import javax.xml.transform.URIResolver;
029import javax.xml.transform.stream.StreamResult;
030import javax.xml.transform.stream.StreamSource;
031import java.io.*;
032import java.util.Map;
033
034/**
035 * Utilities for working with XML
036 * <p>
037 * This is separate from {@link Utilities} in order to avoid introducing a mandatory
038 * dependency on Saxon for anyone using just the structures
039 * </p>
040 */
041public class XsltUtilities {
042
043  public static byte[] saxonTransform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException {
044    TransformerFactory f = new net.sf.saxon.TransformerFactoryImpl();
045    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
046    StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
047    f.setURIResolver(new ZipURIResolver(files));
048    Transformer t = f.newTransformer(xsrc);
049
050    t.setURIResolver(new ZipURIResolver(files));
051    StreamSource src = new StreamSource(new ByteArrayInputStream(source));
052    ByteArrayOutputStream out = new ByteArrayOutputStream();
053    StreamResult res = new StreamResult(out);
054    t.transform(src, res);
055    return out.toByteArray();
056  }
057
058  public static byte[] transform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException {
059    TransformerFactory f = TransformerFactory.newInstance();
060    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
061    StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
062    f.setURIResolver(new ZipURIResolver(files));
063    Transformer t = f.newTransformer(xsrc);
064
065    t.setURIResolver(new ZipURIResolver(files));
066    StreamSource src = new StreamSource(new ByteArrayInputStream(source));
067    ByteArrayOutputStream out = new ByteArrayOutputStream();
068    StreamResult res = new StreamResult(out);
069    t.transform(src, res);
070    return out.toByteArray();
071  }
072
073  public static String saxonTransform(String source, String xslt) throws TransformerException, FileNotFoundException {
074    TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
075    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
076    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
077    Transformer t = f.newTransformer(xsrc);
078    StreamSource src = new StreamSource(new FileInputStream(source));
079    StreamResult res = new StreamResult(new ByteArrayOutputStream());
080    t.transform(src, res);
081    return res.getOutputStream().toString();
082  }
083
084  public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
085    saxonTransform(xsltDir, source, xslt, dest, alt, null);
086  }
087
088  public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map<String, String> params) throws FileNotFoundException, TransformerException {
089    TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
090    f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
091    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
092    f.setURIResolver(new MyURIResolver(xsltDir, alt));
093    Transformer t = f.newTransformer(xsrc);
094    if (params != null) {
095      for (Map.Entry<String, String> entry : params.entrySet()) {
096        t.setParameter(entry.getKey(), entry.getValue());
097      }
098    }
099
100    t.setURIResolver(new MyURIResolver(xsltDir, alt));
101    StreamSource src = new StreamSource(new FileInputStream(source));
102    StreamResult res = new StreamResult(new FileOutputStream(dest));
103    t.transform(src, res);
104  }
105
106  public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
107
108    TransformerFactory f = TransformerFactory.newInstance();
109    StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
110    f.setURIResolver(new MyURIResolver(xsltDir, alt));
111    Transformer t = f.newTransformer(xsrc);
112
113    t.setURIResolver(new MyURIResolver(xsltDir, alt));
114    StreamSource src = new StreamSource(new FileInputStream(source));
115    StreamResult res = new StreamResult(new FileOutputStream(dest));
116    t.transform(src, res);
117
118  }
119
120}