xml validation (no schema)

This commit is contained in:
Michele Artini 2022-12-06 14:41:07 +01:00
parent c68ae255a3
commit 8933fa2273
1 changed files with 14 additions and 11 deletions

View File

@ -9,12 +9,13 @@ import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
@ -46,17 +47,19 @@ public class ResourceValidator {
private void validateXml(final String type, final String content) throws InformationServiceException {
final Schema schema = getXmlSchema(type);
if (schema == null) {
log.warn("no registered schema for " + type);
return;
}
final Validator validator = schema.newValidator();
if (schema != null) {
try {
validator.validate(new StreamSource(new StringReader(content)));
schema.newValidator().validate(new StreamSource(new StringReader(content)));
} catch (final Exception e) {
throw new InformationServiceException(e.getMessage(), e);
}
} else {
try {
DocumentHelper.parseText(content);
} catch (final DocumentException e) {
throw new InformationServiceException(e.getMessage(), e);
}
}
}
private Schema getXmlSchema(final String type) {