String tableName = tablePrefix + element.getNodeName();
if (attributes.length > 0 || textContent != null || hasSiblings(element)) {
// We need to represent this type of node with a table
MutableTable table = (MutableTable) _schema.getTableByName(tableName);
Column idColumn;
MutableColumn foreignKeyColumn;
List<Object[]> tableRows;
if (table == null) {
logger.info("Creating table: {}", tableName);
table = new MutableTable(tableName, TableType.TABLE, _schema);
_schema.addTable(table);
idColumn = getIdColumn(table);
tableRows = new ArrayList<Object[]>();
_tableData.put(tableName, tableRows);
if (parentKeyColumn != null) {
Table parentTable = parentKeyColumn.getTable();
foreignKeyColumn = new MutableColumn(parentTable.getName() + "_id", parentKeyColumn.getType(), table,
table.getColumnCount(), false);
foreignKeyColumn.setNativeType(NATIVE_TYPE_FOREIGN_KEY);
table.addColumn(foreignKeyColumn);
MutableRelationship.createRelationship(new Column[] { parentKeyColumn }, new Column[] { foreignKeyColumn });
} else {
foreignKeyColumn = null;
}
} else {
idColumn = getIdColumn(table);
tableRows = _tableData.get(tableName);
Column[] foreignKeys = table.getForeignKeys();
if (foreignKeys.length == 1) {
foreignKeyColumn = (MutableColumn) foreignKeys[0];
} else {
foreignKeyColumn = null;
}
}
Column textContentColumn = getTextContentColumn(table, element.getNodeName());
Map<Column, String> columnValues = new HashMap<Column, String>();
for (Attr attr : attributes) {
String name = attr.getName();
MutableColumn column = (MutableColumn) table.getColumnByName(name);
if (column == null) {
logger.info("Creating column: {}.{}", tableName, name);
column = new MutableColumn(name, ColumnType.STRING, table, table.getColumnCount(), true);
column.setNativeType(NATIVE_TYPE_ATTRIBUTE);
table.addColumn(column);
}
columnValues.put(column, attr.getValue());
}