| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Controller; |
| 12 |
import net.sf.saxon.expr.*; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.functions.registry.BuiltInFunctionSet; |
| 16 |
import net.sf.saxon.lib.Feature; |
| 17 |
import net.sf.saxon.lib.ParseOptions; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.trans.XsltController; |
| 21 |
import net.sf.saxon.value.AtomicValue; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class Doc extends SystemFunction implements Callable { |
| 28 |
|
| 29 |
private ParseOptions parseOptions; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public ParseOptions getParseOptions() { |
| 37 |
return parseOptions; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public void setParseOptions(ParseOptions parseOptions) { |
| 47 |
this.parseOptions = parseOptions; |
| 48 |
} |
| 49 |
|
| 50 |
public int getCardinality(Expression[] arguments) { |
| 51 |
return arguments[0].getCardinality() & ~StaticProperty.ALLOWS_MANY; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public Expression makeFunctionCall(Expression... arguments) { |
| 56 |
Expression expr = maybePreEvaluate(this, arguments); |
| 57 |
return expr == null ? super.makeFunctionCall(arguments) : expr; |
| 58 |
} |
| 59 |
|
| 60 |
public static Expression maybePreEvaluate(final SystemFunction sf, final Expression[] arguments) { |
| 61 |
if (arguments.length > 1 || |
| 62 |
!sf.getRetainedStaticContext().getConfiguration().getBooleanProperty(Feature.PRE_EVALUATE_DOC_FUNCTION)) { |
| 63 |
sf.getDetails().properties = sf.getDetails().properties | BuiltInFunctionSet.LATE; |
| 64 |
return null; |
| 65 |
|
| 66 |
} else { |
| 67 |
|
| 68 |
return new SystemFunctionCall(sf, arguments) { |
| 69 |
@Override |
| 70 |
public Expression preEvaluate(ExpressionVisitor visitor) { |
| 71 |
Configuration config = visitor.getConfiguration(); |
| 72 |
try { |
| 73 |
GroundedValue firstArg = ((Literal)getArg(0)).getValue(); |
| 74 |
if (firstArg.getLength() == 0) { |
| 75 |
return null; |
| 76 |
} else if (firstArg.getLength() > 1) { |
| 77 |
return this; |
| 78 |
} |
| 79 |
String href = firstArg.head().getStringValue(); |
| 80 |
if (href.indexOf('#') >= 0) { |
| 81 |
return this; |
| 82 |
} |
| 83 |
NodeInfo item = DocumentFn.preLoadDoc(href, sf.getStaticBaseUriString(), config, getLocation()); |
| 84 |
if (item != null) { |
| 85 |
Expression constant = Literal.makeLiteral(item); |
| 86 |
ExpressionTool.copyLocationInfo(getArg(0), constant); |
| 87 |
return constant; |
| 88 |
} |
| 89 |
} catch (Exception err) { |
| 90 |
|
| 91 |
return this; |
| 92 |
} |
| 93 |
return this; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public ZeroOrOne<NodeInfo> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 113 |
AtomicValue hrefVal = (AtomicValue) arguments[0].head(); |
| 114 |
if (hrefVal == null) { |
| 115 |
return ZeroOrOne.empty(); |
| 116 |
} |
| 117 |
String href = hrefVal.getStringValue(); |
| 118 |
PackageData packageData = getRetainedStaticContext().getPackageData(); |
| 119 |
NodeInfo item = DocumentFn.makeDoc(href, getRetainedStaticContext().getStaticBaseUriString(), packageData, getParseOptions(), context, null, false); |
| 120 |
if (item == null) { |
| 121 |
|
| 122 |
throw new XPathException("Failed to load document " + href, "FODC0002", context); |
| 123 |
} |
| 124 |
Controller controller = context.getController(); |
| 125 |
if (parseOptions != null && controller instanceof XsltController) { |
| 126 |
((XsltController)controller).getAccumulatorManager().setApplicableAccumulators( |
| 127 |
item.getTreeInfo(), parseOptions.getApplicableAccumulators() |
| 128 |
); |
| 129 |
} |
| 130 |
return new ZeroOrOne<>(item); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public int getSpecialProperties(Expression[] arguments) { |
| 141 |
return StaticProperty.ORDERED_NODESET | |
| 142 |
StaticProperty.PEER_NODESET | |
| 143 |
StaticProperty.NO_NODES_NEWLY_CREATED | |
| 144 |
StaticProperty.SINGLE_DOCUMENT_NODESET; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
} |
| 153 |
|