| 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.xpath; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.Version; |
| 12 | import net.sf.saxon.lib.Feature; |
| 13 | import net.sf.saxon.lib.FeatureKeys; |
| 14 | import net.sf.saxon.lib.NamespaceConstant; |
| 15 | import net.sf.saxon.lib.Validation; |
| 16 | |
| 17 | import javax.xml.XMLConstants; |
| 18 | import javax.xml.xpath.*; |
| 19 | |
| 20 | /** |
| 21 | * Saxon implementation of the JAXP 1.3 XPathFactory |
| 22 | */ |
| 23 | public class XPathFactoryImpl extends XPathFactory implements Configuration.ApiProvider { |
| 24 | |
| 25 | private Configuration config; |
| 26 | private XPathVariableResolver variableResolver; |
| 27 | private XPathFunctionResolver functionResolver; |
| 28 | |
| 29 | /** |
| 30 | * Default constructor: this creates a Configuration as well as creating the XPathFactory. Any documents |
| 31 | * accessed using this XPathFactory must be built using this same Configuration. |
| 32 | */ |
| 33 | |
| 34 | public XPathFactoryImpl() { |
| 35 | config = Configuration.newConfiguration(); |
| 36 | setConfiguration(config); |
| 37 | Version.platform.registerAllBuiltInObjectModels(config); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Constructor using a user-supplied Configuration. |
| 42 | * This constructor is useful if the document to be queried already exists, as it allows the configuration |
| 43 | * associated with the document to be used with this XPathFactory. |
| 44 | * |
| 45 | * @param config the Saxon configuration |
| 46 | */ |
| 47 | |
| 48 | public XPathFactoryImpl(Configuration config) { |
| 49 | this.config = config; |
| 50 | config.setProcessor(this); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Set the Configuration for the factory |
| 55 | * |
| 56 | * @param config the Saxon Configuration to be used |
| 57 | */ |
| 58 | |
| 59 | public void setConfiguration(Configuration config) { |
| 60 | this.config = config; |
| 61 | config.setProcessor(this); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get the Configuration object used by this XPathFactory |
| 66 | * |
| 67 | * @return the Saxon configuration |
| 68 | */ |
| 69 | |
| 70 | public Configuration getConfiguration() { |
| 71 | return config; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Test whether a given object model is supported. Returns true if the object model |
| 76 | * is the Saxon object model, DOM, JDOM, DOM4J, or XOM |
| 77 | * |
| 78 | * @param model The URI identifying the object model. |
| 79 | * @return true if the object model is one of the following (provided that the supporting |
| 80 | * JAR file is available on the classpath) |
| 81 | * {@link net.sf.saxon.lib.NamespaceConstant#OBJECT_MODEL_SAXON}, |
| 82 | * {@link XPathConstants#DOM_OBJECT_MODEL}, |
| 83 | * {@link net.sf.saxon.lib.NamespaceConstant#OBJECT_MODEL_JDOM}, or |
| 84 | * {@link NamespaceConstant#OBJECT_MODEL_XOM}, or |
| 85 | * {@link net.sf.saxon.lib.NamespaceConstant#OBJECT_MODEL_DOM4J}. |
| 86 | * Saxon also allows user-defined external object models to be registered with the Configuration, and |
| 87 | * this method will return true in respect of any such model. |
| 88 | */ |
| 89 | public boolean isObjectModelSupported(String model) { |
| 90 | boolean debug = System.getProperty("jaxp.debug") != null; |
| 91 | boolean result = silentIsObjectModelSupported(model); |
| 92 | if (debug) { |
| 93 | System.err.println("JAXP: Calling " + getClass().getName() + ".isObjectModelSupported(\"" + model + "\")"); |
| 94 | System.err.println("JAXP: -- returning " + (result ? "true" : "false (check all required libraries are on the class path")); |
| 95 | } |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | private boolean silentIsObjectModelSupported(String model) { |
| 100 | return model.equals(NamespaceConstant.OBJECT_MODEL_SAXON) || config.getExternalObjectModel(model) != null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Set a feature of this XPath implementation. The features currently |
| 105 | * recognized are: |
| 106 | * <ul> |
| 107 | * <li> {@link XMLConstants#FEATURE_SECURE_PROCESSING} </li> |
| 108 | * <li> {@link net.sf.saxon.lib.FeatureKeys#SCHEMA_VALIDATION}: requests schema validation of source documents. |
| 109 | * The property is rejected if the configuration is not schema-aware. </li> |
| 110 | * </ul> |
| 111 | * <p>In addition, any Saxon configuration feature (listed in {@link FeatureKeys} can be used |
| 112 | * provided the value is a boolean. (For non-boolean configuration properties, drop down to the underlying |
| 113 | * Saxon {@link Configuration} object and call <code>setConfigurationProperty()</code>)</p> |
| 114 | * |
| 115 | * @param feature a URI identifying the feature |
| 116 | * @param b true to set the feature on, false to set it off |
| 117 | * @throws XPathFactoryConfigurationException |
| 118 | * if the feature name is not recognized |
| 119 | */ |
| 120 | |
| 121 | public void setFeature(String feature, boolean b) throws XPathFactoryConfigurationException { |
| 122 | if (feature.equals(FEATURE_SECURE_PROCESSING)) { |
| 123 | config.setBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS, !b); |
| 124 | } else if (feature.equals(FeatureKeys.SCHEMA_VALIDATION)) { |
| 125 | config.setSchemaValidationMode(b ? Validation.STRICT : Validation.STRIP); |
| 126 | } else { |
| 127 | try { |
| 128 | config.setBooleanProperty(feature, b); |
| 129 | } catch (IllegalArgumentException err) { |
| 130 | throw new XPathFactoryConfigurationException("Unknown or non-boolean feature: " + feature); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get a feature of this XPath implementation. The only features currently |
| 137 | * recognized are: |
| 138 | * <ul> |
| 139 | * <li> {@link #FEATURE_SECURE_PROCESSING} </li> |
| 140 | * <li> {@link net.sf.saxon.lib.FeatureKeys#SCHEMA_VALIDATION}: requests schema validation of source documents. </li> |
| 141 | * </ul> |
| 142 | * <p>In addition, any Saxon configuration feature (listed in {@link FeatureKeys} can be used |
| 143 | * provided the value is a boolean. (For non-boolean configuration properties, drop down to the underlying |
| 144 | * Saxon {@link Configuration} object and call <code>getConfigurationProperty()</code>)</p> |
| 145 | * |
| 146 | * @param feature a URI identifying the feature |
| 147 | * @return true if the feature is on, false if it is off |
| 148 | * @throws XPathFactoryConfigurationException |
| 149 | * if the feature name is not recognized |
| 150 | */ |
| 151 | |
| 152 | public boolean getFeature(String feature) throws XPathFactoryConfigurationException { |
| 153 | if (feature.equals(FEATURE_SECURE_PROCESSING)) { |
| 154 | return !config.getBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS); |
| 155 | } else if (feature.equals(FeatureKeys.SCHEMA_VALIDATION)) { |
| 156 | return config.getSchemaValidationMode() == Validation.STRICT; |
| 157 | } else { |
| 158 | try { |
| 159 | Object o = config.getConfigurationProperty(feature); |
| 160 | if (o instanceof Boolean) { |
| 161 | return (Boolean) o; |
| 162 | } else { |
| 163 | throw new XPathFactoryConfigurationException( |
| 164 | "Configuration property " + feature + " is not a boolean (it is an instance of " + o.getClass() + ")"); |
| 165 | } |
| 166 | } catch (IllegalArgumentException e) { |
| 167 | throw new XPathFactoryConfigurationException("Unknown feature: " + feature); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Set a resolver for XPath variables. This will be used to obtain the value of |
| 174 | * any variable referenced in an XPath expression. The variable resolver must be allocated |
| 175 | * before the expression is compiled, but it will only be called when the expression |
| 176 | * is evaluated. |
| 177 | * |
| 178 | * @param xPathVariableResolver The object used to resolve references to variables. |
| 179 | */ |
| 180 | public void setXPathVariableResolver(XPathVariableResolver xPathVariableResolver) { |
| 181 | variableResolver = xPathVariableResolver; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Set a resolver for XPath functions. This will be used to obtain an implementation |
| 186 | * of any external function referenced in an XPath expression. This is not required for |
| 187 | * system functions, Saxon extension functions, constructor functions named after types, |
| 188 | * or extension functions bound using a namespace that maps to a Java class. |
| 189 | * |
| 190 | * @param xPathFunctionResolver The object used to resolve references to external functions. |
| 191 | */ |
| 192 | |
| 193 | public void setXPathFunctionResolver(XPathFunctionResolver xPathFunctionResolver) { |
| 194 | functionResolver = xPathFunctionResolver; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Create an XPath evaluator |
| 199 | * |
| 200 | * @return an XPath object, which can be used to compile and execute XPath expressions. |
| 201 | */ |
| 202 | /*@NotNull*/ |
| 203 | public XPath newXPath() { |
| 204 | XPathEvaluator xpath = new XPathEvaluator(config); |
| 205 | xpath.setXPathFunctionResolver(functionResolver); |
| 206 | xpath.setXPathVariableResolver(variableResolver); |
| 207 | return xpath; |
| 208 | } |
| 209 | |
| 210 | private static String FEATURE_SECURE_PROCESSING = XMLConstants.FEATURE_SECURE_PROCESSING; |
| 211 | // "http://javax.xml.XMLConstants/feature/secure-processing"; |
| 212 | |
| 213 | } |
| 214 |