| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.trans; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | |
| 12 | /** |
| 13 | * Interface representing a factory class for instantiating instances of a specific class |
| 14 | */ |
| 15 | public class Instantiator<T> implements Maker<T> { |
| 16 | |
| 17 | private String className; |
| 18 | private Configuration config; |
| 19 | |
| 20 | public Instantiator(String className, Configuration config) { |
| 21 | this.className = className; |
| 22 | this.config = config; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Obtain an instance of type T, either by making a new instance or by reusing an existing instance |
| 27 | * |
| 28 | * @throws XPathException if the attempt fails |
| 29 | */ |
| 30 | |
| 31 | public T make() throws XPathException { |
| 32 | Object o = config.getInstance(className, null); |
| 33 | try { |
| 34 | return (T)o; |
| 35 | } catch (ClassCastException e) { |
| 36 | throw new XPathException("Instantiating " + className + " produced an instance of incompatible class " + o.getClass()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | } |
| 41 | |
| 42 | // Copyright (c) 2015 Saxonica Ltd |