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.InputStream; 24 import java.util.Properties; 25 26 import org.apache.qetest.QetestUtils; 27 28 /** 29 * Factory static class for creating TransformWrappers. 30 * 31 * Includes a 'wrapperMapper' functionality to simplify specifying 32 * the 'flavor' of a wrapper to create. This is optional, but will 33 * allow a user to specify newWrapper("trax.file") and get back an 34 * instance of an org.apache.qetest.xslwrapper.TraxFileWrapper. 35 * 36 * @author Shane Curcuru 37 * @version $Id$ 38 */ 39 public abstract class TransformWrapperFactory 40 { 41 42 /** 43 * Currently known list of implemented wrappers. 44 * 45 * Allows specification of a simple name for each wrapper, 46 * so clients don't necessarily have to know the FQCN or 47 * fully qualified classname of their wrapper.</p> 48 * 49 * <ul>Where: 50 * <li>key = String simple name = xalan|trax|etc...</li> 51 * <li>value = String FQCN of wrapper implementation</li> 52 * <li>Supports: See TransformWrapperFactory.properties</li> 53 * </ul> 54 */ 55 protected static Properties wrapperMapper = null; 56 57 58 /** 59 * Static initializer for wrapperMapper. 60 * Attempts to load TransformWrapperFactory.properties into 61 * our wrapperMapper. 62 */ 63 static 64 { 65 wrapperMapper = new Properties(); 66 67 // Add a default trax flavor, since it's widely used 68 // This should be overwritten below if the properties 69 // file can be loaded 70 wrapperMapper.put("trax", "org.apache.qetest.xslwrapper.TraxFileWrapper"); 71 72 try 73 { 74 InputStream is = 75 TransformWrapperFactory.class.getResourceAsStream( 76 "TransformWrapperFactory.properties"); 77 78 wrapperMapper.load(is); is.close()79 is.close(); 80 } 81 catch (Exception e) 82 { 83 System.err.println( 84 "TransformWrapperFactory.properties.load() threw: " 85 + e.toString()); 86 e.printStackTrace(); 87 } 88 }; 89 90 91 /** 92 * Accessor for our wrapperMapper. 93 * 94 * @return Properties block of wrapper implementations 95 * that we implicitly know about; may be null if we have none 96 */ getDescriptions()97 public static final Properties getDescriptions() 98 { 99 return wrapperMapper; 100 } 101 102 103 /** 104 * Return an instance of a TransformWrapper of requested flavor. 105 * 106 * This static factory method creates TransformWrappers for the 107 * user. Our 'wrapperMapper' functionality allows users to 108 * either specify short names listed in 109 * TransformWrapperFactory.properties (which are mapped to the 110 * appropriate FQCN's) or to directly supply the FQCN of a 111 * wrapper to create. 112 * 113 * @param flavor to create, either a wrapperMapped one or FQCN 114 * 115 * @return instance of a wrapper; will throw an 116 * IllegalArgumentException if we cannot find the asked-for 117 * wrapper class anywhere 118 */ newWrapper(String flavor)119 public static final TransformWrapper newWrapper(String flavor) 120 { 121 122 // Attempt to lookup the flavor: if found, use the 123 // value we got, otherwise default to the same value 124 String className = wrapperMapper.getProperty(flavor, flavor); 125 126 try 127 { 128 // Allow people to use bare classnames in popular packages 129 Class clazz = QetestUtils.testClassForName(className, 130 QetestUtils.defaultPackages, 131 "Wrapper Not Found"); 132 133 return (TransformWrapper) clazz.newInstance(); 134 } 135 catch (Exception e) 136 { 137 throw new IllegalArgumentException("newWrapper(" + flavor + ") threw: " + e.toString()); 138 } 139 } 140 141 142 /** 143 * Simplistic command line support merely prints out wrapperMapper. 144 * 145 * @param args command line args - unused 146 */ main(String[] args)147 public static void main(String[] args) 148 { 149 Properties p = getDescriptions(); 150 System.out.println("TransformWrapperFactory: installed flavors=wrappers"); 151 p.list(System.out); 152 } 153 } 154