| 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.s9api; |
| 9 | |
| 10 | import java.net.URI; |
| 11 | import java.util.function.Consumer; |
| 12 | |
| 13 | /** |
| 14 | * An abstract class providing reusable code for implementing the {@link Destination} interface} |
| 15 | */ |
| 16 | abstract public class AbstractDestination implements Destination { |
| 17 | |
| 18 | protected DestinationHelper helper = new DestinationHelper(this); |
| 19 | private URI baseURI; |
| 20 | |
| 21 | /** |
| 22 | * Set the base URI of the resource being written to this destination |
| 23 | * |
| 24 | * @param baseURI the base URI to be used |
| 25 | */ |
| 26 | |
| 27 | public void setDestinationBaseURI(URI baseURI) { |
| 28 | this.baseURI = baseURI; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get the base URI of the resource being written to this destination |
| 33 | * |
| 34 | * @return the baseURI, or null if none is known |
| 35 | */ |
| 36 | |
| 37 | public URI getDestinationBaseURI() { |
| 38 | return baseURI; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Register a listener to be notified when this destination is closed |
| 43 | * @param listener an object to be notified when writing to the destination |
| 44 | * is successfully completed |
| 45 | */ |
| 46 | |
| 47 | final public void onClose(Action listener) { |
| 48 | helper.onClose(listener); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Close the destination and notify all registered listeners that it has been closed. |
| 53 | * This method is intended for internal use by Saxon. The method first calls {@link #close} |
| 54 | * to close the destination, then it calls {@link Consumer#accept} on each of the |
| 55 | * listeners in turn to notify the fact that it has been closed. |
| 56 | * @throws SaxonApiException if the close() method throws {@code SaxonApiException}. |
| 57 | */ |
| 58 | |
| 59 | public void closeAndNotify() throws SaxonApiException { |
| 60 | helper.closeAndNotify(); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 |