Skip to content

Commit

Permalink
make it possible to override data input content type from command-lin…
Browse files Browse the repository at this point in the history
…e or Ant task without breaking tests and violating the specifications
  • Loading branch information
Vampire committed Aug 11, 2013
1 parent bc8d007 commit 257a0e0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/com/xmlcalabash/core/XProcConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public class XProcConstants {
public static final QName cx_depends_on = new QName("cx",NS_CALABASH_EX,"depends-on");
public static final QName cx_cache = new QName("cx",NS_CALABASH_EX,"cache");
public static final QName cx_type = new QName("cx",NS_CALABASH_EX,"type");
public static final QName cx_forced_content_type = new QName("cx", NS_CALABASH_EX, "forced-content-type");

public static final QName xs_QName = new QName("xs", NS_XMLSCHEMA, "QName");
public static final QName xs_untypedAtomic = new QName("xs", NS_XMLSCHEMA, "untypedAtomic");
Expand Down
4 changes: 2 additions & 2 deletions src/com/xmlcalabash/drivers/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ boolean run(UserArgs userArgs, XProcConfiguration config) throws SaxonApiExcepti
ReadableData rd;
switch (input.getKind()) {
case URI:
rd = new ReadableData(runtime, c_data, input.getUri(), input.getContentType());
rd = new ReadableData(runtime, c_data, input.getUri(), null, input.getContentType());
doc = rd.read();
break;

case INPUT_STREAM:
InputStream inputStream = input.getInputStream();
rd = new ReadableData(runtime, c_data, inputStream, input.getContentType());
rd = new ReadableData(runtime, c_data, inputStream, null, input.getContentType());
doc = rd.read();
inputStream.close();
break;
Expand Down
21 changes: 17 additions & 4 deletions src/com/xmlcalabash/io/ReadableData.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*/
public class ReadableData implements ReadablePipe {
protected String contentType = null;
protected String forcedContentType = null;
private Logger logger = Logger.getLogger(this.getClass().getName());
public static final QName _contentType = new QName("","content-type");
public static final QName c_contentType = new QName("c",XProcConstants.NS_XPROC_STEP, "content-type");
Expand All @@ -62,19 +63,28 @@ public class ReadableData implements ReadablePipe {

/** Creates a new instance of ReadableDocument */
public ReadableData(XProcRuntime runtime, QName wrapper, String uri, String contentType) {
this(runtime, wrapper, uri, null, contentType);
this(runtime, wrapper, uri, null, contentType, null);
}

public ReadableData(XProcRuntime runtime, QName wrapper, String uri, String contentType, String forcedContentType) {
this(runtime, wrapper, uri, null, contentType, forcedContentType);
}

public ReadableData(XProcRuntime runtime, QName wrapper, InputStream inputStream, String contentType) {
this(runtime, wrapper, null, inputStream, contentType);
this(runtime, wrapper, null, inputStream, contentType, null);
}

private ReadableData(XProcRuntime runtime, QName wrapper, String uri, InputStream inputStream, String contentType) {
public ReadableData(XProcRuntime runtime, QName wrapper, InputStream inputStream, String contentType, String forcedContentType) {
this(runtime, wrapper, null, inputStream, contentType, forcedContentType);
}

private ReadableData(XProcRuntime runtime, QName wrapper, String uri, InputStream inputStream, String contentType, String forcedContentType) {
this.runtime = runtime;
this.uri = uri;
this.inputStream = inputStream;
this.wrapper = wrapper;
this.contentType = contentType;
this.forcedContentType = forcedContentType;
}

private DocumentSequence ensureDocuments() {
Expand All @@ -101,7 +111,10 @@ private DocumentSequence ensureDocuments() {
stream = (uri == null) ? inputStream : ("-".equals(uri) ? System.in : getStream(dataURI));
String serverContentType = getContentType();

if (contentType != null && "content/unknown".equals(serverContentType)) {
if ((forcedContentType != null) && !"content/unknown".equals(forcedContentType)) {
// pretend...
serverContentType = forcedContentType;
} else if ((contentType != null) && "content/unknown".equals(serverContentType)) {
// pretend...
serverContentType = contentType;
}
Expand Down
9 changes: 9 additions & 0 deletions src/com/xmlcalabash/model/DataBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class DataBinding extends Binding {
private String href = null;
private QName wrapper = XProcConstants.c_data;
private String contentType = null;
private String forcedContentType = null;

/** Creates a new instance of DocumentBinding */
public DataBinding() {
Expand Down Expand Up @@ -73,6 +74,14 @@ public String getContentType() {
return contentType;
}

public void setForcedContentType(String forcedContentType) {
this.forcedContentType = forcedContentType;
}

public String getForcedContentType() {
return forcedContentType;
}

protected void dump(int depth) {
String indent = "";
for (int count = 0; count < depth; count++) {
Expand Down
7 changes: 7 additions & 0 deletions src/com/xmlcalabash/model/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import static com.xmlcalabash.core.XProcConstants.cx_forced_content_type;

/**
*
* @author ndw
Expand Down Expand Up @@ -722,6 +724,7 @@ private DataBinding readData(XdmNode node) {
String wrappfx = node.getAttributeValue(new QName("wrapper-prefix"));
String wrapns = node.getAttributeValue(new QName("wrapper-namespace"));
String contentType = node.getAttributeValue(new QName("content-type"));
String forcedContentType = node.getAttributeValue(cx_forced_content_type);

if (wrappfx != null && wrapns == null) {
throw XProcException.dynamicError(34, node, "You cannot specify a prefix without a namespace.");
Expand Down Expand Up @@ -756,6 +759,10 @@ private DataBinding readData(XdmNode node) {
doc.setContentType(contentType);
}

if (forcedContentType != null) {
doc.setForcedContentType(forcedContentType);
}

for (XdmNode snode : new RelevantNodes(runtime, node, Axis.CHILD)) {
throw new IllegalArgumentException("Unexpected in document: " + snode.getNodeName());
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/xmlcalabash/util/DefaultXMLCalabashConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public XdmNode loadDocument(Load load) {
}

public ReadablePipe makeReadableData(XProcRuntime runtime, DataBinding binding) {
return new ReadableData(runtime, binding.getWrapper(), binding.getHref(), binding.getContentType());
return new ReadableData(runtime, binding.getWrapper(), binding.getHref(), binding.getContentType(), binding.getForcedContentType());
}

public ReadablePipe makeReadableDocument(XProcRuntime runtime, DocumentBinding binding) {
Expand Down
13 changes: 7 additions & 6 deletions src/com/xmlcalabash/util/UserArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.xml.sax.InputSource;

import static com.xmlcalabash.core.XProcConstants.NS_XPROC;
import static com.xmlcalabash.core.XProcConstants.cx_forced_content_type;
import static com.xmlcalabash.core.XProcConstants.p_data;
import static com.xmlcalabash.core.XProcConstants.p_declare_step;
import static com.xmlcalabash.core.XProcConstants.p_document;
Expand Down Expand Up @@ -686,8 +687,8 @@ public XdmNode getImplicitPipeline(XProcRuntime runtime) throws IOException {
} else {
tree.addStartElement(qname);
tree.addAttribute(new QName("href"), uri);
if (input.getType() == DATA) {
tree.addAttribute(new QName("content-type"), input.getContentType());
if ((input.getType() == DATA) && (input.getContentType() != null)) {
tree.addAttribute(cx_forced_content_type, input.getContentType());
}
tree.startContent();
tree.addEndElement();
Expand All @@ -699,8 +700,8 @@ public XdmNode getImplicitPipeline(XProcRuntime runtime) throws IOException {
if (System.in.equals(inputStream)) {
tree.addStartElement(qname);
tree.addAttribute(new QName("href"), "-");
if (input.getType() == DATA) {
tree.addAttribute(new QName("content-type"), input.getContentType());
if ((input.getType() == DATA) && (input.getContentType() != null)) {
tree.addAttribute(cx_forced_content_type, input.getContentType());
}
tree.startContent();
tree.addEndElement();
Expand All @@ -714,8 +715,8 @@ public XdmNode getImplicitPipeline(XProcRuntime runtime) throws IOException {

tree.addStartElement(qname);
tree.addAttribute(new QName("href"), tempInput.toURI().toASCIIString());
if (input.getType() == DATA) {
tree.addAttribute(new QName("content-type"), input.getContentType());
if ((input.getType() == DATA) && (input.getContentType() != null)) {
tree.addAttribute(cx_forced_content_type, input.getContentType());
}
tree.startContent();
tree.addEndElement();
Expand Down

0 comments on commit 257a0e0

Please sign in to comment.