Browse Source

Allow start and end tokens to be multiple characters long.

PR: 13939

Submitted by: "Don Brown" <mrdon@twdata.org>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273569 13f79535-47bb-0310-9956-ffa450edef68
master
Magesh Umasankar 22 years ago
parent
commit
1dd390137b
2 changed files with 16 additions and 23 deletions
  1. +3
    -0
      WHATSNEW
  2. +13
    -23
      src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java

+ 3
- 0
WHATSNEW View File

@@ -61,6 +61,9 @@ Fixed bugs:


Other changes: Other changes:
-------------- --------------
* The start and end tokens for <translate> may now be longer than a
single character.

* <setproxy> lets you set the username and password for proxies that want authentication * <setproxy> lets you set the username and password for proxies that want authentication


* <loadproperties> has a new encoding attribute. * <loadproperties> has a new encoding attribute.


+ 13
- 23
src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java View File

@@ -74,7 +74,7 @@ import org.apache.tools.ant.util.FileUtils;
/** /**
* Translates text embedded in files using Resource Bundle files. * Translates text embedded in files using Resource Bundle files.
* *
* @author Magesh Umasankar
* @author Magesh Umasankar, Don Brown
*/ */
public class Translate extends MatchingTask { public class Translate extends MatchingTask {


@@ -256,23 +256,11 @@ public class Translate extends MatchingTask {
getLocation()); getLocation());
} }


if (startToken.length() != 1) {
throw new BuildException(
"The starttoken attribute must be a single character.",
getLocation());
}

if (endToken == null) { if (endToken == null) {
throw new BuildException("The endtoken attribute must be set.", throw new BuildException("The endtoken attribute must be set.",
getLocation()); getLocation());
} }


if (endToken.length() != 1) {
throw new BuildException(
"The endtoken attribute must be a single character.",
getLocation());
}

if (bundleLanguage == null) { if (bundleLanguage == null) {
Locale l = Locale.getDefault(); Locale l = Locale.getDefault();
bundleLanguage = l.getLanguage(); bundleLanguage = l.getLanguage();
@@ -391,7 +379,7 @@ public class Translate extends MatchingTask {
*/ */
private void processBundle(final String bundleFile, final int i, private void processBundle(final String bundleFile, final int i,
final boolean checkLoaded) throws BuildException { final boolean checkLoaded) throws BuildException {
final File propsFile = new File(bundleFile + ".properties");
final File propsFile = getProject().resolveFile(bundleFile + ".properties");
FileInputStream ins = null; FileInputStream ins = null;
try { try {
ins = new FileInputStream(propsFile); ins = new FileInputStream(propsFile);
@@ -520,23 +508,25 @@ public class Translate extends MatchingTask {
BufferedReader in BufferedReader in
= new BufferedReader(new InputStreamReader(fis, srcEncoding)); = new BufferedReader(new InputStreamReader(fis, srcEncoding));
String line; String line;
int stLength = startToken.length();
int etLength = endToken.length();
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
int startIndex = -1; int startIndex = -1;
int endIndex = -1; int endIndex = -1;
outer: while (true) { outer: while (true) {
startIndex = line.indexOf(startToken, endIndex + 1);
startIndex = line.indexOf(startToken, endIndex + etLength);
if (startIndex < 0 || if (startIndex < 0 ||
startIndex + 1 >= line.length()) {
startIndex + stLength >= line.length()) {
break; break;
} }
endIndex = line.indexOf(endToken, startIndex + 1);
endIndex = line.indexOf(endToken, startIndex + stLength);
if (endIndex < 0) { if (endIndex < 0) {
break; break;
} }
String matches = line.substring(startIndex + 1,
String matches = line.substring(startIndex + stLength,
endIndex); endIndex);
//If there is a white space or = or :, then
//it isn't to be treated as a valid key.
//If there is a white space or = or :, then
//it isn't to be treated as a valid key.
for (int k = 0; k < matches.length(); k++) { for (int k = 0; k < matches.length(); k++) {
char c = matches.charAt(k); char c = matches.charAt(k);
if (c == ':' || if (c == ':' ||
@@ -558,9 +548,9 @@ outer: while (true) {
} }
line = line.substring(0, startIndex) line = line.substring(0, startIndex)
+ replace + replace
+ line.substring(endIndex + 1);
endIndex = startIndex + replace.length() + 1;
if (endIndex + 1 >= line.length()) {
+ line.substring(endIndex + etLength);
endIndex = startIndex + replace.length() + etLength;
if (endIndex + etLength >= line.length()) {
break; break;
} }
} }


Loading…
Cancel
Save