001: package net.sf.saxon.expr;
002:
003: import net.sf.saxon.type.AtomicType;
004: import net.sf.saxon.type.TypeHierarchy;
005: import net.sf.saxon.trans.XPathException;
006: import net.sf.saxon.trans.StaticError;
007: import net.sf.saxon.Configuration;
008: import net.sf.saxon.om.SequenceIterator;
009: import net.sf.saxon.om.ValueRepresentation;
010: import net.sf.saxon.value.Closure;
011: import net.sf.saxon.value.MemoClosure;
012:
013: import java.io.Serializable;
014:
015: /**
016: * This class doesn't actually do any optimization itself, despite the name. Rather, it is
017: * intended to act as a factory for implementation classes that perform optimization, so that
018: * the appropriate level of optimization can be selected.
019: */
020: public class Optimizer implements Serializable {
021:
022: protected Configuration config;
023:
024: public Optimizer(Configuration config) {
025: this .config = config;
026: }
027:
028: public Configuration getConfiguration() {
029: return config;
030: }
031:
032: /**
033: * Create a GeneralComparison expression
034: */
035:
036: public BinaryExpression makeGeneralComparison(Expression p0,
037: int op, Expression p1, boolean backwardsCompatible) {
038: if (backwardsCompatible) {
039: return new GeneralComparison10(p0, op, p1);
040: } else {
041: return new GeneralComparison(p0, op, p1);
042: }
043: }
044:
045: /**
046: * Attempt to optimize a copy operation. Return null if no optimization is possible.
047: * @param select the expression that selects the items to be copied
048: * @return null if no optimization is possible, or an expression that does an optimized
049: * copy of these items otherwise
050: */
051:
052: public Expression optimizeCopy(Expression select)
053: throws XPathException {
054: final TypeHierarchy th = config.getNamePool()
055: .getTypeHierarchy();
056: if (select.getItemType(th) instanceof AtomicType) {
057: return select;
058: }
059: return null;
060: }
061:
062: /**
063: * Make a Closure, given the expected reference count
064: */
065:
066: public Closure makeClosure(Expression expression, int ref) {
067: if (ref == 1) {
068: return new Closure();
069: } else {
070: return new MemoClosure();
071: }
072: }
073:
074: /**
075: * Examine a path expression to see whether it can be replaced by a call on the key() function;
076: * if so, generate an appropriate key definition and return the call on key(). If not, return null.
077: * @param pathExp The path expression to be converted.
078: */
079:
080: public Expression convertPathExpressionToKey(
081: PathExpression pathExp, StaticContext env)
082: throws XPathException {
083: return null;
084: }
085:
086: /**
087: * Convert a path expression such as a/b/c[predicate] into a filter expression
088: * of the form (a/b/c)[predicate]. This is possible whenever the predicate is non-positional.
089: * The conversion is useful in the case where the path expression appears inside a loop,
090: * where the predicate depends on the loop variable but a/b/c does not.
091: * @param pathExp the path expression to be converted
092: * @return the resulting filterexpression if conversion is possible, or null if not
093: */
094:
095: public FilterExpression convertToFilterExpression(
096: PathExpression pathExp, TypeHierarchy th)
097: throws StaticError {
098: return null;
099: }
100:
101: public SequenceIterator tryIndexedFilter(
102: ValueRepresentation startValue, Expression filter,
103: int isIndexable, XPathContext context)
104: throws XPathException {
105: return null;
106: }
107:
108: /**
109: * Test whether a filter predicate is indexable.
110: * @param filter the predicate expression
111: * @return 0 if not indexable; +1 if the predicate is in the form expression=value; -1 if it is in
112: * the form value=expression
113: */
114:
115: public int isIndexableFilter(Expression filter) {
116: return 0;
117: }
118: }
119:
120: //
121: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
122: // you may not use this file except in compliance with the License. You may obtain a copy of the
123: // License at http://www.mozilla.org/MPL/
124: //
125: // Software distributed under the License is distributed on an "AS IS" basis,
126: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
127: // See the License for the specific language governing rights and limitations under the License.
128: //
129: // The Original Code is: all this file.
130: //
131: // The Initial Developer of the Original Code is Michael H. Kay.
132: //
133: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
134: //
135: // Contributor(s): none.
136: //
|