Debian Codegraph @

net/sf/saxon/resource/DataURIScheme.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This class handles URIs using the data: URI scheme defined in RFC 2397
22 */
23
24 public class DataURIScheme {
25
26 /**
27 * Parse the content of a URI that uses the data: URI scheme defined in RFC 2397, and
28 * return a Resource representing the content of the URI
29 * @param uri a valid URI using the data: URI scheme
30 * @return either a BinaryResource (if the URI specifies "base64") or an UnparsedTextResource
31 * (otherwise) representing the data part of the URI (the part after the comma)
32 * @throws IllegalArgumentException if the URI is invalid
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