KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Parse


1 package net.sf.saxon.functions;
2 import net.sf.saxon.Controller;
3 import net.sf.saxon.event.Builder;
4 import net.sf.saxon.event.Receiver;
5 import net.sf.saxon.event.Sender;
6 import net.sf.saxon.expr.StaticContext;
7 import net.sf.saxon.expr.XPathContext;
8 import net.sf.saxon.om.DocumentInfo;
9 import net.sf.saxon.om.Item;
10 import net.sf.saxon.trans.DynamicError;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.value.AtomicValue;
13 import org.xml.sax.InputSource JavaDoc;
14
15 import javax.xml.transform.sax.SAXSource JavaDoc;
16 import java.io.StringReader JavaDoc;
17
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 JavaDoc 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 JavaDoc sr = new StringReader JavaDoc(content.getStringValue());
49         InputSource JavaDoc is = new InputSource JavaDoc(sr);
50         is.setSystemId(baseURI);
51         SAXSource JavaDoc source = new SAXSource JavaDoc(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(source, s);
60             return (DocumentInfo)b.getCurrentRoot();
61         } catch (XPathException err) {
62             throw new DynamicError(err);
63         }
64     }
65
66 }
67
68
69
70
71 //
72
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
73
// you may not use this file except in compliance with the License. You may obtain a copy of the
74
// License at http://www.mozilla.org/MPL/
75
//
76
// Software distributed under the License is distributed on an "AS IS" basis,
77
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
78
// See the License for the specific language governing rights and limitations under the License.
79
//
80
// The Original Code is: all this file.
81
//
82
// The Initial Developer of the Original Code is Michael H. Kay.
83
//
84
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
85
//
86
// Contributor(s): none.
87
//
88
Popular Tags