Procházet zdrojové kódy

Moved DataURIResolver from FOP to commons; use new URIResolver registry


git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@688508 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-1_0
Maximilian Berger před 16 roky
rodič
revize
f10d6e420f

binární
lib/xmlgraphics-commons-1.4svn.jar Zobrazit soubor


+ 5
- 7
src/java/org/apache/fop/apps/FOURIResolver.java Zobrazit soubor

@@ -34,14 +34,12 @@ import javax.xml.transform.stream.StreamSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.xmlgraphics.util.io.Base64EncodeStream;

import org.apache.fop.util.DataURIResolver;
import org.apache.xmlgraphics.util.uri.CommonURIResolver;

/**
* Provides FOP specific URI resolution. This is the default URIResolver
* {@link FOUserAgent} will use unless overidden.
* {@link FOUserAgent} will use unless overridden.
*
* @see javax.xml.transform.URIResolver
*/
@@ -50,8 +48,8 @@ public class FOURIResolver implements javax.xml.transform.URIResolver {
// log
private Log log = LogFactory.getLog("FOP");

/** URIResolver for RFC 2397 data URLs */
private URIResolver dataURIResolver = new DataURIResolver();
/** Common URIResolver */
private URIResolver commonURIResolver = CommonURIResolver.getInstance();

/** A user settable URI Resolver */
private URIResolver uriResolver = null;
@@ -152,7 +150,7 @@ public class FOURIResolver implements javax.xml.transform.URIResolver {

// data URLs can be quite long so evaluate early and don't try to build a File
// (can lead to problems)
source = dataURIResolver.resolve(href, base);
source = commonURIResolver.resolve(href, base);

// Custom uri resolution
if (source == null && uriResolver != null) {

+ 0
- 78
src/java/org/apache/fop/util/DataURIResolver.java Zobrazit soubor

@@ -1,78 +0,0 @@
/*
* 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 {

/**
* {@inheritDoc}
*/
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));
}
}

}

+ 0
- 67
src/java/org/apache/fop/util/DataURLUtil.java Zobrazit soubor

@@ -1,67 +0,0 @@
/*
* 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.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.commons.io.IOUtils;
import org.apache.xmlgraphics.util.io.Base64EncodeStream;

/**
* Utility classes for generating RFC 2397 data URLs.
*/
public class DataURLUtil {

/**
* Creates a new data URL and returns it as a String.
* @param in the InputStream to read the data from
* @param mediatype the MIME type of the content, or null
* @return the newly created data URL
* @throws IOException if an I/O error occurs
*/
public static String createDataURL(InputStream in, String mediatype) throws IOException {
StringWriter writer = new StringWriter();
writeDataURL(in, mediatype, writer);
return writer.toString();
}

/**
* Generates a data URL and writes it to a Writer.
* @param in the InputStream to read the data from
* @param mediatype the MIME type of the content, or null
* @param writer the Writer to write to
* @throws IOException if an I/O error occurs
*/
public static void writeDataURL(InputStream in, String mediatype, Writer writer)
throws IOException {
writer.write("data:");
if (mediatype != null) {
writer.write(mediatype);
}
writer.write(";base64,");
Base64EncodeStream out = new Base64EncodeStream(
new WriterOutputStream(writer, "US-ASCII"));
IOUtils.copy(in, out);
out.flush();
}
}

+ 0
- 91
src/java/org/apache/fop/util/WriterOutputStream.java Zobrazit soubor

@@ -1,91 +0,0 @@
/*
* 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.IOException;
import java.io.OutputStream;
import java.io.Writer;

/**
* An OutputStream wrapper for a Writer.
*/
public class WriterOutputStream extends OutputStream {

private Writer writer;
private String encoding;

/**
* Creates a new WriterOutputStream.
* @param writer the Writer to write to
*/
public WriterOutputStream(Writer writer) {
this(writer, null);
}

/**
* Creates a new WriterOutputStream.
* @param writer the Writer to write to
* @param encoding the encoding to use, or null if the default encoding should be used
*/
public WriterOutputStream(Writer writer, String encoding) {
this.writer = writer;
this.encoding = encoding;
}

/**
* {@inheritDoc}
*/
public void close() throws IOException {
writer.close();
}

/**
* {@inheritDoc}
*/
public void flush() throws IOException {
writer.flush();
}

/**
* {@inheritDoc}
*/
public void write(byte[] buf, int offset, int length) throws IOException {
if (encoding != null) {
writer.write(new String(buf, offset, length, encoding));
} else {
writer.write(new String(buf, offset, length));
}
}

/**
* {@inheritDoc}
*/
public void write(byte[] buf) throws IOException {
write(buf, 0, buf.length);
}

/**
* {@inheritDoc}
*/
public void write(int b) throws IOException {
write(new byte[] {(byte)b});
}

}

+ 0
- 2
test/java/org/apache/fop/UtilityCodeTestSuite.java Zobrazit soubor

@@ -26,7 +26,6 @@ import org.apache.fop.events.BasicEventTestCase;
import org.apache.fop.pdf.PDFObjectTestCase;
import org.apache.fop.traits.BorderPropsTestCase;
import org.apache.fop.util.ColorUtilTestCase;
import org.apache.fop.util.DataURIResolverTestCase;
import org.apache.fop.util.ElementListUtilsTestCase;
import org.apache.fop.util.PDFNumberTestCase;
import org.apache.fop.util.XMLResourceBundleTestCase;
@@ -49,7 +48,6 @@ public class UtilityCodeTestSuite {
suite.addTest(new TestSuite(ColorUtilTestCase.class));
suite.addTest(new TestSuite(BorderPropsTestCase.class));
suite.addTest(new TestSuite(ElementListUtilsTestCase.class));
suite.addTest(new TestSuite(DataURIResolverTestCase.class));
suite.addTest(new TestSuite(BasicEventTestCase.class));
suite.addTest(new TestSuite(XMLResourceBundleTestCase.class));
//$JUnit-END$

+ 0
- 116
test/java/org/apache/fop/util/DataURIResolverTestCase.java Zobrazit soubor

@@ -1,116 +0,0 @@
/*
* 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.URIResolver;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.IOUtils;

import junit.framework.TestCase;

/**
* Test case for the RFC 2397 data URL/URI resolver.
*/
public class DataURIResolverTestCase extends TestCase {

private static final byte[] TESTDATA = new byte[] {0, 1, 2, 3, 4, 5};

/**
* Tests DataURLUtil.
* @throws Exception if an error occurs
*/
public void testRFC2397Generator() throws Exception {
String url = DataURLUtil.createDataURL(new ByteArrayInputStream(TESTDATA), null);
assertEquals("Generated data URL is wrong", "data:;base64,AAECAwQF", url);

url = DataURLUtil.createDataURL(new ByteArrayInputStream(TESTDATA), "application/pdf");
assertEquals("Generated data URL is wrong", "data:application/pdf;base64,AAECAwQF", url);
}

/**
* Test the URIResolver contract if the protocol doesn't match. Resolver must return null
* in this case.
* @throws Exception if an error occurs
*/
public void testNonMatchingContract() throws Exception {
URIResolver resolver = new DataURIResolver();
Source src;

src = resolver.resolve("http://xmlgraphics.apache.org/fop/index.html", null);
assertNull(src);

src = resolver.resolve("index.html", "http://xmlgraphics.apache.org/fop/");
assertNull(src);
}

private static boolean byteCmp(byte[] src, int srcOffset, byte[] cmp) {
for (int i = 0, c = cmp.length; i < c; i++) {
if (src[srcOffset + i] != cmp[i]) {
return false;
}
}
return true;
}

/**
* Test the DataURIResolver with correct values.
* @throws Exception if an error occurs
*/
public void testDataURLHandling() throws Exception {
URIResolver resolver = new DataURIResolver();
Source src;

src = resolver.resolve("data:;base64,AAECAwQF", null);
assertNotNull(src);
StreamSource streamSource = (StreamSource)src;
byte[] data = IOUtils.toByteArray(streamSource.getInputStream());
assertTrue("Decoded data doesn't match the test data", byteCmp(TESTDATA, 0, data));

src = resolver.resolve(
"data:application/octet-stream;interpreter=fop;base64,AAECAwQF", null);
assertNotNull(src);
streamSource = (StreamSource)src;
assertNotNull(streamSource.getInputStream());
assertNull(streamSource.getReader());
data = IOUtils.toByteArray(streamSource.getInputStream());
assertTrue("Decoded data doesn't match the test data", byteCmp(TESTDATA, 0, data));

src = resolver.resolve("data:,FOP", null);
assertNotNull(src);
streamSource = (StreamSource)src;
assertNull(streamSource.getInputStream());
assertNotNull(streamSource.getReader());
String text = IOUtils.toString(streamSource.getReader());
assertEquals("FOP", text);

/* TODO Un-escaping of special URL chars like %20 hasn't been implemented, yet.
src = resolver.resolve("data:,A%20brief%20note", null);
assertNotNull(src);
streamSource = (StreamSource)src;
text = IOUtils.toString(streamSource.getReader());
assertEquals("A brief note", text);
*/
}

}

Načítá se…
Zrušit
Uložit