KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > type > TargetTemplate


1 package net.sf.invicta.type;
2
3 import java.util.Iterator JavaDoc;
4
5 import net.sf.invicta.InvictaConstants;
6 import net.sf.invicta.api.Target;
7
8 /**
9  *
10  */

11 public class TargetTemplate extends TargetImpl {
12     
13     public final static String JavaDoc MODE_REPLACE = "replace";
14     public final static String JavaDoc MODE_RENAME = "rename";
15     public final static String JavaDoc MODE_ADD_BEFORE = "addBefore";
16     public final static String JavaDoc MODE_ADD_AFTER = "addAfter";
17     public final static String JavaDoc MODE_ADD_BEFORE_ALL = "addBeforeAll";
18     public final static String JavaDoc MODE_ADD_AFTER_ALL = "addAfterAll";
19     
20             
21     protected String JavaDoc mode;
22     protected String JavaDoc templateString;
23     
24     /**
25      * Constructor for TargetTemplate.
26      */

27     public TargetTemplate() {
28         super();
29     }
30
31     /**
32      *
33      */

34     public TargetTemplate(TargetTemplate other) {
35         super((Target)other);
36         this.mode = other.getMode();
37         this.templateString = other.getTemplateString();
38     }
39
40     /**
41      * Returns the mode.
42      * @return String
43      */

44     public String JavaDoc getMode() {
45         return mode;
46     }
47
48     
49     /**
50      * Sets the mode.
51      * @param mode The mode to set
52      */

53     public void setMode(String JavaDoc mode) {
54         this.mode = mode;
55     }
56         
57     /**
58      * Returns the templateString.
59      * @return String
60      */

61     public String JavaDoc getTemplateString() {
62         return templateString;
63     }
64
65     /**
66      * Sets the templateString.
67      * @param templateString The templateString to set
68      */

69     public void setTemplateString(String JavaDoc templateString) {
70         this.templateString = templateString;
71     }
72
73     /**
74      *
75      */

76     public boolean equals(TargetTemplate other) {
77         return equals(other, false);
78     }
79
80     /**
81      *
82      */

83     public boolean equals(TargetTemplate other, boolean compareDepends) {
84         
85         if (compareDepends) {
86     
87             // Check that the list lengths of depends match
88
if (this.dependsList.size() != other.getDependsList().size())
89                 return false;
90         
91             // Check that the depend list is the same.
92
Iterator JavaDoc otherIter = other.getDependsList().iterator();
93             for (Iterator JavaDoc thisIter = this.dependsList.iterator(); thisIter.hasNext();) {
94                 String JavaDoc thisDepend = (String JavaDoc) thisIter.next();
95                 String JavaDoc otherDepend = (String JavaDoc) otherIter.next();
96                 if (!thisDepend.equals(otherDepend))
97                     return false;
98             }
99         }
100         
101         // Compare all other string members
102
return equals(this.name, other.getName()) &&
103                 equals(this.mode, other.getMode()) &&
104                 equals(this.templateString, other.getTemplateString()) &&
105                 equals(this.ifString, other.getIf()) &&
106                 equals(this.unlessString, other.getUnless());
107     }
108
109     
110     /**
111      *
112      */

113     public String JavaDoc getAntTemplateString() {
114         String JavaDoc targetTemplateString;
115         String JavaDoc targetName = InvictaConstants.COMPONENT_NAME_TEMPLATE +
116                             InvictaConstants.TARGET_DELIMITER +
117                            this.name;
118
119         // TODO: Make the length of '#' be automatic.
120
targetTemplateString =
121             "<!--================================================ \n" +
122             "= Target: " + targetName + " \n" +
123             "================================================ --> \n";
124                             
125         // Create the beginning of the ANT target
126
targetTemplateString += "<target name=\"" + targetName + "\" ";
127         
128         // Add optional fields to the target definition
129
if (this.ifString != null)
130             targetTemplateString += "if=\"" + this.ifString + "\" ";
131         if (this.unlessString != null)
132             targetTemplateString += "unless=\"" + this.unlessString + "\" ";
133         if (this.description != null)
134             targetTemplateString += "description=\"" + this.description + "\" ";
135         
136         // Add the depends list to the target definition
137
if (this.dependsList.size() > 0)
138             targetTemplateString += "depends=\"" + getAntTargetDependString() + "\" ";
139         
140         targetTemplateString += ">\n";
141         
142         // Add the ANT template itself.
143
targetTemplateString += this.templateString;
144                         
145         // Add the closing tag of the target
146
targetTemplateString += "\n</target> \n \n ";
147         
148         return targetTemplateString;
149     }
150     
151     /**
152      *
153      */

154     protected String JavaDoc getAntTargetDependString() {
155         String JavaDoc dependString = "";
156         // Go over the list of depend targets.
157
for (Iterator JavaDoc dependIter = this.dependsList.iterator(); dependIter.hasNext();) {
158             String JavaDoc oneDepend = (String JavaDoc) dependIter.next();
159             
160             // If the depend is 'absolute', then get its target as is
161
if (isAbsoluteDepend(oneDepend))
162                 dependString += oneDepend;
163                 
164             // Otherwise, add its target with the component name template suffix
165
else
166                 dependString += InvictaConstants.COMPONENT_NAME_TEMPLATE +
167                                 InvictaConstants.TARGET_DELIMITER +
168                                 oneDepend;
169             
170             // Add a comma if there are more depends on the list.
171
if (dependIter.hasNext())
172                 dependString += InvictaConstants.DEPEND_LIST_DELIMITER;
173         }
174         
175         return dependString;
176     }
177     
178     /**
179      *
180      */

181     protected boolean isAbsoluteDepend(String JavaDoc dependStr) {
182         return dependStr.startsWith(InvictaConstants.TARGET_DEPENDENCY_SPECIAL_PREFIX);
183     }
184
185     /**
186      *
187      */

188     protected boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
189         if ((o1 == null) && (o2 == null))
190             return true;
191         if ((o1 == null) || (o2 == null))
192             return false;
193         return o1.equals(o2);
194     }
195
196 }
197
Popular Tags