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 ; 14 import javax.xml.transform.TransformerException ; 15 16 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 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 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 try { 48 Controller controller = context.getController(); 49 ErrorListener old = controller.getErrorListener(); 51 controller.setErrorListener(new ErrorListener () { 52 public void warning(TransformerException exception) {} 53 public void error(TransformerException exception) {} 54 public void fatalError(TransformerException 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 70 71 public int computeSpecialProperties() { 72 return StaticProperty.ORDERED_NODESET | 73 StaticProperty.PEER_NODESET | 74 StaticProperty.NON_CREATIVE; 75 } 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 href = hrefVal.getStringValue(); 87 Item item = Document.makeDoc(href, expressionBaseURI, context, this); 88 if (item==null) { 89 dynamicError("Failed to load document " + href, "FODC0005", context); 91 return null; 92 } 93 return item; 94 } 95 96 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 href = hrefVal.getStringValue(); 107 Document.sendDoc(href, expressionBaseURI, context, this, out); 108 } 109 110 } 111 112 | Popular Tags |