View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.oscal.tools.cli.core.operations;
28  
29  import gov.nist.secauto.metaschema.binding.io.xml.XmlUtil;
30  
31  import net.sf.saxon.jaxp.SaxonTransformerFactory;
32  
33  import org.apache.logging.log4j.LogManager;
34  import org.apache.logging.log4j.Logger;
35  
36  import java.io.File;
37  import java.io.IOException;
38  
39  import javax.xml.transform.Source;
40  import javax.xml.transform.Transformer;
41  import javax.xml.transform.TransformerException;
42  import javax.xml.transform.TransformerFactory;
43  import javax.xml.transform.stream.StreamResult;
44  import javax.xml.transform.stream.StreamSource;
45  
46  public final class XMLOperations {
47    private static final Logger LOGGER = LogManager.getLogger(XMLOperations.class);
48  
49    private XMLOperations() {
50      // disable construction
51    }
52  
53    public static void renderCatalogHTML(File input, File result) throws IOException, TransformerException {
54      render(input, result,
55          XmlUtil.getStreamSource(XMLOperations.class.getResource("/xsl/oscal-for-bootstrap-html.xsl")));
56    }
57  
58    public static void renderProfileHTML(File input, File result) throws IOException, TransformerException {
59      SaxonTransformerFactory transfomerFactory = (SaxonTransformerFactory) TransformerFactory.newInstance();
60      // Templates resolver = transfomerFactory.newTemplates();
61      // Templates renderer = transfomerFactory.newTemplates();
62  
63      File temp = File.createTempFile("resolved-profile", ".xml");
64  
65      try {
66        Transformer transformer = transfomerFactory
67            .newTransformer(XmlUtil.getStreamSource(XMLOperations.class.getResource("/xsl/profile-resolver.xsl")));
68        transformer.transform(new StreamSource(input), new StreamResult(temp));
69  
70        transformer = transfomerFactory.newTransformer(
71            XmlUtil.getStreamSource(XMLOperations.class.getResource("/xsl/oscal-for-bootstrap-html.xsl")));
72        transformer.transform(new StreamSource(temp), new StreamResult(result));
73      } finally {
74        if (!temp.delete()) {
75          LOGGER.atError().log("failed to delete file: {}", temp);
76        }
77      }
78  
79      // TransformerHandler resolverHandler =
80      // transfomerFactory.newTransformerHandler(resolver);
81      // TransformerHandler rendererHandler =
82      // transfomerFactory.newTransformerHandler(renderer);
83      //
84      // resolverHandler.setResult(new SAXResult(rendererHandler));
85      // rendererHandler.setResult(new StreamResult(result));
86      //
87      // Transformer t = transfomerFactory.newTransformer();
88      // File sourceFile = input.getAbsoluteFile();
89      // StreamSource source = new StreamSource();
90      // String sourceSystemId = sourceFile.toURI().toASCIIString();
91      // log.info("Source: "+sourceSystemId);
92      // source.setSystemId(sourceSystemId);
93      // t.setURIResolver(new LoggingURIResolver(t.getURIResolver()));
94      // resolver.setParameter("document-uri", sourceSystemId);
95      // t.transform(source, new SAXResult(resolverHandler));
96    }
97  
98    public static void render(File input, File result, Source transform) throws TransformerException {
99      TransformerFactory transfomerFactory = TransformerFactory.newInstance();
100     assert transfomerFactory instanceof SaxonTransformerFactory;
101     Transformer transformer = transfomerFactory.newTransformer(transform);
102     transformer.transform(new StreamSource(input), new StreamResult(result));
103   }
104 
105   // private static class LoggingURIResolver implements URIResolver {
106   // private final URIResolver delegate;
107   //
108   //
109   // public LoggingURIResolver(URIResolver delegate) {
110   // super();
111   // this.delegate = delegate;
112   // }
113   //
114   //
115   // @Override
116   // public Source resolve(String href, String base) throws TransformerException {
117   // log.info("Resolve: base='{}', href='{}'", base, href);
118   // return delegate.resolve(href, base);
119   // }
120   //
121   // }
122 }