KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lexer > gen > StringReplace


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lexer.gen;
21
22 import java.io.File JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Task;
28
29 /**
30  * Abstract class for supporting a language source generation.
31  *
32  * @author Miloslav Metelka
33  */

34 public class StringReplace extends Task {
35
36     private String JavaDoc replaceWhat;
37
38     private String JavaDoc replaceWith;
39
40     private String JavaDoc replaceIn;
41
42     private String JavaDoc property;
43
44     public StringReplace() {
45     }
46
47     public String JavaDoc getReplaceWhat() {
48         return replaceWhat;
49     }
50     
51     public void setReplaceWhat(String JavaDoc replaceWhat) {
52         this.replaceWhat = replaceWhat;
53     }
54     
55     public String JavaDoc getReplaceWith() {
56         return replaceWith;
57     }
58     
59     public void setReplaceWith(String JavaDoc replaceWith) {
60         this.replaceWith = replaceWith;
61     }
62     
63     public String JavaDoc getReplaceIn() {
64         return replaceIn;
65     }
66     
67     public void setReplaceIn(String JavaDoc replaceIn) {
68         this.replaceIn = replaceIn;
69     }
70
71     public String JavaDoc getProperty() {
72         return property;
73     }
74     
75     public void setProperty(String JavaDoc property) {
76         this.property = property;
77     }
78
79     public void execute() throws BuildException {
80         if (getReplaceWhat() == null || "".equals(getReplaceWhat())) {
81             throw new BuildException("replaceWhat attribute must be set");
82         }
83         if (getProperty() == null || "".equals(getProperty())) {
84             throw new BuildException("property attribute must be set");
85         }
86
87         String JavaDoc output = getReplaceIn();
88         int startIndex = 0;
89         while (true) {
90             int foundIndex = output.indexOf(getReplaceWhat(), startIndex);
91             if (foundIndex == -1) {
92                 break;
93             }
94
95             output = output.substring(0, foundIndex)
96                 + getReplaceWith()
97                 + output.substring(foundIndex + getReplaceWhat().length());
98
99             foundIndex += getReplaceWith().length();
100         }
101         
102         // Set the target property
103
getOwningTarget().getProject().setProperty(getProperty(), output);
104     }
105     
106 }
107
Popular Tags