You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataURIResolver.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.util;
  19. import java.io.ByteArrayInputStream;
  20. import javax.xml.transform.Source;
  21. import javax.xml.transform.TransformerException;
  22. import javax.xml.transform.URIResolver;
  23. import javax.xml.transform.stream.StreamSource;
  24. // base64 support for "data" urls
  25. import org.apache.xmlgraphics.util.io.Base64DecodeStream;
  26. /**
  27. * Resolves data URLs (described in RFC 2397) returning its data as a StreamSource.
  28. *
  29. * @see javax.xml.transform.URIResolver
  30. * @see <a href="http://www.ietf.org/rfc/rfc2397">RFC 2397</a>
  31. */
  32. public class DataURIResolver implements URIResolver {
  33. /**
  34. * @inheritDoc javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)
  35. */
  36. public Source resolve(String href, String base) throws TransformerException {
  37. if (href.startsWith("data:")) {
  38. return parseDataURI(href);
  39. } else {
  40. return null;
  41. }
  42. }
  43. /**
  44. * Parses inline data URIs as generated by MS Word's XML export and FO
  45. * stylesheet.
  46. *
  47. * @inheritDoc <a href="http://www.ietf.org/rfc/rfc2397">RFC 2397</a>
  48. */
  49. private Source parseDataURI(String href) {
  50. int commaPos = href.indexOf(',');
  51. // header is of the form data:[<mediatype>][;base64]
  52. String header = href.substring(0, commaPos);
  53. String data = href.substring(commaPos + 1);
  54. if (header.endsWith(";base64")) {
  55. byte[] bytes = data.getBytes();
  56. ByteArrayInputStream encodedStream = new ByteArrayInputStream(bytes);
  57. Base64DecodeStream decodedStream = new Base64DecodeStream(
  58. encodedStream);
  59. return new StreamSource(decodedStream);
  60. } else {
  61. // Note that this is not quite the full story here. But since we are
  62. // only interested
  63. // in base64-encoded binary data, the next line will probably never
  64. // be called.
  65. //TODO Handle un-escaping of special URL chars like %20
  66. return new StreamSource(new java.io.StringReader(data));
  67. }
  68. }
  69. }