001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.tools.ant;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023:
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.Task;
026: import org.java.plugin.ObjectFactory;
027: import org.java.plugin.registry.ManifestInfo;
028: import org.java.plugin.registry.ManifestProcessingException;
029: import org.java.plugin.registry.MatchingRule;
030: import org.java.plugin.registry.Version;
031: import org.java.plugin.util.IoUtil;
032:
033: /**
034: * Simple task to read some data from plug-in manifest into project properties.
035: * <p>
036: * Inspired by Sebastian Kopsan.
037: *
038: * @version $Id$
039: */
040: public class PluginInfoTask extends Task {
041: private File manifest;
042: private String propertyId;
043: private String propertyVersion;
044: private String propertyVendor;
045: private String propertyPluginId;
046: private String propertyPluginVersion;
047: private String propertyMatchingRule;
048:
049: /**
050: * @param aManifest plug-in manifest to read data from
051: */
052: public void setManifest(final File aManifest) {
053: manifest = aManifest;
054: }
055:
056: /**
057: * @param propertyName name of the property to read plug-in or plug-in
058: * fragment ID in
059: *
060: * @see org.java.plugin.registry.ManifestInfo#getId()
061: */
062: public void setPropertyId(String propertyName) {
063: propertyId = propertyName;
064: }
065:
066: /**
067: * @param propertyName name of the property to read plug-in or plug-in
068: * fragment version in
069: *
070: * @see org.java.plugin.registry.ManifestInfo#getVersion()
071: */
072: public void setPropertyVersion(String propertyName) {
073: propertyVersion = propertyName;
074: }
075:
076: /**
077: * @param propertyName name of the property to read plug-in or plug-in
078: * fragment vendor in
079: *
080: * @see org.java.plugin.registry.ManifestInfo#getVendor()
081: */
082: public void setPropertyVendor(String propertyName) {
083: propertyVendor = propertyName;
084: }
085:
086: /**
087: * @param propertyName name of the property to read plug-in ID in
088: *
089: * @see org.java.plugin.registry.ManifestInfo#getPluginId()
090: */
091: public void setPropertyPluginId(String propertyName) {
092: propertyPluginId = propertyName;
093: }
094:
095: /**
096: * @param propertyName name of the property to read plug-in version in
097: *
098: * @see org.java.plugin.registry.ManifestInfo#getPluginVersion()
099: */
100: public void setPropertyPluginVersion(String 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.ManifestInfo#getMatchingRule()
109: */
110: public void setPropertyMatchingRule(String propertyName) {
111: this .propertyMatchingRule = propertyName;
112: }
113:
114: /**
115: * @see org.apache.tools.ant.Task#execute()
116: */
117: @Override
118: public void execute() throws BuildException {
119: if (manifest == null) {
120: throw new BuildException("manifest attribute must be set!", //$NON-NLS-1$
121: getLocation());
122: }
123: URL url;
124: try {
125: url = IoUtil.file2url(manifest);
126: } catch (MalformedURLException mue) {
127: throw new BuildException(
128: "failed converting file " + manifest //$NON-NLS-1$
129: + " to URL", mue, getLocation()); //$NON-NLS-1$
130: }
131: ManifestInfo manifestInfo;
132: try {
133: manifestInfo = ObjectFactory.newInstance().createRegistry()
134: .readManifestInfo(url);
135: //log("Data read from manifest " + manifest); //$NON-NLS-1$
136: } catch (ManifestProcessingException mpe) {
137: throw new BuildException(
138: "failed reading data from manifest " //$NON-NLS-1$
139: + url, mpe, getLocation());
140: }
141: if (propertyId != null) {
142: getProject().setProperty(propertyId, manifestInfo.getId());
143: }
144: if (propertyVersion != null) {
145: Version version = manifestInfo.getVersion();
146: getProject().setProperty(propertyVersion,
147: (version != null) ? version.toString() : ""); //$NON-NLS-1$
148: }
149: if (propertyVendor != null) {
150: String value = manifestInfo.getVendor();
151: getProject().setProperty(propertyVendor,
152: (value != null) ? value : ""); //$NON-NLS-1$
153: }
154: if (propertyPluginId != null) {
155: String value = manifestInfo.getPluginId();
156: getProject().setProperty(propertyPluginId,
157: (value != null) ? value : ""); //$NON-NLS-1$
158: }
159: if (propertyPluginVersion != null) {
160: Version version = manifestInfo.getPluginVersion();
161: getProject().setProperty(propertyPluginVersion,
162: (version != null) ? version.toString() : ""); //$NON-NLS-1$
163: }
164: if (propertyMatchingRule != null) {
165: MatchingRule value = manifestInfo.getMatchingRule();
166: getProject().setProperty(propertyMatchingRule,
167: (value != null) ? value.toCode() : ""); //$NON-NLS-1$
168: }
169: }
170: }
|