Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make it possible to override data input content type from command-line or Ant task without breaking tests and violating the specifications #287

Open
wants to merge 1 commit into
base: saxon97
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/xmlcalabash/core/XProcConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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/main/java/com/xmlcalabash/drivers/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ 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();
try {
rd = new ReadableData(runtime, c_data, inputStream, input.getContentType());
rd = new ReadableData(runtime, c_data, inputStream, null, input.getContentType());
doc = rd.read();
} finally {
Closer.close(inputStream);
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/com/xmlcalabash/io/ReadableData.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class ReadableData implements ReadablePipe {
public static final QName _encoding = new QName("","encoding");
public static final QName c_encoding = new QName("c",XProcConstants.NS_XPROC_STEP, "encoding");
private String contentType = null;
private String forcedContentType = null;
private Logger logger = LoggerFactory.getLogger(ReadablePipe.class);
private int pos = 0;
private QName wrapper = null;
Expand All @@ -64,19 +65,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);
}

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) {
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 @@ -96,7 +106,7 @@ private DocumentSequence ensureDocuments() {
try {
read(userContentType, null, inputStream, getContentType());
} finally {
// This is the only case where the inputStream should be
// This is the only case where the inputStream should be
// closed.
inputStream.close();
}
Expand All @@ -117,7 +127,7 @@ public void load(URI dataURI, String serverContentType,
serverContentType);
}
});
// Also no need to close the input stream here, since
// Also no need to close the input stream here, since
// DataStore implementations close the input stream when
// necessary.
}
Expand All @@ -127,7 +137,7 @@ public void load(URI dataURI, String serverContentType,
return documents;
}


// This method does NOT close the InputStream; it relies on the caller to close
// the InputStream, as appropriate.
private void read(String userContentType, URI dataURI,
Expand All @@ -149,7 +159,10 @@ private void read(String userContentType, URI dataURI,

TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(dataURI);
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/main/java/com/xmlcalabash/model/DataBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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 @@ -75,6 +76,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/main/java/com/xmlcalabash/model/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -770,6 +772,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 @@ -804,6 +807,10 @@ private DataBinding readData(XdmNode node) {
doc.setContentType(contentType);
}

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

for (XdmNode snode : new AxisNodes(runtime, node, Axis.CHILD, AxisNodes.PIPELINE)) {
throw new IllegalArgumentException("Unexpected in document: " + snode.getNodeName());
}
Expand Down
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
55 changes: 28 additions & 27 deletions src/main/java/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 @@ -651,14 +652,14 @@ public XdmNode getImplicitPipeline(XProcRuntime runtime) throws IOException {
InputStream libraryInputStream = library.getInputStream();
FileOutputStream fileOutputStream = null;
try {
File tempLibrary = createTempFile("calabashLibrary", null);
tempLibrary.deleteOnExit();
File tempLibrary = createTempFile("calabashLibrary", null);
tempLibrary.deleteOnExit();
fileOutputStream = new FileOutputStream(tempLibrary);
fileOutputStream.getChannel().transferFrom(newChannel(libraryInputStream), 0, MAX_VALUE);
fileOutputStream.getChannel().transferFrom(newChannel(libraryInputStream), 0, MAX_VALUE);
libraries.set(0, new Input(tempLibrary.toURI().toASCIIString()));
} finally {
Closer.close(fileOutputStream);
libraryInputStream.close();
libraryInputStream.close();
}
}

Expand Down Expand Up @@ -718,15 +719,15 @@ public XdmNode getImplicitPipeline(XProcRuntime runtime) throws IOException {
InputStream libraryInputStream = library.getInputStream();
FileOutputStream fileOutputStream = null;
try {
File tempLibrary = createTempFile("calabashLibrary", null);
tempLibrary.deleteOnExit();
File tempLibrary = createTempFile("calabashLibrary", null);
tempLibrary.deleteOnExit();
fileOutputStream = new FileOutputStream(tempLibrary);
fileOutputStream.getChannel().transferFrom(newChannel(libraryInputStream), 0, MAX_VALUE);
fileOutputStream.getChannel().transferFrom(newChannel(libraryInputStream), 0, MAX_VALUE);

tree.addStartElement(p_import);
tree.addAttribute(new QName("href"), tempLibrary.toURI().toASCIIString());
tree.startContent();
tree.addEndElement();
tree.addStartElement(p_import);
tree.addAttribute(new QName("href"), tempLibrary.toURI().toASCIIString());
tree.startContent();
tree.addEndElement();
} finally {
Closer.close(fileOutputStream);
libraryInputStream.close();
Expand Down Expand Up @@ -769,8 +770,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 @@ -782,30 +783,30 @@ 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();
} else {
FileOutputStream fileOutputStream = null;
try {
File tempInput = createTempFile("calabashInput", null);
tempInput.deleteOnExit();
File tempInput = createTempFile("calabashInput", null);
tempInput.deleteOnExit();
fileOutputStream = new FileOutputStream(tempInput);
fileOutputStream.getChannel().transferFrom(newChannel(inputStream), 0, MAX_VALUE);

tree.addStartElement(qname);
tree.addAttribute(new QName("href"), tempInput.toURI().toASCIIString());
if (input.getType() == DATA) {
tree.addAttribute(new QName("content-type"), input.getContentType());
}
tree.startContent();
tree.addEndElement();
fileOutputStream.getChannel().transferFrom(newChannel(inputStream), 0, MAX_VALUE);

tree.addStartElement(qname);
tree.addAttribute(new QName("href"), tempInput.toURI().toASCIIString());
if ((input.getType() == DATA) && (input.getContentType() != null)) {
tree.addAttribute(cx_forced_content_type, input.getContentType());
}
tree.startContent();
tree.addEndElement();
} finally {
Closer.close(fileOutputStream);
inputStream.close();
}
}
}
break;

Expand Down