diff options
author | Jeremias Maerki <jeremias@apache.org> | 2007-06-29 12:46:14 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2007-06-29 12:46:14 +0000 |
commit | 67a9815afa925606d13e13488e5539a57deb5bb2 (patch) | |
tree | b423af5c05f0dead3721e25f0968cdd53ce4cc55 /src/java/org/apache/fop/util/DataURIResolver.java | |
parent | 8a25e4a1b1ed7100b656913ad311138d54cae943 (diff) | |
download | xmlgraphics-fop-67a9815afa925606d13e13488e5539a57deb5bb2.tar.gz xmlgraphics-fop-67a9815afa925606d13e13488e5539a57deb5bb2.zip |
Bugzilla #42278:
Refactoring of color map cache and uri/fo resolution from FopFactory
Submitted by: Adrian Cumiskey <fop-dev@cumiskey.com>
Changes in addition to the patch by jeremias:
- Moved the color map cache to the util package so it doesn't clutter the API (apps) package.
- Factored out the data URL resolution into its own URIResolver class which can now be used separately.
- Added a utility class for generating RFC2397 data URLs.
- Added a unit test for data URL handling.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@551874 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/util/DataURIResolver.java')
-rw-r--r-- | src/java/org/apache/fop/util/DataURIResolver.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/util/DataURIResolver.java b/src/java/org/apache/fop/util/DataURIResolver.java new file mode 100644 index 000000000..4ae4be156 --- /dev/null +++ b/src/java/org/apache/fop/util/DataURIResolver.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.util; + +import java.io.ByteArrayInputStream; + +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; +import javax.xml.transform.stream.StreamSource; + +// base64 support for "data" urls +import org.apache.xmlgraphics.util.io.Base64DecodeStream; + +/** + * Resolves data URLs (described in RFC 2397) returning its data as a StreamSource. + * + * @see javax.xml.transform.URIResolver + * @see <a href="http://www.ietf.org/rfc/rfc2397">RFC 2397</a> + */ +public class DataURIResolver implements URIResolver { + + /** + * @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String) + */ + public Source resolve(String href, String base) throws TransformerException { + if (href.startsWith("data:")) { + return parseDataURI(href); + } else { + return null; + } + } + + /** + * Parses inline data URIs as generated by MS Word's XML export and FO + * stylesheet. + * + * @see <a href="http://www.ietf.org/rfc/rfc2397">RFC 2397</a> + */ + private Source parseDataURI(String href) { + int commaPos = href.indexOf(','); + // header is of the form data:[<mediatype>][;base64] + String header = href.substring(0, commaPos); + String data = href.substring(commaPos + 1); + if (header.endsWith(";base64")) { + byte[] bytes = data.getBytes(); + ByteArrayInputStream encodedStream = new ByteArrayInputStream(bytes); + Base64DecodeStream decodedStream = new Base64DecodeStream( + encodedStream); + return new StreamSource(decodedStream); + } else { + // Note that this is not quite the full story here. But since we are + // only interested + // in base64-encoded binary data, the next line will probably never + // be called. + //TODO Handle un-escaping of special URL chars like %20 + return new StreamSource(new java.io.StringReader(data)); + } + } + +} |