Upgraded lib, added setEscapedContent methods and tests.

This commit is contained in:
Christian P. MOMON 2020-09-15 02:55:54 +02:00
parent 33a35fcf96
commit 1dfc9f0eb2
10 changed files with 114 additions and 11 deletions

View file

@ -18,8 +18,9 @@
<classpathentry kind="lib" path="lib/UnitTesting/junit-4.12.jar" sourcepath="lib/UnitTesting/junit-4.12-sources.jar"/> <classpathentry kind="lib" path="lib/UnitTesting/junit-4.12.jar" sourcepath="lib/UnitTesting/junit-4.12-sources.jar"/>
<classpathentry kind="lib" path="lib/Logs/slf4j-api-1.7.25.jar" sourcepath="lib/Logs/slf4j-api-1.7.25-sources.jar"/> <classpathentry kind="lib" path="lib/Logs/slf4j-api-1.7.25.jar" sourcepath="lib/Logs/slf4j-api-1.7.25-sources.jar"/>
<classpathentry kind="lib" path="lib/Logs/slf4j-log4j12-1.7.25.jar" sourcepath="lib/Logs/slf4j-log4j12-1.7.25-sources.jar"/> <classpathentry kind="lib" path="lib/Logs/slf4j-log4j12-1.7.25.jar" sourcepath="lib/Logs/slf4j-log4j12-1.7.25-sources.jar"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.7.jar" sourcepath="lib/commons-lang3-3.7-sources.jar"/>
<classpathentry kind="lib" path="lib/devinsy-utils-0.8.0.jar" sourcepath="lib/devinsy-utils-0.8.0-sources.zip"/> <classpathentry kind="lib" path="lib/devinsy-utils-0.8.0.jar" sourcepath="lib/devinsy-utils-0.8.0-sources.zip"/>
<classpathentry kind="lib" path="lib/devinsy-strings-0.11.0.jar" sourcepath="lib/devinsy-strings-0.11.0-sources.zip"/> <classpathentry kind="lib" path="lib/devinsy-strings-0.11.0.jar" sourcepath="lib/devinsy-strings-0.11.0-sources.zip"/>
<classpathentry kind="lib" path="lib/commons-lang3-3.11.jar" sourcepath="lib/commons-lang3-3.11-sources.jar"/>
<classpathentry kind="lib" path="lib/commons-text-1.9.jar" sourcepath="lib/commons-text-1.9-sources.jar"/>
<classpathentry kind="output" path="build/classes"/> <classpathentry kind="output" path="build/classes"/>
</classpath> </classpath>

Binary file not shown.

BIN
lib/commons-lang3-3.11.jar Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
lib/commons-text-1.9.jar Normal file

Binary file not shown.

View file

@ -19,6 +19,7 @@
package fr.devinsy.xidyn.data; package fr.devinsy.xidyn.data;
import fr.devinsy.xidyn.presenters.DomPresenterCore; import fr.devinsy.xidyn.presenters.DomPresenterCore;
import fr.devinsy.xidyn.utils.XidynUtils;
/** /**
* The Class TagDataManager. * The Class TagDataManager.
@ -659,6 +660,57 @@ public class TagDataManager
idData.setContent(content); idData.setContent(content);
} }
/**
* Sets the escaped content.
*
* @param id
* the id
* @param line
* the line
* @param content
* the content
*/
public void setEscapedContent(final String id, final int line, final String content)
{
SimpleTagData tag = this.getIdData(id, line);
tag.setContent(XidynUtils.escapeXmlBlank(content));
}
/**
* Sets the escaped content.
*
* @param id
* the id
* @param line
* the line
* @param column
* the column
* @param content
* the content
*/
public void setEscapedContent(final String id, final int line, final String column, final String content)
{
SimpleTagData tag = this.getIdData(id, line, column);
tag.setContent(XidynUtils.escapeXmlBlank(content));
}
/**
* Sets the escaped content.
*
* @param id
* the id
* @param content
* the content
*/
public void setEscapedContent(final String id, final String content)
{
SimpleTagData idData = this.getIdData(id);
idData.setContent(XidynUtils.escapeXmlBlank(content));
}
/** /**
* Sets the iteration strategy. * Sets the iteration strategy.
* *

View file

@ -34,6 +34,8 @@ import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.validation.Schema; import javax.xml.validation.Schema;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -131,7 +133,8 @@ public class XidynUtils
if (errorHandler.hasError()) if (errorHandler.hasError())
{ {
// Most time, error is (with StringPresenter): // Most time, error is (with StringPresenter):
// "Error at line 1 : Document root element "html", must match DOCTYPE root // "Error at line 1 : Document root element "html", must match
// DOCTYPE root
// "null". // "null".
// Error at line 1 : Document is invalid: no grammar found. // Error at line 1 : Document is invalid: no grammar found.
// We ignore it. STU // We ignore it. STU
@ -210,7 +213,8 @@ public class XidynUtils
if (errorHandler.hasError()) if (errorHandler.hasError())
{ {
// Most time, error is (with StringPresenter): // Most time, error is (with StringPresenter):
// "Error at line 1 : Document root element "html", must match DOCTYPE root // "Error at line 1 : Document root element "html", must match
// DOCTYPE root
// "null". // "null".
// Error at line 1 : Document is invalid: no grammar found. // Error at line 1 : Document is invalid: no grammar found.
// We ignore it. STU // We ignore it. STU
@ -243,6 +247,30 @@ public class XidynUtils
return result; return result;
} }
/**
* Escape xml blank.
*
* @param source
* the source
* @return the string
*/
public static String escapeXmlBlank(final String source)
{
String result;
if (StringUtils.isBlank(source))
{
result = "";
}
else
{
result = StringEscapeUtils.escapeXml11(source);
}
//
return result;
}
/** /**
* Good estimation of the target length able to optimize performance. * Good estimation of the target length able to optimize performance.
* *
@ -315,11 +343,12 @@ public class XidynUtils
/** /**
* This method extracts the string before the <i>html</i> tag. * This method extracts the string before the <i>html</i> tag.
* *
* A possible way is to use pattern searching for <i>$(.*)&lt;html&gt;.*^</i>. * A possible way is to use pattern searching for
* But if there is no <i>html</i> tag, all the source is read for nothing. * <i>$(.*)&lt;html&gt;.*^</i>. But if there is no <i>html</i> tag, all the
* source is read for nothing.
* *
* A best way is to analyze each &lt; while it is a XML tag or a DOCTYPE tag or * A best way is to analyze each &lt; while it is a XML tag or a DOCTYPE tag
* the &lt;HTML&gt; tag. * or the &lt;HTML&gt; tag.
* *
* Uses cases: * Uses cases:
* *
@ -330,8 +359,8 @@ public class XidynUtils
* *
* @param source * @param source
* the source * the source
* @return the string before the <i>html</i> tag or null if no <i>html</i> tag * @return the string before the <i>html</i> tag or null if no <i>html</i>
* found. So, "" if there is a <i>html</i> tag without doctype. * tag found. So, "" if there is a <i>html</i> tag without doctype.
*/ */
public static String extractDoctype(final String source) public static String extractDoctype(final String source)
{ {
@ -788,8 +817,8 @@ public class XidynUtils
} }
/** /**
* Any ampersand lt;, ampersand gt; and ampersand amp; sequences in text nodes * Any ampersand lt;, ampersand gt; and ampersand amp; sequences in text
* get read in as symbols. This method converts them back to entities. * nodes get read in as symbols. This method converts them back to entities.
* *
* @param source * @param source
* String that is to have the entities restored.. * String that is to have the entities restored..

View file

@ -255,4 +255,25 @@ public class TagDataManagerTest
Assertions.assertThat(target).isEqualTo(goal); Assertions.assertThat(target).isEqualTo(goal);
} }
/**
* Test set escaped content.
*
* @throws Exception
* the exception
*/
@Test
public void testSetEscapedContent01() throws Exception
{
String source = "<span id='foo'>n/a</span>";
TagDataManager data = new TagDataManager();
data.setEscapedContent("foo", "<b>foo</b>");
String target = PresenterUtils.dynamize(source, data).toString();
String goal = "<span id=\"foo\">&lt;b&gt;foo&lt;/b&gt;</span>";
Assertions.assertThat(target).isEqualTo(goal);
}
} }