private void addEmptyRow(Table nestedTable, HNode hNode) { HTable headersNestedTable = hNode.getNestedTable(); Row emptyRow = nestedTable.addRow(getFactory()); for (HNode nestedHNode : headersNestedTable.getHNodes()) { if (nestedHNode.hasNestedTable()) { addEmptyRow(emptyRow.getNode(nestedHNode.getId()).getNestedTable(), nestedHNode); } else { emptyRow.setValue(nestedHNode.getId(), "", getFactory()); } } }
private void addObjectElement(String key, Object value, HTable headers, Row row) throws JSONException { HNode hNode = addHNode(headers, key); String hNodeId = hNode.getId(); if (value instanceof String) { if (((String) value).isEmpty() && hNode.hasNestedTable()) { addEmptyRow(row.getNode(hNodeId).getNestedTable(), hNode); } row.setValue(hNodeId, (String) value, getFactory()); } else if (value instanceof Integer) { row.setValue(hNodeId, value.toString(), getFactory()); } else if (value instanceof Double) { row.setValue(hNodeId, value.toString(), getFactory()); } else if (value instanceof Long) { row.setValue(hNodeId, value.toString(), getFactory()); } else if (value instanceof Boolean) { row.setValue(hNodeId, value.toString(), getFactory()); } else if (value instanceof JSONObject) { HTable nestedHTable = addNestedHTable(hNode, key, row); Table nestedTable = row.getNode(hNodeId).getNestedTable(); addKeysAndValues((JSONObject) value, nestedHTable, nestedTable); } else if (value instanceof JSONArray) { HTable nestedHTable = addNestedHTable(hNode, key, row); Table nestedTable = row.getNode(hNodeId).getNestedTable(); JSONArray a = (JSONArray) value; for (int i = 0; i < a.length(); i++) { addListElement(a.get(i), nestedHTable, nestedTable); } } else if (value == JSONObject.NULL) { // Ignore } else { throw new Error("Cannot handle " + key + ": " + value + " yet."); } }
private void addListElement(Object listValue, HTable headers, Table dataTable) throws JSONException { if (listValue instanceof JSONObject) { Row row = dataTable.addRow(getFactory()); JSONObject o = (JSONObject) listValue; @SuppressWarnings("unchecked") Iterator<String> it = o.sortedKeys(); while (it.hasNext()) { String key = it.next(); addObjectElement(key, o.get(key), headers, row); } } else if (isPrimitiveValue(listValue)) { HNode hNode = addHNode(headers, "values"); String hNodeId = hNode.getId(); Row row = dataTable.addRow(getFactory()); // TODO, conserve the types of the primitive types. String value = ""; if (listValue instanceof String || listValue instanceof Boolean) { value = (String) listValue; } else if (listValue instanceof Double) { value = Double.toString((Double) listValue); } else if (listValue instanceof Integer) { value = Integer.toString((Integer) listValue); } else if (listValue instanceof Long) { value = Long.toString((Long) listValue); } else { // Pedro 2012/09/14 logger.error("Unexpected value in JSON array:" + listValue.toString()); } row.setValue(hNodeId, value, getFactory()); } else if (listValue instanceof JSONArray) { HNode hNode = addHNode(headers, "nested array"); String hNodeId = hNode.getId(); Row row = dataTable.addRow(getFactory()); HTable nestedHTable = addNestedHTable(hNode, "nested array values", row); Table nestedTable = row.getNode(hNodeId).getNestedTable(); JSONArray a = (JSONArray) listValue; for (int i = 0; i < a.length(); i++) { addListElement(a.get(i), nestedHTable, nestedTable); } } else { logger.error("Cannot handle whatever case is not covered by the if statements. Sorry."); } }