Skip to content

Commit

Permalink
Merge pull request #24 from laurentC35/master
Browse files Browse the repository at this point in the history
Update XSD schema
  • Loading branch information
laurentC35 authored Apr 14, 2020
2 parents e226dc7 + 071bdf5 commit ea55ade
Show file tree
Hide file tree
Showing 15 changed files with 11,903 additions and 8,810 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>fr.insee.lunatic</groupId>
<artifactId>lunatic-model</artifactId>
<packaging>jar</packaging>
<version>1.0.1</version>
<version>1.0.2</version>
<name>Lunatic Model</name>
<description>Classes and converters for the Lunatic model</description>
<url>http://www.insee.fr</url>
Expand Down
80 changes: 43 additions & 37 deletions src/main/java/fr/insee/lunatic/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -11,44 +12,49 @@

public class Constants {

private static final Logger logger = LoggerFactory.getLogger(Constants.class);


private Constants() {

private static final Logger logger = LoggerFactory.getLogger(Constants.class);


private Constants() {

}

public static final String TEMP_FOLDER_PATH = System.getProperty("java.io.tmpdir")+"/"+"lunatic-test";
public static final Path TEMP_FOLDER= getTempDir(TEMP_FOLDER_PATH);
public static final String RESOURCES_FOLDER_PATH = "src/test/resources/examples";
public static final String RESOURCES_FOLDER_JSONF_2_XMLF_PATH = RESOURCES_FOLDER_PATH +"/jsonf-2-xmlf";
public static final String RESOURCES_FOLDER_XMLF_2_JSONF_PATH = RESOURCES_FOLDER_PATH +"/xmlf-2-jsonf";
public static final String RESOURCES_FOLDER_XMLH_2_XMLF_PATH = RESOURCES_FOLDER_PATH +"/xmlh-2-xmlf";
public static final String RESOURCES_FOLDER_XMLH_2_JSONF_PATH = RESOURCES_FOLDER_PATH +"/xmlh-2-jsonf";
public static final String RESOURCES_FOLDER_XMLH_2_JSONH_PATH = RESOURCES_FOLDER_PATH + "/xmlh-2-jsonh";

public static final String XSD_FILE_PATH = "/src/main/resources/xsd/Questionnaire.xsd";
public static final String TRANSFORMATION_XML_2_XMLF = "xslt/flattener.xsl";
public static final String TRANSFORMATION_JSON_2_JSON_CLEANED = "xslt/json-cleaner.xsl";
public static final String NAMESPACE_URI = "http://xml.insee.fr/schema/applis/lunatic";
public static final String PREFIX = "Questionnaire";
public static final String JAXB_SCHEMA_LOCATION = NAMESPACE_URI + " file:"+XSD_FILE_PATH;

public static final String SCHEMAS_FOLDER_PATH = "/xsd";

public static final URL MODEL_FLAT_XSD = Constants.class.getResource(SCHEMAS_FOLDER_PATH+"/LunaticModelFlat.xsd");
public static final URL MODEL_HIERARCHICAL_XSD = Constants.class.getResource(SCHEMAS_FOLDER_PATH+"/LunaticModel.xsd");

public static Path getTempDir(String pathFolder) {
Path tempDirPath = null;
File dir = new File(pathFolder);

if(dir.exists()) {
tempDirPath = Paths.get(pathFolder);
}

public static final String TEMP_FOLDER_PATH = System.getProperty("java.io.tmpdir")+"/"+"lunatic-test";
public static final Path TEMP_FOLDER= getTempDir(TEMP_FOLDER_PATH);
public static final String RESOURCES_FOLDER_PATH = "src/test/resources/examples";
public static final String RESOURCES_FOLDER_JSONF_2_XMLF_PATH = RESOURCES_FOLDER_PATH +"/jsonf-2-xmlf";
public static final String RESOURCES_FOLDER_XMLF_2_JSONF_PATH = RESOURCES_FOLDER_PATH +"/xmlf-2-jsonf";
public static final String RESOURCES_FOLDER_XMLH_2_XMLF_PATH = RESOURCES_FOLDER_PATH +"/xmlh-2-xmlf";
public static final String RESOURCES_FOLDER_XMLH_2_JSONF_PATH = RESOURCES_FOLDER_PATH +"/xmlh-2-jsonf";
public static final String RESOURCES_FOLDER_XMLH_2_JSONH_PATH = RESOURCES_FOLDER_PATH + "/xmlh-2-jsonh";

public static final String XSD_FILE_PATH = "/src/main/resources/xsd/Questionnaire.xsd";
public static final String TRANSFORMATION_XML_2_XMLF = "xslt/flattener.xsl";
public static final String TRANSFORMATION_JSON_2_JSON_CLEANED = "xslt/json-cleaner.xsl";
public static final String NAMESPACE_URI = "http://xml.insee.fr/schema/applis/lunatic";
public static final String PREFIX = "Questionnaire";
public static final String JAXB_SCHEMA_LOCATION = NAMESPACE_URI + " file:"+XSD_FILE_PATH;

public static Path getTempDir(String pathFolder) {
Path tempDirPath = null;
File dir = new File(pathFolder);

if(dir.exists()) {
tempDirPath = Paths.get(pathFolder);
}
else {
try {
tempDirPath = Files.createDirectory(Paths.get(pathFolder));
} catch(IOException e) {
logger.debug("Temp directory fail to initialize");
e.printStackTrace();
}
else {
try {
tempDirPath = Files.createDirectory(Paths.get(pathFolder));
} catch(IOException e) {
logger.debug("Temp directory fail to initialize");
e.printStackTrace();
}
return tempDirPath;
}
return tempDirPath;
}
}
6 changes: 6 additions & 0 deletions src/main/java/fr/insee/lunatic/utils/Modele.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package fr.insee.lunatic.utils;

public enum Modele {
FLAT,
HIERARCHICAL
}
82 changes: 82 additions & 0 deletions src/main/java/fr/insee/lunatic/utils/SchemaValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fr.insee.lunatic.utils;

import fr.insee.lunatic.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.InputStream;

public class SchemaValidator {

private static final Logger LOGGER = LoggerFactory.getLogger(SchemaValidator.class);

private Schema schema;
private Validator validator;

public SchemaValidator(Modele modele) {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
switch (modele){
case FLAT:
schema = sf.newSchema(Constants.MODEL_FLAT_XSD);;
break;
case HIERARCHICAL:
schema = sf.newSchema(Constants.MODEL_HIERARCHICAL_XSD);;
break;
}
} catch (SAXException e) {
e.printStackTrace();
}
validator = schema.newValidator();
}

public boolean validateFile(File file) {
boolean result = true;
validator.reset();
Source source = new StreamSource(file);
try {
source = toDOMSource(source);
validator.validate(source);
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}

public boolean validateIS(InputStream inputStream) {
boolean result = true;
validator.reset();
Source source = new StreamSource(inputStream);
try {
source = toDOMSource(source);
validator.validate(source);
} catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}

public DOMSource toDOMSource(Source source) throws Exception {
if (source instanceof DOMSource) {
return (DOMSource) source;
}
Transformer trans = TransformerFactory.newInstance().newTransformer();
DOMResult result = new DOMResult();
trans.transform(source, result);
return new DOMSource(result.getNode());
}
}
Loading

0 comments on commit ea55ade

Please sign in to comment.