KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2 import net.sf.saxon.Controller;
3 import net.sf.saxon.event.Receiver;
4 import net.sf.saxon.expr.Expression;
5 import net.sf.saxon.expr.StaticContext;
6 import net.sf.saxon.expr.StaticProperty;
7 import net.sf.saxon.expr.XPathContext;
8 import net.sf.saxon.om.Item;
9 import net.sf.saxon.trans.XPathException;
10 import net.sf.saxon.value.AtomicValue;
11 import net.sf.saxon.value.BooleanValue;
12
13 import javax.xml.transform.ErrorListener JavaDoc;
14 import javax.xml.transform.TransformerException JavaDoc;
15
16 /**
17  * Implement the fn:doc() function - a simplified form of the Document function
18  */

19
20 public class Doc extends SystemFunction {
21
22     public static final int DOC = 0;
23     public static final int DOC_AVAILABLE = 1;
24
25     private String JavaDoc expressionBaseURI = null;
26
27     public void checkArguments(StaticContext env) throws XPathException {
28         if (expressionBaseURI == null) {
29             super.checkArguments(env);
30             expressionBaseURI = env.getBaseURI();
31         }
32     }
33
34     /**
35     * preEvaluate: this method suppresses compile-time evaluation by doing nothing
36     */

37
38     public Expression preEvaluate(StaticContext env) {
39         return this;
40     }
41
42     public Item evaluateItem(XPathContext context) throws XPathException {
43         if (operation == DOC) {
44             return doc(context);
45         } else {
46             // operation == DOC_AVAILABLE
47
try {
48                 Controller controller = context.getController();
49                 // suppress all error messages while attempting to fetch the document
50
ErrorListener JavaDoc old = controller.getErrorListener();
51                 controller.setErrorListener(new ErrorListener JavaDoc() {
52                     public void warning(TransformerException JavaDoc exception) {}
53                     public void error(TransformerException JavaDoc exception) {}
54                     public void fatalError(TransformerException JavaDoc exception) {}
55                 });
56                 Item item = doc(context);
57                 controller.setErrorListener(old);
58                 return BooleanValue.get(item != null);
59             } catch (XPathException err) {
60                 return BooleanValue.FALSE;
61             }
62         }
63     }
64
65     /**
66     * Get the static properties of this expression (other than its type). The result is
67     * bit-signficant. These properties are used for optimizations. In general, if
68     * property bit is set, it is true, but if it is unset, the value is unknown.
69     */

70
71     public int computeSpecialProperties() {
72         return StaticProperty.ORDERED_NODESET |
73                 StaticProperty.PEER_NODESET |
74                 StaticProperty.NON_CREATIVE;
75         // Declaring it as a peer node-set expression avoids sorting of expressions such as
76
// doc(XXX)/a/b/c
77
// The doc() function might appear to be creative: but it isn't, because multiple calls
78
// with the same arguments will produce identical results.
79
}
80
81     private Item doc(XPathContext context) throws XPathException {
82         AtomicValue hrefVal = (AtomicValue)argument[0].evaluateItem(context);
83         if (hrefVal==null) {
84             return null;
85         }
86         String JavaDoc href = hrefVal.getStringValue();
87         Item item = Document.makeDoc(href, expressionBaseURI, context, this);
88         if (item==null) {
89             // we failed to read the document
90
dynamicError("Failed to load document " + href, "FODC0005", context);
91             return null;
92         }
93         return item;
94     }
95
96     /**
97      * Copy the document identified by this expression to a given Receiver. This method is used only when it is
98      * known that the document is being copied, because there is then no problem about node identity.
99      */

100
101     public void sendDocument(XPathContext context, Receiver out) throws XPathException {
102         AtomicValue hrefVal = (AtomicValue)argument[0].evaluateItem(context);
103         if (hrefVal==null) {
104             return;
105         }
106         String JavaDoc href = hrefVal.getStringValue();
107         Document.sendDoc(href, expressionBaseURI, context, this, out);
108     }
109     
110 }
111
112 //
113
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
114
// you may not use this file except in compliance with the License. You may obtain a copy of the
115
// License at http://www.mozilla.org/MPL/
116
//
117
// Software distributed under the License is distributed on an "AS IS" basis,
118
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
119
// See the License for the specific language governing rights and limitations under the License.
120
//
121
// The Original Code is: all this file.
122
//
123
// The Initial Developer of the Original Code is Michael H. Kay.
124
//
125
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
126
//
127
// Contributor(s): none.
128
//
129
Popular Tags