1 package net.sf.saxon.event; 2 import net.sf.saxon.OutputURIResolver; 3 import net.sf.saxon.trans.DynamicError; 4 import net.sf.saxon.trans.XPathException; 5 6 import javax.xml.transform.Result ; 7 import javax.xml.transform.stream.StreamResult ; 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.io.OutputStream ; 11 import java.net.*; 12 13 14 20 21 public class StandardOutputResolver implements OutputURIResolver { 22 23 private static StandardOutputResolver theInstance = new StandardOutputResolver(); 24 25 28 29 public static StandardOutputResolver getInstance() { 30 return theInstance; 31 } 32 33 41 42 public Result resolve(String href, String base) throws XPathException { 43 44 46 try { 47 URI absoluteURI; 48 if (href.equals("")) { 49 if (base==null) { 50 throw new DynamicError("The system identifier of the principal output file is unknown"); 51 } 52 absoluteURI= new URI(base); 53 } else { 54 absoluteURI= new URI(href); 55 } 56 if (!absoluteURI.isAbsolute()) { 57 if (base==null) { 58 throw new DynamicError("The system identifier of the principal output file is unknown"); 59 } 60 URI baseURI = new URI(base); 61 absoluteURI = baseURI.resolve(href); 62 } 63 64 if (absoluteURI.getScheme().equals("file")) { 65 File newFile = new File (absoluteURI); 66 try { 67 if (!newFile.exists()) { 68 String parent = newFile.getParent(); 69 if (parent!=null) { 70 File parentPath = new File (parent); 71 if (parentPath != null && !parentPath.exists()) { 72 parentPath.mkdirs(); 73 } 74 newFile.createNewFile(); 75 } 76 } 77 78 StreamResult result = new StreamResult (newFile.toURI().toASCIIString()); 80 return result; 83 84 } catch (java.io.IOException err) { 85 throw new DynamicError("Failed to create output file " + absoluteURI, err); 86 } 87 88 } else { 89 90 93 URLConnection connection = absoluteURI.toURL().openConnection(); 94 connection.setDoInput(false); 95 connection.setDoOutput(true); 96 connection.connect(); 97 OutputStream stream = connection.getOutputStream(); 98 StreamResult result = new StreamResult (stream); 99 result.setSystemId(absoluteURI.toASCIIString()); 100 return result; 101 } 102 } catch (URISyntaxException err) { 103 throw new DynamicError("Invalid syntax for base URI", err); 104 } catch (IllegalArgumentException err2) { 105 throw new DynamicError("Invalid URI syntax", err2); 106 } catch (MalformedURLException err3) { 107 throw new DynamicError("Resolved URL is malformed", err3); 108 } catch (UnknownServiceException err5) { 109 throw new DynamicError("Specified protocol does not allow output", err5); 110 } catch (IOException err4) { 111 throw new DynamicError("Cannot open connection to specified URL", err4); 112 } 113 } 114 115 123 124 public void close(Result result) throws XPathException { 125 if (result instanceof StreamResult ) { 126 OutputStream stream = ((StreamResult )result).getOutputStream(); 127 if (stream != null) { 128 try { 129 stream.close(); 130 } catch (java.io.IOException err) { 131 throw new DynamicError("Failed while closing output file", err); 132 } 133 } 134 } 135 } 136 137 144 } 145 146 147 148 149 150 | Popular Tags |