| 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.lib; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.event.Receiver; |
| 12 | import net.sf.saxon.expr.JPConverter; |
| 13 | import net.sf.saxon.expr.PJConverter; |
| 14 | import net.sf.saxon.om.NodeInfo; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | |
| 17 | import javax.xml.transform.Result; |
| 18 | import javax.xml.transform.Source; |
| 19 | |
| 20 | /** |
| 21 | * This interface must be implemented by any third-party object model that can |
| 22 | * be wrapped with a wrapper that implements the Saxon Object Model (the NodeInfo interface). |
| 23 | * <p>This interface is designed to enable advanced applications to implement and register |
| 24 | * new object model implementations that Saxon can then use without change. Although it is intended |
| 25 | * for external use, it cannot at this stage be considered part of the stable Saxon Public API. |
| 26 | * In particular, it is likely that the interface will grow by the addition of new methods.</p> |
| 27 | * <p>For maximum integration, an object may extend {@link net.sf.saxon.om.TreeModel} as well as implementing |
| 28 | * this interface. To implement <code>TreeModel</code>, it must supply a Builder; in effect this |
| 29 | * means that it will be possible to use the external object model for output as well as for |
| 30 | * input.</p> |
| 31 | */ |
| 32 | |
| 33 | public interface ExternalObjectModel { |
| 34 | |
| 35 | /** |
| 36 | * Get the name of a characteristic class, which, if it can be loaded, indicates that the supporting libraries |
| 37 | * for this object model implementation are available on the classpath |
| 38 | * @return by convention (but not necessarily) the class that implements a document node in the relevant |
| 39 | * external model |
| 40 | */ |
| 41 | |
| 42 | String getDocumentClassName(); |
| 43 | |
| 44 | /** |
| 45 | * Get the URI of the external object model as used in the JAXP factory interfaces for obtaining |
| 46 | * an XPath implementation |
| 47 | * |
| 48 | * @return the URI used to identify this object model in the JAXP XPath factory mechanism. |
| 49 | */ |
| 50 | |
| 51 | String getIdentifyingURI(); |
| 52 | |
| 53 | /** |
| 54 | * Get a converter from XPath values to values in the external object model |
| 55 | * |
| 56 | * @param targetClass the required class of the result of the conversion. If this class represents |
| 57 | * a node or list of nodes in the external object model, the method should return a converter that takes |
| 58 | * a native node or sequence of nodes as input and returns a node or sequence of nodes in the |
| 59 | * external object model representation. Otherwise, it should return null. |
| 60 | * @return a converter, if the targetClass is recognized as belonging to this object model; |
| 61 | * otherwise null |
| 62 | */ |
| 63 | |
| 64 | PJConverter getPJConverter(Class<?> targetClass); |
| 65 | |
| 66 | /** |
| 67 | * Get a converter from values in the external object model to XPath values. |
| 68 | * |
| 69 | * @param sourceClass the class (static or dynamic) of values to be converted |
| 70 | * @param config |
| 71 | * @return a converter, if the sourceClass is recognized as belonging to this object model; |
| 72 | * otherwise null |
| 73 | */ |
| 74 | |
| 75 | JPConverter getJPConverter(Class sourceClass, Configuration config); |
| 76 | |
| 77 | /** |
| 78 | * Get a converter that converts a sequence of XPath nodes to this model's representation |
| 79 | * of a node list. |
| 80 | * <p><i>This method is primarily for the benefit of DOM, which uses its own NodeList |
| 81 | * class to represent collections of nodes. Most other object models use standard |
| 82 | * Java collection objects such as java.util.List</i></p> |
| 83 | * |
| 84 | * @param node an example of the kind of node used in this model |
| 85 | * @return if the model does not recognize this node as one of its own, return null. Otherwise |
| 86 | * return a PJConverter that takes a list of XPath nodes (represented as NodeInfo objects) and |
| 87 | * returns a collection of nodes in this object model |
| 88 | */ |
| 89 | |
| 90 | PJConverter getNodeListCreator(Object node); |
| 91 | |
| 92 | /** |
| 93 | * Test whether this object model recognizes a particular kind of JAXP Result object, |
| 94 | * and if it does, return a Receiver that builds an instance of this data model from |
| 95 | * a sequence of events. If the Result is not recognised, return null. |
| 96 | * |
| 97 | * @param result a JAXP result object |
| 98 | * @return a Receiver that writes to that result, if available; or null otherwise |
| 99 | * @throws net.sf.saxon.trans.XPathException |
| 100 | * if any failure occurs |
| 101 | */ |
| 102 | |
| 103 | Receiver getDocumentBuilder(Result result) throws XPathException; |
| 104 | |
| 105 | /** |
| 106 | * Test whether this object model recognizes a particular kind of JAXP Source object, |
| 107 | * and if it does, send the contents of the document to a supplied Receiver, and return true. |
| 108 | * Otherwise, return false. |
| 109 | * |
| 110 | * @param source a JAXP Source object |
| 111 | * @param receiver the Receiver that is to receive the data from the Source. The caller |
| 112 | * is responsible for opening and closing the receiver. |
| 113 | * @return true if the data from the Source has been sent to the Receiver, false otherwise |
| 114 | * @throws net.sf.saxon.trans.XPathException |
| 115 | * if any failure occurs |
| 116 | */ |
| 117 | |
| 118 | boolean sendSource(Source source, Receiver receiver) throws XPathException; |
| 119 | |
| 120 | /** |
| 121 | * Wrap or unwrap a node using this object model to return the corresponding Saxon node. If the supplied |
| 122 | * source does not belong to this object model, return null |
| 123 | * |
| 124 | * @param source a JAXP Source object |
| 125 | * @param config the Saxon configuration |
| 126 | * @return a NodeInfo corresponding to the Source, if this can be constructed; otherwise null |
| 127 | */ |
| 128 | |
| 129 | NodeInfo unravel(Source source, Configuration config); |
| 130 | |
| 131 | } |
| 132 |