001: package net.sf.saxon.xpath;
002:
003: import net.sf.saxon.Configuration;
004: import net.sf.saxon.FeatureKeys;
005: import net.sf.saxon.om.NamespaceConstant;
006: import net.sf.saxon.om.Validation;
007:
008: import javax.xml.XMLConstants;
009: import javax.xml.xpath.*;
010:
011: /**
012: * Saxon implementation of the JAXP 1.3 XPathFactory
013: */
014: public class XPathFactoryImpl extends XPathFactory {
015:
016: private Configuration config;
017: private XPathVariableResolver variableResolver;
018: private XPathFunctionResolver functionResolver;
019:
020: public XPathFactoryImpl() {
021: config = makeConfiguration();
022: }
023:
024: protected Configuration makeConfiguration() {
025: return new Configuration();
026: }
027:
028: /**
029: * Get the Configuration object
030: */
031:
032: public Configuration getConfiguration() {
033: return config;
034: }
035:
036: /**
037: * Test whether a given object model is supported. Returns true if the object model
038: * is the Saxon object model, DOM, JDOM, or XOM
039: * @param model The URI identifying the object model.
040: * @return true if the object model is one of
041: * {@link NamespaceConstant#OBJECT_MODEL_SAXON},
042: * {@link XPathConstants#DOM_OBJECT_MODEL},
043: * {@link NamespaceConstant#OBJECT_MODEL_JDOM}, or
044: * {@link NamespaceConstant#OBJECT_MODEL_XOM}
045: */
046: public boolean isObjectModelSupported(String model) {
047: if (model.equals(NamespaceConstant.OBJECT_MODEL_SAXON))
048: return true;
049: if (model.equals(XPathConstants.DOM_OBJECT_MODEL))
050: return true;
051: if (model.equals(NamespaceConstant.OBJECT_MODEL_JDOM))
052: return true;
053: if (model.equals(NamespaceConstant.OBJECT_MODEL_XOM))
054: return true;
055: return false;
056: }
057:
058: /**
059: * Set a feature of this XPath implementation. The only features currently
060: * recognized are:
061: * <ul>
062: * <li> {@link XMLConstants#FEATURE_SECURE_PROCESSING} </li>
063: * <li> {@link net.sf.saxon.FeatureKeys#SCHEMA_VALIDATION}: requests schema validation of source documents.
064: * The property is rejected if the configuration is not schema-aware. </li>
065: * </ul>
066: * @param feature a URI identifying the feature
067: * @param b true to set the feature on, false to set it off
068: * @throws XPathFactoryConfigurationException if the feature name is not recognized
069: */
070:
071: public void setFeature(String feature, boolean b)
072: throws XPathFactoryConfigurationException {
073: if (feature.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
074: config.setAllowExternalFunctions(!b);
075: } else if (feature.equals(FeatureKeys.SCHEMA_VALIDATION)) {
076: config.setSchemaValidationMode(b ? Validation.STRICT
077: : Validation.STRIP);
078: } else {
079: throw new XPathFactoryConfigurationException(
080: "Unknown feature: " + feature);
081: }
082: }
083:
084: /**
085: * Get a feature of this XPath implementation. The only features currently
086: * recognized are:
087: * <ul>
088: * <li> {@link XMLConstants#FEATURE_SECURE_PROCESSING} </li>
089: * <li> {@link net.sf.saxon.FeatureKeys#SCHEMA_VALIDATION}: requests schema validation of source documents. </li>
090: * </ul>
091: * @param feature a URI identifying the feature
092: * @return true if the feature is on, false if it is off
093: * @throws XPathFactoryConfigurationException if the feature name is not recognized
094: */
095:
096: public boolean getFeature(String feature)
097: throws XPathFactoryConfigurationException {
098: if (feature.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
099: return !config.isAllowExternalFunctions();
100: } else if (feature.equals(FeatureKeys.SCHEMA_VALIDATION)) {
101: return config.getSchemaValidationMode() == Validation.STRICT;
102: } else {
103: throw new XPathFactoryConfigurationException(
104: "Unknown feature: " + feature);
105: }
106: }
107:
108: /**
109: * Set a resolver for XPath variables. This will be used to obtain the value of
110: * any variable referenced in an XPath expression. The variable resolver must be allocated
111: * before the expression is compiled, but it will only be called when the expression
112: * is evaluated.
113: * @param xPathVariableResolver The object used to resolve references to variables.
114: */
115: public void setXPathVariableResolver(
116: XPathVariableResolver xPathVariableResolver) {
117: variableResolver = xPathVariableResolver;
118: }
119:
120: /**
121: * Set a resolver for XPath functions. This will be used to obtain an implementation
122: * of any external function referenced in an XPath expression. This is not required for
123: * system functions, Saxon extension functions, constructor functions named after types,
124: * or extension functions bound using a namespace that maps to a Java class.
125: * @param xPathFunctionResolver The object used to resolve references to external functions.
126: */
127:
128: public void setXPathFunctionResolver(
129: XPathFunctionResolver xPathFunctionResolver) {
130: functionResolver = xPathFunctionResolver;
131: }
132:
133: /**
134: * Create an XPath evaluator
135: * @return an XPath object, which can be used to compile and execute XPath expressions.
136: */
137: public XPath newXPath() {
138: XPathEvaluator xpath = new XPathEvaluator(config);
139: xpath.setXPathFunctionResolver(functionResolver);
140: xpath.setXPathVariableResolver(variableResolver);
141: //config.registerStandardObjectModels();
142: return xpath;
143: }
144:
145: }
146:
147: //
148: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
149: // you may not use this file except in compliance with the License. You may obtain a copy of the
150: // License at http://www.mozilla.org/MPL/
151: //
152: // Software distributed under the License is distributed on an "AS IS" basis,
153: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
154: // See the License for the specific language governing rights and limitations under the License.
155: //
156: // The Original Code is: all this file.
157: //
158: // The Initial Developer of the Original Code is Michael H. Kay.
159: //
160: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
161: //
162: // Contributor(s):
163: //
|