| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.resource; |
| 9 |
|
| 10 |
|
| 11 |
import net.sf.saxon.Configuration; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.lib.Resource; |
| 14 |
import net.sf.saxon.lib.ResourceFactory; |
| 15 |
import net.sf.saxon.om.Item; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.value.Base64BinaryValue; |
| 18 |
|
| 19 |
import java.io.*; |
| 20 |
import java.net.URLConnection; |
| 21 |
|
| 22 |
|
| 23 |
public class BinaryResource implements Resource { |
| 24 |
|
| 25 |
private String href = null; |
| 26 |
private String contentType = null; |
| 27 |
private byte[] data; |
| 28 |
private URLConnection connection = null; |
| 29 |
private InputStream inputStream = null; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static final ResourceFactory FACTORY = new ResourceFactory() { |
| 36 |
public Resource makeResource(Configuration config, String resourceURI, String contentType, AbstractResourceCollection.InputDetails details) throws XPathException { |
| 37 |
return new BinaryResource(resourceURI, contentType, details.inputStream); |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public BinaryResource(String href, String contentType, InputStream in) { |
| 51 |
this.contentType = contentType; |
| 52 |
this.href = href; |
| 53 |
this.inputStream = in; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public void setData(byte[] data) { |
| 63 |
this.data = data; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public byte[] getData() { |
| 72 |
return data; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public String getResourceURI() { |
| 82 |
return href; |
| 83 |
} |
| 84 |
|
| 85 |
private byte[] readBinaryFromConn(URLConnection con) throws XPathException { |
| 86 |
InputStream raw = null; |
| 87 |
this.connection = con; |
| 88 |
try { |
| 89 |
raw = connection.getInputStream(); |
| 90 |
|
| 91 |
int contentLength = connection.getContentLength(); |
| 92 |
InputStream in = new BufferedInputStream(raw); |
| 93 |
byte[] data = new byte[contentLength]; |
| 94 |
int bytesRead = 0; |
| 95 |
int offset = 0; |
| 96 |
while (offset < contentLength) { |
| 97 |
bytesRead = in.read(data, offset, data.length - offset); |
| 98 |
if (bytesRead == -1) { |
| 99 |
break; |
| 100 |
} |
| 101 |
offset += bytesRead; |
| 102 |
} |
| 103 |
in.close(); |
| 104 |
|
| 105 |
if (offset != contentLength) { |
| 106 |
throw new XPathException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); |
| 107 |
} |
| 108 |
return data; |
| 109 |
} catch (IOException e) { |
| 110 |
throw new XPathException(e); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static byte[] readBinaryFromStream(InputStream in, String path) throws XPathException { |
| 123 |
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 124 |
|
| 125 |
int nRead; |
| 126 |
byte[] data = new byte[16384]; |
| 127 |
|
| 128 |
try { |
| 129 |
while ((nRead = in.read(data, 0, data.length)) != -1) { |
| 130 |
buffer.write(data, 0, nRead); |
| 131 |
} |
| 132 |
buffer.flush(); |
| 133 |
return buffer.toByteArray(); |
| 134 |
} catch (IOException e) { |
| 135 |
throw new XPathException("Failed to read: " + path + " " + e); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public Item getItem(XPathContext context) throws XPathException { |
| 140 |
if (data != null) { |
| 141 |
return new Base64BinaryValue(data); |
| 142 |
} else if (connection != null) { |
| 143 |
data = readBinaryFromConn(connection); |
| 144 |
return new Base64BinaryValue(data); |
| 145 |
} else if (inputStream != null) { |
| 146 |
data = readBinaryFromStream(inputStream, href); |
| 147 |
return new Base64BinaryValue(data); |
| 148 |
} |
| 149 |
return null; |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
public String getContentType() { |
| 154 |
return contentType; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
} |