Commit 95cf27312deec48dcd68b7a319c0eb9eb102d5f2

Authored by Nathanael Jourdane
1 parent 6764621c
Exists in master

Add a model class for VOTableData

src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java 0 → 100644
... ... @@ -0,0 +1,61 @@
  1 +/*
  2 + * This file is a part of EpnTAPClient.
  3 + * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
  4 + * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
  5 + * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
  6 + *
  7 + * This program is free software: you can
  8 + * redistribute it and/or modify it under the terms of the GNU General Public License as published
  9 + * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
  10 + * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  11 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12 + * PURPOSE. See the GNU General Public License for more details. You should have received a copy of
  13 + * the GNU General Public License along with this program. If not, see
  14 + * <http://www.gnu.org/licenses/>.
  15 + */
  16 +
  17 +package eu.omp.irap.vespa.epntapclient.votable.data;
  18 +
  19 +import java.util.ArrayList;
  20 +import java.util.List;
  21 +
  22 +/**
  23 + * @author N. Jourdane
  24 + */
  25 +public class VOTableData {
  26 +
  27 + /**
  28 + * A list of arrays, representing data stored in the VOTable. Each element is a VOTable row,
  29 + * where arrays elements are in the same order as `columnNames`.
  30 + */
  31 + private List<Object[]> data;
  32 +
  33 + /** The name of the columns. `columnNames` length is equals to `data` length */
  34 + private String[] columnsName;
  35 +
  36 +
  37 + public VOTableData(String[] columnsName) {
  38 + this.columnsName = columnsName;
  39 + data = new ArrayList<>();
  40 + }
  41 +
  42 + public List<Object[]> getData() {
  43 + return data;
  44 + }
  45 +
  46 + public String[] getColumnsName() {
  47 + return columnsName;
  48 + }
  49 +
  50 + public int getNbRows() {
  51 + return data.size();
  52 + }
  53 +
  54 + public int getNbColumns() {
  55 + return columnsName.length;
  56 + }
  57 +
  58 + public void addRow(Object[] row) {
  59 + data.add(row);
  60 + }
  61 +}
... ...
src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java
... ... @@ -14,7 +14,7 @@
14 14 * <http://www.gnu.org/licenses/>.
15 15 */
16 16  
17   -package eu.omp.irap.vespa.epntapclient.votable.controller;
  17 +package eu.omp.irap.vespa.epntapclient.votable.data;
18 18  
19 19 import java.io.IOException;
20 20 import java.nio.ByteBuffer;
... ... @@ -38,23 +38,18 @@ import eu.omp.irap.vespa.epntapclient.votable.utils.Debug;
38 38 /**
39 39 * @author N. Jourdane
40 40 */
41   -public class VOTableDataParser {
  41 +public class VOTableDataCtrl {
42 42  
43   - /** The logger for the class VOTableDataParser. */
44   - private static final Logger logger = Logger.getLogger(VOTableDataParser.class.getName());
  43 + /** The logger for the class VOTableDataCtrl. */
  44 + private static final Logger logger = Logger.getLogger(VOTableDataCtrl.class.getName());
45 45  
46   - /**
47   - * A list of arrays, representing data stored in the VOTable. Each element is a VOTable row,
48   - * where arrays elements are in the same order as `columnNames`.
49   - */
50   - private List<Object[]> data;
51   -
52   - /** The name of the columns. `columnNames` length is equals to `data` length */
53   - private String[] columnsName;
  46 + private VOTableData data;
54 47  
55 48 /** The byte array stored in the VOTable Table stream (if any). */
56 49 private ByteBuffer stream;
57 50  
  51 + private int nbColumns;
  52 +
58 53 /** The index of the current position in the bytes array. */
59 54 private int cursor;
60 55  
... ... @@ -67,7 +62,7 @@ public class VOTableDataParser {
67 62 * @param table The table on which we want get the data.
68 63 * @throws IOException If the VOTable data can not be parsed.
69 64 */
70   - public VOTableDataParser(Table table) throws IOException {
  65 + public VOTableDataCtrl(Table table) throws IOException {
71 66 cursor = 0;
72 67  
73 68 List<Field> fields = new ArrayList<>();
... ... @@ -76,51 +71,46 @@ public class VOTableDataParser {
76 71 fields.add((Field) obj);
77 72 }
78 73 }
79   - columnsName = new String[fields.size()];
  74 + String[] columnsName = new String[fields.size()];
  75 + nbColumns = columnsName.length;
80 76 for (int i = 0; i < fields.size(); i++) {
81 77 columnsName[i] = fields.get(i).getName();
82 78 }
83   - VOTableDataParser.logger.info("Columns name: " + new Gson().toJson(columnsName));
  79 + data = new VOTableData(columnsName);
  80 + VOTableDataCtrl.logger.info("Columns name: " + new Gson().toJson(columnsName));
84 81  
85   - data = new ArrayList<>();
  82 + List<Object[]> rows = new ArrayList<>();
86 83  
87 84 if (table.getDATA().getBINARY() != null) {
88 85 parseBinaryStream(table.getDATA().getBINARY().getSTREAM(), fields);
89 86 } else if (table.getDATA().getBINARY2() != null) {
90   - VOTableDataParser.parseBinary2Stream(table.getDATA().getBINARY2().getSTREAM(), fields);
  87 + VOTableDataCtrl.parseBinary2Stream(table.getDATA().getBINARY2().getSTREAM(), fields);
91 88 } else if (table.getDATA().getTABLEDATA() != null) {
92   - VOTableDataParser.parseTableDataStream(table.getDATA().getTABLEDATA(), fields);
  89 + VOTableDataCtrl.parseTableDataStream(table.getDATA().getTABLEDATA(), fields);
93 90 } else if (table.getDATA().getFITS() != null) {
94   - VOTableDataParser.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields);
  91 + VOTableDataCtrl.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields);
95 92 }
96 93  
97   - Debug.printObject("voTableData", data);
  94 + Debug.printObject("voTableData", rows);
98 95 }
99 96  
100 97 /**
101 98 * @return the data stored in the Table.
102 99 */
103   - public List<Object[]> getDataArray() {
  100 + public VOTableData getData() {
104 101 return data;
105 102 }
106 103  
107 104 /**
108   - * @return An array of Table fields name.
109   - */
110   - public String[] getColumnsName() {
111   - return columnsName;
112   - }
113   -
114   - /**
115 105 * @param column The name of the column to get.
116 106 * @return An array of Table fields name.
117 107 * @throws IllegalArgumentException Column name not found in the table.
118 108 */
119 109 public int getColumnIndex(String column) {
120 110 int i = 0;
121   - while (!column.equals(columnsName[i])) {
  111 + while (!column.equals(data.getColumnsName()[i])) {
122 112 i++;
123   - if (i > columnsName.length) {
  113 + if (i > nbColumns) {
124 114 throw new IllegalArgumentException("Column " + column + " not found in the table.");
125 115 }
126 116 }
... ... @@ -128,25 +118,11 @@ public class VOTableDataParser {
128 118 }
129 119  
130 120 /**
131   - * @return The number of rows in the Table.
132   - */
133   - public int getNbRows() {
134   - return data.size();
135   - }
136   -
137   - /**
138   - * @return The number of columns in the Table.
139   - */
140   - public int getNbColumns() {
141   - return columnsName.length;
142   - }
143   -
144   - /**
145 121 * @param rowIndex The index of the row to get.
146 122 * @return The Table row at the specified index.
147 123 */
148 124 public Object[] getRowByIndex(int rowIndex) {
149   - return data.get(rowIndex);
  125 + return data.getData().get(rowIndex);
150 126 }
151 127  
152 128 /**
... ... @@ -155,8 +131,8 @@ public class VOTableDataParser {
155 131 */
156 132 public Map<String, Object> getDicRowByIndex(int rowIndex) {
157 133 Map<String, Object> row = new HashMap<>();
158   - for (int i = 0; i < columnsName.length; i++) {
159   - row.put(columnsName[i], data.get(rowIndex)[i]);
  134 + for (int i = 0; i < nbColumns; i++) {
  135 + row.put(data.getColumnsName()[i], data.getData().get(rowIndex)[i]);
160 136 }
161 137 return row;
162 138 }
... ... @@ -168,7 +144,7 @@ public class VOTableDataParser {
168 144 * @throws IndexOutOfBoundsException If the value is not found at the specified column.
169 145 */
170 146 public Object[] getRowByValue(int columnIndex, Object value) {
171   - for (Object[] row : data) {
  147 + for (Object[] row : data.getData()) {
172 148 if (value.equals(row[columnIndex])) {
173 149 return row;
174 150 }
... ... @@ -185,15 +161,15 @@ public class VOTableDataParser {
185 161 * @throws UnsupportedOperationException Data as arrays are not supported yet.
186 162 */
187 163 private void parseBinaryStream(Stream voStream, List<Field> fields) {
188   - VOTableDataParser.logger.info("Parsing data in BINARY stream...");
  164 + VOTableDataCtrl.logger.info("Parsing data in BINARY stream...");
189 165 String strStream = voStream.getValue().replaceAll("(\\r|\\n)", "");
190 166  
191 167 stream = ByteBuffer.wrap(DatatypeConverter.parseBase64Binary(strStream));
192   - Object[] row = new Object[columnsName.length];
  168 + Object[] row = new Object[nbColumns];
193 169  
194 170 int nValue = 0;
195 171 while (stream.hasRemaining()) {
196   - int nColumn = nValue % columnsName.length;
  172 + int nColumn = nValue % nbColumns;
197 173 Field column = fields.get(nColumn);
198 174 DataType dataType = column.getDatatype();
199 175  
... ... @@ -226,7 +202,7 @@ public class VOTableDataParser {
226 202 }
227 203  
228 204 if (nColumn == 0) {
229   - row = new Object[columnsName.length];
  205 + row = new Object[nbColumns];
230 206 }
231 207  
232 208 if (dataType.equals(DataType.BOOLEAN)) {
... ... @@ -258,14 +234,14 @@ public class VOTableDataParser {
258 234 double value = stream.getDouble();
259 235 row[nColumn] = Double.isNaN(value) ? null : value;
260 236 } else {
261   - VOTableDataParser.logger.warning("Data type " + dataType + " is not supported.");
  237 + VOTableDataCtrl.logger.warning("Data type " + dataType + " is not supported.");
262 238 }
263 239  
264 240 // logger.debug(columnsName[nColumn] + ": " + row[nColumn])
265 241  
266 242 row[nColumn] = row[nColumn];
267   - if (nColumn == columnsName.length - 1) {
268   - data.add(row);
  243 + if (nColumn == nbColumns - 1) {
  244 + data.addRow(row);
269 245 }
270 246 nValue++;
271 247 }
... ... @@ -277,7 +253,7 @@ public class VOTableDataParser {
277 253 * @param fields The Fields corresponding to the Table.
278 254 */
279 255 private static void parseBinary2Stream(Stream stream, List<Field> fields) {
280   - VOTableDataParser.logger.info("Parsing data in BINARY2 stream...");
  256 + VOTableDataCtrl.logger.info("Parsing data in BINARY2 stream...");
281 257 }
282 258  
283 259 /**
... ... @@ -285,7 +261,7 @@ public class VOTableDataParser {
285 261 * @param fields The Fields corresponding to the Table.
286 262 */
287 263 private static void parseTableDataStream(TableData tabledata, List<Field> fields) {
288   - VOTableDataParser.logger.info("Parsing data in TABLEDATA stream...");
  264 + VOTableDataCtrl.logger.info("Parsing data in TABLEDATA stream...");
289 265 }
290 266  
291 267 /**
... ... @@ -293,6 +269,6 @@ public class VOTableDataParser {
293 269 * @param fields The Fields corresponding to the Table.
294 270 */
295 271 private static void parseFITSStream(Stream stream, List<Field> fields) {
296   - VOTableDataParser.logger.info("Parsing data in FITS stream...");
  272 + VOTableDataCtrl.logger.info("Parsing data in FITS stream...");
297 273 }
298 274 }
... ...