xml validation (no schema)
This commit is contained in:
parent
c68ae255a3
commit
8933fa2273
|
@ -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,16 +47,18 @@ 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();
|
||||
|
||||
try {
|
||||
validator.validate(new StreamSource(new StringReader(content)));
|
||||
} catch (final Exception e) {
|
||||
throw new InformationServiceException(e.getMessage(), e);
|
||||
if (schema != null) {
|
||||
try {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue