KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > dumper > ExportedProjectDefinitionDumper


1 package net.sf.invicta.dumper;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import net.sf.invicta.InvictaConstants;
7 import net.sf.invicta.InvictaException;
8 import net.sf.invicta.api.InvictaComponent;
9 import net.sf.invicta.api.Product;
10 import net.sf.invicta.api.DependItem;
11 import net.sf.invicta.template.TemplateHelper;
12 import org.apache.commons.lang.StringUtils;
13
14
15 /**
16  * Creates an export project definition file of a project according to
17  * its definition files.
18  *
19  */

20 public class ExportedProjectDefinitionDumper extends InvictaBasicDumper {
21     protected static final String JavaDoc DEFAULT_EXPORT = "false";
22     protected static final String JavaDoc DIST_PRODUCT_DIR_PROPERTY =
23         InvictaConstants.DIST_PRODUCT_DIR_PROPERTY;
24         
25     protected static final String JavaDoc PROJECT_DEF_HEADER_TEMPLATE =
26         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
27         "<!DOCTYPE invictaProject PUBLIC \"Invicta Project Definition 1.0\" \"InvictaProject.dtd\">\n" +
28         "<invictaProject>\n" +
29         "\t<component name=\"%{projectName}\" type=\"" +
30         InvictaConstants.DEFAULT_LIBRARIES_TYPE +"\" dir=\"${project.%{projectName}.home}\">\n";
31
32     private static final String JavaDoc PROJECT_DEF_FOOTER_TEMPLATE =
33         "\n\t</component>\n" +
34         "\n</invictaProject>\n";
35         
36
37     /**
38      * Constructor for ExportedProjectDefinitionDumper.
39      */

40     public ExportedProjectDefinitionDumper() {
41         super();
42     }
43
44     /**
45      *
46      */

47     public String JavaDoc getName() {
48         return "exportedProjectDefinition";
49     }
50
51     /**
52      * Returns the exported project definition file content.
53      */

54     public String JavaDoc getDumpContent() throws InvictaException {
55         
56         StringBuffer JavaDoc dump = new StringBuffer JavaDoc();
57     
58         TemplateHelper.format(PROJECT_DEF_HEADER_TEMPLATE, getProject(), dump);
59         
60         // Go over all components and create an exported products
61
// definition for each one.
62
for (Iterator JavaDoc iter = getProject().getComponents().iterator(); iter.hasNext();) {
63             InvictaComponent component = (InvictaComponent) iter.next();
64                                             
65             String JavaDoc name = getName(component.getName());
66             String JavaDoc dependProducts = getDependProducts(component);
67             boolean foundPrimary = false;
68             String JavaDoc exportedSelfProducts = "";
69             // Go over all self products of the component.
70
for (Iterator JavaDoc prodIter = component.getSelfProducts().iterator(); prodIter.hasNext();) {
71                 Product product = (Product) prodIter.next();
72                 String JavaDoc productFile = canonizeFile(product.getFile(), component.getDir());
73                 if (product.getName() == null)
74                     foundPrimary = true;
75                 
76                 if (product.getExport()) {
77                     if (exportedSelfProducts.length() != 0)
78                         exportedSelfProducts += ",";
79                     exportedSelfProducts += getName(product.getFullName());
80                 }
81                 
82                 String JavaDoc actualDependProduct;
83                 if (product.getDependNames().size() > 0)
84                     actualDependProduct = getProductDependProducts(component.getName(), product);
85                 else
86                     actualDependProduct = dependProducts;
87
88                 dump.append(
89                     getProductDefinition(product, productFile, actualDependProduct));
90             }
91             
92             // If no primary product was found, but there are non-primary products,
93
// then add a primary product that points to them.
94
if (!foundPrimary &&
95                 (exportedSelfProducts.length() > 0)) {
96                 dump.append(
97                     getDefaultProductDefinition(component.getName(), exportedSelfProducts));
98                     
99             } else if (component.getSelfProducts().size() == 0) {
100                 dump.append(
101                     getDefaultProductDefinition(component.getName(), dependProducts));
102             }
103         }
104                                                                         
105         dump.append(PROJECT_DEF_FOOTER_TEMPLATE);
106         
107         return dump.toString();
108     }
109
110     /**
111      * Returns a string of all products a component depends on.
112      *
113      * @param component
114      * @return String
115      */

116     protected String JavaDoc getDependProducts(InvictaComponent component) {
117         String JavaDoc dependProducts = "";
118         List JavaDoc dependItems = component.getDependItems();
119         // Go over all depend items of the component
120
for (Iterator JavaDoc iter = dependItems.iterator(); iter.hasNext();) {
121             DependItem dependItem = (DependItem) iter.next();
122                     
123             // Get the dependent copmponent.
124
InvictaComponent dependComponent = getProject().getComponent(dependItem.getName());
125                     
126             // If the dependent item specified products, then add them to
127
// the depend list
128
if (dependItem.getProductsList().size() > 0) {
129                 List JavaDoc productList = dependItem.getProductsList();
130                 for (Iterator JavaDoc iterator = dependItem.getProductsList().iterator();iterator.hasNext();) {
131                     String JavaDoc productName = (String JavaDoc) iterator.next();
132                     if (dependProducts.length() != 0)
133                         dependProducts += ",";
134                     dependProducts += getName(dependItem.getName()) + "." + productName;
135                 }
136             } else {
137                 
138                 // If not products were specified, add all exported
139
// products.
140
for (Iterator JavaDoc iterator = dependComponent.getExportedProducts().iterator();iterator.hasNext();) {
141                     Product product = (Product) iterator.next();
142                     if (dependProducts.length() != 0)
143                         dependProducts += ",";
144                     dependProducts += getName(product.getFullName());
145                 }
146             }
147         }
148         return dependProducts;
149     }
150
151     /**
152      * Returns all products that a given product depends on.
153      */

154     protected String JavaDoc getProductDependProducts(String JavaDoc componentName, Product product) {
155         String JavaDoc dependProducts = "";
156         // Go over all depend products of the given product.
157
for (Iterator JavaDoc iter = product.getDependNames().iterator(); iter.hasNext();) {
158             String JavaDoc producName = (String JavaDoc) iter.next();
159             
160             if (dependProducts.length() != 0)
161                 dependProducts += ",";
162             dependProducts += getName(componentName) + "." + producName;
163         }
164         return dependProducts;
165     }
166     
167     /**
168      * Canonize a given file's path by removing extra characters and
169      * adding relative project and component directories to the given
170      * path.
171      */

172     protected String JavaDoc canonizeFile(String JavaDoc file, String JavaDoc distDir){
173         if (file == null)
174             return null;
175                             
176         String JavaDoc doubleSeparator = InvictaConstants.FILE_SEPARATOR + InvictaConstants.FILE_SEPARATOR;
177         String JavaDoc specialDoubleSeparator = InvictaConstants.FILE_SEPARATOR + "." + InvictaConstants.FILE_SEPARATOR;
178         String JavaDoc path = InvictaConstants.FILE_SEPARATOR
179          + getProject().getProjectPropertyValue(DIST_PRODUCT_DIR_PROPERTY)
180          + InvictaConstants.FILE_SEPARATOR
181          + distDir + InvictaConstants.FILE_SEPARATOR + file;
182         path = StringUtils.replace(path, doubleSeparator, InvictaConstants.FILE_SEPARATOR);
183         path = StringUtils.replace(path, specialDoubleSeparator, InvictaConstants.FILE_SEPARATOR);
184         return path;
185     }
186     
187     
188     /**
189      * Returns the name of an exported product while removing the
190      * name of the project if exists.
191      *
192      * @param oldName
193      * @return String
194      */

195     protected String JavaDoc getName(String JavaDoc oldName) {
196         String JavaDoc projectPrefix = getProject().getName() + ".";
197         
198         String JavaDoc newName = oldName;
199         
200         // Remove the first word if a separator exists
201
int dotIndex = newName.indexOf(projectPrefix);
202         if (dotIndex != -1)
203             newName = newName.substring(dotIndex + projectPrefix.length());
204                     
205         return newName;
206     }
207     
208     /**
209      * Returns a string of one exported product definition.
210      *
211      */

212     protected String JavaDoc getProductDefinition(Product product, String JavaDoc productFile,
213                                           String JavaDoc dependProducts) {
214         String JavaDoc name = getName(product.getFullName());
215         // Create the initial product definiton.
216
String JavaDoc productDef = "\t\t<product name=\"" + name + "\"";
217         
218         productDef += " static=\"true\"";
219         productDef += " export=\"" + DEFAULT_EXPORT + "\" ";
220
221         // Add the file name if exists.
222
if (product.getFile() != null) {
223             productDef += "file=\"" + productFile + "\" ";
224         }
225     
226         if (product.getType() != null)
227             productDef += " type=\"" + product.getType() + "\"";
228         
229         if ((dependProducts != null) && (dependProducts.length() != 0))
230             productDef += "\n\t\t depends=\"" + dependProducts + "\"";
231         
232         if (product.getAppName() != null)
233             productDef += " appName=\"" + product.getAppName() + "\"";
234                     
235         productDef +="/>\n";
236                         
237         return productDef;
238     }
239
240     /**
241      *
242      */

243     protected String JavaDoc getDefaultProductDefinition(String JavaDoc name, String JavaDoc dependProducts) {
244         String JavaDoc productDef = "\t\t<product name=\"" + getName(name) + "\"";
245         
246         productDef += " static=\"true\"";
247         productDef += " export=\"" + DEFAULT_EXPORT + "\" ";
248             
249         if ((dependProducts != null) && (dependProducts.length() != 0))
250             productDef += "\n\t\t depends=\"" + dependProducts + "\"";
251                             
252         productDef +="/>\n";
253                         
254         return productDef;
255     }
256     
257 }
258
Popular Tags