KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > tools > ant > PluginInfoTask


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2005 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.tools.ant;
20
21 import java.io.File JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.java.plugin.ObjectFactory;
28 import org.java.plugin.registry.ManifestProcessingException;
29 import org.java.plugin.registry.Version;
30 import org.java.plugin.registry.PluginRegistry.ManifestInfo;
31 import org.java.plugin.util.IoUtil;
32
33 /**
34  * Simple task to read some data from plug-in manifest into project properties.
35  * <p>
36  * Inspired by Sebastian Kopsan.
37  *
38  * @version $Id: PluginInfoTask.java,v 1.3 2006/04/09 17:21:41 ddimon Exp $
39  */

40 public class PluginInfoTask extends Task {
41     private File JavaDoc manifest;
42     private String JavaDoc propertyId;
43     private String JavaDoc propertyVersion;
44     private String JavaDoc propertyVendor;
45     private String JavaDoc propertyPluginId;
46     private String JavaDoc propertyPluginVersion;
47     private String JavaDoc propertyMatchingRule;
48
49     /**
50      * @param aManifest plug-in manifest to read data from
51      */

52     public void setManifest(final File JavaDoc aManifest) {
53         manifest = aManifest;
54     }
55     
56     /**
57      * @param propertyName name of the property to read plug-in or plug-in
58      * fragment ID in
59      *
60      * @see org.java.plugin.registry.PluginRegistry.ManifestInfo#getId()
61      */

62     public void setPropertyId(String JavaDoc propertyName) {
63         propertyId = propertyName;
64     }
65     
66     /**
67      * @param propertyName name of the property to read plug-in or plug-in
68      * fragment version in
69      *
70      * @see org.java.plugin.registry.PluginRegistry.ManifestInfo#getVersion()
71      */

72     public void setPropertyVersion(String JavaDoc propertyName) {
73         propertyVersion = propertyName;
74     }
75     
76     /**
77      * @param propertyName name of the property to read plug-in or plug-in
78      * fragment vendor in
79      *
80      * @see org.java.plugin.registry.PluginRegistry.ManifestInfo#getVendor()
81      */

82     public void setPropertyVendor(String JavaDoc propertyName) {
83         propertyVendor = propertyName;
84     }
85     
86     /**
87      * @param propertyName name of the property to read plug-in ID in
88      *
89      * @see org.java.plugin.registry.PluginRegistry.ManifestInfo#getPluginId()
90      */

91     public void setPropertyPluginId(String JavaDoc propertyName) {
92         propertyPluginId = propertyName;
93     }
94     
95     /**
96      * @param propertyName name of the property to read plug-in version in
97      *
98      * @see org.java.plugin.registry.PluginRegistry.ManifestInfo#getPluginVersion()
99      */

100     public void setPropertyPluginVersion(String JavaDoc propertyName) {
101         propertyPluginVersion = propertyName;
102     }
103     
104     /**
105      * @param propertyName name of the property to read plug-in fragment
106      * matching rule in
107      *
108      * @see org.java.plugin.registry.PluginRegistry.ManifestInfo#getMatchingRule()
109      */

110     public void setPropertyMatchingRule(String JavaDoc propertyName) {
111         this.propertyMatchingRule = propertyName;
112     }
113     
114     /**
115      * @see org.apache.tools.ant.Task#execute()
116      */

117     public void execute() throws BuildException {
118         if (manifest == null) {
119             throw new BuildException("manifest attribute must be set!", //$NON-NLS-1$
120
getLocation());
121         }
122         URL JavaDoc url;
123         try {
124             url = IoUtil.file2url(manifest);
125         } catch (MalformedURLException JavaDoc mue) {
126             throw new BuildException("failed converting file " + manifest //$NON-NLS-1$
127
+ " to URL", mue, getLocation()); //$NON-NLS-1$
128
}
129         ManifestInfo manifestInfo;
130         try {
131             manifestInfo = ObjectFactory.newInstance().createRegistry()
132                 .readManifestInfo(url);
133             log("Data read from manifest " + manifest); //$NON-NLS-1$
134
} catch (ManifestProcessingException mpe) {
135             throw new BuildException("failed reading data from manifest " //$NON-NLS-1$
136
+ url, mpe, getLocation());
137         }
138         if (propertyId != null) {
139             getProject().setProperty(propertyId, manifestInfo.getId());
140         }
141         if (propertyVersion != null) {
142             Version version = manifestInfo.getVersion();
143             getProject().setProperty(propertyVersion,
144                     (version != null) ? version.toString() : ""); //$NON-NLS-1$
145
}
146         if (propertyVendor != null) {
147             String JavaDoc value = manifestInfo.getVendor();
148             getProject().setProperty(propertyVendor,
149                     (value != null) ? value : ""); //$NON-NLS-1$
150
}
151         if (propertyPluginId != null) {
152             String JavaDoc value = manifestInfo.getPluginId();
153             getProject().setProperty(propertyPluginId,
154                     (value != null) ? value : ""); //$NON-NLS-1$
155
}
156         if (propertyPluginVersion != null) {
157             Version version = manifestInfo.getPluginVersion();
158             getProject().setProperty(propertyPluginVersion,
159                     (version != null) ? version.toString() : ""); //$NON-NLS-1$
160
}
161         if (propertyMatchingRule != null) {
162             String JavaDoc value = manifestInfo.getMatchingRule();
163             getProject().setProperty(propertyMatchingRule,
164                     (value != null) ? value : ""); //$NON-NLS-1$
165
}
166     }
167 }
168
Popular Tags