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 ; 12 import java.net.URISyntaxException ; 13 14 17 18 public class ResolveURI extends SystemFunction { 19 20 String 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 32 33 public Item evaluateItem(XPathContext context) throws XPathException { 34 URI relativeURI; 35 AtomicValue arg0 = (AtomicValue)argument[0].evaluateItem(context); 36 if (arg0 == null) { 37 return null; 38 } 39 String relative = arg0.getStringValue(); 40 try { 41 relativeURI = new URI (relative); 42 } catch (URISyntaxException 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 base; 52 URI 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 (base); 65 } catch (URISyntaxException err) { 66 dynamicError("Base URI " + Err.wrap(base) + " is invalid: " + err.getMessage(), 67 "FORG0002", context); 68 return null; 69 } 70 URI resolvedURI = baseURI.resolve(relativeURI); 77 return new AnyURIValue(resolvedURI.toString()); 78 } 79 80 } 81 82 | Popular Tags |