Skip to content

Commit

Permalink
#154 Improved tags replacement and keep root tag (#158)
Browse files Browse the repository at this point in the history
Change-Id: Ia8b9341ff6412d7ae677fc6194d799b373fd033a
Signed-off-by: MalinaStefaniaStoicanescu <malina.stoicanescu@thalesgroup.com>
  • Loading branch information
MalinaStefaniaStoicanescu committed Jan 10, 2022
1 parent 9d91692 commit 023b5f5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ protected String getContent(AttributeValueXHTML value, AttributeOwner owner) {
if (value.getDefinition().getLongName().equals("ReqIF.Text")) {
content = textParser.transformToHTML(content, owner, rootTag);
} else {
content = transformToText(content);
content = transformToText(content, rootTag);
}
} catch (IOException ex) {
ex.printStackTrace();
Expand All @@ -461,7 +461,11 @@ protected String getRootTag(XhtmlContent theValue) {
}

protected String transformToText(String content) {
return LabelHelper.transformHTMLToText(content);
return transformToText(content, null);
}

protected String transformToText(String content, String rootTag) {
return LabelHelper.transformHTMLToText(content, rootTag);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,29 @@ public static String transformHTMLToText(String content) {
return transformHTMLToTextWithLineFeed(content).replace("\r\n", " ").replace("\n", " ").trim();
}

public static String transformHTMLToText(String content, String rootTag) {
String result = transformHTMLToTextWithLineFeed(content).replace("\r\n", " ").replace("\n", " ").trim();
if (rootTag != null && keepHtmlTags()) {
return "<" + rootTag + ">" + result + "</" + rootTag + ">";
}
return result;
}

public static String transformHTMLToTextWithLineFeed(String content) {
Boolean keepXhtmlTages = (Boolean) ReqImporterPreferencesUtil
.getValueForPreferenceKey(RequirementsPreferencesConstants.REQUIREMENT_KEEP_XHTML_TAGS, Boolean.class);
if (!keepXhtmlTages) {
if (!keepHtmlTags()) {
content = content.replaceAll("<xhtml:br/>", " ").replaceAll("<[^>]*>", "").trim();
} else {
content = content.replaceAll("(?!</xhtml)(?!<xhtml)<[^>]*>", "").replace("xhtml:", "");
}
content = content.replaceAll("(?!</xhtml)(?!<xhtml)<[^>]*>", "").replace("xhtml:", "");
// Decode special characters
content = URI.decode(content);
// Unescape HTML special character entities
content = StringEscapeUtils.unescapeHtml(content);
return content;
}

public static boolean keepHtmlTags() {
return (Boolean) ReqImporterPreferencesUtil
.getValueForPreferenceKey(RequirementsPreferencesConstants.REQUIREMENT_KEEP_XHTML_TAGS, Boolean.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

public class KeepXHTMLTagsTest extends BasicTestCase {

private final String DIV = "div";
private final String BR_TAG = "<br/>";
private final String DIV_TAG = "<div>";
private final String DIV_END_TAG = "/div";
private String testString = "Test text<xhtml:br/>";
private IPreferenceStore store = RequirementsPreferencesPlugin.getDefault().getPreferenceStore();

Expand All @@ -32,14 +35,18 @@ public void test() throws Exception {

public void testWithTags() {
store.setValue(RequirementsPreferencesConstants.REQUIREMENT_KEEP_XHTML_TAGS, true);
String result = LabelHelper.transformHTMLToTextWithLineFeed(testString);
String result = LabelHelper.transformHTMLToText(testString, DIV);
assertTrue(result.indexOf(BR_TAG) >= 0);
assertEquals(result.indexOf(DIV_TAG), 0);
assertTrue(result.indexOf(DIV_END_TAG) >= 0);
}

public void testWithoutTags() {
store.setValue(RequirementsPreferencesConstants.REQUIREMENT_KEEP_XHTML_TAGS, false);
String result = LabelHelper.transformHTMLToTextWithLineFeed(testString);
String result = LabelHelper.transformHTMLToText(testString, DIV);
assertTrue(result.indexOf(BR_TAG) < 0);
assertTrue(result.indexOf(DIV_TAG) < 0);
assertTrue(result.indexOf(DIV_END_TAG) < 0);
}

}

0 comments on commit 023b5f5

Please sign in to comment.