/* * 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; /** * Factory static class for creating TransformWrappers. * * Includes a 'wrapperMapper' functionality to simplify specifying the 'flavor' * of a wrapper to create. This is optional, but will allow a user to specify * newWrapper("trax.file") and get back an instance of an * org.apache.qetest.xslwrapper.TraxFileWrapper. */ public class TransformWrapperFactory { /** * Currently known list of implemented wrappers. * */ protected final static Properties wrapperMapper = new Properties(); /** * Static initializer for wrapperMapper. Attempts to load * TransformWrapperFactory.properties into our wrapperMapper. */ static { // Add a default trax flavor, since it's widely used // This should be overwritten below if the properties // file can be loaded. wrapperMapper.put("trax.systemId", "org.apache.qetest.xslwrapper.TraxSystemIdWrapper"); wrapperMapper.put("trax.file", "org.apache.qetest.xslwrapper.TraxFileWrapper"); wrapperMapper.put("trax.stream", "org.apache.qetest.xslwrapper.TraxStreamWrapper"); wrapperMapper.put("trax.dom", "org.apache.qetest.xslwrapper.TraxDOMWrapper"); wrapperMapper.put("trax.sax", "org.apache.qetest.xslwrapper.TraxSAXWrapper"); wrapperMapper.put("trax.localPath", "org.apache.qetest.xslwrapper.TraxLocalPathWrapper"); wrapperMapper.put("trax.systemId3", "org.apache.qetest.xslwrapper.TraxSystemId3Wrapper"); } /** * Accessor for our wrapperMapper. * @return Properties block of wrapper implementations * that we implicitly know about; may be null if we have none */ public static final Properties getDescriptions() { return wrapperMapper; } /** * Return an instance of a TransformWrapper of requested flavor. * * This static factory method creates TransformWrappers for the user. Our * 'wrapperMapper' functionality allows users to either specify short names * listed in TransformWrapperFactory.properties (which are mapped to the * appropriate FQCN's) or to directly supply the FQCN of a wrapper to * create. * * @param flavor to create, either a wrapperMapped one or FQCN * * @return instance of a wrapper; will throw an IllegalArgumentException if * we cannot find the asked-for wrapper class anywhere */ public static final TransformWrapper newWrapper(String flavor) { // Attempt to lookup the flavor: if found, use the // value we got, otherwise default to the same value String className = wrapperMapper.getProperty(flavor, flavor); try { // Allow people to use bare classnames in popular packages Class clazz = Class.forName(className); return (TransformWrapper) clazz.newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { throw new IllegalArgumentException("newWrapper(" + flavor + ") threw: " + e.toString()); } } }