KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > Replacement


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  */

23 package org.enhydra.tool.common;
24
25 import java.util.ResourceBundle JavaDoc;
26
27 /**
28  * The replacement class defines a search and replace operation
29  * for updating source code that was copied from a template file.
30  * Replacement object are used by the predefined generators
31  * to insert things like application and package names into
32  * java source files.
33  */

34 public class Replacement {
35     static ResourceBundle JavaDoc res =
36         ResourceBundle.getBundle ("org.enhydra.tool.common.Res"); // nores
37

38     //
39

40     private String JavaDoc find = new String JavaDoc ();
41     private String JavaDoc[] replaceWith = new String JavaDoc[0];
42
43     /**
44      * Define a replacement operation using an replacement array.
45      *
46      * @param find
47      * String to look for. These strings should be deliniated
48      * with '@' when not used for a file path. When
49      * the replacement does reference a file path, the find
50      * value should be prefixed with 'at_' and suffixed
51      * with '_at'.
52      * @param replaceWith
53      * One or more strings to use as a replacement for the find string. If the array
54      * contains more than one string, a new line is inserted for each element of the
55      * replaceWith array.
56      *
57      * @throws GenerationException
58      * Thrown if either parameter is null, or the find element is an empty string
59      * or if the replaceWith array has a length of zero.
60      */

61     public Replacement (String JavaDoc inFind,
62                         String JavaDoc[] inReplaceWith) throws ToolException {
63         construct (inFind, inReplaceWith);
64     }
65
66     /**
67      * Define a replacement operation using a single replacement string.
68      *
69      * @param find
70      * String to look for. These strings should be deliniated
71      * with '@' when not used for a file path. When
72      * the replacement does reference a file path, the find
73      * value should be prefixed with 'at_' and suffixed
74      * with '_at'.
75      * @param replaceWith
76      * String to use as a replacement for the find string.
77      *
78      * @throws GenerationException
79      * Thrown if either parameter is null or either element references
80      * an empty string.
81      */

82     public Replacement (String JavaDoc inFind,
83                         String JavaDoc inReplaceWith) throws ToolException {
84         String JavaDoc[] rw = new String JavaDoc[1];
85
86         if (inReplaceWith == null) {
87             inReplaceWith = new String JavaDoc ();
88         }
89
90         rw[0] = inReplaceWith;
91
92         construct (inFind, rw);
93     }
94
95     /**
96      * Get the string to search for.
97      *
98      * @return
99      * The search for string.
100      *
101      */

102     public String JavaDoc getFind () {
103         return find;
104     }
105
106     /**
107      * Get replacement text that can be a one or more stings. More
108      * than one string indicates that the token should be replaced
109      * with multiple lines.
110      *
111      * @return
112      * An array of replacement strings.
113      */

114     public String JavaDoc[] getReplaceWith () {
115         return replaceWith;
116     }
117
118     /**
119      * Set the replaceWith to an array.
120      *
121      * @param with
122      * One or more strings to use as a replacement for the find string. If
123      * the array contains more than one string, a new line is inserted for
124      * each element of the replaceWith array.
125      */

126     public void setReplaceWith (String JavaDoc[] with) {
127         replaceWith = with;
128     }
129
130     /**
131      * Set the replaceWith to a single string value.
132      *
133      * @param with
134      * String to use as a replacement for the find string.
135      */

136     public void setReplaceWith (String JavaDoc with) {
137         String JavaDoc[] array = new String JavaDoc[1];
138
139         array[0] = with;
140         replaceWith = array;
141     }
142
143     //
144
//
145

146     private void construct (String JavaDoc inFind,
147                             String JavaDoc[] inReplaceWith) throws ToolException {
148         if (inFind == null || inFind.trim ().length () == 0) {
149             throw new ToolException (res.getString ("Unable_to_create"));
150         }
151
152         if (inReplaceWith == null || inReplaceWith.length == 0) {
153             replaceWith = new String JavaDoc[1];
154             replaceWith[0] = new String JavaDoc ();
155         }
156
157         find = inFind;
158         replaceWith = inReplaceWith;
159     }
160
161 }
162
163
Popular Tags