001: package net.sf.saxon.functions;
002:
003: import net.sf.saxon.Err;
004: import net.sf.saxon.expr.StaticContext;
005: import net.sf.saxon.expr.XPathContext;
006: import net.sf.saxon.om.Item;
007: import net.sf.saxon.trans.XPathException;
008: import net.sf.saxon.value.AtomicValue;
009: import net.sf.saxon.value.StringValue;
010: import net.sf.saxon.value.AnyURIValue;
011:
012: import java.net.URI;
013: import java.net.URISyntaxException;
014:
015: /**
016: * This class supports the resolve-uri() functions in XPath 2.0
017: */
018:
019: public class ResolveURI extends SystemFunction {
020:
021: String expressionBaseURI = null;
022:
023: public void checkArguments(StaticContext env) throws XPathException {
024: if (expressionBaseURI == null) {
025: super .checkArguments(env);
026: expressionBaseURI = env.getBaseURI();
027: }
028: }
029:
030: /**
031: * Evaluate the function at run-time
032: */
033:
034: public Item evaluateItem(XPathContext context)
035: throws XPathException {
036: URI relativeURI;
037: AtomicValue arg0 = (AtomicValue) argument[0]
038: .evaluateItem(context);
039: if (arg0 == null) {
040: return null;
041: }
042: String relative = arg0.getStringValue();
043: try {
044: relativeURI = new URI(relative);
045: } catch (URISyntaxException err) {
046: dynamicError("Relative URI " + Err.wrap(relative)
047: + " is invalid: " + err.getMessage(), "FORG0002",
048: context);
049: return null;
050: }
051: if (relativeURI.isAbsolute()) {
052: return new StringValue(relative);
053: }
054:
055: String base;
056: URI baseURI;
057: if (argument.length == 2) {
058: base = argument[1].evaluateAsString(context);
059: } else {
060: base = expressionBaseURI;
061: if (expressionBaseURI == null) {
062: dynamicError("Base URI in static context is unknown",
063: "FONS0005", context);
064: return null;
065: }
066: }
067: try {
068: baseURI = new URI(base);
069: } catch (URISyntaxException err) {
070: dynamicError("Base URI " + Err.wrap(base) + " is invalid: "
071: + err.getMessage(), "FORG0002", context);
072: return null;
073: }
074: // if (!baseURI.isAbsolute()) {
075: // // this rule has been removed from the spec (April 2005)
076: // dynamicError("Base URI " + Err.wrap(base) + " is not an absolute URI",
077: // "FORG0009", context);
078: // return null;
079: // }
080: URI resolvedURI = baseURI.resolve(relativeURI);
081: return new AnyURIValue(resolvedURI.toString());
082: }
083:
084: }
085:
086: //
087: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
088: // you may not use this file except in compliance with the License. You may obtain a copy of the
089: // License at http://www.mozilla.org/MPL/
090: //
091: // Software distributed under the License is distributed on an "AS IS" basis,
092: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
093: // See the License for the specific language governing rights and limitations under the License.
094: //
095: // The Original Code is: all this file.
096: //
097: // The Initial Developer of the Original Code is Michael H. Kay.
098: //
099: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
100: //
101: // Contributor(s): none.
102: //
|