View | Details | Raw Unified | Return to bug 17297
Collapse All | Expand All

(-)src/main/org/apache/tools/ant/taskdefs/optional/i18n/Translate.java (-43 / +59 lines)
Lines 510-558 Link Here
510
                        int stLength = startToken.length();
510
                        int stLength = startToken.length();
511
                        int etLength = endToken.length();
511
                        int etLength = endToken.length();
512
                        while ((line = in.readLine()) != null) {
512
                        while ((line = in.readLine()) != null) {
513
                            int startIndex = -1;
513
						// 2003-02-21 new replace algorithme by tbee (tbee@tbee.org) 
514
                            int endIndex = -1;
514
						// because it wasn't able to replace something like "@aaa;@bbb;"
515
outer:                      while (true) {
515
						
516
                                startIndex = line.indexOf(startToken, endIndex + etLength);
516
						// is there a startToken
517
                                if (startIndex < 0 ||
517
						// and there is still stuff following the startToken
518
                                    startIndex + stLength >= line.length()) {
518
						int startIndex = line.indexOf(startToken);
519
                                    break;
519
						while ( startIndex >= 0 && (startIndex+startToken.length()) <= line.length() )
520
                                }
520
						{
521
                                endIndex = line.indexOf(endToken, startIndex + stLength);
521
							// the new value, this needs to be here 
522
                                if (endIndex < 0) {
522
							// because it is required to calculate the next position to search from 
523
                                    break;
523
							// at the end of the loop
524
                                }
524
				            String replace = null;
525
                                String matches = line.substring(startIndex + stLength,
525
				
526
                                                                endIndex);
526
							// we found a starttoken, is there an endtoken following?
527
                                //If there is a white space or = or :, then
527
							// start at token+tokenlength because start and end token may be indentical
528
                                //it isn't to be treated as a valid key.
528
							int endIndex = line.indexOf(endToken, startIndex + startToken.length());
529
                                for (int k = 0; k < matches.length(); k++) {
529
							if (endIndex < 0) startIndex += 1;
530
                                    char c = matches.charAt(k);
530
							else
531
                                    if (c == ':' ||
531
							{
532
                                        c == '=' ||
532
								// grab the token
533
                                        Character.isSpaceChar(c)) {
533
								String token = line.substring(startIndex + startToken.length(), endIndex);
534
                                        endIndex = endIndex - 1;
534
535
                                        continue outer;
535
				                // If there is a white space or = or :, then
536
                                    }
536
				                // it isn't to be treated as a valid key.
537
                                }
537
				                boolean validToken = true;
538
                                String replace = null;
538
				                for (int k = 0; k < token.length() && validToken; k++) 
539
                                replace = (String) resourceMap.get(matches);
539
				                {
540
                                    //If the key hasn't been loaded into resourceMap,
540
				                    char c = token.charAt(k);
541
                                    //use the key itself as the value also.
541
				                    if ( c == ':' 
542
                                if (replace == null) {
542
				                      || c == '=' 
543
                                    log("Warning: The key: " + matches
543
				                      || Character.isSpaceChar(c)
544
                                        + " hasn't been defined.",
544
				                       ) 
545
                                        Project.MSG_DEBUG);
545
				                    {
546
                                    replace = matches;
546
				                    	validToken = false;
547
                                }
547
				                    }
548
                                line = line.substring(0, startIndex)
548
				                }
549
                                    + replace
549
				                if (!validToken) startIndex += 1;
550
                                    + line.substring(endIndex + etLength);
550
				                else
551
                                endIndex = startIndex + replace.length() + etLength;
551
				                {
552
                                if (endIndex + etLength >= line.length()) {
552
				                	// find the replace string
553
                                    break;
553
				                	if (resourceMap.containsKey(token)) replace = (String)resourceMap.get(token);
554
                                }
554
				                	else                                replace = token;
555
                            }
555
				                    
556
				                    
557
				                    // generate the new line
558
				                    line = line.substring(0, startIndex)
559
				                         + replace
560
				                         + line.substring(endIndex + endToken.length());
561
562
									// set start position for next search
563
									startIndex += replace.length();
564
				                }                				
565
							}
566
							
567
							// find next starttoken
568
							startIndex = line.indexOf(startToken, startIndex);			
569
						}
570
571
556
                            out.write(line);
572
                            out.write(line);
557
                            out.newLine();
573
                            out.newLine();
558
                        }
574
                        }

Return to bug 17297