1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 package org.apache.qetest.xslwrapper;
  21 
  22 import java.util.Properties;
  23 import javax.xml.transform.Source;
  24 import javax.xml.transform.Transformer;
  25 import javax.xml.transform.stream.StreamResult;
  26 import javax.xml.transform.stream.StreamSource;
  27 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  28 
  29 /**
  30  * Implementation of TransformWrapper that uses the TrAX API and uses systemId
  31  * URL's for it's sources; plus always transforms the xml file <b>three</b>
  32  * times.
  33  *
  34  * This is the most common usage: transformer = factory.newTransformer(new
  35  * StreamSource(xslURL)); transformer.transform(new StreamSource(xmlURL), new
  36  * StreamResult(resultFileName));
  37  *
  38  * <b>Important!</b> The underlying System property of
  39  * javax.xml.transform.TransformerFactory will determine the actual TrAX
  40  * implementation used. This value will be reported out in our
  41  * getProcessorInfo() method.
  42  */
  43 public class TraxSystemId3Wrapper extends TraxSystemIdWrapper {
  44 
  45     /**
  46      * Get a general description of this wrapper itself.
  47      *
  48      * @return Uses TrAX to perform THREE transforms from StreamSource(systemId)
  49      */
  50     @Override
  51     public String getDescription() {
  52         return "Uses TrAX to perform THREE transforms from StreamSource(systemId)";
  53     }
  54 
  55     /**
  56      * Get a specific description of the wrappered processor.
  57      *
  58      * @return specific description of the underlying processor or transformer
  59      * implementation: this should include both the general product name, as
  60      * well as specific version info. If possible, should be implemented without
  61      * actively creating an underlying processor.
  62      */
  63     @Override
  64     public Properties getProcessorInfo() {
  65         Properties p = TraxWrapperUtils.getTraxInfo();
  66         p.put("traxwrapper.method", "systemId3");
  67         p.put("traxwrapper.desc", getDescription());
  68         return p;
  69     }
  70 
  71     /**
  72      * Transform supplied xmlName file with the stylesheet in the xslName file
  73      * into a resultName file <b>three</b> times.
  74      *
  75      * Names are assumed to be local path\filename references, and will be
  76      * converted to URLs as needed for any underlying processor implementation.
  77      *
  78      * @param xmlName local path\filename of XML file to transform
  79      * @param xslName local path\filename of XSL stylesheet to use
  80      * @param resultName local path\filename to put result in
  81      *
  82      * @throws Exception any underlying exceptions from the wrapped processor
  83      * are simply allowed to propagate; throws a RuntimeException if any other
  84      * problems prevent us from actually completing the operation
  85      */
  86     @Override
  87     public void transform(String xmlName, String xslName, String resultName)
  88             throws Exception {
  89         preventFootShooting();
  90 
  91         // Read/build xsl from a URL
  92         Transformer transformer = factory.newTransformer(
  93                 new StreamSource(filenameToURL(xslName)));
  94 
  95         // Set any of our options as Attributes on the transformer
  96         TraxWrapperUtils.setAttributes(transformer, newProcessorOpts);
  97 
  98         // Apply any parameters needed
  99         applyParameters(transformer);
 100 
 101         // Read/build xml, transform, and write results
 102         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 103                 new StreamResult(resultName));
 104 
 105         // This is to test transformer re-use
 106         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 107                 new StreamResult(resultName));
 108 
 109         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 110                 new StreamResult(resultName));
 111     }
 112 
 113     /**
 114      * Transform supplied xmlName file with a pre-built/pre-compiled stylesheet
 115      * into a resultName file <b>three</b> times.
 116      *
 117      * User must have called buildStylesheet(xslName) beforehand, obviously.
 118      * Names are assumed to be local path\filename references, and will be
 119      * converted to URLs as needed.
 120      *
 121      * @param xmlName local path\filename of XML file to transform
 122      * @param resultName local path\filename to put result in
 123      *
 124      * @throws Exception any underlying exceptions from the wrapped processor
 125      * are simply allowed to propagate; throws a RuntimeException if any other
 126      * problems prevent us from actually completing the operation; throws an
 127      * IllegalStateException if isStylesheetReady() == false.
 128      *
 129      * @see #buildStylesheet(String xslName)
 130      */
 131     @Override
 132     public void transformWithStylesheet(String xmlName, String resultName)
 133             throws Exception {
 134         if (!isStylesheetReady()) {
 135             throw new IllegalStateException("transformWithStylesheet() when isStylesheetReady() == false");
 136         }
 137 
 138         preventFootShooting();
 139 
 140         // Get Transformer from Templates
 141         Transformer transformer = builtTemplates.newTransformer();
 142 
 143         // Set any of our options as Attributes on the transformer
 144         TraxWrapperUtils.setAttributes(transformer, newProcessorOpts);
 145 
 146         // Apply any parameters needed
 147         applyParameters(transformer);
 148 
 149         // read/build xml, transform, and write results
 150         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 151                 new StreamResult(resultName));
 152 
 153         // This is to test transformer re-use
 154         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 155                 new StreamResult(resultName));
 156 
 157         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 158                 new StreamResult(resultName));
 159     }
 160 
 161     /**
 162      * Transform supplied xmlName file with a stylesheet found in an
 163      * xml-stylesheet PI into a resultName file.
 164      *
 165      * Names are assumed to be local path\filename references, and will be
 166      * converted to URLs as needed. Implementations will use whatever facilities
 167      * exist in their wrapped processor to fetch and build the stylesheet to
 168      * use for the transform.
 169      *
 170      * @param xmlName local path\filename of XML file to transform
 171      * @param resultName local path\filename to put result in
 172      *
 173      * @throws Exception any underlying exceptions from the wrapped processor
 174      * are simply allowed to propagate; throws a RuntimeException if any other
 175      * problems prevent us from actually completing the operation
 176      */
 177     @Override
 178     public void transformEmbedded(String xmlName, String resultName)
 179             throws Exception {
 180         preventFootShooting();
 181         // Read xsl from the xml document
 182         Source xslSource = factory.getAssociatedStylesheet(new StreamSource(filenameToURL(xmlName)),
 183                 null, null, null);
 184 
 185         // Build xsl from a URL
 186         Transformer transformer = factory.newTransformer(xslSource);
 187 
 188         // Set any of our options as Attributes on the transformer
 189         TraxWrapperUtils.setAttributes(transformer, newProcessorOpts);
 190 
 191         // Apply any parameters needed
 192         applyParameters(transformer);
 193 
 194         // read/build xml, transform, and write results
 195         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 196                 new StreamResult(resultName));
 197 
 198         // This is to test transformer re-use
 199         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 200                 new StreamResult(resultName));
 201 
 202         transformer.transform(new StreamSource(filenameToURL(xmlName)),
 203                 new StreamResult(resultName));
 204     }
 205 }