Author: bodewig
Date: Mon Feb 1 09:01:55 2010
New Revision: 905216
URL: http://svn.apache.org/viewvc?rev=905216&view=rev
Log:
extract method
Modified:
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java
Modified:
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java?rev=905216&r1=905215&r2=905216&view=diff
==============================================================================
---
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java
(original)
+++
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java
Mon Feb 1 09:01:55 2010
@@ -530,88 +530,7 @@
if (needsWork) {
log("Processing " + srcFiles[j],
Project.MSG_DEBUG);
- BufferedWriter out = null;
- BufferedReader in = null;
- try {
- FileOutputStream fos = new FileOutputStream(dest);
- out
- = new BufferedWriter(new OutputStreamWriter(fos,
destEncoding));
- FileInputStream fis = new FileInputStream(src);
- in
- = new BufferedReader(new InputStreamReader(fis,
srcEncoding));
- String line;
- LineTokenizer lineTokenizer = new LineTokenizer();
- lineTokenizer.setIncludeDelims(true);
- line = lineTokenizer.getToken(in);
- while ((line) != null) {
- // 2003-02-21 new replace algorithm by tbee
([email protected])
- // because it wasn't able to replace something
like "@aaa;@bbb;"
-
- // is there a startToken
- // and there is still stuff following the
startToken
- int startIndex = line.indexOf(startToken);
- while (startIndex >= 0
- && (startIndex + startToken.length()) <=
line.length()) {
- // the new value, this needs to be here
- // because it is required to calculate the
next position to
- // search from at the end of the loop
- String replace = null;
-
- // we found a starttoken, is there an endtoken
following?
- // start at token+tokenlength because start
and end
- // token may be indentical
- int endIndex = line.indexOf(
- endToken, startIndex +
startToken.length());
- if (endIndex < 0) {
- startIndex += 1;
- } else {
- // grab the token
- String token = line.substring(
- startIndex + startToken.length(),
endIndex);
-
- // If there is a white space or = or :,
then
- // it isn't to be treated as a valid key.
- boolean validToken = true;
- for (int k = 0; k < token.length() &&
validToken; k++) {
- char c = token.charAt(k);
- if (c == ':' || c == '='
- || Character.isSpaceChar(c)) {
- validToken = false;
- }
- }
- if (!validToken) {
- startIndex += 1;
- } else {
- // find the replace string
- if (resourceMap.containsKey(token)) {
- replace = (String)
resourceMap.get(token);
- } else {
- log("Replacement string missing
for: "
- + token, Project.MSG_VERBOSE);
- replace = startToken + token +
endToken;
- }
-
-
- // generate the new line
- line = line.substring(0, startIndex)
- + replace
- + line.substring(endIndex +
endToken.length());
-
- // set start position for next search
- startIndex += replace.length();
- }
- }
-
- // find next starttoken
- startIndex = line.indexOf(startToken,
startIndex);
- }
- out.write(line);
- line = lineTokenizer.getToken(in);
- }
- } finally {
- FileUtils.close(in);
- FileUtils.close(out);
- }
+ translateOneFile(src, dest);
++filesProcessed;
} else {
log("Skipping " + srcFiles[j]
@@ -625,4 +544,87 @@
}
log("Translation performed on " + filesProcessed + " file(s).",
Project.MSG_DEBUG);
}
+
+ private void translateOneFile(File src, File dest) throws IOException {
+ BufferedWriter out = null;
+ BufferedReader in = null;
+ try {
+ FileOutputStream fos = new FileOutputStream(dest);
+ out = new BufferedWriter(new OutputStreamWriter(fos,
destEncoding));
+ FileInputStream fis = new FileInputStream(src);
+ in = new BufferedReader(new InputStreamReader(fis, srcEncoding));
+ String line;
+ LineTokenizer lineTokenizer = new LineTokenizer();
+ lineTokenizer.setIncludeDelims(true);
+ line = lineTokenizer.getToken(in);
+ while ((line) != null) {
+ // 2003-02-21 new replace algorithm by tbee ([email protected])
+ // because it wasn't able to replace something like
"@aaa;@bbb;"
+
+ // is there a startToken
+ // and there is still stuff following the startToken
+ int startIndex = line.indexOf(startToken);
+ while (startIndex >= 0
+ && (startIndex + startToken.length()) <= line.length())
{
+ // the new value, this needs to be here
+ // because it is required to calculate the next position to
+ // search from at the end of the loop
+ String replace = null;
+
+ // we found a starttoken, is there an endtoken following?
+ // start at token+tokenlength because start and end
+ // token may be indentical
+ int endIndex = line.indexOf(endToken, startIndex
+ + startToken.length());
+ if (endIndex < 0) {
+ startIndex += 1;
+ } else {
+ // grab the token
+ String token = line.substring(startIndex
+ + startToken.length(),
+ endIndex);
+
+ // If there is a white space or = or :, then
+ // it isn't to be treated as a valid key.
+ boolean validToken = true;
+ for (int k = 0; k < token.length() && validToken; k++)
{
+ char c = token.charAt(k);
+ if (c == ':' || c == '='
+ || Character.isSpaceChar(c)) {
+ validToken = false;
+ }
+ }
+ if (!validToken) {
+ startIndex += 1;
+ } else {
+ // find the replace string
+ if (resourceMap.containsKey(token)) {
+ replace = (String) resourceMap.get(token);
+ } else {
+ log("Replacement string missing for: " + token,
+ Project.MSG_VERBOSE);
+ replace = startToken + token + endToken;
+ }
+
+
+ // generate the new line
+ line = line.substring(0, startIndex) + replace
+ + line.substring(endIndex + endToken.length());
+
+ // set start position for next search
+ startIndex += replace.length();
+ }
+ }
+
+ // find next starttoken
+ startIndex = line.indexOf(startToken, startIndex);
+ }
+ out.write(line);
+ line = lineTokenizer.getToken(in);
+ }
+ } finally {
+ FileUtils.close(in);
+ FileUtils.close(out);
+ }
+ }
}