Skip to content

Commit

Permalink
Merge pull request #45 from fgeorges/master
Browse files Browse the repository at this point in the history
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 68ebfa4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
4 changes: 4 additions & 0 deletions src/com/xmlcalabash/core/XProcException.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ public Step getStep() {
return step; return step;
} }


public XdmNode getNode() {
return node;
}

public SourceLocator getLocator() { public SourceLocator getLocator() {
XdmNode locNode = null; XdmNode locNode = null;
if (step != null) locNode = step.getNode(); if (step != null) locNode = step.getNode();
Expand Down
4 changes: 3 additions & 1 deletion src/com/xmlcalabash/functions/IterationPosition.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.sf.saxon.om.SequenceIterator; import net.sf.saxon.om.SequenceIterator;
import com.xmlcalabash.core.XProcRuntime; import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.core.XProcConstants; import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcData;
import com.xmlcalabash.core.XProcException; import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.runtime.XStep; import com.xmlcalabash.runtime.XStep;
import net.sf.saxon.tree.iter.SingletonIterator; import net.sf.saxon.tree.iter.SingletonIterator;
Expand Down Expand Up @@ -80,7 +81,8 @@ public ExtensionFunctionCall makeCallExpression() {
private class IterationPositionCall extends ExtensionFunctionCall { private class IterationPositionCall extends ExtensionFunctionCall {
public SequenceIterator call(SequenceIterator[] arguments, XPathContext context) throws XPathException { public SequenceIterator call(SequenceIterator[] arguments, XPathContext context) throws XPathException {
XProcRuntime runtime = tl_runtime.get(); 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... // FIXME: this can't be the best way to do this...
// step == null in use-when // step == null in use-when
if (step != null && !(step instanceof XCompoundStep)) { if (step != null && !(step instanceof XCompoundStep)) {
Expand Down
71 changes: 51 additions & 20 deletions src/com/xmlcalabash/io/ReadableData.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* @author ndw * @author ndw
*/ */
public class ReadableData implements ReadablePipe { public class ReadableData implements ReadablePipe {
protected String contentType = null;
private Logger logger = Logger.getLogger(this.getClass().getName()); private Logger logger = Logger.getLogger(this.getClass().getName());
public static final QName _contentType = new QName("","content-type"); public static final QName _contentType = new QName("","content-type");
public static final QName c_contentType = new QName("c",XProcConstants.NS_XPROC_STEP, "content-type"); public static final QName c_contentType = new QName("c",XProcConstants.NS_XPROC_STEP, "content-type");
Expand All @@ -53,7 +54,7 @@ public class ReadableData implements ReadablePipe {
private int pos = 0; private int pos = 0;
private QName wrapper = null; private QName wrapper = null;
private String uri = null; private String uri = null;
private String contentType = null; private String serverContentType = null;
private XProcRuntime runtime = null; private XProcRuntime runtime = null;
private DocumentSequence documents = null; private DocumentSequence documents = null;
private Step reader = null; private Step reader = null;
Expand All @@ -64,30 +65,31 @@ public ReadableData(XProcRuntime runtime, QName wrapper, String uri, String cont
this.uri = uri; this.uri = uri;
this.wrapper = wrapper; this.wrapper = wrapper;
this.contentType = contentType; this.contentType = contentType;
documents = new DocumentSequence(runtime); }


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

documents = new DocumentSequence(runtime);


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


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


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


InputStream stream;

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


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


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


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


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


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


public boolean closed() { public boolean closed() {
return true; return true;
} }


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


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


public XdmNode read() throws SaxonApiException { public XdmNode read() throws SaxonApiException {
XdmNode doc = documents.get(pos++); DocumentSequence docs = ensureDocuments();
XdmNode doc = docs.get(pos++);
if (reader != null) { if (reader != null) {
runtime.finest(null, reader.getNode(), reader.getName() + " read '" + (doc == null ? "null" : doc.getBaseURI()) + "' from " + this); runtime.finest(null, reader.getNode(), reader.getName() + " read '" + (doc == null ? "null" : doc.getBaseURI()) + "' from " + this);
} }
Expand All @@ -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) { private boolean isText(String contentType, String charset) {
return ("application/xml".equals(contentType) return ("application/xml".equals(contentType)
|| contentType.endsWith("+xml") || contentType.endsWith("+xml")
Expand Down
8 changes: 4 additions & 4 deletions src/com/xmlcalabash/io/ReadableDocument.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
* @author ndw * @author ndw
*/ */
public class ReadableDocument implements ReadablePipe { public class ReadableDocument implements ReadablePipe {
protected DocumentSequence documents = null;
protected String uri = null;
protected XProcRuntime runtime = null;
private int pos = 0; private int pos = 0;
private DocumentSequence documents = null;
private String base = null; private String base = null;
private String uri = null;
private XProcRuntime runtime = null;
private XdmNode node = null; private XdmNode node = null;
private boolean readDoc = false; private boolean readDoc = false;
private Step reader = null; private Step reader = null;
Expand Down Expand Up @@ -125,7 +125,7 @@ public XdmNode read() throws SaxonApiException {
return doc; return doc;
} }


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


readDoc = true; readDoc = true;
Expand Down
6 changes: 1 addition & 5 deletions src/com/xmlcalabash/library/Error.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -109,11 +109,7 @@ public void run() throws SaxonApiException {


step.reportError(treeWriter.getResult()); step.reportError(treeWriter.getResult());


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


1 change: 1 addition & 0 deletions src/com/xmlcalabash/model/Parser.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1395,6 +1395,7 @@ private DeclareStep readDeclareStep(XdmNode node) {
} }
} }


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

0 comments on commit 68ebfa4

Please sign in to comment.