Author: jm
Date: 2012-10-22 09:44:06 -0700 (Mon, 22 Oct 2012)
New Revision: 30688
Modified:
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVCyWriter.java
Log:
Fixes #1526: Added support for serializing column mutability. Added backwards
compatibility with older schema format
Modified:
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
===================================================================
---
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
2012-10-19 23:54:31 UTC (rev 30687)
+++
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/read/datatable/CSVCyReader.java
2012-10-22 16:44:06 UTC (rev 30688)
@@ -146,6 +146,14 @@
TableInfo readHeader(CSVReader reader) throws IOException,
ClassNotFoundException {
String[] values = reader.readNext();
+ int schemaVersion;
+ if (values.length == 2 && "CyCSV-Version".equals(values[0])) {
+ schemaVersion = Integer.parseInt(values[1]);
+ values = reader.readNext();
+ } else {
+ schemaVersion = 0;
+ }
+
TableInfo table = new TableInfo();
ColumnInfo[] columns = new ColumnInfo[values.length];
for (int i = 0; i < values.length; i++) {
@@ -157,32 +165,24 @@
if (!readSchema) {
return table;
}
- values = reader.readNext();
- for (int i = 0; i < values.length; i++) {
- Matcher matcher = classPattern.matcher(values[i]);
- matcher.matches();
- String typeName = matcher.group(1);
- Class<?> type = Class.forName(typeName);
- if (type.equals(List.class)) {
- String elementName = matcher.group(3);
- Class<?> elementType =
Class.forName(elementName);
- columns[i].setListElementType(elementType);
- } else {
- columns[i].setType(type);
- }
- }
- values = reader.readNext();
- table.setTitle(values[0]);
- for (String option : values[1].split(",")) {
- if ("public".equals(option)) {
- table.setPublic(true);
- } else if ("mutable".equals(option)) {
- table.setMutable(true);
- }
- }
+
+ SchemaDelegate delegate = getSchemaDelegate(schemaVersion);
+ delegate.readSchema(reader, table);
+
return table;
}
+ private SchemaDelegate getSchemaDelegate(int schemaVersion) {
+ switch (schemaVersion) {
+ case 0:
+ return new SchemaDelegate0();
+ case 1:
+ return new SchemaDelegate1();
+ default:
+ throw new IllegalArgumentException("Unsupported CyCSV
version: " + schemaVersion);
+ }
+ }
+
@Override
public CyTable[] getTables() {
if (table == null) {
@@ -191,4 +191,67 @@
return new CyTable[] { table };
}
+ static interface SchemaDelegate {
+ void readSchema(CSVReader reader, TableInfo table) throws
IOException, ClassNotFoundException;
+ }
+
+ static class AbstractSchemaDelegate implements SchemaDelegate {
+ @Override
+ public void readSchema(CSVReader reader, TableInfo table)
throws IOException, ClassNotFoundException {
+ handleColumnTypes(reader, table);
+ handleColumnOptions(reader, table);
+ handleTableOptions(reader, table);
+ }
+
+ protected void handleTableOptions(CSVReader reader, TableInfo
table) throws IOException {
+ String[] values = reader.readNext();
+ table.setTitle(values[0]);
+ for (String option : values[1].split(",")) {
+ if ("public".equals(option)) {
+ table.setPublic(true);
+ } else if ("mutable".equals(option)) {
+ table.setMutable(true);
+ }
+ }
+ }
+
+ protected void handleColumnOptions(CSVReader reader, TableInfo
table) throws IOException, ClassNotFoundException {
+ ColumnInfo[] columns = table.getColumns();
+ String[] values = reader.readNext();
+ for (int i = 0; i < values.length; i++) {
+ for (String option : values[i].split(",")) {
+ if ("mutable".equals(option)) {
+ columns[i].setMutable(true);
+ }
+ }
+ }
+ }
+
+ protected void handleColumnTypes(CSVReader reader, TableInfo
table) throws IOException, ClassNotFoundException {
+ ColumnInfo[] columns = table.getColumns();
+ String[] values = reader.readNext();
+ for (int i = 0; i < values.length; i++) {
+ Matcher matcher =
classPattern.matcher(values[i]);
+ matcher.matches();
+ String typeName = matcher.group(1);
+ Class<?> type = Class.forName(typeName);
+ if (type.equals(List.class)) {
+ String elementName = matcher.group(3);
+ Class<?> elementType =
Class.forName(elementName);
+
columns[i].setListElementType(elementType);
+ } else {
+ columns[i].setType(type);
+ }
+ }
+ }
+ }
+
+ static class SchemaDelegate0 extends AbstractSchemaDelegate {
+ @Override
+ protected void handleColumnOptions(CSVReader reader, TableInfo
table) throws IOException, ClassNotFoundException {
+ }
+ }
+
+ static class SchemaDelegate1 extends AbstractSchemaDelegate {
+ }
}
Modified:
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVCyWriter.java
===================================================================
---
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVCyWriter.java
2012-10-19 23:54:31 UTC (rev 30687)
+++
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/datatable/csv/CSVCyWriter.java
2012-10-22 16:44:06 UTC (rev 30688)
@@ -20,6 +20,7 @@
public class CSVCyWriter implements CyWriter {
+ private static final int SCHEMA_VERSION = 1;
private final OutputStream outputStream;
private final CyTable table;
private final boolean writeSchema;
@@ -72,6 +73,9 @@
}
});
taskMonitor.setProgress(0.4);
+ if (writeSchema) {
+ writeVersion(writer);
+ }
writeHeader(writer, columns);
if (writeSchema) {
writeSchema(writer, columns);
@@ -84,19 +88,27 @@
taskMonitor.setProgress(1.0);
}
+ private void writeVersion(CSVWriter writer) {
+ writer.writeNext(new String[] { "CyCSV-Version",
String.valueOf(SCHEMA_VERSION) });
+ }
+
private void writeSchema(CSVWriter writer, List<CyColumn> columns) {
- String[] values = new String[columns.size()];
+ String[] types = new String[columns.size()];
+ String[] options = new String[columns.size()];
for (int i = 0; i < columns.size(); i++) {
CyColumn column = columns.get(i);
Class<?> type = column.getType();
if (List.class.isAssignableFrom(type)) {
- values[i] = String.format("%s<%s>",
List.class.getCanonicalName(), column.getListElementType().getCanonicalName());
+ types[i] = String.format("%s<%s>",
List.class.getCanonicalName(), column.getListElementType().getCanonicalName());
} else {
- values[i] = type.getCanonicalName();
+ types[i] = type.getCanonicalName();
}
+ options[i] = column.isImmutable() ? "" : "mutable";
}
- writer.writeNext(values);
- values = new String[2];
+ writer.writeNext(types);
+ writer.writeNext(options);
+
+ String[] values = new String[2];
values[0] = table.getTitle();
StringBuilder builder = new StringBuilder();
--
You received this message because you are subscribed to the Google Groups
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/cytoscape-cvs?hl=en.