URL adminXSD = AdminXML.class.getResource("admin.xsd");
if (adminXSD==null) {
throw new RuntimeException("Cannot find admin.xsd relative to class path.");
}
validate.addNamespaceMap(AdminXML.NAMESPACE,adminXSD);
ItemFilter checkValidity = new ItemFilter() {
ItemDestination output;
int level = -1;
public void send(Item item)
throws XMLException
{
switch (item.getType()) {
case DocumentItem:
level = -1;
break;
case ElementItem:
level++;
break;
case ElementEndItem:
level--;
ElementEnd end = (ElementEnd)item;
if (level<0) {
if (end.getValidity()!=Validity.VALID) {
StringBuilder builder = new StringBuilder();
Iterator errors = validate.getErrors();
while (errors.hasNext()) {
builder.append("\n");
builder.append(errors.next().toString());
}
throw new XMLException("Element "+end.getName()+" is not valid:"+builder.toString());
}
Name name = end.getName();
boolean found = false;
for (int i=0; !found && i<names.length; i++) {
if (names[i].equals(name)) {
found = true;
}
}
if (!found) {
throw new XMLException("Unexpected document element "+name);
}
}
break;
}
output.send(item);
}
public void attach(ItemDestination output) {
this.output = output;
}
};
validate.attach(checkValidity);
checkValidity.attach(end);
return validate;
}