NIHVIVO-3753 fix sparql csv out to remain rectangular with optional clauses

This commit is contained in:
brianjlowe 2012-06-14 16:31:42 +00:00
parent c348c140af
commit 909c2adeee

View file

@ -272,13 +272,16 @@ public class SparqlQueryServlet extends BaseEditController {
private void toCsv(Writer out, ResultSet results) { private void toCsv(Writer out, ResultSet results) {
// The Skife library wouldn't quote and escape the normal way, // The Skife library wouldn't quote and escape the normal way,
// so I'm trying it manually. // so I'm trying it manually.
List<String> varNames = results.getResultVars();
int width = varNames.size();
while (results.hasNext()) { while (results.hasNext()) {
QuerySolution solution = (QuerySolution) results.next(); QuerySolution solution = (QuerySolution) results.next();
List<String> valueList = new LinkedList<String>(); String[] valueArray = new String[width];
Iterator<String> varNameIt = solution.varNames(); Iterator<String> varNameIt = varNames.iterator();
int index = 0;
while (varNameIt.hasNext()) { while (varNameIt.hasNext()) {
String varName = varNameIt.next(); String varName = varNameIt.next();
String value = ""; String value = null;
try { try {
Literal lit = solution.getLiteral(varName); Literal lit = solution.getLiteral(varName);
if (lit != null) { if (lit != null) {
@ -290,24 +293,27 @@ public class SparqlQueryServlet extends BaseEditController {
} catch (Exception e) { } catch (Exception e) {
try { try {
Resource res = solution.getResource(varName); Resource res = solution.getResource(varName);
if (res != null) {
if (res.isAnon()) { if (res.isAnon()) {
value = res.getId().toString(); value = res.getId().toString();
} else { } else {
value = res.getURI(); value = res.getURI();
} }
}
} catch (Exception f) {} } catch (Exception f) {}
} }
valueList.add(value); valueArray[index] = value;
index++;
} }
Iterator<String> valueIt = valueList.iterator();
StringBuffer rowBuff = new StringBuffer(); StringBuffer rowBuff = new StringBuffer();
while (valueIt.hasNext()) { for (int i = 0; i < valueArray.length; i++) {
String value = valueIt.next(); String value = valueArray[i];
if (value != null) {
value.replaceAll("\"", "\\\""); value.replaceAll("\"", "\\\"");
rowBuff.append("\"").append(value).append("\""); rowBuff.append("\"").append(value).append("\"");
if (valueIt.hasNext()) { }
if (i + 1 < width) {
rowBuff.append(","); rowBuff.append(",");
} }
} }