Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/BidiTextProcessorTestCase.java131
-rw-r--r--bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/LatinTextProcessorTestCase.java65
-rw-r--r--bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/TextProcessorTestCase.java22
-rw-r--r--bundles/org.eclipse.osgi/core/adaptor/org/eclipse/osgi/util/TextProcessor.java38
4 files changed, 195 insertions, 61 deletions
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/BidiTextProcessorTestCase.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/BidiTextProcessorTestCase.java
index 023902e2f..b57db8028 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/BidiTextProcessorTestCase.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/BidiTextProcessorTestCase.java
@@ -18,15 +18,14 @@ import org.eclipse.osgi.util.TextProcessor;
*
*/
public class BidiTextProcessorTestCase extends TextProcessorTestCase {
-
+
// left to right marker
protected static char LRM = '\u200e';
// left to right embedding
protected static char LRE = '\u202a';
// pop directional format
protected static char PDF = '\u202c';
-
-
+
private static String PATH_1_RESULT = LRE + "d" + PDF + ":" + LRM + "\\" + LRM + LRE + "test" + PDF + "\\" + LRM + LRE + "\u05d0\u05d1\u05d2\u05d3 \u05d4\u05d5" + PDF + "\\" + LRM + LRE + "segment" + PDF;
private static String PATH_2_RESULT = LRM + "\\" + LRM + LRE + "test" + PDF + "\\" + LRM + LRE + "\u05d0\u05d1\u05d2\u05d3 \u05d4\u05d5" + PDF + "\\" + LRM + LRE + "segment" + PDF;
private static String PATH_3_RESULT = LRE + "d" + PDF + ":" + LRM + "\\" + LRM + LRE + "\u05ea\u05e9\u05e8\u05e7\u05e6 abcdef-\u05e5\u05e4\u05e3" + PDF + "\\" + LRM + LRE + "xyz" + PDF + "\\" + LRM + LRE + "abcdef" + PDF + "\\" + LRM + LRE + "\u05e2\u05e1\u05e0" + PDF;
@@ -36,62 +35,138 @@ public class BidiTextProcessorTestCase extends TextProcessorTestCase {
private static String PATH_7_RESULT = LRE + "d" + PDF + ":" + LRM + "\\" + LRM + LRE + "\u05ea\u05e9\u05e8\u05e7\u05e6 abcdef-\u05e5\u05e4\u05e3" + PDF + "\\" + LRM + LRE + "xyz" + PDF + "\\" + LRM + LRE + "abcdef" + PDF + "\\" + LRM + LRE + "\u05e2\u05e1\05e0" + PDF + "\\" + LRM + LRE + "Test" + PDF + "." + LRM + LRE + "java" + PDF;
private static String PATH_8_RESULT = LRM + "\\" + LRM + LRE + "test" + PDF + "\\" + LRM + LRE + "jkl\u05d0\u05d1\u05d2\u05d3 \u05d4\u05d5" + PDF + "\\" + LRM + LRE + "segment" + PDF;
private static String PATH_9_RESULT = LRM + "\\" + LRM + LRE + "test" + PDF + "\\" + LRM + LRE + "\u05d0\u05d1\u05d2\u05d3 \u05d4\u05d5jkl" + PDF + "\\" + LRM + LRE + "segment" + PDF;
-
+ private static String PATH_10_RESULT = LRE + "d" + PDF + ":" + LRM + "\\" + LRM + LRE + "t" + PDF + "\\" + LRM + LRE + "\u05d0" + PDF + "\\" + LRM + LRE + "segment" + PDF;
+ private static String PATH_11_RESULT = "\\" + LRM + LRE + "t" + PDF + "\\" + LRM + LRE + "\u05d0" + PDF + "\\" + LRM + LRE + "segment" + PDF;
+ private static String PATH_12_RESULT = LRE + "d" + PDF + ":" + LRM + "\\" + LRM;
+ private static String PATH_13_RESULT = LRM + "\\" + LRM + LRE + "test" + PDF;
+
+ private static String OTHER_STRING_NO_DELIM = "\u05ea\u05e9\u05e8\u05e7\u05e6 abcdef-\u05e5\u05e4\u05e3";
+
private static String OTHER_STRING_1_RESULT = LRM + "*" + LRM + "." + LRM + LRE + "java" + PDF;
private static String OTHER_STRING_2_RESULT = LRM + "*" + LRM + "." + LRM + LRE + "\u05d0\u05d1\u05d2" + PDF;
private static String OTHER_STRING_3_RESULT = LRE + "\u05d0\u05d1\u05d2 " + PDF + "=" + LRM + LRE + " \u05ea\u05e9\u05e8\u05e7\u05e6" + PDF;
-
-
- private static String[] RESULT_DEFAULT_PATHS = {
- PATH_1_RESULT, PATH_2_RESULT, PATH_3_RESULT, PATH_4_RESULT, PATH_5_RESULT,
- PATH_6_RESULT, PATH_7_RESULT, PATH_8_RESULT, PATH_9_RESULT
- };
-
- private static String[] RESULT_STAR_PATHS = { OTHER_STRING_1_RESULT, OTHER_STRING_2_RESULT };
- private static String[] RESULT_EQUALS_PATHS = { OTHER_STRING_3_RESULT };
-
+ // result strings if null delimiter is passed for *.<string> texts
+ private static String OTHER_STRING_1_ND_RESULT = LRE + "*" + PDF + "." + LRM + LRE + "java" + PDF;
+ private static String OTHER_STRING_2_ND_RESULT = LRE + "*" + PDF + "." + LRM + LRE + "\u05d0\u05d1\u05d2" + PDF;
+
+ private static String[] RESULT_DEFAULT_PATHS = {PATH_1_RESULT, PATH_2_RESULT, PATH_3_RESULT, PATH_4_RESULT, PATH_5_RESULT, PATH_6_RESULT, PATH_7_RESULT, PATH_8_RESULT, PATH_9_RESULT, PATH_10_RESULT, PATH_11_RESULT, PATH_12_RESULT, PATH_13_RESULT};
+
+ private static String[] RESULT_STAR_PATHS = {OTHER_STRING_1_RESULT, OTHER_STRING_2_RESULT};
+ private static String[] RESULT_EQUALS_PATHS = {OTHER_STRING_3_RESULT};
+ private static String[] RESULT_STAR_PATHS_ND = {OTHER_STRING_1_ND_RESULT, OTHER_STRING_2_ND_RESULT};
+
protected String defaultDelimiters = TextProcessor.getDefaultDelimiters();
-
+
/**
* Constructor.
*
* @param name test name
*/
- public BidiTextProcessorTestCase(String name){
+ public BidiTextProcessorTestCase(String name) {
super(name);
}
/*
* Test TextProcessor for file paths.
*/
- public void testBidiPaths(){
- for (int i = 0; i < TEST_DEFAULT_PATHS.length; i++){
+ public void testBidiPaths() {
+ for (int i = 0; i < TEST_DEFAULT_PATHS.length; i++) {
String result = TextProcessor.process(TEST_DEFAULT_PATHS[i]);
verifyBidiResult("Path " + (i + 1), result, RESULT_DEFAULT_PATHS[i]);
}
}
-
+
+ public void testBidiPathsWithNullDelimiter() {
+ // should use default delimiters
+ for (int i = 0; i < TEST_DEFAULT_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_DEFAULT_PATHS[i], null);
+ verifyBidiResult("Path " + (i + 1), result, RESULT_DEFAULT_PATHS[i]);
+ }
+ }
+
+ public void testBidiStringWithNoDelimiters() {
+ String result = TextProcessor.process(OTHER_STRING_NO_DELIM);
+ assertEquals("Other string containing no delimiters not equivalent.", OTHER_STRING_NO_DELIM, result);
+ }
+
/*
* Test other possible uses for TextProcessor, including file associations and
* variable assignment statements.
*/
- public void testOtherStrings(){
+ public void testOtherStrings() {
+ int testNum = 1;
+ for (int i = 0; i < TEST_STAR_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_STAR_PATHS[i], "*.");
+ verifyBidiResult("Other string " + testNum, result, RESULT_STAR_PATHS[i]);
+ testNum++;
+ }
+
+ for (int i = 0; i < TEST_EQUALS_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_EQUALS_PATHS[i], "=");
+ verifyBidiResult("Other string " + testNum, result, RESULT_EQUALS_PATHS[i]);
+ testNum++;
+ }
+ }
+
+ public void testOtherStringsWithNullDelimiter() {
+ int testNum = 1;
+ for (int i = 0; i < TEST_STAR_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_STAR_PATHS[i], null);
+ verifyBidiResult("Other string " + testNum, result, RESULT_STAR_PATHS_ND[i]);
+ testNum++;
+ }
+ // should be the same result as what was input
+ for (int i = 0; i < TEST_EQUALS_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_EQUALS_PATHS[i], null);
+ verifyBidiResult("Other string " + testNum, result, TEST_EQUALS_PATHS[i]);
+ testNum++;
+ }
+ }
+
+ /*
+ * Test the result to ensure markers aren't added more than once if the
+ * string is processed multiple times.
+ */
+ public void testDoubleProcessPaths() {
+ for (int i = 0; i < TEST_DEFAULT_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_DEFAULT_PATHS[i]);
+ result = TextProcessor.process(result);
+ verifyBidiResult("Path " + (i + 1), result, RESULT_DEFAULT_PATHS[i]);
+ }
+ }
+
+ /*
+ * Test the result to ensure markers aren't added more than once if the
+ * string is processed multiple times.
+ */
+ public void testDoubleProcessOtherStrings() {
int testNum = 1;
- for (int i = 0; i < TEST_STAR_PATHS.length; i++){
+ for (int i = 0; i < TEST_STAR_PATHS.length; i++) {
String result = TextProcessor.process(TEST_STAR_PATHS[i], "*.");
+ result = TextProcessor.process(result, "*.");
verifyBidiResult("Other string " + testNum, result, RESULT_STAR_PATHS[i]);
testNum++;
}
-
- for (int i = 0; i < TEST_EQUALS_PATHS.length; i++){
+
+ for (int i = 0; i < TEST_EQUALS_PATHS.length; i++) {
String result = TextProcessor.process(TEST_EQUALS_PATHS[i], "=");
- verifyBidiResult("Other string " + testNum, result, RESULT_EQUALS_PATHS[i]);
+ result = TextProcessor.process(result, "=");
+ verifyBidiResult("Other string " + testNum, result, RESULT_EQUALS_PATHS[i]);
testNum++;
}
}
-
- private void verifyBidiResult(String testName, String result, String expected){
- assertTrue(testName + " result string is not the same as expected string.",
- result.equals(expected));
+
+ public void testEmptyStringParams() {
+ verifyBidiResult("TextProcessor.process(String) for empty string ", TextProcessor.process(""), EMPTY_STRING);
+ verifyBidiResult("TextProcessor.process(String, String) for empty strings ", TextProcessor.process("", ""), EMPTY_STRING);
+ }
+
+ public void testNullParams() {
+ assertNull("TextProcessor.process(String) for null param ", TextProcessor.process(null));
+ assertNull("TextProcessor.process(String, String) for params ", TextProcessor.process(null, null));
+ }
+
+ private void verifyBidiResult(String testName, String result, String expected) {
+ assertTrue(testName + " result string is not the same as expected string.", result.equals(expected));
}
}
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/LatinTextProcessorTestCase.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/LatinTextProcessorTestCase.java
index 7a2bf8062..089db3945 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/LatinTextProcessorTestCase.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/LatinTextProcessorTestCase.java
@@ -9,25 +9,25 @@ import org.eclipse.osgi.util.TextProcessor;
*/
public class LatinTextProcessorTestCase extends TextProcessorTestCase {
private static String[] ALL_PATHS;
- static{
+ static {
// merge all test strings into one array for Latin locales
int size = TEST_DEFAULT_PATHS.length + TEST_STAR_PATHS.length + TEST_EQUALS_PATHS.length;
ALL_PATHS = new String[size];
int idx = 0;
- for (int i = 0; i < TEST_DEFAULT_PATHS.length; i++){
+ for (int i = 0; i < TEST_DEFAULT_PATHS.length; i++) {
ALL_PATHS[idx] = TEST_DEFAULT_PATHS[i];
idx++;
}
- for (int i = 0; i < TEST_STAR_PATHS.length; i++){
+ for (int i = 0; i < TEST_STAR_PATHS.length; i++) {
ALL_PATHS[idx] = TEST_STAR_PATHS[i];
idx++;
}
- for (int i = 0; i < TEST_EQUALS_PATHS.length; i++){
+ for (int i = 0; i < TEST_EQUALS_PATHS.length; i++) {
ALL_PATHS[idx] = TEST_EQUALS_PATHS[i];
idx++;
}
}
-
+
/**
* Constructor for class.
*
@@ -36,30 +36,59 @@ public class LatinTextProcessorTestCase extends TextProcessorTestCase {
public LatinTextProcessorTestCase(String name) {
super(name);
}
-
- public void testLatinPaths(){
+
+ public void testLatinPaths() {
// test all strings using process(String) method
- for (int i = 0; i < ALL_PATHS.length; i++){
+ for (int i = 0; i < ALL_PATHS.length; i++) {
String result = TextProcessor.process(ALL_PATHS[i]);
verifyLatinResult("String " + (i + 1), result, ALL_PATHS[i]);
}
}
-
- public void testLatinOtherStrings(){
+
+ public void testLatinPathsWithNullDelimiter() {
+ // should use default delimiters
+ for (int i = 0; i < ALL_PATHS.length; i++) {
+ String result = TextProcessor.process(ALL_PATHS[i], null);
+ verifyLatinResult("String " + (i + 1), result, ALL_PATHS[i]);
+ }
+ }
+
+ public void testLatinOtherStrings() {
// test the process(String, String) method
- for (int i = 0; i < TEST_STAR_PATHS.length; i++){
+ for (int i = 0; i < TEST_STAR_PATHS.length; i++) {
String result = TextProcessor.process(TEST_STAR_PATHS[i], "*.");
verifyLatinResult("File association " + (i + 1), result, TEST_STAR_PATHS[i]);
}
-
- for (int i = 0; i < TEST_EQUALS_PATHS.length; i++){
- String result = TextProcessor.process(TEST_EQUALS_PATHS[i], "*.");
+
+ for (int i = 0; i < TEST_EQUALS_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_EQUALS_PATHS[i], "=");
+ verifyLatinResult("Equals expression " + (i + 1), result, TEST_EQUALS_PATHS[i]);
+ }
+ }
+
+ public void testLatinOtherStringsWithNoDelimiter() {
+ for (int i = 0; i < TEST_STAR_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_STAR_PATHS[i], null);
+ verifyLatinResult("File association " + (i + 1), result, TEST_STAR_PATHS[i]);
+ }
+
+ for (int i = 0; i < TEST_EQUALS_PATHS.length; i++) {
+ String result = TextProcessor.process(TEST_EQUALS_PATHS[i], null);
verifyLatinResult("Equals expression " + (i + 1), result, TEST_EQUALS_PATHS[i]);
}
}
-
- private void verifyLatinResult(String testName, String expected, String result){
- assertTrue(testName + " result string is not the same as string passed in.",
- result.equals(expected));
+
+ private void verifyLatinResult(String testName, String expected, String result) {
+ assertTrue(testName + " result string is not the same as string passed in.", result.equals(expected));
+ }
+
+ public void testEmptyStringParams() {
+ verifyLatinResult("TextProcessor.process(String) for empty string ", TextProcessor.process(""), EMPTY_STRING);
+ verifyLatinResult("TextProcessor.process(String, String) for empty strings ", TextProcessor.process("", ""), EMPTY_STRING);
+ }
+
+ public void testNullParams() {
+ assertNull("TextProcessor.process(String) for null param ", TextProcessor.process(null));
+ assertNull("TextProcessor.process(String, String) for params ", TextProcessor.process(null, null));
}
}
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/TextProcessorTestCase.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/TextProcessorTestCase.java
index 5d81d0dcf..7658554bd 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/TextProcessorTestCase.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/util/TextProcessorTestCase.java
@@ -8,7 +8,7 @@ import junit.framework.TestCase;
*/
public abstract class TextProcessorTestCase extends TestCase {
// strings to test
-
+
// paths
protected static String PATH_1 = "d:\\test\\\u05d0\u05d1\u05d2\u05d3 \u05d4\u05d5\\segment";
protected static String PATH_2 = "\\test\\\u05d0\u05d1\u05d2\u05d3 \u05d4\u05d5\\segment";
@@ -19,20 +19,24 @@ public abstract class TextProcessorTestCase extends TestCase {
protected static String PATH_7 = "d:\\\u05ea\u05e9\u05e8\u05e7\u05e6 abcdef-\u05e5\u05e4\u05e3\\xyz\\abcdef\\\u05e2\u05e1\05e0\\Test.java";
protected static String PATH_8 = "\\test\\jkl\u05d0\u05d1\u05d2\u05d3 \u05d4\u05d5\\segment";
protected static String PATH_9 = "\\test\\\u05d0\u05d1\u05d2\u05d3 \u05d4\u05d5jkl\\segment";
+ protected static String PATH_10 = "d:\\t\\\u05d0\\segment";
+ protected static String PATH_11 = "\\t\\\u05d0\\segment";
+ protected static String PATH_12 = "d:\\";
+ protected static String PATH_13 = "\\t";
// other strings - file associations and assignment statements
protected static String OTHER_STRING_1 = "*.java";
protected static String OTHER_STRING_2 = "*.\u05d0\u05d1\u05d2";
protected static String OTHER_STRING_3 = "\u05d0\u05d1\u05d2 = \u05ea\u05e9\u05e8\u05e7\u05e6";
-
- protected static String[] TEST_DEFAULT_PATHS = {
- PATH_1, PATH_2, PATH_3, PATH_4, PATH_5, PATH_6, PATH_7, PATH_8, PATH_9
- };
-
- protected static String[] TEST_STAR_PATHS = { OTHER_STRING_1, OTHER_STRING_2};
-
+
+ protected static String EMPTY_STRING = "";
+
+ protected static String[] TEST_DEFAULT_PATHS = {PATH_1, PATH_2, PATH_3, PATH_4, PATH_5, PATH_6, PATH_7, PATH_8, PATH_9};
+
+ protected static String[] TEST_STAR_PATHS = {OTHER_STRING_1, OTHER_STRING_2};
+
protected static String[] TEST_EQUALS_PATHS = {OTHER_STRING_3};
-
+
/**
* Constructor for class
*
diff --git a/bundles/org.eclipse.osgi/core/adaptor/org/eclipse/osgi/util/TextProcessor.java b/bundles/org.eclipse.osgi/core/adaptor/org/eclipse/osgi/util/TextProcessor.java
index 1f9722e18..bd763262b 100644
--- a/bundles/org.eclipse.osgi/core/adaptor/org/eclipse/osgi/util/TextProcessor.java
+++ b/bundles/org.eclipse.osgi/core/adaptor/org/eclipse/osgi/util/TextProcessor.java
@@ -124,12 +124,13 @@ public class TextProcessor {
* resulting string will not return the same values as would be returned
* for the original string.
* </p>
- * @param str the text to process
- * @param delimiter delimiters by which the string will be segmented
+ * @param str the text to process, if <code>null</code> return the string as it was passed in
+ * @param delimiter delimiters by which the string will be segmented, if <code>null</code>
+ * the default delimiters are used
* @return the processed string
*/
public static String process(String str, String delimiter) {
- if (!isBidi)
+ if (!isBidi || str == null)
return str;
delimiter = delimiter == null ? getDefaultDelimiters() : delimiter;
@@ -140,6 +141,7 @@ public class TextProcessor {
// no delimiters found so revert to default behaviour
if (tokenizer.countTokens() == 1)
return str;
+
StringBuffer buf = new StringBuffer(); // the string to build
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
@@ -153,9 +155,33 @@ public class TextProcessor {
} else {
// (non-delimiter) text has basic RTL orientation
// see (#4) of algorithm
- buf.append(LRE);
- buf.append(token);
- buf.append(PDF);
+ // do not double process an already processed string
+ boolean skipToken = false;
+ char firstChar = token.charAt(0);
+ if (firstChar == LRM) {
+ // the token is an LRM character
+ if (token.length() == 1) {
+ if (lastToken == null) {
+ // LRM is the first token, add to buffer
+ buf.append(token);
+ }
+ skipToken = true; // don't process this token
+ } else {
+ // remove the LRM and process the rest of the token
+ token = token.substring(1, token.length());
+ firstChar = token.charAt(0);
+ }
+ }
+ // don't process the token if it consists solely of a directional character
+ if (!skipToken) {
+ if (firstChar == LRE) {
+ buf.append(token);
+ } else {
+ buf.append(LRE);
+ buf.append(token);
+ buf.append(PDF);
+ }
+ }
}
lastToken = token;
}

Back to the top