KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.invicta.handler;
2
3 import java.util.Map JavaDoc;
4
5 import net.sf.invicta.InvictaConstants;
6 import net.sf.invicta.InvictaException;
7 import net.sf.invicta.api.InvictaComponent;
8 import net.sf.invicta.api.Product;
9
10
11 /**
12  *
13  * Map: All optional
14  * component
15  * type
16  * name
17  * defaultFile
18  * onlyBaseName
19  * includeSuffix
20  */

21 public class ProductHandler extends InvictaBasicHandler {
22     private final static boolean DEFAULT_ONLY_BASE_NAME = false;
23     private final static boolean DEFAULT_INCLUDE_SUFFIX = false;
24
25     /**
26      *
27      */

28     public String JavaDoc getName() {
29         return "product";
30     }
31
32     /**
33      *
34      */

35     public String JavaDoc handle(Map JavaDoc paramsMap) throws InvictaException {
36                                             
37         String JavaDoc componentReference = getParameter("component");
38         String JavaDoc type = getParameter("type");
39         String JavaDoc name = getParameter("name");
40         String JavaDoc defaultProductFile = getParameter("defaultFile");
41         boolean onlyBaseName = getBooleanParameter("onlyBaseName", DEFAULT_ONLY_BASE_NAME);
42         boolean includeSuffix = getBooleanParameter("includeSuffix", DEFAULT_INCLUDE_SUFFIX);
43                     
44         InvictaComponent component;
45         if (componentReference == null) {
46             component = getComponent();
47         } else {
48             String JavaDoc componentName = getComponent().getPropertyValue(componentReference);
49             component = getProject().getComponent(componentName);
50         }
51         
52         Product product = component.getSelfProduct(type, name);
53             
54         if ((product == null) && (defaultProductFile != null))
55             return defaultProductFile;
56             
57         if (product == null)
58             throw new InvictaHandlerException(this, "No product defined for component '" +
59                 component.getName() +
60                 "' with type=" + type + ", name=" + name);
61         
62         if (onlyBaseName)
63             return getBaseName(product.getFile(), includeSuffix);
64         else
65             return product.getPath();
66     }
67
68
69     /**
70      *
71      */

72     protected String JavaDoc getBaseName(String JavaDoc path, boolean includeSuffix) {
73         String JavaDoc base = path;
74         int index = base.lastIndexOf(InvictaConstants.FILE_SEPARATOR);
75         if (index != -1)
76             base = base.substring(index + 1);
77     
78         if (!includeSuffix) {
79             index = base.lastIndexOf(".");
80         
81             if (index != -1)
82                 base = base.substring(0, index);
83         }
84         return base;
85     }
86
87 }
88
Popular Tags