|
27 | 27 | import java.io.ByteArrayOutputStream; |
28 | 28 | import java.io.IOException; |
29 | 29 | import java.io.OutputStream; |
| 30 | +import java.net.URL; |
30 | 31 | import java.nio.file.Files; |
31 | 32 | import java.nio.file.Paths; |
32 | 33 | import java.time.LocalDate; |
|
62 | 63 | import javax.xml.transform.stream.StreamResult; |
63 | 64 | import java.io.InputStream; |
64 | 65 |
|
| 66 | + |
| 67 | +import java.io.OutputStream; |
| 68 | +import javax.xml.transform.Result; |
| 69 | +import javax.xml.transform.Source; |
| 70 | +import javax.xml.transform.Transformer; |
| 71 | +import javax.xml.transform.TransformerFactory; |
| 72 | +import javax.xml.transform.sax.SAXResult; |
| 73 | +import javax.xml.transform.stream.StreamSource; |
| 74 | + |
| 75 | +import org.apache.fop.apps.FOUserAgent; |
| 76 | +import org.apache.fop.apps.Fop; |
| 77 | +import org.apache.fop.apps.FopFactory; |
| 78 | +import org.apache.fop.apps.MimeConstants; |
| 79 | + |
| 80 | + |
65 | 81 | public class DdiExportUtil { |
66 | 82 |
|
67 | 83 | private static final Logger logger = Logger.getLogger(DdiExportUtil.class.getCanonicalName()); |
@@ -2090,6 +2106,40 @@ private static boolean checkParentElement(XMLStreamWriter xmlw, String elementNa |
2090 | 2106 | return true; |
2091 | 2107 | } |
2092 | 2108 |
|
| 2109 | + public static void datasetPdfDDI(InputStream datafile, OutputStream outputStream) throws XMLStreamException { |
| 2110 | + try { |
| 2111 | + InputStream styleSheetInput = DdiExportUtil.class.getClassLoader().getResourceAsStream("edu/harvard/iq/dataverse/ddi-to-fo.xsl"); |
| 2112 | + |
| 2113 | + final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); |
| 2114 | + FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); |
| 2115 | + |
| 2116 | + try { |
| 2117 | + Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outputStream); |
| 2118 | + // Setup XSLT |
| 2119 | + TransformerFactory factory = TransformerFactory.newInstance(); |
| 2120 | + Transformer transformer = factory.newTransformer(new StreamSource(styleSheetInput)); |
| 2121 | + |
| 2122 | + // Set the value of a <param> in the stylesheet |
| 2123 | + transformer.setParameter("versionParam", "2.0"); |
| 2124 | + |
| 2125 | + // Setup input for XSLT transformation |
| 2126 | + Source src = new StreamSource(datafile); |
| 2127 | + |
| 2128 | + // Resulting SAX events (the generated FO) must be piped through to FOP |
| 2129 | + Result res = new SAXResult(fop.getDefaultHandler()); |
| 2130 | + |
| 2131 | + // Start XSLT transformation and FOP processing |
| 2132 | + transformer.transform(src, res); |
| 2133 | + |
| 2134 | + } catch (Exception e) { |
| 2135 | + logger.severe(e.getMessage()); |
| 2136 | + } |
| 2137 | + } catch (Exception e) { |
| 2138 | + logger.info("Second try"); |
| 2139 | + logger.severe(e.getMessage()); |
| 2140 | + } |
| 2141 | + } |
| 2142 | + |
2093 | 2143 | public static void datasetHtmlDDI(InputStream datafile, OutputStream outputStream) throws XMLStreamException { |
2094 | 2144 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
2095 | 2145 |
|
|
0 commit comments