KickJava   Java API By Example, From Geeks To Geeks.

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


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 // standard imports
26
import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 /**
31  * ReplacementSet is a utility class for working with Replacement objects.
32  */

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

37     //
38
private Replacement[] replacements = new Replacement[0];
39
40     /**
41      * Create an empty replacement set.
42      */

43     public ReplacementSet() {}
44
45     public ReplacementSet(Replacement[] reps) {
46         replacements = reps;
47     }
48
49     /**
50      * Get the generator replacement that has the specified replacement name.
51      *
52      * @returns
53      * A Generatorreplacement that has the specified name.
54      *
55      * @throws ToolException
56      * Thrown if the no Replacement exists in the current set with
57      * the specified name.
58      *
59      */

60     public Replacement lookup(String JavaDoc find) throws ToolException {
61         Replacement replacement = null;
62
63         for (int i = 0; i < replacements.length; i++) {
64             if (replacements[i].getFind().equalsIgnoreCase(find)) {
65                 replacement = replacements[i];
66                 break;
67             }
68         }
69         if (replacement == null) {
70             throw new ToolException(ResUtil.format(res.getString("Unable_to_lookup"),
71                                                    find));
72         }
73         return replacement;
74     }
75
76     /**
77      * Remove the generator replacement that has the specified replacement name.
78      *
79      * @throws GeneratorException
80      * Thrown if the no Replacement exists in the current set with
81      * the specified find key.
82      *
83      */

84     public void delete(String JavaDoc find) throws ToolException {}
85
86     /**
87      * Add a new generator replacement.
88      *
89      * @throws GeneratorException
90      * Thrown if the no Generatorreplacement exists in the current set with
91      * the specified name.
92      *
93      */

94     public void add(Replacement newRep) throws ToolException {
95         ArrayList JavaDoc list = null;
96
97         list = new ArrayList JavaDoc(Arrays.asList(replacements));
98         list.add(newRep);
99         list.trimToSize();
100         replacements = new Replacement[list.size()];
101         replacements = (Replacement[]) list.toArray(replacements);
102     }
103
104     /**
105      * Convert the option set into an array of individual
106      * GeneratorOption objects.
107      *
108      * @return
109      * The options as an GeneratorOption array.
110      */

111     public Replacement[] toArray() {
112         return replacements;
113     }
114
115     public String JavaDoc[][] toStringArray() {
116         String JavaDoc[][] strings = new String JavaDoc[replacements.length][2];
117
118         for (int i = 0; i < replacements.length; i++) {
119             strings[i][0] = replacements[i].getFind();
120             strings[i][1] = replacements[i].getReplaceWith()[0];
121         }
122         return strings;
123     }
124
125 }
126
Popular Tags