KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > handler > DependFilesetHandler


1 package net.sf.invicta.handler;
2
3 import java.io.File JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import net.sf.invicta.InvictaException;
9 import net.sf.invicta.api.InvictaComponent;
10 import net.sf.invicta.api.Product;
11
12 /**
13  * Map:
14  * element - (String, Required) The name of the ANT element. Examples 'fileset', 'lib'
15  * attribute - (String, Required) The name of the attribute for the value. Examples: 'dir', 'includes'.
16  * locationProperty - (String, optional, default=none) A property of the depend component that should be copied.
17  * If the property is not given, then get the exported Jars of all components.
18  * includeProperty (String, Optional, default=none)
19  * excludeType - (String, optional, default=none)
20  * ignoreProperty - (String, optional, default=none) A property that specifies whether to return an empty result (if true).
21  *
22  */

23 public class DependFilesetHandler extends InvictaBasicHandler {
24     protected final static String JavaDoc DIR_ATTR = "dir";
25     protected final static boolean DEFAULT_IGNORE = false;
26     protected String JavaDoc element;
27     protected String JavaDoc attribute;
28     protected String JavaDoc locationPropertyName;
29     protected String JavaDoc includePropertyName;
30     protected String JavaDoc excludeType;
31
32     /**
33      *
34      */

35     public String JavaDoc handle(Map JavaDoc paramsMap) throws InvictaException {
36         this.element = getRequiredParameter("element");
37         this.attribute = getRequiredParameter("attribute");
38         
39         this.locationPropertyName = getParameter("locationProperty");
40         this.includePropertyName = getParameter("includeProperty");
41         this.excludeType = getParameter("excludeType");
42         
43         if (getParameterPropertyBooleanValue("ignoreProperty", DEFAULT_IGNORE)) {
44             return "";
45         }
46         return prepareDependFilesets();
47     }
48
49     /**
50      *
51      */

52     private String JavaDoc prepareDependFilesets() {
53         
54         String JavaDoc filesetsStr = "";
55         if (this.locationPropertyName != null) {
56         
57             // Go over all depend components
58
for (Iterator JavaDoc iter = getComponent().getDependComponents().iterator(); iter.hasNext();) {
59                 InvictaComponent dependComponent = (InvictaComponent) iter.next();
60             
61                 // Get the required property
62
String JavaDoc propertyRefValue = dependComponent.getPropertyReferenceValue(locationPropertyName);
63                 
64                 // Take the value of the include property.
65
String JavaDoc includeRefValue = null;
66                 if (includePropertyName != null) {
67                     includeRefValue = dependComponent.getPropertyReferenceValue(includePropertyName);
68                     // If this property is not defined, this dependent component should not be added to fileset
69
if (includeRefValue == null)
70                         continue;
71                 }
72                                 
73                 // If the depend component has this property
74
if (propertyRefValue != null)
75                     filesetsStr += prepareFileset(propertyRefValue, includeRefValue);
76             }
77         } else {
78             filesetsStr += prepareProductsFilesets();
79         }
80         return filesetsStr;
81     }
82     
83     /**
84      *
85      */

86     private String JavaDoc prepareProductsFilesets() {
87         String JavaDoc filesetsStr = "";
88         Map JavaDoc productKeys = new HashMap JavaDoc();
89         
90         // Go over all products.
91
for (Iterator JavaDoc iter = getComponent().getRecursiveProducts().iterator(); iter.hasNext();) {
92             Product product = (Product) iter.next();
93                                                                 
94             if ((product.getFile() != null) &&
95                 !((this.excludeType != null) && (product.getType() != null) && (product.getType().equals(this.excludeType))) &&
96                 (!productKeys.containsKey(product.getKey()))) {
97                     
98                 filesetsStr += prepareFileset(product.getPath(), null);
99                 productKeys.put(product.getKey(), new Object JavaDoc());
100             }
101         }
102         return filesetsStr;
103     }
104     
105     /**
106      *
107      */

108     private String JavaDoc prepareFileset(String JavaDoc attrValue, String JavaDoc includeValue) {
109         String JavaDoc fileset = "<" + this.element + " ";
110         
111         String JavaDoc fileName = attrValue;
112         // Add 'dir' attribute to the project home if attrName is not dir.
113
if (!this.attribute.equals(DIR_ATTR)) {
114             File JavaDoc file = new File JavaDoc(attrValue);
115             String JavaDoc dir = file.getParent();
116             fileName = file.getName();
117             if (dir == null)
118                 dir = getProject().getProjectPropertyValue("home");
119             fileset += DIR_ATTR + "=\"" + dir + "\" ";
120         }
121         
122         fileset += this.attribute + "=\"" + fileName + "\">\n";
123         
124         if (includeValue != null) {
125             fileset += "<include name=\"" + includeValue + "\"/>\n";
126         }
127         
128         fileset += "</" + this.element + ">\n";
129         
130         return fileset;
131     }
132     
133     /**
134      *
135      */

136     public String JavaDoc getName() {
137         return "dependFileset";
138     }
139
140 }
141
Popular Tags