1 package net.sf.saxon.functions; 2 import net.sf.saxon.Controller; 3 import net.sf.saxon.event.Builder; 4 import net.sf.saxon.event.Receiver; 5 import net.sf.saxon.event.Sender; 6 import net.sf.saxon.expr.StaticContext; 7 import net.sf.saxon.expr.XPathContext; 8 import net.sf.saxon.om.DocumentInfo; 9 import net.sf.saxon.om.Item; 10 import net.sf.saxon.trans.DynamicError; 11 import net.sf.saxon.trans.XPathException; 12 import net.sf.saxon.value.AtomicValue; 13 import org.xml.sax.InputSource ; 14 15 import javax.xml.transform.sax.SAXSource ; 16 import java.io.StringReader ; 17 18 19 24 25 public class Parse extends SystemFunction { 26 27 String baseURI; 28 29 33 34 public void checkArguments(StaticContext env) throws XPathException { 35 if (baseURI == null) { 36 super.checkArguments(env); 37 baseURI = env.getBaseURI(); 38 } 39 } 40 41 44 45 public Item evaluateItem(XPathContext c) throws XPathException { 46 Controller controller = c.getController(); 47 AtomicValue content = (AtomicValue)argument[0].evaluateItem(c); 48 StringReader sr = new StringReader (content.getStringValue()); 49 InputSource is = new InputSource (sr); 50 is.setSystemId(baseURI); 51 SAXSource source = new SAXSource (is); 52 source.setSystemId(baseURI); 53 Builder b = controller.makeBuilder(); 54 Receiver s = controller.makeStripper(b); 55 if (controller.getExecutable().stripsInputTypeAnnotations()) { 56 s = controller.getConfiguration().getAnnotationStripper(s); 57 } 58 try { 59 new Sender(controller.makePipelineConfiguration()).send(source, s); 60 return (DocumentInfo)b.getCurrentRoot(); 61 } catch (XPathException err) { 62 throw new DynamicError(err); 63 } 64 } 65 66 } 67 68 69 70 71 | Popular Tags |