-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- refactor: method to gather all labels in in EnoCatalog class - feat(tooltip): utils class with regex to make tooltips Lunatic-compliant - feat(tooltip): add a Eno processing class to clean tooltips in labels - feat(tooltip): update method that add quotes in static labels in "resolve labels" DDI processing - test: unit and integration tests on the tooltip feature
- Loading branch information
Showing
13 changed files
with
2,162 additions
and
55 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
36 changes: 36 additions & 0 deletions
36
eno-core/src/main/java/fr/insee/eno/core/processing/common/steps/EnoCleanTooltips.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,36 @@ | ||
package fr.insee.eno.core.processing.common.steps; | ||
|
||
import fr.insee.eno.core.model.EnoQuestionnaire; | ||
import fr.insee.eno.core.model.label.EnoLabel; | ||
import fr.insee.eno.core.processing.ProcessingStep; | ||
import fr.insee.eno.core.reference.EnoCatalog; | ||
import fr.insee.eno.core.utils.TooltipUtils; | ||
|
||
/** | ||
* Processing to make tooltip in labels from Pogues or DDI compliant with Lunatic. | ||
* @see TooltipUtils for details. | ||
* */ | ||
public class EnoCleanTooltips implements ProcessingStep<EnoQuestionnaire> { | ||
|
||
private final TooltipUtils tooltipUtils = new TooltipUtils(); | ||
|
||
private final EnoCatalog enoCatalog; | ||
|
||
public EnoCleanTooltips(EnoCatalog enoCatalog) { | ||
this.enoCatalog = enoCatalog; | ||
} | ||
|
||
/** | ||
* For each label present in the given Eno questionnaire, clean Lunatic tooltips. | ||
* @param enoQuestionnaire Eno questionnaire. | ||
*/ | ||
@Override | ||
public void apply(EnoQuestionnaire enoQuestionnaire) { | ||
enoCatalog.getLabels().forEach(this::cleanTooltipsInLabel); | ||
} | ||
|
||
private void cleanTooltipsInLabel(EnoLabel enoLabel) { | ||
enoLabel.setValue(tooltipUtils.cleanTooltips(enoLabel.getValue())); | ||
} | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
eno-core/src/main/java/fr/insee/eno/core/utils/TooltipUtils.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,47 @@ | ||
package fr.insee.eno.core.utils; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* <p>In the current implementation, tooltips are inputted this way in Pogues: | ||
* <code>[some word](. "Tooltip's content")</code>.</p> | ||
* <p>Yet tooltips work with simple quotes in Lunatic, so simple quotes must be replaced with apostrophes, | ||
* and double quotes with simple quotes to be valid: | ||
* </code>[some word](. 'Tooltip’s content')</code>.</p> | ||
* */ | ||
public class TooltipUtils { | ||
|
||
private static final String APOSTROPHE_CHARACTER = "’"; | ||
private static final String DOUBLE_QUOTES_TOOLTIP_REGEX = "\\[[^\\]]+\\]\\(\\.\\s\"[^\"]+\"\\)"; | ||
|
||
private final Pattern pattern; | ||
|
||
public TooltipUtils() { | ||
// compile pattern only once | ||
this.pattern = Pattern.compile(DOUBLE_QUOTES_TOOLTIP_REGEX); | ||
} | ||
|
||
/** | ||
* Searches for Lunatic tooltips in the given string. If any, these are cleaned to ensure compliance with Lunatic | ||
* (Lunatic tooltips work with single quotes). | ||
* @param label String value of a label. | ||
* @return String value of the label with Lunatic-compliant tooltips. | ||
*/ | ||
public String cleanTooltips(String label) { | ||
Matcher matcher = pattern.matcher(label); | ||
|
||
StringBuilder result = new StringBuilder(); | ||
|
||
while (matcher.find()) { | ||
String tooltip = matcher.group(); | ||
tooltip = tooltip.replace("'", APOSTROPHE_CHARACTER); | ||
tooltip = tooltip.replace("\"", "'"); | ||
matcher.appendReplacement(result, tooltip); | ||
} | ||
|
||
matcher.appendTail(result); | ||
return result.toString(); | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
eno-core/src/test/java/fr/insee/eno/core/processing/common/steps/EnoCleanTooltipsTest.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,61 @@ | ||
package fr.insee.eno.core.processing.common.steps; | ||
|
||
import fr.insee.eno.core.DDIToEno; | ||
import fr.insee.eno.core.exceptions.business.DDIParsingException; | ||
import fr.insee.eno.core.model.EnoQuestionnaire; | ||
import fr.insee.eno.core.model.question.SimpleMultipleChoiceQuestion; | ||
import fr.insee.eno.core.parameter.EnoParameters; | ||
import fr.insee.eno.core.parameter.Format; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class EnoCleanTooltipsTest { | ||
|
||
@Test | ||
void integrationTestFromDDI() throws DDIParsingException { | ||
// Given + When | ||
// | ||
EnoParameters enoParameters = EnoParameters.of( | ||
EnoParameters.Context.DEFAULT, EnoParameters.ModeParameter.CAWI, Format.LUNATIC); | ||
enoParameters.setSequenceNumbering(false); | ||
enoParameters.setQuestionNumberingMode(EnoParameters.QuestionNumberingMode.NONE); | ||
enoParameters.setArrowCharInQuestions(false); | ||
// | ||
EnoQuestionnaire enoQuestionnaire = DDIToEno.transform( | ||
this.getClass().getClassLoader().getResourceAsStream("integration/ddi/ddi-tooltips.xml"), | ||
enoParameters); | ||
|
||
// Then | ||
// NB: pay attention to the difference between simple quote and apostrophe | ||
assertEquals("\"Sequence [label](. 'There is a tooltip on the label.')\"", | ||
enoQuestionnaire.getSequences().get(0).getLabel().getValue()); | ||
assertEquals("\"Declaration with a tooltip [here](. 'Tooltip’s content.').\"", | ||
enoQuestionnaire.getSequences().get(0).getInstructions().get(0).getLabel().getValue()); | ||
// | ||
String expectedQuestionLabel = "\"Question label with a [tooltip](. " + | ||
"'The tooltip, also known as infotip or hint, is a common graphical user interface element in which, " + | ||
"when hovering over a screen element or component, a text box displays information about that element." + | ||
"').\""; | ||
assertEquals(expectedQuestionLabel, enoQuestionnaire.getSingleResponseQuestions().get(0).getLabel().getValue()); | ||
assertEquals("\"Before question [label](. 'Some text').\"", | ||
enoQuestionnaire.getSingleResponseQuestions().get(0).getDeclarations().get(0).getLabel().getValue()); | ||
assertEquals("\"After question [label](. 'Some text').\"", | ||
enoQuestionnaire.getSingleResponseQuestions().get(0).getInstructions().get(0).getLabel().getValue()); | ||
assertEquals("\"Error message with a [tooltip](. 'Some text')\"", | ||
enoQuestionnaire.getSingleResponseQuestions().get(0).getControls().get(0).getMessage().getValue()); | ||
// | ||
assertEquals("\"[Code 1](. 'Tooltip text of code 1')\"", | ||
enoQuestionnaire.getCodeLists().get(0).getCodeItems().get(0).getLabel().getValue()); | ||
assertEquals("\"[Code 2](. 'Tooltip text of code 2')\"", | ||
enoQuestionnaire.getCodeLists().get(0).getCodeItems().get(1).getLabel().getValue()); | ||
// | ||
SimpleMultipleChoiceQuestion simpleMCQ = (SimpleMultipleChoiceQuestion) | ||
enoQuestionnaire.getMultipleResponseQuestions().get(0); | ||
assertEquals("\"[Code 1](. 'Tooltip text of code 1')\"", | ||
simpleMCQ.getCodeResponses().get(0).getLabel().getValue()); | ||
assertEquals("\"[Code 2](. 'Tooltip text of code 2')\"", | ||
simpleMCQ.getCodeResponses().get(1).getLabel().getValue()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.