Merge branch 'master' of git://github.com/haraldk/TwelveMonkeys

This commit is contained in:
Erlend Hamnaberg 2010-02-07 19:03:55 +01:00
commit 673527abe5
4 changed files with 89 additions and 15 deletions

View File

@ -51,13 +51,13 @@ public class XMLSerializer {
// Main problem: Sun's Java 5 does not have LS 3.0 support // Main problem: Sun's Java 5 does not have LS 3.0 support
// This class has no dependencies, which probably makes it more useful // This class has no dependencies, which probably makes it more useful
// TODO: Don't insert initial and ending line-break for text-nodes
// TODO: Support not inserting line-breaks, to preserve space
// TODO: Support line breaking (at configurable width) // TODO: Support line breaking (at configurable width)
// TODO: Support skipping XML declaration?
// TODO: Support standalone? // TODO: Support standalone?
// TODO: Support more than version 1.0? // TODO: Support more than version 1.0?
// TODO: Consider using IOException to communicate trouble, rather than RTE, // TODO: Consider using IOException to communicate trouble, rather than RTE,
// to be more compatible... // to be more compatible...
// TODO: Support not inserting line-breaks, to preserve space
// TODO: Idea: Create a SerializationContext that stores attributes on // TODO: Idea: Create a SerializationContext that stores attributes on
// serialization, to keep the serialization thread-safe // serialization, to keep the serialization thread-safe
@ -152,7 +152,7 @@ public class XMLSerializer {
writeComment(pOut, pNode, pContext); writeComment(pOut, pNode, pContext);
break; break;
case Node.PROCESSING_INSTRUCTION_NODE: case Node.PROCESSING_INSTRUCTION_NODE:
writeProcessingInstruction(pOut, pNode); writeProcessingInstruction(pOut, (ProcessingInstruction) pNode);
break; break;
case Node.ATTRIBUTE_NODE: case Node.ATTRIBUTE_NODE:
throw new IllegalArgumentException("Malformed input Document: Attribute nodes should only occur inside Element nodes"); throw new IllegalArgumentException("Malformed input Document: Attribute nodes should only occur inside Element nodes");
@ -167,9 +167,16 @@ public class XMLSerializer {
} }
} }
private void writeProcessingInstruction(final PrintWriter pOut, final Node pNode) { private void writeProcessingInstruction(final PrintWriter pOut, final ProcessingInstruction pNode) {
pOut.print("\n<?"); pOut.print("\n<?");
pOut.print(pNode.getNodeValue()); pOut.print(pNode.getTarget());
String value = pNode.getData();
if (value != null) {
pOut.print(" ");
pOut.print(value);
}
pOut.println("?>"); pOut.println("?>");
} }
@ -180,8 +187,11 @@ public class XMLSerializer {
pOut.print(maybeEscapeElementValue(value)); pOut.print(maybeEscapeElementValue(value));
} }
else if (!StringUtil.isEmpty(value)) { else if (!StringUtil.isEmpty(value)) {
indentToLevel(pOut, pContext); String escapedValue = maybeEscapeElementValue(value.trim());
pOut.println(maybeEscapeElementValue(value.trim())); //if (escapedValue.length() + (pContext.level * pContext.indent.length()) > 78) {
indentToLevel(pOut, pContext);
//}
pOut.println(escapedValue);
} }
} }
@ -219,7 +229,7 @@ public class XMLSerializer {
} }
String value = pNode.getNodeValue(); String value = pNode.getNodeValue();
validateCommenValue(value); validateCommentValue(value);
if (value.startsWith(" ")) { if (value.startsWith(" ")) {
pOut.print("<!--"); pOut.print("<!--");
@ -248,7 +258,7 @@ public class XMLSerializer {
int startEscape = needsEscapeElement(pValue); int startEscape = needsEscapeElement(pValue);
if (startEscape < 0) { if (startEscape < 0) {
// If no escpaing is needed, simply return original // If no escaping is needed, simply return original
return pValue; return pValue;
} }
else { else {
@ -374,6 +384,7 @@ public class XMLSerializer {
default: default:
} }
} }
return -1; return -1;
} }
@ -384,7 +395,7 @@ public class XMLSerializer {
return pValue; return pValue;
} }
private static String validateCommenValue(final String pValue) { private static String validateCommentValue(final String pValue) {
if (pValue.indexOf("--") >= 0) { if (pValue.indexOf("--") >= 0) {
throw new IllegalArgumentException("Malformed input document: Comment may not contain the string '--'"); throw new IllegalArgumentException("Malformed input document: Comment may not contain the string '--'");
} }
@ -407,6 +418,9 @@ public class XMLSerializer {
// TODO: Attributes should probably include namespaces, so that it works // TODO: Attributes should probably include namespaces, so that it works
// even if the document was created using attributes instead of namespaces... // even if the document was created using attributes instead of namespaces...
// In that case, prefix will be null...
// TODO: Don't insert duplicate/unnecessary namesspace declarations
// Handle namespace // Handle namespace
String namespace = pNode.getNamespaceURI(); String namespace = pNode.getNamespaceURI();
@ -444,6 +458,7 @@ public class XMLSerializer {
} }
} }
// TODO: Consider not indenting/newline if the first child is a text node
// Iterate children if any // Iterate children if any
if (pNode.hasChildNodes()) { if (pNode.hasChildNodes()) {
pOut.print(">"); pOut.print(">");
@ -452,11 +467,9 @@ public class XMLSerializer {
} }
NodeList children = pNode.getChildNodes(); NodeList children = pNode.getChildNodes();
//pContext.level++;
for (int i = 0; i < children.getLength(); i++) { for (int i = 0; i < children.getLength(); i++) {
writeNodeRecursive(pOut, children.item(i), pContext.push()); writeNodeRecursive(pOut, children.item(i), pContext.push());
} }
//pContext.level--;
if (!pContext.preserveSpace) { if (!pContext.preserveSpace) {
indentToLevel(pOut, pContext); indentToLevel(pOut, pContext);
@ -515,12 +528,15 @@ public class XMLSerializer {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
DocumentBuilder builder; DocumentBuilder builder;
try { try {
builder = factory.newDocumentBuilder(); builder = factory.newDocumentBuilder();
} }
catch (ParserConfigurationException e) { catch (ParserConfigurationException e) {
//noinspection ThrowableInstanceNeverThrown BOGUS
throw (IOException) new IOException(e.getMessage()).initCause(e); throw (IOException) new IOException(e.getMessage()).initCause(e);
} }
DOMImplementation dom = builder.getDOMImplementation(); DOMImplementation dom = builder.getDOMImplementation();
Document document = dom.createDocument("http://www.twelvemonkeys.com/xml/test", "test", dom.createDocumentType("test", null, null)); Document document = dom.createDocument("http://www.twelvemonkeys.com/xml/test", "test", dom.createDocumentType("test", null, null));

View File

@ -154,7 +154,7 @@ public final class BufferedImageInputStream extends ImageInputStreamImpl impleme
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (mStream != null) { if (mStream != null) {
mStream.close(); //mStream.close();
mStream = null; mStream = null;
mBuffer = null; mBuffer = null;
} }

View File

@ -1 +1,2 @@
- Support all DIB formats? - Support all DIB formats?
- Use Sun's BMP metadata format for image metadata + separate stream metadata?

View File

@ -1,5 +1,6 @@
package com.twelvemonkeys.servlet.image; package com.twelvemonkeys.servlet.image;
import com.twelvemonkeys.image.ImageUtil;
import com.twelvemonkeys.io.FileUtil; import com.twelvemonkeys.io.FileUtil;
import com.twelvemonkeys.servlet.OutputStreamAdapter; import com.twelvemonkeys.servlet.OutputStreamAdapter;
import org.jmock.Mock; import org.jmock.Mock;
@ -165,11 +166,31 @@ public class ImageServletResponseImplTestCase extends MockObjectTestCase {
assertTrue("Content has no data", out.size() > 0); assertTrue("Content has no data", out.size() > 0);
// Test that image data is still readable // Test that image data is still readable
/*
File tempFile = File.createTempFile("imageservlet-test-", ".jpeg");
FileOutputStream stream = new FileOutputStream(tempFile);
out.writeTo(stream);
stream.close();
System.err.println("open " + tempFile);
*/
BufferedImage outImage = ImageIO.read(new ByteArrayInputStream(out.toByteArray())); BufferedImage outImage = ImageIO.read(new ByteArrayInputStream(out.toByteArray()));
assertNotNull(outImage); assertNotNull(outImage);
assertEquals(IMAGE_DIMENSION_PNG.width, outImage.getWidth()); assertEquals(IMAGE_DIMENSION_PNG.width, outImage.getWidth());
assertEquals(IMAGE_DIMENSION_PNG.height, outImage.getHeight()); assertEquals(IMAGE_DIMENSION_PNG.height, outImage.getHeight());
assertSimilarImage(ImageIO.read(mContext.getResource("/" + IMAGE_NAME_PNG)), outImage, 96f);
BufferedImage image = flatten(ImageIO.read(mContext.getResource("/" + IMAGE_NAME_PNG)), Color.BLACK);
/*
tempFile = File.createTempFile("imageservlet-test-", ".png");
stream = new FileOutputStream(tempFile);
ImageIO.write(image, "PNG", stream);
stream.close();
System.err.println("open " + tempFile);
*/
// JPEG compression trashes the image completely...
assertSimilarImage(image, outImage, 144f);
} }
@Test @Test
@ -200,11 +221,46 @@ public class ImageServletResponseImplTestCase extends MockObjectTestCase {
assertTrue("Content has no data", out.size() > 0); assertTrue("Content has no data", out.size() > 0);
// Test that image data is still readable // Test that image data is still readable
/*
File tempFile = File.createTempFile("imageservlet-test-", ".jpeg");
FileOutputStream stream = new FileOutputStream(tempFile);
out.writeTo(stream);
stream.close();
System.err.println("open " + tempFile);
*/
BufferedImage outImage = ImageIO.read(new ByteArrayInputStream(out.toByteArray())); BufferedImage outImage = ImageIO.read(new ByteArrayInputStream(out.toByteArray()));
assertNotNull(outImage); assertNotNull(outImage);
assertEquals(IMAGE_DIMENSION_GIF.width, outImage.getWidth()); assertEquals(IMAGE_DIMENSION_GIF.width, outImage.getWidth());
assertEquals(IMAGE_DIMENSION_GIF.height, outImage.getHeight()); assertEquals(IMAGE_DIMENSION_GIF.height, outImage.getHeight());
assertSimilarImage(ImageIO.read(mContext.getResource("/" + IMAGE_NAME_GIF)), outImage, 96f);
BufferedImage image = flatten(ImageIO.read(mContext.getResource("/" + IMAGE_NAME_GIF)), Color.WHITE);
/*
tempFile = File.createTempFile("imageservlet-test-", ".png");
stream = new FileOutputStream(tempFile);
ImageIO.write(image, "PNG", stream);
stream.close();
System.err.println("open " + tempFile);
*/
assertSimilarImage(image, outImage, 96f);
}
private static BufferedImage flatten(final BufferedImage pImage, final Color pBackgroundColor) {
BufferedImage image = ImageUtil.toBuffered(pImage, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
try {
g.setComposite(AlphaComposite.DstOver);
g.setColor(pBackgroundColor);
g.fillRect(0, 0, pImage.getWidth(), pImage.getHeight());
}
finally {
g.dispose();
}
return image;
} }
/** /**
@ -261,6 +317,7 @@ public class ImageServletResponseImplTestCase extends MockObjectTestCase {
assertNotNull(outImage); assertNotNull(outImage);
assertEquals(image.getWidth(), outImage.getWidth()); assertEquals(image.getWidth(), outImage.getWidth());
assertEquals(image.getHeight(), outImage.getHeight()); assertEquals(image.getHeight(), outImage.getHeight());
assertSimilarImage(image, outImage, 0);
} }
// TODO: Test with AOI attributes (rename thes to source-region?) // TODO: Test with AOI attributes (rename thes to source-region?)