Skip to content
Permalink
Browse files

Merge pull request #45 from fgeorges/master

Propagate the p:error input in the Java exception; reorganize code to make it easy to extend both classes; minor changes.
  • Loading branch information
ndw committed May 16, 2012
2 parents 54b6b63 + f1eb313 commit 68ebfa4d164663a8c38d5c6e18bf77c956ed4181
@@ -196,6 +196,10 @@ public Step getStep() {
return step;
}

public XdmNode getNode() {
return node;
}

public SourceLocator getLocator() {
XdmNode locNode = null;
if (step != null) locNode = step.getNode();
@@ -9,6 +9,7 @@
import net.sf.saxon.om.SequenceIterator;
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcData;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.runtime.XStep;
import net.sf.saxon.tree.iter.SingletonIterator;
@@ -80,7 +81,8 @@ public ExtensionFunctionCall makeCallExpression() {
private class IterationPositionCall extends ExtensionFunctionCall {
public SequenceIterator call(SequenceIterator[] arguments, XPathContext context) throws XPathException {
XProcRuntime runtime = tl_runtime.get();
XStep step = runtime.getXProcData().getStep();
XProcData data = runtime.getXProcData();
XStep step = data.getStep();
// FIXME: this can't be the best way to do this...
// step == null in use-when
if (step != null && !(step instanceof XCompoundStep)) {
@@ -45,6 +45,7 @@
* @author ndw
*/
public class ReadableData implements ReadablePipe {
protected String contentType = 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");
@@ -53,7 +54,7 @@
private int pos = 0;
private QName wrapper = null;
private String uri = null;
private String contentType = null;
private String serverContentType = null;
private XProcRuntime runtime = null;
private DocumentSequence documents = null;
private Step reader = null;
@@ -64,30 +65,31 @@ public ReadableData(XProcRuntime runtime, QName wrapper, String uri, String cont
this.uri = uri;
this.wrapper = wrapper;
this.contentType = contentType;
documents = new DocumentSequence(runtime);
}

String userContentType = parseContentType(contentType);
String userCharset = parseCharset(contentType);
private DocumentSequence ensureDocuments() {
if (documents != null) {
return documents;
}

documents = new DocumentSequence(runtime);

if (uri == null) {
return;
return documents;
}

URI dataURI;
try {
dataURI = new URI(uri);
} catch (URISyntaxException use) {
throw new XProcException(use);
}
String userContentType = parseContentType(contentType);
String userCharset = parseCharset(contentType);
URI dataURI = getDataUri(uri);

TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(dataURI);

InputStream stream;

try {
URL url = dataURI.toURL();
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
String serverContentType = connection.getContentType();
stream = getStream(dataURI);
String serverContentType = getContentType();

if ("content/unknown".equals(serverContentType) && contentType != null) {
// pretend...
@@ -109,7 +111,7 @@ public ReadableData(XProcRuntime runtime, QName wrapper, String uri, String cont
// FIXME: provide some way to override this!!!

String charset = serverCharset;
if ("file".equals(url.getProtocol())
if ("file".equals(dataURI.getScheme())
&& serverCharset == null
&& serverBaseContentType.equals(userContentType)) {
charset = userCharset;
@@ -199,6 +201,7 @@ public ReadableData(XProcRuntime runtime, QName wrapper, String uri, String cont
tree.endDocument();

documents.add(tree.getResult());
return documents;
}

public void canReadSequence(boolean sequence) {
@@ -218,23 +221,26 @@ public void setReader(Step step) {
}

public boolean moreDocuments() {
return pos < documents.size();
DocumentSequence docs = ensureDocuments();
return pos < docs.size();
}

public boolean closed() {
return true;
}

public int documentCount() {
return documents.size();
DocumentSequence docs = ensureDocuments();
return docs.size();
}

public DocumentSequence documents() {
return documents;
return ensureDocuments();
}

public XdmNode read() throws SaxonApiException {
XdmNode doc = documents.get(pos++);
DocumentSequence docs = ensureDocuments();
XdmNode doc = docs.get(pos++);
if (reader != null) {
runtime.finest(null, reader.getNode(), reader.getName() + " read '" + (doc == null ? "null" : doc.getBaseURI()) + "' from " + this);
}
@@ -243,6 +249,31 @@ public XdmNode read() throws SaxonApiException {

// =======================================================================

protected URI getDataUri(String uri) {
try {
return new URI(uri);
} catch (URISyntaxException use) {
throw new XProcException(use);
}
}

protected InputStream getStream(URI uri) {
try {
URL url = uri.toURL();
URLConnection connection = url.openConnection();
serverContentType = connection.getContentType();
return connection.getInputStream();
} catch (IOException ioe) {
throw new XProcException(XProcConstants.dynamicError(29), ioe);
}
}

protected String getContentType() {
return serverContentType;
}

// =======================================================================

private boolean isText(String contentType, String charset) {
return ("application/xml".equals(contentType)
|| contentType.endsWith("+xml")
@@ -43,11 +43,11 @@
* @author ndw
*/
public class ReadableDocument implements ReadablePipe {
protected DocumentSequence documents = null;
protected String uri = null;
protected XProcRuntime runtime = null;
private int pos = 0;
private DocumentSequence documents = null;
private String base = null;
private String uri = null;
private XProcRuntime runtime = null;
private XdmNode node = null;
private boolean readDoc = false;
private Step reader = null;
@@ -125,7 +125,7 @@ public XdmNode read() throws SaxonApiException {
return doc;
}

private void readDoc() {
protected void readDoc() {
XdmNode doc;

readDoc = true;
@@ -109,11 +109,7 @@ public void run() throws SaxonApiException {

step.reportError(treeWriter.getResult());

if (errorCode != null) {
throw new XProcException(errorCode, doc.getStringValue());
} else {
throw new XProcException();
}
throw new XProcException(errorCode, doc, doc.getStringValue());
}
}

@@ -1395,6 +1395,7 @@ private DeclareStep readDeclareStep(XdmNode node) {
}
}

System.err.println("step: " + step);
step.checkPrimaryIO();
rest = steps;
}

0 comments on commit 68ebfa4

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