| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.resource; |
| 9 |
|
| 10 |
import net.sf.saxon.functions.URIQueryParameters; |
| 11 |
import net.sf.saxon.lib.ParseOptions; |
| 12 |
import net.sf.saxon.lib.Resource; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.value.Base64BinaryValue; |
| 15 |
|
| 16 |
import java.io.ByteArrayInputStream; |
| 17 |
import java.io.UnsupportedEncodingException; |
| 18 |
import java.net.URI; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class DataURIScheme { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static Resource decode(URI uri) { |
| 36 |
assert uri.getScheme().equals("data"); |
| 37 |
String path = uri.getSchemeSpecificPart(); |
| 38 |
int comma = path.indexOf(','); |
| 39 |
if (comma < 0) { |
| 40 |
throw new IllegalArgumentException("Missing comma in data URI"); |
| 41 |
} |
| 42 |
String header = path.substring(0, comma); |
| 43 |
String content = path.substring(comma+1); |
| 44 |
boolean isBase64 = header.endsWith(";base64"); |
| 45 |
String contentType = header.substring(0, isBase64 ? comma-7 : comma); |
| 46 |
if (isBase64) { |
| 47 |
try { |
| 48 |
byte[] octets = Base64BinaryValue.decode(content); |
| 49 |
BinaryResource resource = |
| 50 |
new BinaryResource(uri.toString(), contentType, new ByteArrayInputStream(octets)); |
| 51 |
resource.setData(octets); |
| 52 |
return resource; |
| 53 |
} catch (XPathException e) { |
| 54 |
throw new IllegalArgumentException(e.getMessage()); |
| 55 |
} |
| 56 |
} else { |
| 57 |
String encoding = getEncoding(contentType); |
| 58 |
if (encoding == null) { |
| 59 |
encoding = "US-ASCII"; |
| 60 |
} |
| 61 |
try { |
| 62 |
byte[] utf8content = content.getBytes("UTF-8"); |
| 63 |
AbstractResourceCollection.InputDetails details = new AbstractResourceCollection.InputDetails(); |
| 64 |
details.contentType = getMediaType(contentType); |
| 65 |
details.encoding = encoding; |
| 66 |
details.inputStream = new ByteArrayInputStream(utf8content); |
| 67 |
details.onError = URIQueryParameters.ON_ERROR_FAIL; |
| 68 |
details.parseOptions = new ParseOptions(); |
| 69 |
return new UnparsedTextResource(uri.toString(), details); |
| 70 |
} catch (UnsupportedEncodingException e) { |
| 71 |
throw new IllegalArgumentException("Unsupported encoding " + encoding); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
private static String getMediaType(String contentType) { |
| 77 |
int semicolon = contentType.indexOf(';'); |
| 78 |
if (semicolon < 0) { |
| 79 |
return contentType; |
| 80 |
} else { |
| 81 |
return contentType.substring(0, semicolon); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
private static String getEncoding(String contentType) { |
| 86 |
String[] parts = contentType.split(";"); |
| 87 |
for (String part : parts) { |
| 88 |
if (part.startsWith("charset=")) { |
| 89 |
return part.substring(8); |
| 90 |
} |
| 91 |
} |
| 92 |
return null; |
| 93 |
} |
| 94 |
} |
| 95 |
|