/*
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.qetest.xslwrapper;
import java.util.Properties;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import static jaxp.library.JAXPTestUtilities.filenameToURL;
/**
* Implementation of TransformWrapper that uses the TrAX API and uses systemId
* URL's for it's sources; plus always transforms the xml file three
* times.
*
* This is the most common usage: transformer = factory.newTransformer(new
* StreamSource(xslURL)); transformer.transform(new StreamSource(xmlURL), new
* StreamResult(resultFileName));
*
* Important! The underlying System property of
* javax.xml.transform.TransformerFactory will determine the actual TrAX
* implementation used. This value will be reported out in our
* getProcessorInfo() method.
*/
public class TraxSystemId3Wrapper extends TraxSystemIdWrapper {
/**
* Get a general description of this wrapper itself.
*
* @return Uses TrAX to perform THREE transforms from StreamSource(systemId)
*/
@Override
public String getDescription() {
return "Uses TrAX to perform THREE transforms from StreamSource(systemId)";
}
/**
* Get a specific description of the wrappered processor.
*
* @return specific description of the underlying processor or transformer
* implementation: this should include both the general product name, as
* well as specific version info. If possible, should be implemented without
* actively creating an underlying processor.
*/
@Override
public Properties getProcessorInfo() {
Properties p = TraxWrapperUtils.getTraxInfo();
p.put("traxwrapper.method", "systemId3");
p.put("traxwrapper.desc", getDescription());
return p;
}
/**
* Transform supplied xmlName file with the stylesheet in the xslName file
* into a resultName file three times.
*
* Names are assumed to be local path\filename references, and will be
* converted to URLs as needed for any underlying processor implementation.
*
* @param xmlName local path\filename of XML file to transform
* @param xslName local path\filename of XSL stylesheet to use
* @param resultName local path\filename to put result in
*
* @throws Exception any underlying exceptions from the wrapped processor
* are simply allowed to propagate; throws a RuntimeException if any other
* problems prevent us from actually completing the operation
*/
@Override
public void transform(String xmlName, String xslName, String resultName)
throws Exception {
preventFootShooting();
// Read/build xsl from a URL
Transformer transformer = factory.newTransformer(
new StreamSource(filenameToURL(xslName)));
// Set any of our options as Attributes on the transformer
TraxWrapperUtils.setAttributes(transformer, newProcessorOpts);
// Apply any parameters needed
applyParameters(transformer);
// Read/build xml, transform, and write results
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
// This is to test transformer re-use
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
}
/**
* Transform supplied xmlName file with a pre-built/pre-compiled stylesheet
* into a resultName file three times.
*
* User must have called buildStylesheet(xslName) beforehand, obviously.
* Names are assumed to be local path\filename references, and will be
* converted to URLs as needed.
*
* @param xmlName local path\filename of XML file to transform
* @param resultName local path\filename to put result in
*
* @throws Exception any underlying exceptions from the wrapped processor
* are simply allowed to propagate; throws a RuntimeException if any other
* problems prevent us from actually completing the operation; throws an
* IllegalStateException if isStylesheetReady() == false.
*
* @see #buildStylesheet(String xslName)
*/
@Override
public void transformWithStylesheet(String xmlName, String resultName)
throws Exception {
if (!isStylesheetReady()) {
throw new IllegalStateException("transformWithStylesheet() when isStylesheetReady() == false");
}
preventFootShooting();
// Get Transformer from Templates
Transformer transformer = builtTemplates.newTransformer();
// Set any of our options as Attributes on the transformer
TraxWrapperUtils.setAttributes(transformer, newProcessorOpts);
// Apply any parameters needed
applyParameters(transformer);
// read/build xml, transform, and write results
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
// This is to test transformer re-use
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
}
/**
* Transform supplied xmlName file with a stylesheet found in an
* xml-stylesheet PI into a resultName file.
*
* Names are assumed to be local path\filename references, and will be
* converted to URLs as needed. Implementations will use whatever facilities
* exist in their wrapped processor to fetch and build the stylesheet to
* use for the transform.
*
* @param xmlName local path\filename of XML file to transform
* @param resultName local path\filename to put result in
*
* @throws Exception any underlying exceptions from the wrapped processor
* are simply allowed to propagate; throws a RuntimeException if any other
* problems prevent us from actually completing the operation
*/
@Override
public void transformEmbedded(String xmlName, String resultName)
throws Exception {
preventFootShooting();
// Read xsl from the xml document
Source xslSource = factory.getAssociatedStylesheet(new StreamSource(filenameToURL(xmlName)),
null, null, null);
// Build xsl from a URL
Transformer transformer = factory.newTransformer(xslSource);
// Set any of our options as Attributes on the transformer
TraxWrapperUtils.setAttributes(transformer, newProcessorOpts);
// Apply any parameters needed
applyParameters(transformer);
// read/build xml, transform, and write results
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
// This is to test transformer re-use
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
transformer.transform(new StreamSource(filenameToURL(xmlName)),
new StreamResult(resultName));
}
}