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

CSV serialization for result formats of new W3C parsing functions #2363

Closed
Closed
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
Original file line number Diff line number Diff line change
@@ -14,17 +14,19 @@
*/
public final class CsvXmlConverter extends CsvConverter {
/** QName. */
protected static final QNm Q_FN_CSV = new QNm("csv", QueryText.FN_URI);
public static final QNm Q_FN_CSV = new QNm("csv", QueryText.FN_URI);
/** QName. */
protected static final QNm Q_FN_ROWS = new QNm("rows", QueryText.FN_URI);
/** QName. */
protected static final QNm Q_FN_ROW = new QNm("row", QueryText.FN_URI);
public static final QNm Q_FN_ROW = new QNm("row", QueryText.FN_URI);
/** QName. */
protected static final QNm Q_FN_FIELD = new QNm("field", QueryText.FN_URI);
/** QName. */
protected static final QNm Q_FN_COLUMNS = new QNm("columns", QueryText.FN_URI);
/** QName. */
protected static final QNm Q_FN_COLUMN = new QNm("column", QueryText.FN_URI);
public static final QNm Q_FN_COLUMN = new QNm("column", QueryText.FN_URI);
/** QName. */
public static final QNm Q_COLUMN = new QNm("column");

/** Document node. */
private FBuilder doc;
@@ -71,7 +73,7 @@ protected void entry(final byte[] value) {

final FBuilder elem = FElem.build(Q_FN_FIELD);
final byte[] name = headers.get(column);
if(name != null && name.length > 0) elem.add(Q_FN_COLUMN, name);
if(name != null && name.length > 0) elem.add(Q_COLUMN, name);
record.add(elem.add(shared.token(value)));
}

8 changes: 1 addition & 7 deletions basex-core/src/main/java/org/basex/io/serial/Serializer.java
Original file line number Diff line number Diff line change
@@ -6,8 +6,6 @@
import java.io.*;
import java.util.*;

import org.basex.build.csv.*;
import org.basex.build.csv.CsvOptions.*;
import org.basex.build.json.*;
import org.basex.build.json.JsonOptions.*;
import org.basex.data.*;
@@ -81,11 +79,7 @@ public static Serializer get(final OutputStream os, final SerializerOptions sopt
case XHTML: return new XHTMLSerializer(os, so);
case HTML: return new HTMLSerializer(os, so);
case TEXT: return new TextSerializer(os, so);
case CSV:
final CsvOptions copts = so.get(SerializerOptions.CSV);
return copts.get(CsvOptions.FORMAT) == CsvFormat.XQUERY
? new CsvXQuerySerializer(os, so)
: new CsvDirectSerializer(os, so);
case CSV: return CsvSerializer.get(os, so);
case JSON:
final JsonSerialOptions jopts = so.get(SerializerOptions.JSON);
final JsonFormat jformat = jopts.get(JsonOptions.FORMAT);
Original file line number Diff line number Diff line change
@@ -7,14 +7,14 @@
import java.io.*;
import java.util.function.*;

import org.basex.build.csv.*;
import org.basex.build.json.*;
import org.basex.core.*;
import org.basex.io.*;
import org.basex.query.*;
import org.basex.query.expr.path.*;
import org.basex.query.util.hash.*;
import org.basex.query.value.item.*;
import org.basex.query.value.map.*;
import org.basex.query.value.node.*;
import org.basex.query.value.type.*;
import org.basex.util.*;
@@ -102,8 +102,8 @@ public final class SerializerOptions extends Options {
new EnumOption<>("json-lines", YesNo.NO);

/** Specific serialization parameter. */
public static final OptionsOption<CsvOptions> CSV =
new OptionsOption<>("csv", new CsvOptions());
public static final ValueOption CSV =
new ValueOption("csv", SeqType.MAP_ZO, XQMap.empty());
/** Specific serialization parameter. */
public static final OptionsOption<JsonSerialOptions> JSON =
new OptionsOption<>("json", new JsonSerialOptions());
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.basex.io.serial.csv;

import static org.basex.query.QueryError.*;

import java.io.*;

import org.basex.build.csv.*;
import org.basex.io.serial.*;
import org.basex.query.*;
import org.basex.query.value.*;
import org.basex.query.value.array.*;
import org.basex.query.value.item.*;
import org.basex.util.list.*;

/**
* This class serializes a sequence of arrays as CSV. The input must conform to the result
* format of fn:csv-to-arrays.
*
* @author BaseX Team, BSD License
* @author Gunther Rademacher
*/
public final class CsvArraysSerializer extends CsvSerializer {
/**
* Constructor.
* @param os output stream
* @param sopts serialization parameters
* @param copts csv options
* @throws IOException I/O exception
*/
public CsvArraysSerializer(final OutputStream os, final SerializerOptions sopts,
final CsvOptions copts) throws IOException {
super(os, sopts, copts);
}

@Override
public void serialize(final Item item) throws IOException {
if(!(item instanceof XQArray))
throw CSV_SERIALIZE_X_X.getIO("Array expected, found " + item.seqType(), item);
final TokenList tl = new TokenList();
try {
for(final Value value : ((XQArray) item).iterable()) {
if(!value.isItem()) throw CSV_SERIALIZE_X_X.getIO(
"Item expected, found " + value.seqType(), value);
tl.add(((Item) value).string(null));
}
} catch(final QueryException ex) {
throw new QueryIOException(ex);
}
record(tl);
}
}
Original file line number Diff line number Diff line change
@@ -37,12 +37,13 @@ public final class CsvDirectSerializer extends CsvSerializer {
* Constructor.
* @param os output stream
* @param sopts serialization parameters
* @param copts csv options
* @throws IOException I/O exception
*/
public CsvDirectSerializer(final OutputStream os, final SerializerOptions sopts)
throws IOException {
public CsvDirectSerializer(final OutputStream os, final SerializerOptions sopts,
final CsvOptions copts) throws IOException {

super(os, sopts);
super(os, sopts, copts);
headers = header ? new TokenList() : null;
atts = copts.get(CsvOptions.FORMAT) == CsvFormat.ATTRIBUTES;
lax = copts.get(CsvOptions.LAX) || atts;
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.basex.io.serial.csv;

import static org.basex.query.QueryError.*;

import java.io.*;

import org.basex.build.csv.*;
import org.basex.io.serial.*;
import org.basex.query.*;
import org.basex.query.func.fn.*;
import org.basex.query.value.*;
import org.basex.query.value.array.*;
import org.basex.query.value.item.*;
import org.basex.query.value.map.*;
import org.basex.util.list.*;

/**
* This class serializes a map as CSV. The input must conform to the result format of
* fn:parse-csvs.
*
* @author BaseX Team, BSD License
* @author Gunther Rademacher
*/
public final class CsvMapSerializer extends CsvSerializer {
/**
* Constructor.
* @param os output stream
* @param sopts serialization parameters
* @param copts csv options
* @throws IOException I/O exception
*/
public CsvMapSerializer(final OutputStream os, final SerializerOptions sopts,
final CsvOptions copts) throws IOException {
super(os, sopts, copts);
}

@Override
public void serialize(final Item item) throws IOException {
if(sep && level == 0) out.print(' ');

if(!(item instanceof XQMap))
throw CSV_SERIALIZE_X_X.getIO("Top level must be a map, found " + item.type, item);

final XQMap m = (XQMap) item;
final TokenList tl = new TokenList();
try {
// print header
if(header) {
if(!m.contains(FnParseCsv.COLUMNS)) throw CSV_SERIALIZE_X.getIO("Map has no 'columns' key");
row(m.get(FnParseCsv.COLUMNS), tl);
}
// print rows
if(!m.contains(FnParseCsv.ROWS)) throw CSV_SERIALIZE_X.getIO("Map has no 'rows' key");
for(final Item record : m.get(FnParseCsv.ROWS)) row(((XQArray) record).iterable(), tl);
} catch(final QueryException ex) {
throw new QueryIOException(ex);
}
sep = true;
}

/**
* Serializes a single line (header or contents).
* @param line line to be serialized
* @param tl token list
* @throws QueryException query exception
* @throws IOException I/O exception
*/
private void row(final Iterable<? extends Value> line, final TokenList tl)
throws QueryException, IOException {
for(final Value value : line) {
if(!value.isItem()) throw CSV_SERIALIZE_X_X.getIO(
"Item expected, found " + value.seqType(), value);
tl.add(((Item) value).string(null));
}
record(tl);
}
}
Loading