Skip to content

Commit

Permalink
#169 Keep quotes as text at import + display the attributes unescaped (
Browse files Browse the repository at this point in the history
  • Loading branch information
MalinaStefaniaStoicanescu committed Aug 11, 2022
1 parent 71fac83 commit e8c1124
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,7 @@ protected String evaluateExpression(Session session, Requirement requirement, St
resultBuilder.append(value);
}
String evaluationResult = resultBuilder.toString();
String sanytizedResult = LabelHelper.transformHTMLToTextWithLineFeed(evaluationResult);

String sanytizedResult = LabelHelper.unescape(LabelHelper.transformHTMLToText(evaluationResult));
return reduceString(sanytizedResult, maxLength);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ protected String getContent(AttributeValueXHTML value, AttributeOwner owner) {
// We've got to store root tag here since ReqIF10XhtmlUtil.getXhtmlString does not include the original root tag of the XHTML element
rootTag = getRootTag(theValue);
content = ReqIF10XhtmlUtil.getXhtmlString(theValue);
if (value.getDefinition().getLongName().equals("ReqIF.Text")) {
if (LabelHelper.keepHtmlTags() || value.getDefinition().getLongName().equals("ReqIF.Text")) {
content = textParser.transformToHTML(content, owner, rootTag);
} else {
content = transformToText(content, rootTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public String getText(Object object) {
} else {
result += value;
}
result = LabelHelper.transformHTMLToText(result);
result = LabelHelper.unescape(LabelHelper.transformHTMLToText(result));
return reduceReqNameLen(result, RequirementsPreferencesPlugin.getDefault().getPreferenceStore()
.getString(RequirementsPreferencesConstants.REQUIREMENT_LABEL_MAX_LEN_KEY));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*******************************************************************************
* Copyright (c) 2016, 2017 THALES GLOBAL SERVICES.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
Expand All @@ -23,6 +23,7 @@
import org.polarsys.kitalpha.vp.requirements.Requirements.AttributeDefinition;
import org.polarsys.kitalpha.vp.requirements.Requirements.StringValueAttribute;
import org.polarsys.kitalpha.vp.requirements.model.edit.decorators.ItemProviderAdapterDecorator;
import org.polarsys.kitalpha.vp.requirements.model.helpers.LabelHelper;

/**
* @author Joao Barata
Expand All @@ -49,8 +50,7 @@ public String getText(Object object) {
}

result.append(" ");
result.append(attribute.getValue());

result.append(LabelHelper.unescape(attribute.getValue()));
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ public class LabelHelper {
private LabelHelper() {
// Private constructor
}

public static String unescape(String content) {
return StringEscapeUtils.unescapeHtml(content);
}

public static String toOneLine(String content) {
return content.replace("\r\n", " ").replace("\n", " ").trim();
}

public static String transformHTMLToText(String content) {
return transformHTMLToTextWithLineFeed(content).replace("\r\n", " ").replace("\n", " ").trim();
return toOneLine(transformHTMLToTextWithLineFeed(content));
}

public static String transformHTMLToText(String content, String rootTag) {
String result = transformHTMLToTextWithLineFeed(content).replace("\r\n", " ").replace("\n", " ").trim();
String result = toOneLine(transformHTMLToTextWithLineFeed(content));
if (rootTag != null && keepHtmlTags()) {
return "<" + rootTag + ">" + result + "</" + rootTag + ">";
}
Expand All @@ -36,13 +45,13 @@ public static String transformHTMLToText(String content, String rootTag) {
public static String transformHTMLToTextWithLineFeed(String content) {
if (!keepHtmlTags()) {
content = content.replaceAll("<xhtml:br/>", " ").replaceAll("<[^>]*>", "").trim();
// // Decode special characters
content = URI.decode(content);
// // Unescape HTML special character entities
content = unescape(content);
} else {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
package org.polarsys.capella.vp.requirements.ju.preferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.polarsys.capella.core.transition.common.context.TransitionContext;
import org.polarsys.capella.test.framework.api.BasicTestCase;
import org.polarsys.capella.vp.requirements.importer.preferences.RequirementsPreferencesConstants;
import org.polarsys.capella.vp.requirements.importer.preferences.RequirementsPreferencesPlugin;
import org.polarsys.capella.vp.requirements.importer.transposer.bridge.ReqIFTextParser;
import org.polarsys.kitalpha.vp.requirements.Requirements.Requirement;
import org.polarsys.kitalpha.vp.requirements.Requirements.RequirementsFactory;
import org.polarsys.kitalpha.vp.requirements.model.helpers.LabelHelper;

public class KeepXHTMLTagsTest extends BasicTestCase {
Expand All @@ -24,7 +28,10 @@ public class KeepXHTMLTagsTest extends BasicTestCase {
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 String testString = "<xhtml:div>Test text <xhtml:br/></xhtml:div>";
private String testQuotes = "<xhtml:div>Capella &quot;&lt;is&gt;&quot; great <xhtml:a href=\"to%20.pdf\">here</xhtml:a></xhtml:div>";
private String expectedResultWithoutTags= "Capella \"<is>\" great here";
private String expectedResultWithTags = "<div>Capella \"&lt;is&gt;\" great <a href=\"to%20.pdf\">here</a></div>";
private IPreferenceStore store = RequirementsPreferencesPlugin.getDefault().getPreferenceStore();

@Override
Expand All @@ -35,10 +42,16 @@ public void test() throws Exception {

public void testWithTags() {
store.setValue(RequirementsPreferencesConstants.REQUIREMENT_KEEP_XHTML_TAGS, true);
String result = LabelHelper.transformHTMLToText(testString, DIV);
ReqIFTextParser parser = new ReqIFTextParser(new TransitionContext());
Requirement dummyRequirement = RequirementsFactory.eINSTANCE.createRequirement();
String result = parser.transformToHTML(testString, dummyRequirement);

assertTrue(result.indexOf(BR_TAG) >= 0);
assertEquals(result.indexOf(DIV_TAG), 0);
assertTrue(result.indexOf(DIV_END_TAG) >= 0);

String resultQuotes = parser.transformToHTML(testQuotes, dummyRequirement);
assertEquals(resultQuotes, expectedResultWithTags);
}

public void testWithoutTags() {
Expand All @@ -47,6 +60,9 @@ public void testWithoutTags() {
assertTrue(result.indexOf(BR_TAG) < 0);
assertTrue(result.indexOf(DIV_TAG) < 0);
assertTrue(result.indexOf(DIV_END_TAG) < 0);

String resultTestWithQuotes = LabelHelper.transformHTMLToText(testQuotes);
assertEquals(resultTestWithQuotes, expectedResultWithoutTags);
}

}

0 comments on commit e8c1124

Please sign in to comment.