Permalink
Browse files

Added cx:report-errors step

  • Loading branch information...
ndw committed Jan 1, 2012
1 parent 5c31d2d commit f0ec1c0067321684217ea4de42ee6a4cc28bda0b
@@ -429,6 +429,15 @@
<p:output port="result"/>
</p:declare-step>
<p:declare-step type="cx:report-errors">
<p:input port="source" primary="true"/>
<p:input port="report" sequence="true"/>
<p:output port="result" sequence="true"/>
<p:option name="code" e:type="xsd:QName"/>
<p:option name="code-prefix" e:type="xsd:NCName"/>
<p:option name="code-namespace" e:type="xsd:anyURI"/>
</p:declare-step>
<!--
<p:declare-step type="cx:nvdl">
<p:input port="source" primary="true"/>
View
@@ -116,6 +116,7 @@
<xi:include href="steps/cx-metadata-extractor.xml"/>
<xi:include href="steps/cx-namespace-delete.xml"/>
<xi:include href="steps/cx-pretty-print.xml"/>
<xi:include href="steps/cx-report-errors.xml"/>
<xi:include href="steps/cx-send-mail.xml"/>
<xi:include href="steps/cx-set-cookies.xml"/>
<xi:include href="steps/cx-uri-info.xml"/>
@@ -0,0 +1,26 @@
<refentry xmlns:p="http://www.w3.org/ns/xproc"
xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax"
xmlns:pxp="http://exproc.org/proposed/steps"
xmlns:pxf="http://exproc.org/proposed/steps/file"
xmlns:ml="http://xmlcalabash.com/ns/extensions/marklogic"
xmlns:cxu="http://xmlcalabash.com/ns/extensions/xmlunit"
xmlns="http://docbook.org/ns/docbook"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:pos="http://exproc.org/proposed/steps/os"
xml:id="cx-report-errors">
<refnamediv>
<refname>cx:report-errors</refname>
<refpurpose>Send error report to the user</refpurpose>
</refnamediv>
<refsynopsisdiv>
<xi:include href="../declarations.xml" xpointer="xpath(/*/*[@type='cx:report-errors'])"/>
</refsynopsisdiv>
<refsection>
<title>Description</title>
<para>If the <literal>report</literal> port is not empty, it is sent to
the error listener (as one or more warnings). The <literal>source</literal> is
always copied to the <literal>result</literal>.</para>
<para>If an error code is specified, the step also raises that error.</para>
</refsection>
</refentry>
@@ -175,8 +175,11 @@
<implementation type="cx:nvdl pxp:nvdl"
class-name="com.xmlcalabash.extensions.NVDL"/>
<implementation type="cx:unzip pxp:unzip"
class-name="com.xmlcalabash.extensions.Unzip"/>
<implementation type="cx:nvdl pxp:nvdl"
class-name="com.xmlcalabash.extensions.NVDL"/>
<implementation type="cx:report-errors"
class-name="com.xmlcalabash.extensions.ReportErrors"/>
<implementation type="cx:uri-info"
class-name="com.xmlcalabash.extensions.UriInfo"/>
@@ -35,6 +35,15 @@
<p:output port="result"/>
</p:declare-step>
<p:declare-step type="cx:report-errors">
<p:input port="source" primary="true"/>
<p:input port="report" sequence="true"/>
<p:output port="result" sequence="true"/>
<p:option name="code" cx:type="xsd:QName"/>
<p:option name="code-prefix" cx:type="xsd:NCName"/>
<p:option name="code-namespace" cx:type="xsd:anyURI"/>
</p:declare-step>
<!-- ============================================================ -->
<p:declare-step type="cx:nvdl">
@@ -0,0 +1,115 @@
package com.xmlcalabash.extensions;
import com.xmlcalabash.core.XProcConstants;
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.model.RuntimeValue;
import com.xmlcalabash.runtime.XAtomicStep;
import net.sf.saxon.s9api.Axis;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XdmSequenceIterator;
/**
* Created by IntelliJ IDEA.
* User: ndw
* Date: Oct 8, 2008
* Time: 7:44:07 AM
* To change this template use File | Settings | File Templates.
*/
public class ReportErrors extends DefaultStep {
private static final QName c_error = new QName(XProcConstants.NS_XPROC_STEP, "error");
private static final QName _code = new QName("code");
private static final QName _code_prefix = new QName("code-prefix");
private static final QName _code_namespace = new QName("code-namespace");
private ReadablePipe source = null;
private ReadablePipe report = null;
private WritablePipe result = null;
/**
* Creates a new instance of Identity
*/
public ReportErrors(XProcRuntime runtime, XAtomicStep step) {
super(runtime,step);
}
public void setInput(String port, ReadablePipe pipe) {
if ("source".equals(port)) {
source = pipe;
} else {
report = 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 codeNameStr = null;
String cpfx = null;
String cns = null;
RuntimeValue codeNameValue = getOption(_code);
if (codeNameValue != null) {
codeNameStr = codeNameValue.getString();
cpfx = getOption(_code_prefix, (String) null);
cns = getOption(_code_namespace, (String) null);
}
if (cpfx == null && cns != null) {
cpfx = "ERR";
}
if (cpfx != null && cns == null) {
throw XProcException.dynamicError(34, "You can't specify a prefix without a namespace");
}
if (cns != null && codeNameStr.contains(":")) {
throw XProcException.dynamicError(34, "You can't specify a namespace if the code name contains a colon");
}
QName errorCode = null;
if (codeNameStr != null) {
if (codeNameStr.contains(":")) {
errorCode = new QName(codeNameStr, codeNameValue.getNode());
} else {
errorCode = new QName(cpfx == null ? "" : cpfx, cns, codeNameStr);
}
}
while (report.moreDocuments()) {
XdmNode doc = report.read();
XdmSequenceIterator iter = doc.axisIterator(Axis.DESCENDANT, c_error);
if (iter.hasNext()) {
while (iter.hasNext()) {
runtime.warning(this, doc, iter.next().getStringValue());
}
} else {
runtime.warning(this, doc, doc.getStringValue());
}
}
if (errorCode != null) {
throw new XProcException(errorCode, "error code?");
}
while (source.moreDocuments()) {
XdmNode doc = source.read();
runtime.finest(this, step.getNode(), "ReportErrors step " + step.getName() + " read " + doc.getDocumentURI());
result.write(doc);
}
}
}

0 comments on commit f0ec1c0

Please sign in to comment.