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