org.hl7.fhir.utilities.XsltUtilities Maven / Gradle / Ivy
package org.hl7.fhir.utilities;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
import net.sf.saxon.TransformerFactoryImpl;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.util.Map;
/**
* Utilities for working with XML
*
* This is separate from {@link Utilities} in order to avoid introducing a mandatory
* dependency on Saxon for anyone using just the structures
*
*/
public class XsltUtilities {
public static byte[] saxonTransform(Map files, byte[] source, byte[] xslt) throws TransformerException {
TransformerFactory f = new net.sf.saxon.TransformerFactoryImpl();
f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
f.setURIResolver(new ZipURIResolver(files));
Transformer t = f.newTransformer(xsrc);
t.setURIResolver(new ZipURIResolver(files));
StreamSource src = new StreamSource(new ByteArrayInputStream(source));
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult res = new StreamResult(out);
t.transform(src, res);
return out.toByteArray();
}
public static byte[] transform(Map files, byte[] source, byte[] xslt) throws TransformerException {
TransformerFactory f = TransformerFactory.newInstance();
f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
f.setURIResolver(new ZipURIResolver(files));
Transformer t = f.newTransformer(xsrc);
t.setURIResolver(new ZipURIResolver(files));
StreamSource src = new StreamSource(new ByteArrayInputStream(source));
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamResult res = new StreamResult(out);
t.transform(src, res);
return out.toByteArray();
}
public static String saxonTransform(String source, String xslt) throws TransformerException, FileNotFoundException {
TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
Transformer t = f.newTransformer(xsrc);
StreamSource src = new StreamSource(new FileInputStream(source));
StreamResult res = new StreamResult(new ByteArrayOutputStream());
t.transform(src, res);
return res.getOutputStream().toString();
}
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
saxonTransform(xsltDir, source, xslt, dest, alt, null);
}
public static void saxonTransform(String xsltDir, String source, String xslt, String dest, URIResolver alt, Map params) throws FileNotFoundException, TransformerException {
TransformerFactoryImpl f = new net.sf.saxon.TransformerFactoryImpl();
f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
f.setURIResolver(new MyURIResolver(xsltDir, alt));
Transformer t = f.newTransformer(xsrc);
if (params != null) {
for (Map.Entry entry : params.entrySet()) {
t.setParameter(entry.getKey(), entry.getValue());
}
}
t.setURIResolver(new MyURIResolver(xsltDir, alt));
StreamSource src = new StreamSource(new FileInputStream(source));
StreamResult res = new StreamResult(new FileOutputStream(dest));
t.transform(src, res);
}
public static void transform(String xsltDir, String source, String xslt, String dest, URIResolver alt) throws FileNotFoundException, TransformerException {
TransformerFactory f = TransformerFactory.newInstance();
StreamSource xsrc = new StreamSource(new FileInputStream(xslt));
f.setURIResolver(new MyURIResolver(xsltDir, alt));
Transformer t = f.newTransformer(xsrc);
t.setURIResolver(new MyURIResolver(xsltDir, alt));
StreamSource src = new StreamSource(new FileInputStream(source));
StreamResult res = new StreamResult(new FileOutputStream(dest));
t.transform(src, res);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy