From b629fa1f13691d91f4ee5ca4df50869517645527 Mon Sep 17 00:00:00 2001 From: MBorne Date: Thu, 8 Aug 2024 17:39:30 +0200 Subject: [PATCH] refactor(model): validator-core - remove support for XML models (refs #351) --- .../command/DocumentValidatorCommand.java | 2 - .../ign/validator/io/AbstractModelReader.java | 2 +- .../java/fr/ign/validator/io/ModelReader.java | 2 +- .../ign/validator/io/ModelReaderFactory.java | 4 +- .../fr/ign/validator/io/XmlModelReader.java | 71 ----------------- .../io/xml/AttributeTypeAdapter.java | 79 ------------------- .../validator/io/xml/FileModelAdapter.java | 58 -------------- .../fr/ign/validator/model/AttributeType.java | 3 - .../validator/model/AttributeTypeFactory.java | 2 +- .../fr/ign/validator/model/DocumentModel.java | 13 --- .../fr/ign/validator/model/FeatureType.java | 15 ---- .../fr/ign/validator/model/FileModel.java | 8 -- 12 files changed, 6 insertions(+), 253 deletions(-) delete mode 100644 validator-core/src/main/java/fr/ign/validator/io/XmlModelReader.java delete mode 100644 validator-core/src/main/java/fr/ign/validator/io/xml/AttributeTypeAdapter.java delete mode 100644 validator-core/src/main/java/fr/ign/validator/io/xml/FileModelAdapter.java diff --git a/validator-core/src/main/java/fr/ign/validator/command/DocumentValidatorCommand.java b/validator-core/src/main/java/fr/ign/validator/command/DocumentValidatorCommand.java index bbcc7200..4b21b3e0 100644 --- a/validator-core/src/main/java/fr/ign/validator/command/DocumentValidatorCommand.java +++ b/validator-core/src/main/java/fr/ign/validator/command/DocumentValidatorCommand.java @@ -20,8 +20,6 @@ import org.apache.logging.log4j.MarkerManager; import org.locationtech.jts.geom.Geometry; -import com.ctc.wstx.util.StringUtil; - import fr.ign.validator.Context; import fr.ign.validator.Version; import fr.ign.validator.command.options.DocumentEmpriseOption; diff --git a/validator-core/src/main/java/fr/ign/validator/io/AbstractModelReader.java b/validator-core/src/main/java/fr/ign/validator/io/AbstractModelReader.java index 7bfdc1d0..42284334 100644 --- a/validator-core/src/main/java/fr/ign/validator/io/AbstractModelReader.java +++ b/validator-core/src/main/java/fr/ign/validator/io/AbstractModelReader.java @@ -21,7 +21,7 @@ import fr.ign.validator.tools.ModelHelper; /** - * Common implementation for JSON and XML ModelReader. + * Common implementation for JSON and XML ModelReader (removed). * * @author MBorne */ diff --git a/validator-core/src/main/java/fr/ign/validator/io/ModelReader.java b/validator-core/src/main/java/fr/ign/validator/io/ModelReader.java index 00a79ad8..1c27ebfe 100644 --- a/validator-core/src/main/java/fr/ign/validator/io/ModelReader.java +++ b/validator-core/src/main/java/fr/ign/validator/io/ModelReader.java @@ -21,7 +21,7 @@ public interface ModelReader { public String getFormat(); /** - * Read File as a DocumentModel (files.xml) + * Read File as a DocumentModel * * @param documentModelPath * @return diff --git a/validator-core/src/main/java/fr/ign/validator/io/ModelReaderFactory.java b/validator-core/src/main/java/fr/ign/validator/io/ModelReaderFactory.java index bfe3f233..60e722bb 100644 --- a/validator-core/src/main/java/fr/ign/validator/io/ModelReaderFactory.java +++ b/validator-core/src/main/java/fr/ign/validator/io/ModelReaderFactory.java @@ -2,6 +2,8 @@ import java.net.URL; +import fr.ign.validator.exception.InvalidModelException; + /** * Create ModelReader instances * @@ -26,7 +28,7 @@ private ModelReaderFactory() { */ public static ModelReader createModelReader(URL url) { if (url.toString().endsWith(".xml")) { - return new XmlModelReader(); + throw new InvalidModelException("Fail to load "+url+" (XML model support has been removed, use JSON format)"); } else { return new JsonModelReader(); } diff --git a/validator-core/src/main/java/fr/ign/validator/io/XmlModelReader.java b/validator-core/src/main/java/fr/ign/validator/io/XmlModelReader.java deleted file mode 100644 index d38c4476..00000000 --- a/validator-core/src/main/java/fr/ign/validator/io/XmlModelReader.java +++ /dev/null @@ -1,71 +0,0 @@ -package fr.ign.validator.io; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; - -import fr.ign.validator.exception.InvalidModelException; -import fr.ign.validator.exception.ModelNotFoundException; -import fr.ign.validator.model.DocumentModel; -import fr.ign.validator.model.FeatureType; - -/** - * Load models using deprecated XML format - * - * @author MBorne - */ -public class XmlModelReader extends AbstractModelReader { - - private JAXBContext context; - private Unmarshaller unmarshaller; - - public XmlModelReader() { - log.trace(MARKER, "Initializing XmlModelReader..."); - try { - this.context = JAXBContext.newInstance(FeatureType.class, DocumentModel.class); - this.unmarshaller = context.createUnmarshaller(); - } catch (JAXBException e) { - throw new RuntimeException("fail to load JAXB context", e); - } - } - - @Override - public String getFormat() { - return "xml"; - } - - @Override - public DocumentModel loadDocumentModel(URL documentModelUrl) throws ModelNotFoundException, InvalidModelException { - log.info(MARKER, "Loading DocumentModel from {} ...", documentModelUrl); - - /* - * loading documentModel - */ - InputStream is = getInputStream(documentModelUrl); - try { - DocumentModel documentModel = (DocumentModel) unmarshaller.unmarshal(is); - loadFeatureTypes(documentModel, documentModelUrl); - return documentModel; - } catch (JAXBException | IOException e) { - String message = String.format("Fail to load FeatureType from %1s", documentModelUrl); - throw new InvalidModelException(message, e); - } - } - - @Override - public FeatureType loadFeatureType(URL featureTypeUrl) throws ModelNotFoundException, InvalidModelException { - log.info(MARKER, "loadFeatureType({}) ...", featureTypeUrl); - InputStream is = getInputStream(featureTypeUrl); - try { - return (FeatureType) unmarshaller.unmarshal(is); - } catch (JAXBException e) { - String message = String.format("Fail to load FeatureType from %1s", featureTypeUrl); - throw new InvalidModelException(message, e); - } - } - -} diff --git a/validator-core/src/main/java/fr/ign/validator/io/xml/AttributeTypeAdapter.java b/validator-core/src/main/java/fr/ign/validator/io/xml/AttributeTypeAdapter.java deleted file mode 100644 index 67c246e6..00000000 --- a/validator-core/src/main/java/fr/ign/validator/io/xml/AttributeTypeAdapter.java +++ /dev/null @@ -1,79 +0,0 @@ -package fr.ign.validator.io.xml; - -import java.util.List; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlType; -import javax.xml.bind.annotation.adapters.XmlAdapter; - -import fr.ign.validator.model.AttributeConstraints; -import fr.ign.validator.model.AttributeType; - -@XmlType(propOrder = { - "name", "type", "definition", "regexp", "size", "nullable", "listOfValues", "identifiant", "reference" -}) -public class AttributeTypeAdapter extends XmlAdapter> { - - /** - * Legacy XML format - * - * @author MBorne - */ - public static class AdaptedAttributeType { - public String name; - public String type; - public String definition; - public String regexp; - public Integer size; - public boolean nullable; - public boolean identifier; - public String reference; - @XmlElementWrapper(name = "listOfValues") - @XmlElement(name = "value") - public List listOfValues; - } - - @Override - public AdaptedAttributeType marshal(AttributeType attributeType) throws Exception { - if (null == attributeType) { - return null; - } - - AdaptedAttributeType adaptedAttributeType = new AdaptedAttributeType(); - adaptedAttributeType.name = attributeType.getName(); - adaptedAttributeType.type = attributeType.getTypeName(); - adaptedAttributeType.definition = attributeType.getDescription(); - - AttributeConstraints constraints = attributeType.getConstraints(); - adaptedAttributeType.nullable = !constraints.isRequired(); - adaptedAttributeType.identifier = constraints.isUnique(); - adaptedAttributeType.regexp = constraints.getPattern(); - adaptedAttributeType.size = constraints.getMaxLength(); - adaptedAttributeType.listOfValues = constraints.getEnumValues(); - adaptedAttributeType.reference = constraints.getReference(); - return adaptedAttributeType; - } - - @Override - public AttributeType unmarshal(AttributeTypeAdapter.AdaptedAttributeType adaptedValueType) throws Exception { - if (null == adaptedValueType) { - return null; - } - - AttributeType attributeType = AttributeType.forName(adaptedValueType.type); - attributeType.setName(adaptedValueType.name); - attributeType.setDescription(adaptedValueType.definition); - - AttributeConstraints constraints = attributeType.getConstraints(); - constraints.setRequired(!adaptedValueType.nullable); - constraints.setUnique(adaptedValueType.identifier); - constraints.setPattern(adaptedValueType.regexp); - constraints.setMaxLength(adaptedValueType.size); - constraints.getEnumValues(adaptedValueType.listOfValues); - constraints.setReference(adaptedValueType.reference); - - return attributeType; - } - -} diff --git a/validator-core/src/main/java/fr/ign/validator/io/xml/FileModelAdapter.java b/validator-core/src/main/java/fr/ign/validator/io/xml/FileModelAdapter.java deleted file mode 100644 index 2ddb8edb..00000000 --- a/validator-core/src/main/java/fr/ign/validator/io/xml/FileModelAdapter.java +++ /dev/null @@ -1,58 +0,0 @@ -package fr.ign.validator.io.xml; - -import javax.xml.bind.annotation.adapters.XmlAdapter; - -import fr.ign.validator.model.FileModel; -import fr.ign.validator.model.FileModelFactory; -import fr.ign.validator.model.FileModel.MandatoryMode; - -/** - * FileModel serialization adapter - * - * Note that "regexp" is renamed to "path" - * - * @author MBorne - * - */ -public class FileModelAdapter extends XmlAdapter { - - public static class AdaptedFileModel { - public String name; - public String type; - public String path; - public MandatoryMode mandatory = MandatoryMode.WARN; - } - - @Override - public FileModel unmarshal(AdaptedFileModel adaptedFileModel) throws Exception { - if (null == adaptedFileModel) { - return null; - } - FileModel fileModel = FileModelFactory.createFileModelByType(adaptedFileModel.type); - if (null == fileModel) { - throw new Exception( - String.format( - "attribut type invalide dans balise : {}", adaptedFileModel.type - ) - ); - } - fileModel.setName(adaptedFileModel.name); - fileModel.setPath(adaptedFileModel.path); - fileModel.setMandatory(adaptedFileModel.mandatory); - return fileModel; - } - - @Override - public AdaptedFileModel marshal(FileModel fileModel) throws Exception { - if (null == fileModel) { - return null; - } - AdaptedFileModel adaptedFileModel = new AdaptedFileModel(); - adaptedFileModel.type = fileModel.getType(); - adaptedFileModel.name = fileModel.getName(); - adaptedFileModel.path = fileModel.getPath(); - adaptedFileModel.mandatory = fileModel.getMandatory(); - return adaptedFileModel; - } - -} diff --git a/validator-core/src/main/java/fr/ign/validator/model/AttributeType.java b/validator-core/src/main/java/fr/ign/validator/model/AttributeType.java index 0805a8d5..c8ba3b29 100644 --- a/validator-core/src/main/java/fr/ign/validator/model/AttributeType.java +++ b/validator-core/src/main/java/fr/ign/validator/model/AttributeType.java @@ -4,7 +4,6 @@ import java.util.List; import javax.xml.bind.annotation.XmlTransient; -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; @@ -12,7 +11,6 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include; import fr.ign.validator.data.Attribute; -import fr.ign.validator.io.xml.AttributeTypeAdapter; import fr.ign.validator.validation.Validator; import fr.ign.validator.validation.attribute.AttributeRequiredValidator; import fr.ign.validator.validation.attribute.CharactersValidator; @@ -24,7 +22,6 @@ * * @param the matching java type */ -@XmlJavaTypeAdapter(AttributeTypeAdapter.class) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonInclude(value = Include.NON_NULL) public abstract class AttributeType implements Model, Cloneable { diff --git a/validator-core/src/main/java/fr/ign/validator/model/AttributeTypeFactory.java b/validator-core/src/main/java/fr/ign/validator/model/AttributeTypeFactory.java index fc0def50..44e96ae1 100644 --- a/validator-core/src/main/java/fr/ign/validator/model/AttributeTypeFactory.java +++ b/validator-core/src/main/java/fr/ign/validator/model/AttributeTypeFactory.java @@ -10,7 +10,7 @@ * Prototype based factory providing AttributeType creation by name (Boolean, * Double, etc.) * - * @see src/main/resources/META-INF/services/fr.ign.validator.model.AttributeType + * @see src/main/resources/META-INF/services/fr.ign.validator.model.AttributeType containing the list of this types. * * @author MBorne * diff --git a/validator-core/src/main/java/fr/ign/validator/model/DocumentModel.java b/validator-core/src/main/java/fr/ign/validator/model/DocumentModel.java index 31af6852..773e1621 100644 --- a/validator-core/src/main/java/fr/ign/validator/model/DocumentModel.java +++ b/validator-core/src/main/java/fr/ign/validator/model/DocumentModel.java @@ -4,12 +4,6 @@ import java.util.ArrayList; import java.util.List; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlTransient; -import javax.xml.bind.annotation.XmlType; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; @@ -33,10 +27,6 @@ * * @author MBorne */ -@XmlRootElement(name = "document") -@XmlType(propOrder = { - "name", "regexp", "fileModels", "staticTables" -}) public class DocumentModel implements Model { public static final Logger log = LogManager.getRootLogger(); public static final Marker MARKER = MarkerManager.getMarker("DocumentModel"); @@ -103,7 +93,6 @@ public void setName(String name) { this.name = name; } - @XmlTransient public DocumentConstraints getConstraints() { return constraints; } @@ -132,8 +121,6 @@ public void setRegexp(String regexp) { this.constraints.setFolderName(regexp); } - @XmlElementWrapper(name = "files") - @XmlElement(name = "file") @JsonProperty("files") public List getFileModels() { return fileModels; diff --git a/validator-core/src/main/java/fr/ign/validator/model/FeatureType.java b/validator-core/src/main/java/fr/ign/validator/model/FeatureType.java index 5ea34eda..69782074 100644 --- a/validator-core/src/main/java/fr/ign/validator/model/FeatureType.java +++ b/validator-core/src/main/java/fr/ign/validator/model/FeatureType.java @@ -3,12 +3,6 @@ import java.util.ArrayList; import java.util.List; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlTransient; -import javax.xml.bind.annotation.XmlType; - import com.fasterxml.jackson.annotation.JsonProperty; import fr.ign.validator.model.type.GeometryType; @@ -18,10 +12,6 @@ * * @author MBorne */ -@XmlRootElement -@XmlType(propOrder = { - "name", "description", "attributes", "constraints" -}) public class FeatureType implements Model { /** * Parent (optional) @@ -72,13 +62,11 @@ public FeatureType getParent() { * * @param featureType */ - @XmlTransient public void setParent(FeatureType parent) { this.parent = parent; } @Override - @XmlElement(name = "typeName") public String getName() { return name; } @@ -120,7 +108,6 @@ public String getDescription() { return description; } - @XmlElement public void setDescription(String description) { this.description = description; } @@ -136,8 +123,6 @@ public List> getAttributes() { return attributes; } - @XmlElementWrapper(name = "attributes") - @XmlElement(name = "attribute") @JsonProperty("columns") public void setAttributes(List> attributes) { this.attributes = attributes; diff --git a/validator-core/src/main/java/fr/ign/validator/model/FileModel.java b/validator-core/src/main/java/fr/ign/validator/model/FileModel.java index fddcba4e..f323ad36 100644 --- a/validator-core/src/main/java/fr/ign/validator/model/FileModel.java +++ b/validator-core/src/main/java/fr/ign/validator/model/FileModel.java @@ -7,10 +7,6 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlEnum; -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; @@ -24,7 +20,6 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo; import fr.ign.validator.data.DocumentFile; -import fr.ign.validator.io.xml.FileModelAdapter; import fr.ign.validator.model.file.DirectoryModel; import fr.ign.validator.model.file.MetadataModel; import fr.ign.validator.model.file.MultiTableModel; @@ -38,7 +33,6 @@ * * @author MBorne */ -@XmlJavaTypeAdapter(FileModelAdapter.class) @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") @JsonSubTypes({ @Type(value = DirectoryModel.class, name = DirectoryModel.TYPE), @@ -51,7 +45,6 @@ public abstract class FileModel implements Model { public static final Logger log = LogManager.getRootLogger(); public static final Marker MARKER = MarkerManager.getMarker("FileModel"); - @XmlEnum(String.class) public enum MandatoryMode { /** * Ignore if file is missing @@ -129,7 +122,6 @@ public String getPath() { return path; } - @XmlElement(name = "regexp") public void setPath(String path) { this.path = path; }