Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct encoding of quote and add a unit test #472

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ protected static String elementStart (String tag, String [][] attrs)
* to entities as necessary and removing control characters disallowed
* by XML. The null string will be converted to an empty string.
*/
private static String encodeContent (String content)
protected static String encodeContent (String content)
{
if (content == null) {
content = "";
Expand All @@ -611,7 +611,7 @@ private static String encodeContent (String content)
n = 0;
while ((n = buffer.indexOf ("&", n)) > -1) {
buffer.insert (n+1, "amp;");
n +=5;
n += 5;
}
n = 0;
while ((n = buffer.indexOf ("<", n)) > -1) {
Expand All @@ -632,7 +632,7 @@ private static String encodeContent (String content)
* converting quote characters to entities and removing control
* characters disallowed by XML.
*/
private static String encodeValue (String value)
protected static String encodeValue (String value)
{
StringBuffer buffer = new StringBuffer (value);

Expand All @@ -650,9 +650,9 @@ private static String encodeValue (String value)
n = 0;
while ((n = buffer.indexOf ("&", n)) > -1) {
buffer.insert (n+1, "amp;");
n +=5;
n += 5;
}
n = 0;
n = 0;
while ((n = buffer.indexOf ("<", n)) > -1) {
buffer.replace (n, n+1, "&lt;");
n += 4;
Expand All @@ -662,11 +662,11 @@ private static String encodeValue (String value)
buffer.replace (n, n+1, "&gt;");
n += 4;
}
n = 0;
n = 0;
while ((n = buffer.indexOf ("\"", n)) > -1) {
// [LP] fix for invalid escaping, "" quotes were accidentally left in place.
buffer.replace (n, n+1, "&quot;");
n +=7;
n += 6;
}

return buffer.toString ();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package edu.harvard.hul.ois.jhove;

import static org.junit.Assert.assertEquals;

import java.util.logging.Logger;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class HandlerBaseTest {
private static final Logger LOGGER = Logger.getLogger(HandlerBaseTest.class.getName());
private static final String GIVES = " => ";
private static final String DUMMY = "dummy";

@Test
public void testEncodeContent() {
/* Test values */
final String[] VALUES = {
DUMMY, "<<>>\"\"''&&"
};

final String[] EXPECTED = {
DUMMY, "&lt;&lt;&gt;&gt;\"\"''&amp;&amp;"
};

String encodeValue;
for (int i = 0; i < VALUES.length; i++) {
encodeValue = HandlerBase.encodeContent(VALUES[i]);
LOGGER.info("testEncodeContent: " + VALUES[i] + GIVES + encodeValue);
assertEquals(EXPECTED[i], encodeValue);
}
}

@Test
public void testEncodeValue() {
/* Test values */
final String[] VALUES = {
DUMMY, "<<>>\"'&", "" + (char)0xf + "\"\""
};
final String[] EXPECTED = {
DUMMY, "&lt;&lt;&gt;&gt;&quot;'&amp;", "&quot;&quot;"
};

String encodeValue;
for (int i = 0; i < VALUES.length; i++) {
encodeValue = HandlerBase.encodeValue(VALUES[i]);
LOGGER.info("testEncodeValue: " + VALUES[i] + GIVES + encodeValue);
assertEquals(EXPECTED[i], encodeValue);
}
}

}