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 23 import java.io.FileNotFoundException; 24 import java.io.FileOutputStream; 25 import java.io.PrintWriter; 26 import java.lang.reflect.Field; 27 import java.util.Enumeration; 28 import java.util.Hashtable; 29 import java.util.Properties; 30 31 import javax.xml.transform.ErrorListener; 32 import javax.xml.transform.Transformer; 33 import javax.xml.transform.TransformerFactory; 34 import javax.xml.transform.URIResolver; 35 36 import org.apache.xalan.trace.PrintTraceListener; 37 import org.apache.xalan.trace.TraceManager; 38 import org.apache.xalan.transformer.TransformerImpl; 39 40 /** 41 * Cheap-o utilities for Trax*Wrapper implementations. 42 * 43 * @author Shane Curcuru 44 * @version $Id$ 45 */ 46 public abstract class TraxWrapperUtils 47 { 48 49 /** 50 * Get a generic description of the TrAX related info. 51 * 52 * @return Properties block with basic info about any TrAX 53 * implementing processor, plus specific version information 54 * about Xalan-J 2.x or Xerces-J 1.x if found 55 */ getTraxInfo()56 public static Properties getTraxInfo() 57 { 58 Properties p = new Properties(); 59 p.put("traxwrapper.api", "trax"); 60 p.put("traxwrapper.language", "java"); 61 try 62 { 63 Properties sysProps = System.getProperties(); 64 p.put("traxwrapper.jaxp.transform", 65 sysProps.getProperty("javax.xml.transform.TransformerFactory", "unset")); 66 p.put("traxwrapper.jaxp.parser.dom", 67 sysProps.getProperty("javax.xml.parsers.DocumentBuilderFactory", "unset")); 68 p.put("traxwrapper.jaxp.parser.sax", 69 sysProps.getProperty("javax.xml.parsers.SAXParserFactory", "unset")); 70 } 71 // In case we're in an Applet 72 catch (SecurityException se) { /* no-op, ignore */ } 73 74 // Look for some Xalan/Xerces specific version info 75 try 76 { 77 Class clazz = Class.forName("org.apache.xerces.framework.Version"); 78 // Found 1.x, grab it's version fields 79 Field f = clazz.getField("fVersion"); 80 p.put("traxwrapper.xerces.version", (String)f.get(null)); 81 } 82 catch (Exception e1) { /* no-op, ignore */ } 83 84 try 85 { 86 Class clazz = Class.forName("org.apache.xalan.processor.XSLProcessorVersion"); 87 Field f = clazz.getField("S_VERSION"); 88 p.put("traxwrapper.xalan.version", (String)f.get(null)); 89 } 90 catch (Exception e2) { /* no-op, ignore */ } 91 92 return p; 93 } 94 95 96 /** 97 * Apply specific Attributes to a TransformerFactory OR 98 * Transformer, OR call specific setFoo() API's on a 99 * TransformerFactory OR Transformer. 100 * 101 * Filters on hashkeys.startsWith("Processor.setAttribute.") 102 * Most Attributes are simply passed to factory.setAttribute(), 103 * however certain special cases are handled: 104 * setURIResolver, setErrorListener. 105 * Exceptions thrown by underlying transformer are propagated. 106 * 107 * This takes an Object so that an underlying worker method can 108 * process either a TransformerFactory or a Transformer. 109 * 110 * @param factory TransformerFactory to call setAttributes on. 111 * @param attrs Hashtable of potential attributes to set. 112 */ setAttributes(Object setPropsOn, Hashtable attrs)113 public static void setAttributes(Object setPropsOn, 114 Hashtable attrs) 115 throws IllegalArgumentException 116 { 117 if ((null == setPropsOn) || (null == attrs)) 118 return; 119 120 Enumeration attrKeys = null; 121 try 122 { 123 // Attempt to use as a Properties block.. 124 attrKeys = ((Properties)attrs).propertyNames(); 125 } 126 catch (ClassCastException cce) 127 { 128 // .. but fallback to get as Hashtable instead 129 attrKeys = attrs.keys(); 130 } 131 132 while (attrKeys.hasMoreElements()) 133 { 134 String key = (String) attrKeys.nextElement(); 135 // Only attempt to set the attr if it matches our marker 136 if ((null != key) 137 && (key.startsWith(TransformWrapper.SET_PROCESSOR_ATTRIBUTES))) 138 { 139 Object value = null; 140 try 141 { 142 // Attempt to use as a Properties block.. 143 value = ((Properties)attrs).getProperty(key); 144 // But, if null, then try getting as hash anyway 145 if (null == value) 146 { 147 value = attrs.get(key); 148 } 149 } 150 catch (ClassCastException cce) 151 { 152 // .. but fallback to get as Hashtable instead 153 value = attrs.get(key); 154 } 155 // Strip off our marker for the property name 156 String processorKey = key.substring(TransformWrapper.SET_PROCESSOR_ATTRIBUTES.length()); 157 // Ugly, but it works -sc 158 if (setPropsOn instanceof TransformerFactory) 159 setAttribute((TransformerFactory)setPropsOn, processorKey, value); 160 else if (setPropsOn instanceof Transformer) 161 setAttribute((Transformer)setPropsOn, processorKey, value); 162 // else - ignore it, no-op 163 } 164 } 165 166 } 167 168 169 /** Token specifying a call to setURIResolver. */ 170 public static String SET_URI_RESOLVER = "setURIResolver"; 171 172 /** Token specifying a call to setErrorListener. */ 173 public static String SET_ERROR_LISTENER = "setErrorListener"; 174 175 /** Token specifying a call to setup a trace listener. */ 176 public static String SET_TRACE_LISTENER = "setTraceListener"; 177 178 /** 179 * Apply specific Attributes to a TransformerFactory OR call 180 * specific setFoo() API's on a TransformerFactory. 181 * 182 * Filters on hashkeys.startsWith("Processor.setAttribute.") 183 * Most Attributes are simply passed to factory.setAttribute(), 184 * however certain special cases are handled: 185 * setURIResolver, setErrorListener. 186 * Exceptions thrown by underlying transformer are propagated. 187 * 188 * @see setAttribute(Transformer, String, Object) 189 * @param factory TransformerFactory to call setAttributes on. 190 * @param key specific attribute or special case attr. 191 * @param value to set for key. 192 */ setAttribute(TransformerFactory factory, String key, Object value)193 private static void setAttribute(TransformerFactory factory, 194 String key, 195 Object value) 196 throws IllegalArgumentException 197 { 198 if ((null == factory) || (null == key)) 199 return; 200 201 // Note: allow exceptions to propagate here 202 203 // Check if this is a special case to call a specific 204 // API, or the general case to call setAttribute(key...) 205 if (SET_URI_RESOLVER.equals(key)) 206 { 207 factory.setURIResolver((URIResolver)value); 208 } 209 else if (SET_ERROR_LISTENER.equals(key)) 210 { 211 factory.setErrorListener((ErrorListener)value); 212 } 213 else if (SET_TRACE_LISTENER.equals(key)) 214 { 215 // no-op 216 } 217 else 218 { 219 // General case; just call setAttribute 220 factory.setAttribute(key, value); 221 } 222 } 223 224 225 /** 226 * Apply specific Attributes to a Transformer OR call 227 * specific setFoo() API's on a Transformer. 228 * 229 * Filters on hashkeys.startsWith("Processor.setAttribute.") 230 * Only certain special cases are handled: 231 * setURIResolver, setErrorListener. 232 * Exceptions thrown by underlying transformer are propagated. 233 * 234 * @see setAttribute(TransformerFactory, String, Object) 235 * @param factory TransformerFactory to call setAttributes on. 236 * @param key specific attribute or special case attr. 237 * @param value to set for key. 238 */ setAttribute(Transformer transformer, String key, Object value)239 private static void setAttribute(Transformer transformer, 240 String key, 241 Object value) 242 throws IllegalArgumentException 243 { 244 if ((null == transformer) || (null == key)) 245 return; 246 247 // Note: allow exceptions to propagate here 248 249 // Check if this is a special case to call a specific 250 // API, or the general case to call setAttribute(key...) 251 if (SET_URI_RESOLVER.equals(key)) 252 { 253 transformer.setURIResolver((URIResolver)value); 254 } 255 else if (SET_ERROR_LISTENER.equals(key)) 256 { 257 transformer.setErrorListener((ErrorListener)value); 258 } 259 else if (SET_TRACE_LISTENER.equals(key) && transformer instanceof TransformerImpl) 260 { 261 // Android-changed: TransformerImpl in 2.7.1 doesn't have getTraceManager() method. 262 // TraceManager traceManager = ((TransformerImpl)transformer).getTraceManager(); 263 try { 264 FileOutputStream writeStream = new FileOutputStream((String)value); 265 PrintWriter printWriter = new PrintWriter(writeStream, true); 266 PrintTraceListener traceListener = new PrintTraceListener(printWriter); 267 traceListener.m_traceElements = true; 268 traceListener.m_traceGeneration = true; 269 traceListener.m_traceSelection = true; 270 traceListener.m_traceTemplates = true; 271 // Android-changed: TransformerImpl in 2.7.1 doesn't have getTraceManager() method. 272 // traceManager.addTraceListener(traceListener); 273 } catch (FileNotFoundException fnfe) { 274 System.out.println("File not found: " + fnfe); 275 } catch (Exception e) { 276 System.out.println("Exception: " + e); 277 } 278 } 279 else 280 { 281 // General case; no-op; no equivalent 282 } 283 } 284 } 285