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 24 /** 25 * Factory static class for creating TransformWrappers. 26 * 27 * Includes a 'wrapperMapper' functionality to simplify specifying the 'flavor' 28 * of a wrapper to create. This is optional, but will allow a user to specify 29 * newWrapper("trax.file") and get back an instance of an 30 * org.apache.qetest.xslwrapper.TraxFileWrapper. 31 */ 32 public class TransformWrapperFactory { 33 34 /** 35 * Currently known list of implemented wrappers. 36 * <ul> 37 * <p>Allows specification of a simple name for each wrapper, so clients 38 * don't necessarily have to know the FQCN or fully qualified class name of 39 * their wrapper.</p> 40 * </ul> 41 */ 42 protected final static Properties wrapperMapper = new Properties(); 43 44 /** 45 * Static initializer for wrapperMapper. Attempts to load 46 * TransformWrapperFactory.properties into our wrapperMapper. 47 */ 48 static { 49 // Add a default trax flavor, since it's widely used 50 // This should be overwritten below if the properties 51 // file can be loaded. 52 wrapperMapper.put("trax.systemId", "org.apache.qetest.xslwrapper.TraxSystemIdWrapper"); 53 wrapperMapper.put("trax.file", "org.apache.qetest.xslwrapper.TraxFileWrapper"); 54 wrapperMapper.put("trax.stream", "org.apache.qetest.xslwrapper.TraxStreamWrapper"); 55 wrapperMapper.put("trax.dom", "org.apache.qetest.xslwrapper.TraxDOMWrapper"); 56 wrapperMapper.put("trax.sax", "org.apache.qetest.xslwrapper.TraxSAXWrapper"); 57 wrapperMapper.put("trax.localPath", "org.apache.qetest.xslwrapper.TraxLocalPathWrapper"); 58 wrapperMapper.put("trax.systemId3", "org.apache.qetest.xslwrapper.TraxSystemId3Wrapper"); 59 } 60 61 /** 62 * Accessor for our wrapperMapper. 63 * @return Properties block of wrapper implementations that we implicitly 64 * know about; may be null if we have none 65 */ 66 public static final Properties getDescriptions() { 67 return wrapperMapper; 68 } 69 70 /** 71 * Return an instance of a TransformWrapper of requested flavor. 72 * 73 * This static factory method creates TransformWrappers for the user. Our 74 * 'wrapperMapper' functionality allows users to either specify short names 75 * listed in TransformWrapperFactory.properties (which are mapped to the 76 * appropriate FQCN's) or to directly supply the FQCN of a wrapper to 77 * create. 78 * 79 * @param flavor to create, either a wrapperMapped one or FQCN 80 * 81 * @return instance of a wrapper; will throw an IllegalArgumentException if 82 * we cannot find the asked-for wrapper class anywhere 83 */ 84 public static final TransformWrapper newWrapper(String flavor) { 85 // Attempt to lookup the flavor: if found, use the value we got, 86 // otherwise default to the same value 87 String className = wrapperMapper.getProperty(flavor, flavor); 88 89 try { 90 // Allow people to use bare classnames in popular packages 91 Class clazz = Class.forName(className); 92 return (TransformWrapper) clazz.newInstance(); 93 } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { 94 throw new IllegalArgumentException("newWrapper(" + flavor + ") threw: " + e.toString()); 95 } 96 } 97 }