Skip to content
Permalink
Browse files

Fix issue #133 by making pxp:zip sensitive to c:data/@encoding settin…

…gs on the stored document
  • Loading branch information
ndw committed Dec 11, 2013
1 parent f31ff67 commit ffa56c8d9fae590f01a4f3beb47b6a3c557350e4
Showing with 62 additions and 12 deletions.
  1. +62 −12 src/com/xmlcalabash/extensions/Zip.java
@@ -6,6 +6,8 @@
import java.io.InputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
@@ -19,6 +21,7 @@
import java.util.Map;
import java.util.TimeZone;
import java.util.zip.CRC32;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
@@ -32,6 +35,8 @@
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.model.RuntimeValue;
import com.xmlcalabash.util.Base64;
import com.xmlcalabash.util.JSONtoXML;
import com.xmlcalabash.util.TreeWriter;
import com.xmlcalabash.util.S9apiUtils;
import com.xmlcalabash.util.RelevantNodes;
@@ -41,6 +46,7 @@
import com.xmlcalabash.library.DefaultStep;
import com.xmlcalabash.library.HttpRequest;
import com.xmlcalabash.runtime.XAtomicStep;
import com.xmlcalabash.util.XMLtoJSON;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.XdmNode;
@@ -71,6 +77,10 @@
private static final QName _detailed = new QName("detailed");
private static final QName _status = new QName("status");
private static final QName _value = new QName("value");
private static final QName c_encoding = new QName("c", XProcConstants.NS_XPROC_STEP, "encoding");
private static final QName c_body = new QName("c", XProcConstants.NS_XPROC_STEP, "body");
private static final QName c_json = new QName("c", XProcConstants.NS_XPROC_STEP, "json");
private static final QName _content_type = new QName("content-type");
private final static int bufsize = 8192;

private final static QName serializerAttrs[] = {
@@ -139,12 +149,8 @@ public void run() throws SaxonApiException {

parseManifest(man);

File zipFile = null;
try {
zipFile = new File(new URI(zipFn));
} catch (URISyntaxException e) {
throw new XProcException(e);
}
URI uri = getOption(_href).getBaseURI().resolve(zipFn);
File zipFile = new File(uri);

File zipParent = zipFile.getParentFile();
File zipTemp = null;
@@ -352,9 +358,7 @@ public void update(ZipFile inZip, ZipOutputStream outZip, boolean freshen) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (srcManifest.containsKey(uri.toString())) {
XdmNode doc = srcManifest.get(href);
Serializer serializer = makeSerializer(file.getOptions());
serializer.setOutputStream(baos);
S9apiUtils.serialize(runtime, doc, serializer);
store(file, doc, baos);
} else {
URLConnection connection = uri.toURL().openConnection();
InputStream stream = connection.getInputStream();
@@ -376,9 +380,7 @@ public void update(ZipFile inZip, ZipOutputStream outZip, boolean freshen) {

if (srcManifest.containsKey(href)) {
XdmNode doc = srcManifest.get(href);
Serializer serializer = makeSerializer(file.getOptions());
serializer.setOutputStream(outZip);
S9apiUtils.serialize(runtime, doc, serializer);
store(file, doc, outZip);
} else {
URL url = uri.toURL();
URLConnection connection = url.openConnection();
@@ -592,6 +594,54 @@ private long readLastModified(URI uri) {
}
}

protected void store(FileToZip file, XdmNode doc, OutputStream out) throws SaxonApiException, IOException {
XdmNode root = S9apiUtils.getDocumentElement(doc);

if (((XProcConstants.NS_XPROC_STEP.equals(root.getNodeName().getNamespaceURI())
&& "base64".equals(root.getAttributeValue(_encoding)))
|| ("".equals(root.getNodeName().getNamespaceURI())
&& "base64".equals(root.getAttributeValue(c_encoding))))) {
storeBinary(file, doc, out);
} else if (XProcConstants.c_result.equals(root.getNodeName())
&& root.getAttributeValue(_content_type) != null
&& root.getAttributeValue(_content_type).startsWith("text/")) {
storeText(file, doc, out);
} else if (runtime.transparentJSON()
&& (((c_body.equals(root.getNodeName())
&& ("application/json".equals(root.getAttributeValue(_content_type))
|| "text/json".equals(root.getAttributeValue(_content_type))))
|| c_json.equals(root.getNodeName()))
|| JSONtoXML.JSONX_NS.equals(root.getNodeName().getNamespaceURI())
|| JSONtoXML.JXML_NS.equals(root.getNodeName().getNamespaceURI())
|| JSONtoXML.MLJS_NS.equals(root.getNodeName().getNamespaceURI()))) {
storeJSON(file, doc, out);
} else {
storeXML(file, doc, out);
}
}

public void storeBinary(FileToZip file, XdmNode doc, OutputStream out) throws IOException {
byte[] decoded = Base64.decode(doc.getStringValue());
out.write(decoded);
}

public void storeText(FileToZip file, XdmNode doc, OutputStream out) throws IOException {
out.write(doc.getStringValue().getBytes());
}

public void storeJSON(FileToZip file, XdmNode doc, OutputStream out) {
PrintWriter writer = new PrintWriter(out);
String json = XMLtoJSON.convert(doc);
writer.print(json);
writer.close();
}

public void storeXML(FileToZip file, XdmNode doc, OutputStream out) throws SaxonApiException {
Serializer serializer = makeSerializer(file.getOptions());
serializer.setOutputStream(out);
S9apiUtils.serialize(runtime, doc, serializer);
}

public Serializer makeSerializer(Hashtable<QName,String> options) {
Serializer serializer = new Serializer();

0 comments on commit ffa56c8

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