01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.Controller;
04: import net.sf.saxon.event.Builder;
05: import net.sf.saxon.event.Receiver;
06: import net.sf.saxon.event.Sender;
07: import net.sf.saxon.expr.StaticContext;
08: import net.sf.saxon.expr.XPathContext;
09: import net.sf.saxon.om.DocumentInfo;
10: import net.sf.saxon.om.Item;
11: import net.sf.saxon.trans.DynamicError;
12: import net.sf.saxon.trans.XPathException;
13: import net.sf.saxon.value.AtomicValue;
14: import org.xml.sax.InputSource;
15:
16: import javax.xml.transform.sax.SAXSource;
17: import java.io.StringReader;
18:
19: /**
20: * This class implements the saxon:parse() extension function,
21: * which is specially-recognized by the system because it needs access
22: * to parts of the static context
23: */
24:
25: public class Parse extends SystemFunction {
26:
27: String baseURI;
28:
29: /**
30: * Method supplied by each class of function to check arguments during parsing, when all
31: * the argument expressions have been read
32: */
33:
34: public void checkArguments(StaticContext env) throws XPathException {
35: if (baseURI == null) {
36: super .checkArguments(env);
37: baseURI = env.getBaseURI();
38: }
39: }
40:
41: /**
42: * Evaluate in a general context
43: */
44:
45: public Item evaluateItem(XPathContext c) throws XPathException {
46: Controller controller = c.getController();
47: AtomicValue content = (AtomicValue) argument[0].evaluateItem(c);
48: StringReader sr = new StringReader(content.getStringValue());
49: InputSource is = new InputSource(sr);
50: is.setSystemId(baseURI);
51: SAXSource source = new SAXSource(is);
52: source.setSystemId(baseURI);
53: Builder b = controller.makeBuilder();
54: Receiver s = controller.makeStripper(b);
55: if (controller.getExecutable().stripsInputTypeAnnotations()) {
56: s = controller.getConfiguration().getAnnotationStripper(s);
57: }
58: try {
59: new Sender(controller.makePipelineConfiguration()).send(
60: source, s);
61: return (DocumentInfo) b.getCurrentRoot();
62: } catch (XPathException err) {
63: throw new DynamicError(err);
64: }
65: }
66:
67: }
68:
69: //
70: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
71: // you may not use this file except in compliance with the License. You may obtain a copy of the
72: // License at http://www.mozilla.org/MPL/
73: //
74: // Software distributed under the License is distributed on an "AS IS" basis,
75: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
76: // See the License for the specific language governing rights and limitations under the License.
77: //
78: // The Original Code is: all this file.
79: //
80: // The Initial Developer of the Original Code is Michael H. Kay.
81: //
82: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
83: //
84: // Contributor(s): none.
85: //
|