Permalink
Browse files

Implemented the pxp:compress and pxp:uncompress steps

  • Loading branch information...
ndw committed Jan 30, 2013
1 parent f3f5e97 commit f345da010003fbd4f54a8d8735ff9668cf13fdc8
@@ -214,6 +214,12 @@
<implementation type="cx:send-mail"
class-name="com.xmlcalabash.extensions.SendMail"/>
+<implementation type="cx:compress cx:gzip pxp:compress pxp:gzip"
+ class-name="com.xmlcalabash.extensions.Compress"/>
+
+<implementation type="cx:uncompress cx:gunzip pxp:uncompress pxp:gunzip"
+ class-name="com.xmlcalabash.extensions.Uncompress"/>
+
<!-- Fileutils -->
<implementation type="cxf:info pxf:info"
@@ -354,4 +354,58 @@
<p:option name="command" select="'update'" cx:type="update|freshen|create|delete"/>
</p:declare-step>
+<p:declare-step type="pxp:gzip">
+ <p:input port="source"/>
+ <p:output port="result"/>
+ <p:option name="href" cx:type="xsd:anyURI"/>
+ <p:option name="byte-order-mark" cx:type="xsd:boolean"/>
+ <p:option name="cdata-section-elements" select="''" cx:type="ListOfQNames"/>
+ <p:option name="doctype-public" cx:type="xsd:string"/>
+ <p:option name="doctype-system" cx:type="xsd:anyURI"/>
+ <p:option name="encoding" cx:type="xsd:string"/>
+ <p:option name="escape-uri-attributes" select="'false'" cx:type="xsd:boolean"/>
+ <p:option name="include-content-type" select="'true'" cx:type="xsd:boolean"/>
+ <p:option name="indent" select="'false'" cx:type="xsd:boolean"/>
+ <p:option name="media-type" cx:type="xsd:string"/>
+ <p:option name="method" select="'xml'" cx:type="xsd:QName"/>
+ <p:option name="normalization-form" select="'none'" cx:type="NormalizationForm"/>
+ <p:option name="omit-xml-declaration" select="'true'" cx:type="xsd:boolean"/>
+ <p:option name="standalone" select="'omit'" cx:type="true|false|omit"/>
+ <p:option name="undeclare-prefixes" cx:type="xsd:boolean"/>
+ <p:option name="version" select="'1.0'" cx:type="xsd:string"/>
+</p:declare-step>
+
+<p:declare-step type="pxp:gunzip">
+ <p:input port="source"/>
+ <p:output port="result"/>
+</p:declare-step>
+
+<p:declare-step type="pxp:compress">
+ <p:input port="source"/>
+ <p:output port="result"/>
+ <p:option name="href" cx:type="xsd:anyURI"/>
+ <p:option name="compression-method" cx:type="xsd:string"/>
+ <p:option name="byte-order-mark" cx:type="xsd:boolean"/>
+ <p:option name="cdata-section-elements" select="''" cx:type="ListOfQNames"/>
+ <p:option name="doctype-public" cx:type="xsd:string"/>
+ <p:option name="doctype-system" cx:type="xsd:anyURI"/>
+ <p:option name="encoding" cx:type="xsd:string"/>
+ <p:option name="escape-uri-attributes" select="'false'" cx:type="xsd:boolean"/>
+ <p:option name="include-content-type" select="'true'" cx:type="xsd:boolean"/>
+ <p:option name="indent" select="'false'" cx:type="xsd:boolean"/>
+ <p:option name="media-type" cx:type="xsd:string"/>
+ <p:option name="method" select="'xml'" cx:type="xsd:QName"/>
+ <p:option name="normalization-form" select="'none'" cx:type="NormalizationForm"/>
+ <p:option name="omit-xml-declaration" select="'true'" cx:type="xsd:boolean"/>
+ <p:option name="standalone" select="'omit'" cx:type="true|false|omit"/>
+ <p:option name="undeclare-prefixes" cx:type="xsd:boolean"/>
+ <p:option name="version" select="'1.0'" cx:type="xsd:string"/>
+</p:declare-step>
+
+<p:declare-step type="pxp:uncompress">
+ <p:input port="source"/>
+ <p:output port="result"/>
+ <p:option name="compression-method" cx:type="xsd:string"/>
+</p:declare-step>
+
</p:library>
@@ -0,0 +1,53 @@
+/*
+ * Store.java
+ *
+ * Copyright 2008 Mark Logic Corporation.
+ * Portions Copyright 2007 Sun Microsystems, Inc.
+ * All rights reserved.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * https://xproc.dev.java.net/public/CDDL+GPL.html or
+ * docs/CDDL+GPL.txt in the distribution. See the License for the
+ * specific language governing permissions and limitations under the
+ * License. When distributing the software, include this License Header
+ * Notice in each file and include the License file at docs/CDDL+GPL.txt.
+ */
+
+package com.xmlcalabash.extensions;
+
+import com.xmlcalabash.core.XProcException;
+import com.xmlcalabash.core.XProcRuntime;
+import com.xmlcalabash.library.Store;
+import com.xmlcalabash.runtime.XAtomicStep;
+import net.sf.saxon.s9api.QName;
+import net.sf.saxon.s9api.SaxonApiException;
+
+/**
+ *
+ * @author ndw
+ *
+ * N.B. This step implements both pxp:gzip and pxp:compress
+ */
+public class Compress extends Store {
+ private static final QName _compression_method = new QName("compression-method");
+
+ public Compress(XProcRuntime runtime, XAtomicStep step) {
+ super(runtime,step);
+ }
+
+ public void run() throws SaxonApiException {
+ String cmethod = getOption(_compression_method, "gzip");
+ if ("gzip".equals(cmethod)) {
+ method = CompressionMethod.GZIP;
+ } else {
+ throw XProcException.stepError(999, "The only compression method supported is 'gzip'.");
+ }
+
+ super.run();
+ }
+}
+
@@ -0,0 +1,96 @@
+/*
+ * Load.java
+ *
+ * Copyright 2008 Mark Logic Corporation.
+ * Portions Copyright 2007 Sun Microsystems, Inc.
+ * All rights reserved.
+ *
+ * The contents of this file are subject to the terms of either the GNU
+ * General Public License Version 2 only ("GPL") or the Common
+ * Development and Distribution License("CDDL") (collectively, the
+ * "License"). You may not use this file except in compliance with the
+ * License. You can obtain a copy of the License at
+ * https://xproc.dev.java.net/public/CDDL+GPL.html or
+ * docs/CDDL+GPL.txt in the distribution. See the License for the
+ * specific language governing permissions and limitations under the
+ * License. When distributing the software, include this License Header
+ * Notice in each file and include the License file at docs/CDDL+GPL.txt.
+ */
+
+package com.xmlcalabash.extensions;
+
+import com.xmlcalabash.core.XProcException;
+import com.xmlcalabash.core.XProcRuntime;
+import com.xmlcalabash.io.ReadablePipe;
+import com.xmlcalabash.io.WritablePipe;
+import com.xmlcalabash.library.DefaultStep;
+import com.xmlcalabash.runtime.XAtomicStep;
+import com.xmlcalabash.util.Base64;
+import com.xmlcalabash.util.S9apiUtils;
+import net.sf.saxon.s9api.QName;
+import net.sf.saxon.s9api.SaxonApiException;
+import net.sf.saxon.s9api.XdmNode;
+import org.xml.sax.InputSource;
+
+import java.io.ByteArrayInputStream;
+
+/**
+ *
+ * @author ndw
+ */
+public class Uncompress extends DefaultStep {
+ protected static final String logger = "com.xmlcalabash.extensions.gunzip";
+ private static final QName _compression_method = new QName("compression-method");
+
+ private ReadablePipe source = null;
+ private WritablePipe result = null;
+
+ /**
+ * Creates a new instance of Load
+ */
+ public Uncompress(XProcRuntime runtime, XAtomicStep step) {
+ super(runtime,step);
+ }
+
+ public void setInput(String port, ReadablePipe pipe) {
+ source = pipe;
+ }
+
+ public void setOutput(String port, WritablePipe pipe) {
+ result = pipe;
+ }
+
+ public void reset() {
+ source.resetReader();
+ result.resetWriter();
+ }
+
+ public void run() throws SaxonApiException {
+ super.run();
+
+ String cmethod = getOption(_compression_method, "gzip");
+ if (!"gzip".equals(cmethod)) {
+ throw XProcException.stepError(999, "The only compression method supported is 'gzip'.");
+ }
+
+ XdmNode root = S9apiUtils.getDocumentElement(source.read());
+ byte[] decoded = null;
+
+ // N.B. The Base64.decode() method *automatically* expands gzipped data!
+ if ("base64".equals(root.getAttributeValue(_encoding))) {
+ decoded = Base64.decode(root.getStringValue());
+ } else {
+ throw XProcException.stepError(999, "Input to cx:gunzip must be binary encoded data.");
+ }
+
+ try {
+ ByteArrayInputStream bais = new ByteArrayInputStream(decoded);
+ InputSource is = new InputSource(bais);
+ XdmNode doc = runtime.parse(is);
+ result.write(doc);
+ } catch (Exception e) {
+ throw new XProcException(e);
+ }
+ }
+}
+
Oops, something went wrong.

0 comments on commit f345da0

Please sign in to comment.