1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 /* 19 * $Id$ 20 */ 21 package org.apache.qetest.xslwrapper; 22 import java.util.Properties; 23 24 import javax.xml.transform.Source; 25 import javax.xml.transform.Transformer; 26 import javax.xml.transform.stream.StreamResult; 27 import javax.xml.transform.stream.StreamSource; 28 29 import org.apache.qetest.QetestUtils; 30 31 /** 32 * Implementation of TransformWrapper that uses the TrAX API and 33 * uses systemId URL's for it's sources; plus always transforms 34 * the xml file <b>three</b> times. 35 * 36 * This is the most common usage: 37 * transformer = factory.newTransformer(new StreamSource(xslURL)); 38 * transformer.transform(new StreamSource(xmlURL), new StreamResult(resultFileName)); 39 * 40 * <b>Important!</b> The underlying System property of 41 * javax.xml.transform.TransformerFactory will determine the actual 42 * TrAX implementation used. This value will be reported out in 43 * our getProcessorInfo() method. 44 * 45 * @author Shane Curcuru 46 * @version $Id$ 47 */ 48 public class TraxSystemId3Wrapper extends TraxSystemIdWrapper 49 { 50 51 /** 52 * Get a general description of this wrapper itself. 53 * 54 * @return Uses TrAX to perform THREE transforms from StreamSource(systemId) 55 */ getDescription()56 public String getDescription() 57 { 58 return "Uses TrAX to perform THREE transforms from StreamSource(systemId)"; 59 } 60 61 62 /** 63 * Get a specific description of the wrappered processor. 64 * 65 * @return specific description of the underlying processor or 66 * transformer implementation: this should include both the 67 * general product name, as well as specific version info. If 68 * possible, should be implemented without actively creating 69 * an underlying processor. 70 */ getProcessorInfo()71 public Properties getProcessorInfo() 72 { 73 Properties p = TraxWrapperUtils.getTraxInfo(); 74 p.put("traxwrapper.method", "systemId3"); 75 p.put("traxwrapper.desc", getDescription()); 76 return p; 77 } 78 79 80 /** 81 * Transform supplied xmlName file with the stylesheet in the 82 * xslName file into a resultName file <b>three</b> times. 83 * 84 * Names are assumed to be local path\filename references, and 85 * will be converted to URLs as needed for any underlying 86 * processor implementation. 87 * 88 * @param xmlName local path\filename of XML file to transform 89 * @param xslName local path\filename of XSL stylesheet to use 90 * @param resultName local path\filename to put result in 91 * 92 * @return array of longs denoting timing of only these parts of 93 * our operation: IDX_OVERALL, IDX_XSLBUILD, IDX_TRANSFORM 94 * 95 * @throws Exception any underlying exceptions from the 96 * wrappered processor are simply allowed to propagate; throws 97 * a RuntimeException if any other problems prevent us from 98 * actually completing the operation 99 */ transform(String xmlName, String xslName, String resultName)100 public long[] transform(String xmlName, String xslName, String resultName) 101 throws Exception 102 { 103 preventFootShooting(); 104 long startTime = 0; 105 long xslBuild = 0; 106 long transform = 0; 107 108 // Timed: read/build xsl from a URL 109 startTime = System.currentTimeMillis(); 110 Transformer transformer = factory.newTransformer( 111 new StreamSource(QetestUtils.filenameToURL(xslName))); 112 xslBuild = System.currentTimeMillis() - startTime; 113 114 // Untimed: Set any of our options as Attributes on the transformer 115 TraxWrapperUtils.setAttributes(transformer, newProcessorOpts); 116 117 // Untimed: Apply any parameters needed 118 applyParameters(transformer); 119 120 // Timed: read/build xml, transform, and write results 121 startTime = System.currentTimeMillis(); 122 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 123 new StreamResult(resultName)); 124 transform = System.currentTimeMillis() - startTime; 125 126 // Only time the first transform, but do two more 127 // This is to test transformer re-use 128 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 129 new StreamResult(resultName)); 130 131 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 132 new StreamResult(resultName)); 133 134 long[] times = getTimeArray(); 135 times[IDX_OVERALL] = xslBuild + transform; 136 times[IDX_XSLBUILD] = xslBuild; 137 times[IDX_TRANSFORM] = transform; 138 return times; 139 } 140 141 142 /** 143 * Transform supplied xmlName file with a pre-built/pre-compiled 144 * stylesheet into a resultName file <b>three</b> times. 145 * 146 * User must have called buildStylesheet(xslName) beforehand, 147 * obviously. 148 * Names are assumed to be local path\filename references, and 149 * will be converted to URLs as needed. 150 * 151 * @param xmlName local path\filename of XML file to transform 152 * @param resultName local path\filename to put result in 153 * 154 * @return array of longs denoting timing of only these parts of 155 * our operation: IDX_OVERALL, IDX_XSLBUILD, IDX_TRANSFORM 156 * 157 * @throws Exception any underlying exceptions from the 158 * wrappered processor are simply allowed to propagate; throws 159 * a RuntimeException if any other problems prevent us from 160 * actually completing the operation; throws an 161 * IllegalStateException if isStylesheetReady() == false. 162 * 163 * @see #buildStylesheet(String xslName) 164 */ transformWithStylesheet(String xmlName, String resultName)165 public long[] transformWithStylesheet(String xmlName, String resultName) 166 throws Exception 167 { 168 if (!isStylesheetReady()) 169 throw new IllegalStateException("transformWithStylesheet() when isStylesheetReady() == false"); 170 171 preventFootShooting(); 172 long startTime = 0; 173 long transform = 0; 174 175 // UNTimed: get Transformer from Templates 176 Transformer transformer = builtTemplates.newTransformer(); 177 178 // Untimed: Set any of our options as Attributes on the transformer 179 TraxWrapperUtils.setAttributes(transformer, newProcessorOpts); 180 181 // Untimed: Apply any parameters needed 182 applyParameters(transformer); 183 184 // Timed: read/build xml, transform, and write results 185 startTime = System.currentTimeMillis(); 186 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 187 new StreamResult(resultName)); 188 transform = System.currentTimeMillis() - startTime; 189 190 // Only time the first transform, but do two more 191 // This is to test transformer re-use 192 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 193 new StreamResult(resultName)); 194 195 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 196 new StreamResult(resultName)); 197 198 long[] times = getTimeArray(); 199 times[IDX_OVERALL] = transform; 200 times[IDX_TRANSFORM] = transform; 201 return times; 202 } 203 204 205 /** 206 * Transform supplied xmlName file with a stylesheet found in an 207 * xml-stylesheet PI into a resultName file. 208 * 209 * Names are assumed to be local path\filename references, and 210 * will be converted to URLs as needed. Implementations will 211 * use whatever facilities exist in their wrappered processor 212 * to fetch and build the stylesheet to use for the transform. 213 * 214 * @param xmlName local path\filename of XML file to transform 215 * @param resultName local path\filename to put result in 216 * 217 * @return array of longs denoting timing of only these parts of 218 * our operation: IDX_OVERALL, IDX_XSLREAD (time to find XSL 219 * reference from the xml-stylesheet PI), IDX_XSLBUILD, (time 220 * to then build the Transformer therefrom), IDX_TRANSFORM 221 * 222 * @throws Exception any underlying exceptions from the 223 * wrappered processor are simply allowed to propagate; throws 224 * a RuntimeException if any other problems prevent us from 225 * actually completing the operation 226 */ transformEmbedded(String xmlName, String resultName)227 public long[] transformEmbedded(String xmlName, String resultName) 228 throws Exception 229 { 230 preventFootShooting(); 231 long startTime = 0; 232 long xslRead = 0; 233 long xslBuild = 0; 234 long transform = 0; 235 236 // Timed: readxsl from the xml document 237 startTime = System.currentTimeMillis(); 238 Source xslSource = factory.getAssociatedStylesheet(new StreamSource(QetestUtils.filenameToURL(xmlName)), 239 null, null, null); 240 xslRead = System.currentTimeMillis() - startTime; 241 242 // Timed: build xsl from a URL 243 startTime = System.currentTimeMillis(); 244 Transformer transformer = factory.newTransformer(xslSource); 245 xslBuild = System.currentTimeMillis() - startTime; 246 247 // Untimed: Set any of our options as Attributes on the transformer 248 TraxWrapperUtils.setAttributes(transformer, newProcessorOpts); 249 250 // Untimed: Apply any parameters needed 251 applyParameters(transformer); 252 253 // Timed: read/build xml, transform, and write results 254 startTime = System.currentTimeMillis(); 255 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 256 new StreamResult(resultName)); 257 transform = System.currentTimeMillis() - startTime; 258 259 // Only time the first transform, but do two more 260 // This is to test transformer re-use 261 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 262 new StreamResult(resultName)); 263 264 transformer.transform(new StreamSource(QetestUtils.filenameToURL(xmlName)), 265 new StreamResult(resultName)); 266 267 long[] times = getTimeArray(); 268 times[IDX_OVERALL] = xslRead + xslBuild + transform; 269 times[IDX_XSLREAD] = xslRead; 270 times[IDX_XSLBUILD] = xslBuild; 271 times[IDX_TRANSFORM] = transform; 272 return times; 273 } 274 } 275