2013-07-03 21:05:08 +02:00
|
|
|
/*
|
|
|
|
* Licensed to ElasticSearch and Shay Banon under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. ElasticSearch licenses this
|
|
|
|
* file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.elasticsearch.common;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
2013-12-23 12:14:25 -06:00
|
|
|
import com.google.common.collect.Maps;
|
2014-01-06 21:58:46 +01:00
|
|
|
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
2013-07-03 21:05:08 +02:00
|
|
|
|
2013-12-30 16:12:00 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2013-07-03 21:05:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public class Table {
|
|
|
|
|
2013-12-30 16:12:00 +01:00
|
|
|
private List<Cell> headers = new ArrayList<Cell>();
|
|
|
|
private List<List<Cell>> rows = new ArrayList<List<Cell>>();
|
|
|
|
private Map<String, List<Cell>> map = Maps.newHashMap();
|
|
|
|
private List<Cell> currentCells;
|
|
|
|
private boolean inHeaders = false;
|
2013-07-03 21:05:08 +02:00
|
|
|
|
|
|
|
public Table startHeaders() {
|
|
|
|
inHeaders = true;
|
|
|
|
currentCells = new ArrayList<Cell>();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Table endHeaders() {
|
|
|
|
inHeaders = false;
|
|
|
|
headers = currentCells;
|
|
|
|
currentCells = null;
|
2013-12-23 12:14:25 -06:00
|
|
|
|
|
|
|
/* Create associative structure for columns that
|
|
|
|
* contain the same cells as the rows:
|
|
|
|
*
|
|
|
|
* header1 => [Cell, Cell, ...]
|
|
|
|
* header2 => [Cell, Cell, ...]
|
|
|
|
* header3 => [Cell, Cell, ...]
|
|
|
|
*/
|
|
|
|
for (Cell header : headers) {
|
2013-12-30 16:12:00 +01:00
|
|
|
map.put(header.value.toString(), new ArrayList<Cell>());
|
2013-12-23 12:14:25 -06:00
|
|
|
}
|
|
|
|
|
2013-07-03 21:05:08 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Table startRow() {
|
|
|
|
if (headers.isEmpty()) {
|
2014-01-06 21:58:46 +01:00
|
|
|
throw new ElasticsearchIllegalArgumentException("no headers added...");
|
2013-07-03 21:05:08 +02:00
|
|
|
}
|
|
|
|
currentCells = new ArrayList<Cell>(headers.size());
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-12-23 12:12:50 -06:00
|
|
|
public Table endRow(boolean check) {
|
|
|
|
if (check && (currentCells.size() != headers.size())) {
|
2014-01-03 09:22:02 -06:00
|
|
|
StringBuilder s = new StringBuilder();
|
|
|
|
s.append("mismatch on number of cells ");
|
|
|
|
s.append(currentCells.size());
|
|
|
|
s.append(" in a row compared to header ");
|
|
|
|
s.append(headers.size());
|
2014-01-06 21:58:46 +01:00
|
|
|
throw new ElasticsearchIllegalArgumentException(s.toString());
|
2013-07-03 21:05:08 +02:00
|
|
|
}
|
|
|
|
rows.add(currentCells);
|
|
|
|
currentCells = null;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-12-23 12:12:50 -06:00
|
|
|
public Table endRow() {
|
|
|
|
endRow(true);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-07-03 21:05:08 +02:00
|
|
|
public Table addCell(Object value) {
|
|
|
|
return addCell(value, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
public Table addCell(Object value, String attributes) {
|
|
|
|
if (!inHeaders) {
|
|
|
|
if (currentCells.size() == headers.size()) {
|
2014-01-06 21:58:46 +01:00
|
|
|
throw new ElasticsearchIllegalArgumentException("can't add more cells to a row than the header");
|
2013-07-03 21:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Map<String, String> mAttr;
|
|
|
|
if (attributes.length() == 0) {
|
|
|
|
if (inHeaders) {
|
|
|
|
mAttr = ImmutableMap.of();
|
|
|
|
} else {
|
|
|
|
// get the attributes of the header cell we are going to add to
|
|
|
|
mAttr = headers.get(currentCells.size()).attr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mAttr = new HashMap<String, String>();
|
|
|
|
if (!inHeaders) {
|
|
|
|
// get the attributes of the header cell we are going to add
|
|
|
|
mAttr.putAll(headers.get(currentCells.size()).attr);
|
|
|
|
}
|
2013-11-26 22:14:03 +01:00
|
|
|
String[] sAttrs = Strings.splitStringToArray(attributes, ';');
|
2013-07-03 21:05:08 +02:00
|
|
|
for (String sAttr : sAttrs) {
|
|
|
|
if (sAttr.length() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-04 18:58:53 -05:00
|
|
|
int idx = sAttr.indexOf(':');
|
2013-07-03 21:05:08 +02:00
|
|
|
mAttr.put(sAttr.substring(0, idx), sAttr.substring(idx + 1));
|
|
|
|
}
|
|
|
|
}
|
2013-12-24 11:01:01 -06:00
|
|
|
|
|
|
|
Cell cell = new Cell(value, mAttr);
|
2013-12-30 16:12:00 +01:00
|
|
|
int cellIndex = currentCells.size();
|
2013-12-24 11:01:01 -06:00
|
|
|
currentCells.add(cell);
|
|
|
|
|
|
|
|
// If we're in a value row, also populate the named column.
|
|
|
|
if (!inHeaders) {
|
2013-12-30 16:12:00 +01:00
|
|
|
String hdr = (String) headers.get(cellIndex).value;
|
2013-12-24 11:01:01 -06:00
|
|
|
map.get(hdr).add(cell);
|
|
|
|
}
|
|
|
|
|
2013-07-03 21:05:08 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Cell> getHeaders() {
|
|
|
|
return this.headers;
|
|
|
|
}
|
|
|
|
|
2013-12-23 12:14:25 -06:00
|
|
|
public List<List<Cell>> getRows() {
|
2013-07-03 21:05:08 +02:00
|
|
|
return rows;
|
|
|
|
}
|
|
|
|
|
2013-12-30 16:12:00 +01:00
|
|
|
public Map<String, List<Cell>> getAsMap() {
|
|
|
|
return this.map;
|
2013-12-23 12:14:25 -06:00
|
|
|
}
|
|
|
|
|
2013-12-30 16:12:00 +01:00
|
|
|
public Cell findHeaderByName(String header) {
|
|
|
|
for (Cell cell : headers) {
|
|
|
|
if (cell.value.toString().equals(header)) {
|
|
|
|
return cell;
|
2013-12-23 12:12:50 -06:00
|
|
|
}
|
|
|
|
}
|
2013-12-30 16:12:00 +01:00
|
|
|
return null;
|
2013-12-23 12:12:50 -06:00
|
|
|
}
|
|
|
|
|
2013-07-03 21:05:08 +02:00
|
|
|
public static class Cell {
|
|
|
|
public final Object value;
|
|
|
|
public final Map<String, String> attr;
|
|
|
|
|
2014-01-03 00:41:26 +01:00
|
|
|
public Cell(Object value, Cell other) {
|
|
|
|
this.value = value;
|
|
|
|
this.attr = other.attr;
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:07:51 -05:00
|
|
|
public Cell(Object value) {
|
|
|
|
this.value = value;
|
|
|
|
this.attr = new HashMap<String, String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Cell(Object value, Map<String, String> attr) {
|
2013-07-03 21:05:08 +02:00
|
|
|
this.value = value;
|
|
|
|
this.attr = attr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|