KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2 import net.sf.saxon.Err;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.XPathContext;
5 import net.sf.saxon.om.Item;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.value.AtomicValue;
8 import net.sf.saxon.value.StringValue;
9 import net.sf.saxon.value.AnyURIValue;
10
11 import java.net.URI JavaDoc;
12 import java.net.URISyntaxException JavaDoc;
13
14 /**
15 * This class supports the resolve-uri() functions in XPath 2.0
16 */

17
18 public class ResolveURI extends SystemFunction {
19
20     String JavaDoc expressionBaseURI = null;
21
22     public void checkArguments(StaticContext env) throws XPathException {
23         if (expressionBaseURI == null) {
24             super.checkArguments(env);
25             expressionBaseURI = env.getBaseURI();
26         }
27     }
28
29     /**
30     * Evaluate the function at run-time
31     */

32
33     public Item evaluateItem(XPathContext context) throws XPathException {
34         URI JavaDoc relativeURI;
35         AtomicValue arg0 = (AtomicValue)argument[0].evaluateItem(context);
36         if (arg0 == null) {
37             return null;
38         }
39         String JavaDoc relative = arg0.getStringValue();
40         try {
41             relativeURI = new URI JavaDoc(relative);
42         } catch (URISyntaxException JavaDoc err) {
43             dynamicError("Relative URI " + Err.wrap(relative) + " is invalid: " + err.getMessage(),
44                     "FORG0002", context);
45             return null;
46         }
47         if (relativeURI.isAbsolute()) {
48             return new StringValue(relative);
49         }
50
51         String JavaDoc base;
52         URI JavaDoc baseURI;
53         if (argument.length == 2) {
54             base = argument[1].evaluateAsString(context);
55         } else {
56             base = expressionBaseURI;
57             if (expressionBaseURI == null) {
58                 dynamicError("Base URI in static context is unknown",
59                         "FONS0005", context);
60                 return null;
61             }
62         }
63         try {
64             baseURI = new URI JavaDoc(base);
65         } catch (URISyntaxException JavaDoc err) {
66             dynamicError("Base URI " + Err.wrap(base) + " is invalid: " + err.getMessage(),
67                     "FORG0002", context);
68             return null;
69         }
70 // if (!baseURI.isAbsolute()) {
71
// // this rule has been removed from the spec (April 2005)
72
// dynamicError("Base URI " + Err.wrap(base) + " is not an absolute URI",
73
// "FORG0009", context);
74
// return null;
75
// }
76
URI JavaDoc resolvedURI = baseURI.resolve(relativeURI);
77         return new AnyURIValue(resolvedURI.toString());
78     }
79
80 }
81
82 //
83
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
84
// you may not use this file except in compliance with the License. You may obtain a copy of the
85
// License at http://www.mozilla.org/MPL/
86
//
87
// Software distributed under the License is distributed on an "AS IS" basis,
88
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
89
// See the License for the specific language governing rights and limitations under the License.
90
//
91
// The Original Code is: all this file.
92
//
93
// The Initial Developer of the Original Code is Michael H. Kay.
94
//
95
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
96
//
97
// Contributor(s): none.
98
//
99
Popular Tags