Skip to content

Commit

Permalink
Bugfix for #1803: [Externalize Strings] IndexOutOfBoundsException. (#…
Browse files Browse the repository at this point in the history
…1805)

Fixes #1803
  • Loading branch information
nettozahler authored Dec 6, 2024
1 parent 30d7c1d commit 9887b92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2023 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -18,6 +18,7 @@
import java.util.Properties;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.Pattern;

import org.eclipse.osgi.util.NLS;

Expand Down Expand Up @@ -51,14 +52,20 @@

import org.eclipse.jdt.internal.corext.util.JavaModelUtil;


/**
* calculates hints for the nls-refactoring out of a compilation unit.
* - package fragments of the accessor class and the resource bundle
* - accessor class name, resource bundle name
*/
public class NLSHint {

/**
* A text block begins with three double-quote characters followed by zero or more white spaces followed by
* a line terminator. A text block cannot be on a single line, nor can the contents of the text block follow
* the three opening double-quotes (and maybe whitespace) immediately without a line terminator.
*/
private static final Pattern TEXT_BLOCK_START_PATTERN = Pattern.compile("^\"\"\"\\s*\r?\n"); //$NON-NLS-1$

private String fAccessorName;
private IPackageFragment fAccessorPackage;
private String fResourceBundleName;
Expand Down Expand Up @@ -159,7 +166,7 @@ public boolean visit(QualifiedName node) {
}
SimpleName name= node.getName();
NLSElement element= new NLSElement(node.getName().getIdentifier(), name.getStartPosition(),
name.getLength(), nlsLine.size() - 1, true);
name.getLength(), nlsLine.size() - 1, true);
nlsLine.add(element);
String bundleName;
ICompilationUnit bundleCU= (ICompilationUnit)type.getJavaElement().getAncestor(IJavaElement.COMPILATION_UNIT);
Expand Down Expand Up @@ -261,10 +268,11 @@ private static AccessorClassReference findFirstAccessorReference(NLSLine[] lines
}

public static String stripQuotes(String str, IJavaProject project) {
if (JavaModelUtil.is15OrHigher(project)) {
if (str.startsWith("\"\"\"") && str.endsWith("\"\"\"")) { //$NON-NLS-1$ //$NON-NLS-2$
return getTextBlock(str.substring(3, str.length() - 3));
}
// Test if the given string is a text block and start with the "cheapest" check
if (str.endsWith("\"\"\"") //$NON-NLS-1$
&& TEXT_BLOCK_START_PATTERN.matcher(str).find()
&& JavaModelUtil.is15OrHigher(project)) {
return getTextBlock(str.substring(3, str.length() - 3));
}
return str.substring(1, str.length() - 1);
}
Expand All @@ -277,15 +285,10 @@ private static NLSLine[] createRawLines(ICompilationUnit cu) {
}
}


public String getAccessorClassName() {
return fAccessorName;
}

// public boolean isEclipseNLS() {
// return fIsEclipseNLS;
// }

public IPackageFragment getAccessorClassPackage() {
return fAccessorPackage;
}
Expand Down Expand Up @@ -559,5 +562,4 @@ private static int getHexadecimalValue(char c) {
return -1;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,12 @@ public void test08() throws Exception {
String expected= " abc\ndef\n";
assertEquals(expected, y);
}

@Test
public void test09() throws Exception {
String x = "\"\"\"";
String y = NLSHint.stripQuotes(x, javaProject15.getJavaProject());
String expected = "\"";
assertEquals(expected, y);
}
}

0 comments on commit 9887b92

Please sign in to comment.