-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Correct pom * Upgrade to junit 5 * Remove unused and dangerous xalan dependency * upgrading version * adding pairwise * changing Pairwise into PairwiseLinks * Changing Pairwise into PairwiseLinks * adding symLinks for pairwise * fix single item in links list * add maxOccurs in symLinks * add pairwise test file * Create PairwiseTest.java * add `symLinksCleaning` method in cleaning class * (fix) add out test folder * same test in one go * remove file writing in PairwiseTest * update pairwise test file * generify symLinks entry type in cleaning * (fix) PairwiseLinks xAxis single attribute * (fix) single symLinks attribute in PairwiseLinks update symLinks cleaning * change symLinks type to string * bump version * reduce "symLinksCleaning" cognitive complexity * refactor SymLinks cleaning in separate class * create test class for SymLinks cleaning * null case management * add 'no validation' warn in javadoc * reorder string.equals(...) usages * try with resource for jsonReader * sandbox test on JsonObject getString method * restore component list in PairwiseLinks * bump version * fix: superfluous quote marks in symLinks cleaning * update symlinks * synchronize symlinks * test: update pairwise hierarchical test file Co-Authored-By: François Bulot <[email protected]> * test: add flat versions of pairwise test file * refactor: small enhancement * feat: add deserialize for input stream * fix: update "symLinks" with variable name * test: enhance pairwise tests * test: better test on PairwiseLinks deserialization * Feat/serializer for eno3 (#87) * feat: create serialization exception class * feat: add second json serialize method * refactor: utils class in utils package * test: json utils class * test: unit tests on json serializers * build: bump rc version * feat: adapt suggester rules to new model * Fix/resizing (#96) * test: add questionnaire in resources * style: linter thing * test: case for the identified issue * fix: invert cleaning steps solves the issue * build: bump rc version * test: refactor pairwise test * fix: update json-cleaning xslt for pairwise * Fix/resizing take 2 (#97) * test: add questionnaire in resources * style: linter thing * test: case for the identified issue * fix: invert cleaning steps solves the issue * build: bump rc version * test: refactor pairwise test * fix: update json-cleaning xslt for pairwise * chore: bump rc version * fix: revert "adapt suggester rules to new model" This reverts commit f481734 * fix: correction for storeName/resizing --------- Co-authored-by: Fabrice Bibonne <[email protected]> Co-authored-by: Nicolas Senave <[email protected]> Co-authored-by: Nicolas Sénave <[email protected]> Co-authored-by: Bulot François <[email protected]> Co-authored-by: François Bulot <[email protected]> Co-authored-by: Benoit Werquin <[email protected]> Co-authored-by: David Darras <[email protected]>
- Loading branch information
1 parent
95730d4
commit 38f5404
Showing
23 changed files
with
3,859 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/main/java/fr/insee/lunatic/conversion/JSONSymLinksCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package fr.insee.lunatic.conversion; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.json.*; | ||
import java.io.*; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class JSONSymLinksCleaner { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(JSONSymLinksCleaner.class); | ||
|
||
/** | ||
* Given a Lunatic JSON flat questionnaire, replace the source/target fields by key/values in "symLinks" attribute | ||
* in "PairwiseLinks" components to be compliant with the Lunatic JS library. | ||
* Warning: no validation is done on the string input. */ | ||
public String clean(String stringFlatQuestionnaire) throws IOException { | ||
|
||
if (stringFlatQuestionnaire == null) { | ||
logger.warn("null string given in JSON SymLinks cleaner."); | ||
return null; | ||
} | ||
|
||
// Read the json string into a JsonObject | ||
try (JsonReader jsonReader = Json.createReader( | ||
new ByteArrayInputStream(stringFlatQuestionnaire.getBytes(StandardCharsets.UTF_8)))) { | ||
JsonObject jsonQuestionnaire = jsonReader.readObject(); | ||
|
||
// We will copy the entire input json object, except the "symLinks" attribute in PairwiseLinks components | ||
JsonObjectBuilder jsonQuestionnaireBuilder = Json.createObjectBuilder(); | ||
editQuestionnaire(jsonQuestionnaire, jsonQuestionnaireBuilder); | ||
|
||
OutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
JsonWriter jsonWriter = Json.createWriter(outputStream); | ||
jsonWriter.writeObject(jsonQuestionnaireBuilder.build()); | ||
|
||
String result = outputStream.toString(); | ||
outputStream.close(); | ||
|
||
return result; | ||
} | ||
|
||
} | ||
|
||
private static void editQuestionnaire(JsonObject jsonQuestionnaire, JsonObjectBuilder jsonQuestionnaireBuilder) { | ||
jsonQuestionnaire.forEach((key, jsonValue) -> { | ||
if (! "components".equals(key)) { | ||
jsonQuestionnaireBuilder.add(key, jsonValue); | ||
} else { | ||
editComponents(jsonQuestionnaireBuilder, (JsonArray) jsonValue); | ||
} | ||
}); | ||
} | ||
|
||
private static void editComponents(JsonObjectBuilder jsonQuestionnaireBuilder, JsonArray jsonComponents) { | ||
JsonArrayBuilder jsonComponentsBuilder = Json.createArrayBuilder(); | ||
for (JsonValue jsonValue1 : jsonComponents) { | ||
JsonObject jsonComponent = (JsonObject) jsonValue1; | ||
if (! "PairwiseLinks".equals(jsonComponent.getString("componentType"))) { | ||
jsonComponentsBuilder.add(jsonValue1); | ||
} else { | ||
editPairwiseLinks(jsonComponentsBuilder, (JsonObject) jsonValue1); | ||
} | ||
} | ||
jsonQuestionnaireBuilder.add("components", jsonComponentsBuilder.build()); | ||
} | ||
|
||
private static void editPairwiseLinks(JsonArrayBuilder jsonComponentsBuilder, JsonObject jsonPairwiseLinks) { | ||
JsonObjectBuilder jsonPairwiseBuilder = Json.createObjectBuilder(); | ||
jsonPairwiseLinks.forEach((key2, jsonValue2) -> { | ||
if (! "symLinks".equals(key2)) { | ||
jsonPairwiseBuilder.add(key2, jsonValue2); | ||
} else { | ||
editSymLinks(jsonPairwiseBuilder, (JsonObject) jsonValue2); | ||
} | ||
}); | ||
jsonComponentsBuilder.add(jsonPairwiseBuilder.build()); | ||
} | ||
|
||
private static void editSymLinks(JsonObjectBuilder jsonPairwiseBuilder, JsonObject jsonSymLinks) { | ||
JsonObjectBuilder jsonSymLinksBuilder = Json.createObjectBuilder(); | ||
JsonObjectBuilder jsonLINKSBuilder = Json.createObjectBuilder(); | ||
String symLinksName = jsonSymLinks.getJsonString("name").getString(); | ||
jsonSymLinks.getJsonArray("LINK").forEach(jsonValue3 -> { | ||
JsonObject jsonSourceTarget = (JsonObject) jsonValue3; | ||
JsonString sourceKey = (JsonString) jsonSourceTarget.get("source"); | ||
JsonString targetKey = (JsonString) jsonSourceTarget.get("target"); | ||
// target field is not mandatory and can be null | ||
// json converted from xml can contain "null" string values | ||
if (targetKey != null && !"null".equals(targetKey.getString())) { | ||
jsonLINKSBuilder.add(sourceKey.getString(), targetKey); | ||
} else { | ||
jsonLINKSBuilder.addNull(sourceKey.getString()); | ||
} | ||
}); | ||
jsonSymLinksBuilder.add(symLinksName, jsonLINKSBuilder.build()); | ||
jsonPairwiseBuilder.add("symLinks", jsonSymLinksBuilder.build()); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/fr/insee/lunatic/exception/SerializationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package fr.insee.lunatic.exception; | ||
|
||
/** Thrown when an error occurs during serialization of a questionnaire object. */ | ||
public class SerializationException extends Exception { | ||
|
||
public SerializationException(String message, Exception e) { | ||
super(message, e); | ||
} | ||
|
||
} |
Oops, something went wrong.