From 95cf27312deec48dcd68b7a319c0eb9eb102d5f2 Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane <nathanael.jourdane@irap.omp.eu> Date: Thu, 21 Apr 2016 18:08:08 +0200 Subject: [PATCH] Add a model class for VOTableData --- src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java | 92 ++++++++++++++++++++++++++++++++++---------------------------------------------------------- 2 files changed, 95 insertions(+), 58 deletions(-) create mode 100644 src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java new file mode 100644 index 0000000..52b7181 --- /dev/null +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java @@ -0,0 +1,61 @@ +/* + * This file is a part of EpnTAPClient. + * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer. + * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861 + * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie. + * + * This program is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 3 of the License, or (at your option) any later + * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. You should have received a copy of + * the GNU General Public License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + */ + +package eu.omp.irap.vespa.epntapclient.votable.data; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author N. Jourdane + */ +public class VOTableData { + + /** + * A list of arrays, representing data stored in the VOTable. Each element is a VOTable row, + * where arrays elements are in the same order as `columnNames`. + */ + private List<Object[]> data; + + /** The name of the columns. `columnNames` length is equals to `data` length */ + private String[] columnsName; + + + public VOTableData(String[] columnsName) { + this.columnsName = columnsName; + data = new ArrayList<>(); + } + + public List<Object[]> getData() { + return data; + } + + public String[] getColumnsName() { + return columnsName; + } + + public int getNbRows() { + return data.size(); + } + + public int getNbColumns() { + return columnsName.length; + } + + public void addRow(Object[] row) { + data.add(row); + } +} diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java index a47d892..029d0c4 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java @@ -14,7 +14,7 @@ * <http://www.gnu.org/licenses/>. */ -package eu.omp.irap.vespa.epntapclient.votable.controller; +package eu.omp.irap.vespa.epntapclient.votable.data; import java.io.IOException; import java.nio.ByteBuffer; @@ -38,23 +38,18 @@ import eu.omp.irap.vespa.epntapclient.votable.utils.Debug; /** * @author N. Jourdane */ -public class VOTableDataParser { +public class VOTableDataCtrl { - /** The logger for the class VOTableDataParser. */ - private static final Logger logger = Logger.getLogger(VOTableDataParser.class.getName()); + /** The logger for the class VOTableDataCtrl. */ + private static final Logger logger = Logger.getLogger(VOTableDataCtrl.class.getName()); - /** - * A list of arrays, representing data stored in the VOTable. Each element is a VOTable row, - * where arrays elements are in the same order as `columnNames`. - */ - private List<Object[]> data; - - /** The name of the columns. `columnNames` length is equals to `data` length */ - private String[] columnsName; + private VOTableData data; /** The byte array stored in the VOTable Table stream (if any). */ private ByteBuffer stream; + private int nbColumns; + /** The index of the current position in the bytes array. */ private int cursor; @@ -67,7 +62,7 @@ public class VOTableDataParser { * @param table The table on which we want get the data. * @throws IOException If the VOTable data can not be parsed. */ - public VOTableDataParser(Table table) throws IOException { + public VOTableDataCtrl(Table table) throws IOException { cursor = 0; List<Field> fields = new ArrayList<>(); @@ -76,51 +71,46 @@ public class VOTableDataParser { fields.add((Field) obj); } } - columnsName = new String[fields.size()]; + String[] columnsName = new String[fields.size()]; + nbColumns = columnsName.length; for (int i = 0; i < fields.size(); i++) { columnsName[i] = fields.get(i).getName(); } - VOTableDataParser.logger.info("Columns name: " + new Gson().toJson(columnsName)); + data = new VOTableData(columnsName); + VOTableDataCtrl.logger.info("Columns name: " + new Gson().toJson(columnsName)); - data = new ArrayList<>(); + List<Object[]> rows = new ArrayList<>(); if (table.getDATA().getBINARY() != null) { parseBinaryStream(table.getDATA().getBINARY().getSTREAM(), fields); } else if (table.getDATA().getBINARY2() != null) { - VOTableDataParser.parseBinary2Stream(table.getDATA().getBINARY2().getSTREAM(), fields); + VOTableDataCtrl.parseBinary2Stream(table.getDATA().getBINARY2().getSTREAM(), fields); } else if (table.getDATA().getTABLEDATA() != null) { - VOTableDataParser.parseTableDataStream(table.getDATA().getTABLEDATA(), fields); + VOTableDataCtrl.parseTableDataStream(table.getDATA().getTABLEDATA(), fields); } else if (table.getDATA().getFITS() != null) { - VOTableDataParser.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields); + VOTableDataCtrl.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields); } - Debug.printObject("voTableData", data); + Debug.printObject("voTableData", rows); } /** * @return the data stored in the Table. */ - public List<Object[]> getDataArray() { + public VOTableData getData() { return data; } /** - * @return An array of Table fields name. - */ - public String[] getColumnsName() { - return columnsName; - } - - /** * @param column The name of the column to get. * @return An array of Table fields name. * @throws IllegalArgumentException Column name not found in the table. */ public int getColumnIndex(String column) { int i = 0; - while (!column.equals(columnsName[i])) { + while (!column.equals(data.getColumnsName()[i])) { i++; - if (i > columnsName.length) { + if (i > nbColumns) { throw new IllegalArgumentException("Column " + column + " not found in the table."); } } @@ -128,25 +118,11 @@ public class VOTableDataParser { } /** - * @return The number of rows in the Table. - */ - public int getNbRows() { - return data.size(); - } - - /** - * @return The number of columns in the Table. - */ - public int getNbColumns() { - return columnsName.length; - } - - /** * @param rowIndex The index of the row to get. * @return The Table row at the specified index. */ public Object[] getRowByIndex(int rowIndex) { - return data.get(rowIndex); + return data.getData().get(rowIndex); } /** @@ -155,8 +131,8 @@ public class VOTableDataParser { */ public Map<String, Object> getDicRowByIndex(int rowIndex) { Map<String, Object> row = new HashMap<>(); - for (int i = 0; i < columnsName.length; i++) { - row.put(columnsName[i], data.get(rowIndex)[i]); + for (int i = 0; i < nbColumns; i++) { + row.put(data.getColumnsName()[i], data.getData().get(rowIndex)[i]); } return row; } @@ -168,7 +144,7 @@ public class VOTableDataParser { * @throws IndexOutOfBoundsException If the value is not found at the specified column. */ public Object[] getRowByValue(int columnIndex, Object value) { - for (Object[] row : data) { + for (Object[] row : data.getData()) { if (value.equals(row[columnIndex])) { return row; } @@ -185,15 +161,15 @@ public class VOTableDataParser { * @throws UnsupportedOperationException Data as arrays are not supported yet. */ private void parseBinaryStream(Stream voStream, List<Field> fields) { - VOTableDataParser.logger.info("Parsing data in BINARY stream..."); + VOTableDataCtrl.logger.info("Parsing data in BINARY stream..."); String strStream = voStream.getValue().replaceAll("(\\r|\\n)", ""); stream = ByteBuffer.wrap(DatatypeConverter.parseBase64Binary(strStream)); - Object[] row = new Object[columnsName.length]; + Object[] row = new Object[nbColumns]; int nValue = 0; while (stream.hasRemaining()) { - int nColumn = nValue % columnsName.length; + int nColumn = nValue % nbColumns; Field column = fields.get(nColumn); DataType dataType = column.getDatatype(); @@ -226,7 +202,7 @@ public class VOTableDataParser { } if (nColumn == 0) { - row = new Object[columnsName.length]; + row = new Object[nbColumns]; } if (dataType.equals(DataType.BOOLEAN)) { @@ -258,14 +234,14 @@ public class VOTableDataParser { double value = stream.getDouble(); row[nColumn] = Double.isNaN(value) ? null : value; } else { - VOTableDataParser.logger.warning("Data type " + dataType + " is not supported."); + VOTableDataCtrl.logger.warning("Data type " + dataType + " is not supported."); } // logger.debug(columnsName[nColumn] + ": " + row[nColumn]) row[nColumn] = row[nColumn]; - if (nColumn == columnsName.length - 1) { - data.add(row); + if (nColumn == nbColumns - 1) { + data.addRow(row); } nValue++; } @@ -277,7 +253,7 @@ public class VOTableDataParser { * @param fields The Fields corresponding to the Table. */ private static void parseBinary2Stream(Stream stream, List<Field> fields) { - VOTableDataParser.logger.info("Parsing data in BINARY2 stream..."); + VOTableDataCtrl.logger.info("Parsing data in BINARY2 stream..."); } /** @@ -285,7 +261,7 @@ public class VOTableDataParser { * @param fields The Fields corresponding to the Table. */ private static void parseTableDataStream(TableData tabledata, List<Field> fields) { - VOTableDataParser.logger.info("Parsing data in TABLEDATA stream..."); + VOTableDataCtrl.logger.info("Parsing data in TABLEDATA stream..."); } /** @@ -293,6 +269,6 @@ public class VOTableDataParser { * @param fields The Fields corresponding to the Table. */ private static void parseFITSStream(Stream stream, List<Field> fields) { - VOTableDataParser.logger.info("Parsing data in FITS stream..."); + VOTableDataCtrl.logger.info("Parsing data in FITS stream..."); } } -- libgit2 0.21.2