[jsword-svn] r1691 - in trunk/common/src: main/java/org/crosswire/common/diff test/java/org/crosswire/common/diff
dmsmith at www.crosswire.org
dmsmith at www.crosswire.org
Thu Aug 23 17:22:58 MST 2007
Author: dmsmith
Date: 2007-08-23 17:22:57 -0700 (Thu, 23 Aug 2007)
New Revision: 1691
Modified:
trunk/common/src/main/java/org/crosswire/common/diff/PatchEntry.java
trunk/common/src/test/java/org/crosswire/common/diff/PatchEntryTest.java
Log:
Fixed a diff problem where the difference was a newline.
Modified: trunk/common/src/main/java/org/crosswire/common/diff/PatchEntry.java
===================================================================
--- trunk/common/src/main/java/org/crosswire/common/diff/PatchEntry.java 2007-08-23 21:01:26 UTC (rev 1690)
+++ trunk/common/src/main/java/org/crosswire/common/diff/PatchEntry.java 2007-08-24 00:22:57 UTC (rev 1691)
@@ -21,6 +21,8 @@
*/
package org.crosswire.common.diff;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -170,9 +172,17 @@
{
Difference diff = (Difference) iter.next();
txt.append(diff.getEditType().getSymbol());
- txt.append(diff.getText());
+ txt.append(encode(diff.getText()));
txt.append('\n');
}
+// String result = txt.toString();
+// // Replicate the JavaScript encodeURI() function (not including %20)
+// result = result.replace("%3D", "=").replace("%3B", ";").replace("%27", "'")
+// .replace("%2C", ",").replace("%2F", "/").replace("%7E", "~")
+// .replace("%21", "!").replace("%40", "@").replace("%23", "#")
+// .replace("%24", "$").replace("%26", "&").replace("%28", "(")
+// .replace("%29", ")").replace("%2B", "+").replace("%3A", ":")
+// .replace("%3F", "?");
return txt.toString();
}
@@ -228,15 +238,11 @@
for (int lineCount = 1; lineCount < text.length; lineCount++)
{
- if (text[lineCount].length() > 0)
+ line = text[lineCount];
+ if (line.length() > 0)
{
- sign = text[lineCount].charAt(0);
- line = text[lineCount].substring(1);
- // Lines with zero length are the difference of a new line.
- if (line.length() == 0)
- {
- line = "\n"; //$NON-NLS-1$
- }
+ sign = line.charAt(0);
+ line = decode(line.substring(1));
diffs.add(new Difference(EditType.fromSymbol(sign), line));
}
}
@@ -411,6 +417,67 @@
}
/**
+ * This algorithm allows for \n to be included in a difference.
+ * Thus it needs to be escaped. We will use URL encoding of \n.
+ * But this makes % a meta-character, thus it needs to be encoded too.
+ * @param str
+ * @return
+ */
+ private String encode(String str)
+ {
+ int strlen = str.length();
+ StringBuffer buf = new StringBuffer(2 * strlen);
+ for (int i = 0; i < strlen; i++)
+ {
+ char c = str.charAt(i);
+ switch (c)
+ {
+ case '%':
+ buf.append("%25"); //$NON-NLS-1$
+ break;
+ case '\n':
+ buf.append("%0A"); //$NON-NLS-1$
+ break;
+ default:
+ buf.append(c);
+ }
+ }
+ return buf.toString();
+ }
+
+ /**
+ * Undo encoding
+ * @param str the encoded string
+ * @return the un-encoded string
+ */
+ private String decode(String str)
+ {
+ int strlen = str.length();
+ StringBuffer buf = new StringBuffer(2 * strlen);
+ for (int i = 0; i < strlen; i++)
+ {
+ char c = str.charAt(i);
+ if (c == '%')
+ {
+ if ("%0A".equals(str.substring(i, i + 3))) //$NON-NLS-1$
+ {
+ buf.append('\n');
+ }
+ else // if ("%25".equals(str.substring(i, i + 3))
+ {
+ buf.append('%');
+ }
+ i += 2;
+ }
+ else
+ {
+ buf.append(c);
+ }
+ }
+ return buf.toString();
+ }
+
+ /**
* Chunk size for context length.
*/
private static final int MARGIN = 4;
Modified: trunk/common/src/test/java/org/crosswire/common/diff/PatchEntryTest.java
===================================================================
--- trunk/common/src/test/java/org/crosswire/common/diff/PatchEntryTest.java 2007-08-23 21:01:26 UTC (rev 1690)
+++ trunk/common/src/test/java/org/crosswire/common/diff/PatchEntryTest.java 2007-08-24 00:22:57 UTC (rev 1691)
@@ -42,21 +42,13 @@
assertEquals("PatchEntry.fromText: #2.", "@@ -1 +1 @@\n-a\n+b\n", new PatchEntry("@@ -1 +1 @@\n-a\n+b\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertEquals("PatchEntry.fromText: #3.", "@@ -1,3 +0,0 @@\n-abc\n", new PatchEntry("@@ -1,3 +0,0 @@\n-abc\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
assertEquals("PatchEntry.fromText: #4.", "@@ -0,0 +1,3 @@\n+abc\n", new PatchEntry("@@ -0,0 +1,3 @@\n+abc\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- assertEquals("PatchEntry.fromText: #4.", "@@ -1,7 +1,6 @@\n foo\n-\n\n bar\n", new PatchEntry("@@ -1,7 +1,6 @@\n foo\n-\n\n bar\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- String text1 = "foo\nbar";
- String text2 = "foobar";
- Patch patch = new Patch(text1, text2);
- String patchText = patch.toText();
- //@@ -1,7 +1,6 @@
- // foo
- //-
- //
- // bar
-
- Patch patch2 = new Patch();
- patch2 = patch2.fromText(patchText);
- System.out.println(patch2.apply(text1).getText());
- // should print "foobar" but prints "bar"
+ assertEquals("PatchEntry.fromText: #5.", "@@ -1,7 +1,6 @@\n foo\n-%0A\n bar\n", new PatchEntry("@@ -1,7 +1,6 @@\n foo\n-%0A\n bar\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertEquals("PatchEntry.fromText: #6.", "@@ -1,4 +1,3 @@\n foo\n-%0A\n", new PatchEntry("@@ -1,4 +1,3 @@\n foo\n-%0A\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertEquals("PatchEntry.fromText: #7.", "@@ -1,4 +1,3 @@\n-%0A\n foo\n", new PatchEntry("@@ -1,4 +1,3 @@\n-%0A\n foo\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertEquals("PatchEntry.fromText: #8.", "@@ -1,3 +1,4 @@\n foo\n+%0A\n", new PatchEntry("@@ -1,3 +1,4 @@\n foo\n+%0A\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertEquals("PatchEntry.fromText: #9.", "@@ -1,3 +1,4 @@\n+%0A\n foo\n", new PatchEntry("@@ -1,3 +1,4 @@\n+%0A\n foo\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertEquals("PatchEntry.fromText: #10.", "@@ -1,4 +1,4 @@\n-foo%0A\n+%0Afoo\n", new PatchEntry("@@ -1,4 +1,4 @@\n-foo%0A\n+%0Afoo\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ assertEquals("PatchEntry.fromText: #11.", "@@ -1,4 +1,4 @@\n-%0Afoo\n+foo%0A\n", new PatchEntry("@@ -1,4 +1,4 @@\n-%0Afoo\n+foo%0A\n").toString()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public void testMatchAddContext()
More information about the jsword-svn
mailing list