📄 xmldatasetschemareader.java
字号:
public DataSetParser(DataSet ds, String... tableNames) {
this.ds = ds == null ? new DataSet() : ds;
this.tableNames = new ArrayList(tableNames.length);
Collections.addAll(this.tableNames, tableNames);
}
public DataSet getDataSet() {
return ds;
}
public void startDocument() throws SAXException {
super.startDocument();
this.tablesAdded = new ArrayList<String>();
this.relationsAdded = new ArrayList<String>();
}
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
this.attrs = atts;
if (qName.equals("xs:element")) {
elementDepth++;
switch(elementDepth) {
case 1:
//this is a DataSet
ds.setName(attrs.getValue("name"));
break;
case 2:
//this is a table tag
String tableName = attrs.getValue("name");
// skip table if already in DataSet
if ( ds.getTable(tableName) != null ) {
System.out.println("TABLE " + tableName + " ALREADY IN DATASET; SKIPPING.");
table = null;
break;
}
if ( tableNames.size() > 0 && !tableNames.contains(tableName)) {
table = null;
break;
}
table = ds.createTable(tableName);
String val = attrs.getValue("appendRowSupported");
table.setAppendRowSupported(val == null || val.equalsIgnoreCase("true"));
val = attrs.getValue("deleteRowSupported");
table.setDeleteRowSupported(val == null || val.equalsIgnoreCase("true"));
tablesAdded.add(tableName);
break;
case 3:
if ( table == null ) break;
//this is a column tag: get the immutable attributes
String colName = attrs.getValue("name");
String colExpression = attrs.getValue("expression");
String colType = attrs.getValue("type");
if (colExpression != null)
column = table.createExpression(colName, colExpression);
else
column = table.createColumn(decodeColumnType(colType), colName);
//set the required flag
val = attrs.getValue("minOccurs");
if (val != null && val.equals("")) {
column.setRequired(true);
}
//find out if this is a keycolumn
val = attrs.getValue("keyColumn");
column.setKeyColumn(val == null ? false : val.equalsIgnoreCase("true"));
//find out if this column is readOnly
val = attrs.getValue("readOnly");
column.setReadOnly(val == null ? false : val.equalsIgnoreCase("true"));
//grab the default, if one is supplied
String defaultValue = attrs.getValue("default"); //TODO This will require some kind of type conversion
if (defaultValue != null && !defaultValue.equals(""))
column.setDefaultValue(getDefaultValue(defaultValue));
break;
default:
//error condition
System.out.println("Error in DataSetParser");
}
} else if (qName.equals("dataProvider")) {
String classType = attrs.getValue("class");
if (classType != null) {
try {
dataProvider = (DataProvider)Class.forName(classType).newInstance();
table.setDataProvider(dataProvider);
//TODO There needs to be a more general configuration solution
if (dataProvider instanceof SQLDataProvider) {
String tableName = attrs.getValue("tableName");
if (tableName != null && !tableName.equals("")) {
TableCommand cmd = new TableCommand(tableName);
cmd.setWhereClause(attrs.getValue("whereClause"));
cmd.setOrderByClause(attrs.getValue("orderByClause"));
cmd.setHavingClause(attrs.getValue("havingClause"));
dataProvider.setCommand(cmd);
} else {
SQLCommand command = new SQLCommand();
// CLEAN: setCustom() is no longer available (PWW 11/01/2005)
// command.setCustom(true);
command.setSelectSQL(attrs.getValue("select"));
command.setInsertSQL(attrs.getValue("insert"));
command.setUpdateSQL(attrs.getValue("update"));
command.setDeleteSQL(attrs.getValue("delete"));
dataProvider.setCommand(command);
}
}
} catch (Exception e) {
//hmmm
e.printStackTrace();
}
}
} else if (qName.equals("dataRelation")) {
String relationName = attrs.getValue("name");
String parentColumnName = attrs.getValue("parentColumn");
String childColumnName = attrs.getValue("childColumn");
String parentTableName = parentColumnName.substring(0, parentColumnName.indexOf("."));
String childTableName = childColumnName.substring(0, childColumnName.indexOf("."));
try {
if ( tableNames.size() > 0 &&
tableNames.contains(parentTableName) &&
tableNames.contains(childTableName)) {
DataColumn parentColumn = (DataColumn)ds.getElement(parentColumnName);
DataColumn childColumn = (DataColumn)ds.getElement(childColumnName);
if ( parentColumn != null && childColumn != null ) {
DataRelation relation = ds.createRelation(relationName, parentColumn, childColumn);
relationsAdded.add(relationName);
}
} else {
System.err.println("DataRelation: Either parent " + parentColumnName + " or child " + childColumnName +
" is missing in DataSet. Tables might not be loaded, or the respective columns might not be. " +
"Skipping.");
}
} catch ( Exception e ) {
System.out.println("failed on Parent: " + parentColumnName);
}
} else if (qName.equals("dataValue")) {
try {
DataValue v = ds.createValue(attrs.getValue("name"), attrs.getValue("expression"));
} catch (IllegalArgumentException x) {
throw new org.xml.sax.SAXException(x.getMessage(), x);
}
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("xs:element")) {
switch(elementDepth) {
case 1:
//this is a dataset tag
break;
case 2:
//this is a table tag
break;
case 3:
//this is a column tag
break;
default:
//error condition
System.out.println("Error in DataSetParser");
}
elementDepth--;
}
}
public void endDocument() throws SAXException {
}
public List<String> getTablesAdded() {
return tablesAdded;
}
public List<String> getRelationsAdded() {
return relationsAdded;
}
private Class decodeColumnType(String type) {
if (type.equals("xs:string"))
return String.class;
if (type.equals("xs:decimal"))
return BigDecimal.class;
if (type.equals("xs:integer") || type.equals("xs:int"))
return Integer.class;
if (type.equals("xs:boolean"))
return Boolean.class;
if (type.equals("xs:date") || type.equals("xs:time") || type.equals("xs.dateTime"))
return Date.class;
if (type.equals("xs:unsignedByte"))
return Byte.class;
LOG.log(Level.WARNING, "unexpected classType: '{0}'", type);
return Object.class;
}
private Object getDefaultValue(String value) {
Class type = column.getType();
if (type == String.class)
return value;
if (type == BigDecimal.class)
return new BigDecimal(value);
if (type == Integer.class)
return Integer.decode(value);
if (type == Boolean.class)
return Boolean.valueOf(value);
if (type == Date.class)
return new Date(Date.parse(value));
if (type == Byte.class)
return Byte.valueOf(value);
LOG.log(Level.WARNING, "unexpected classType: '{0}'", type.getName());
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -