Skip to content
Permalink
Browse files

Fix issue #222 by carefully serializing text content

  • Loading branch information
ndw committed Aug 26, 2015
1 parent 03b727d commit 4e969e9b352df4f2a2b72db6af7ef2996eac8ab1
Showing with 36 additions and 25 deletions.
  1. +19 −23 src/main/java/com/xmlcalabash/library/Store.java
  2. +17 −2 src/main/java/com/xmlcalabash/library/XSLT.java
@@ -261,42 +261,38 @@ public void store(OutputStream outstr) throws IOException {
}
}

private URI storeText(XdmNode doc, String href, String base,
String media) {
private URI storeText(XdmNode doc, String href, String base, String media) {
final Serializer serializer = makeSerializer();
serializer.setOutputProperty(Serializer.Property.METHOD, "text");

if (media == null) {
media = "text/plain";
}

try {
final String text = doc.getStringValue();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream outstr = baos;

if (href == null) {
OutputStream outstr = null;
ByteArrayOutputStream baos = null;
baos = new ByteArrayOutputStream();
outstr = baos;
try{
if (method == CompressionMethod.GZIP) {
GZIPOutputStream gzout = new GZIPOutputStream(outstr);
outstr = gzout;
}
if (method == CompressionMethod.GZIP) {
GZIPOutputStream gzout = new GZIPOutputStream(outstr);
outstr = gzout;
}

outstr.write(text.getBytes());
} finally {
outstr.close();
}
serializer.setOutputStream(outstr);
try {
S9apiUtils.serialize(runtime, doc, serializer);
} catch (SaxonApiException e) {
throw new IOException(e);
}

if (href == null) {
returnData(baos);
return null;
} else {
DataStore store = runtime.getDataStore();
return store.writeEntry(href, base, media, new DataWriter() {
public void store(OutputStream outstr) throws IOException {
if (method == CompressionMethod.GZIP) {
GZIPOutputStream gzout = new GZIPOutputStream(outstr);
outstr = gzout;
}

outstr.write(text.getBytes());
outstr.write(baos.toByteArray());
}
});
}
@@ -19,6 +19,7 @@

package com.xmlcalabash.library;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Hashtable;
@@ -49,6 +50,7 @@
import net.sf.saxon.lib.UnparsedTextURIResolver;
import net.sf.saxon.s9api.DocumentBuilder;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XdmDestination;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
@@ -271,7 +273,20 @@ public void run() throws SaxonApiException {
tree.addAttribute(_content_type, "text/plain");
tree.addAttribute(cx_decode,"true");
tree.startContent();
tree.addText(xformed.toString());

// Serialize the content as text so that we don't wind up with encoded XML characters
Serializer serializer = makeSerializer();
serializer.setOutputProperty(Serializer.Property.METHOD, "text");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.setOutputStream(baos);
try {
S9apiUtils.serialize(runtime, xformed, serializer);
} catch (SaxonApiException e2) {
throw new XProcException(e2);
}

tree.addText(baos.toString());
tree.addEndElement();
tree.endDocument();
resultPipe.write(tree.getResult());
@@ -369,7 +384,7 @@ public void close(Result result) throws TransformerException {
tree.startDocument(doc.getBaseURI());
tree.addStartElement(XProcConstants.c_result);
tree.addAttribute(_content_type, "text/plain");
tree.addAttribute(cx_decode,"true");
tree.addAttribute(cx_decode, "true");
tree.startContent();
tree.addText(doc.toString());
tree.addEndElement();

0 comments on commit 4e969e9

Please sign in to comment.
You can’t perform that action at this time.