Diff attached
On Thu, 3 Oct 2002, Magesh Umasankar wrote:
> Please send the patch using cvs diff -u
>
> Thanks,
> Magesh
>
> ***********************************************
> * Diplomat: A person who tells you to go to *
> * hell in such a way that you actually look *
> * forward to the trip. *
> ***********************************************
> ----- Original Message -----
> From: "Don Brown" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Wednesday, October 02, 2002 7:33 PM
> Subject: Translate i18n task modifications
>
>
> > To make the translate task useable for my project, I've made a few
> > modifications which you may or may not find useful. The modified
> > translate task, Translate2, is at
> > http://www.twdata.org/dakine/Translate2.java
> >
> > In summary the changes are:
> > * The starting and ending tokens can now be of any length
> > * The resources files are resolved with Project.resolveFile()
> > * Loaded the resources with java.util.Properties rather than the hand
> > crafted parser
> >
> > These changes made it possible for me to replace my runtime resource
> > replacement tags at build time (multiple language support is not
> > required).
> >
> > I could apply these changes to the original Translate and provide a patch
> > if needed. Thanks for the great project and all the hard work.
> >
> > Don
> >
> >
> > --
> > To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
> > For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> >
>
>
> --
> To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>
Index: Translate.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java,v
retrieving revision 1.16
diff -u -r1.16 Translate.java
--- Translate.java 25 Jul 2002 15:21:15 -0000 1.16
+++ Translate.java 3 Oct 2002 18:24:13 -0000
@@ -64,6 +64,7 @@
import java.util.Hashtable;
import java.util.Locale;
import java.util.Vector;
+import java.util.Properties;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
@@ -74,7 +75,7 @@
/**
* Translates text embedded in files using Resource Bundle files.
*
- * @author Magesh Umasankar
+ * @author Magesh Umasankar, Don Brown
*/
public class Translate extends MatchingTask {
@@ -254,24 +255,12 @@
if (startToken == null) {
throw new BuildException("The starttoken attribute must be set.",
getLocation());
- }
-
- if (startToken.length() != 1) {
- throw new BuildException(
- "The starttoken attribute must be a single character.",
- getLocation());
- }
+ }
if (endToken == null) {
throw new BuildException("The endtoken attribute must be set.",
getLocation());
- }
-
- if (endToken.length() != 1) {
- throw new BuildException(
- "The endtoken attribute must be a single character.",
- getLocation());
- }
+ }
if (bundleLanguage == null) {
Locale l = Locale.getDefault();
@@ -391,7 +380,7 @@
*/
private void processBundle(final String bundleFile, final int i,
final boolean checkLoaded) throws
BuildException {
- final File propsFile = new File(bundleFile + ".properties");
+ final File propsFile = getProject().resolveFile(bundleFile +
".properties");
FileInputStream ins = null;
try {
ins = new FileInputStream(propsFile);
@@ -416,52 +405,9 @@
*/
private void loadResourceMap(FileInputStream ins) throws BuildException {
try {
- BufferedReader in = null;
- InputStreamReader isr = new InputStreamReader(ins, bundleEncoding);
- in = new BufferedReader(isr);
- String line = null;
- while ((line = in.readLine()) != null) {
- //So long as the line isn't empty and isn't a comment...
- if (line.trim().length() > 1 &&
- ('#' != line.charAt(0) || '!' != line.charAt(0))) {
- //Legal Key-Value separators are :, = and white space.
- int sepIndex = line.indexOf('=');
- if (-1 == sepIndex) {
- sepIndex = line.indexOf(':');
- }
- if (-1 == sepIndex) {
- for (int k = 0; k < line.length(); k++) {
- if (Character.isSpaceChar(line.charAt(k))) {
- sepIndex = k;
- break;
- }
- }
- }
- //Only if we do have a key is there going to be a value
- if (-1 != sepIndex) {
- String key = line.substring(0, sepIndex).trim();
- String value = line.substring(sepIndex + 1).trim();
- //Handle line continuations, if any
- while (value.endsWith("\\")) {
- value = value.substring(0, value.length() - 1);
- if ((line = in.readLine()) != null) {
- value = value + line.trim();
- } else {
- break;
- }
- }
- if (key.length() > 0) {
- //Has key already been loaded into resourceMap?
- if (resourceMap.get(key) == null) {
- resourceMap.put(key, value);
- }
- }
- }
- }
- }
- if (in != null) {
- in.close();
- }
+ Properties props = new Properties();
+ props.load(ins);
+ resourceMap = props;
} catch (IOException ioe) {
throw new BuildException(ioe.getMessage(), getLocation());
}
@@ -520,32 +466,23 @@
BufferedReader in
= new BufferedReader(new InputStreamReader(fis,
srcEncoding));
String line;
+ int stLength = startToken.length();
+ int etLength = endToken.length();
while ((line = in.readLine()) != null) {
int startIndex = -1;
int endIndex = -1;
outer: while (true) {
- startIndex = line.indexOf(startToken, endIndex
+ 1);
+ startIndex = line.indexOf(startToken, endIndex
+ etLength);
if (startIndex < 0 ||
- startIndex + 1 >= line.length()) {
+ startIndex + stLength >= line.length()) {
break;
}
- endIndex = line.indexOf(endToken, startIndex +
1);
+ endIndex = line.indexOf(endToken, startIndex +
stLength);
if (endIndex < 0) {
break;
}
- String matches = line.substring(startIndex + 1,
+ String matches = line.substring(startIndex +
stLength,
endIndex);
- //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++) {
- char c = matches.charAt(k);
- if (c == ':' ||
- c == '=' ||
- Character.isSpaceChar(c)) {
- endIndex = endIndex - 1;
- continue outer;
- }
- }
String replace = null;
replace = (String) resourceMap.get(matches);
//If the key hasn't been loaded into
resourceMap,
@@ -556,11 +493,12 @@
Project.MSG_DEBUG);
replace = matches;
}
+
line = line.substring(0, startIndex)
+ 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;
}
}
@@ -575,6 +513,7 @@
}
} else {
log("Skipping " + srcFiles[j] +
+
" as destination file is up to date",
Project.MSG_VERBOSE);
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>