KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > FeatureWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build;
12
13 import java.io.IOException JavaDoc;
14 import java.io.OutputStream JavaDoc;
15 import java.util.LinkedHashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.osgi.service.resolver.BundleDescription;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.pde.internal.build.builder.FeatureBuildScriptGenerator;
21 import org.eclipse.update.core.*;
22 import org.eclipse.update.core.model.URLEntryModel;
23
24 public class FeatureWriter extends XMLWriter implements IPDEBuildConstants {
25     protected Feature feature;
26     protected FeatureBuildScriptGenerator generator;
27     private Map JavaDoc parameters = new LinkedHashMap JavaDoc(10);
28
29     public FeatureWriter(OutputStream JavaDoc out, Feature feature, FeatureBuildScriptGenerator generator) throws IOException JavaDoc {
30         super(out);
31         this.feature = feature;
32         this.generator = generator;
33     }
34
35     public void printFeature() throws CoreException {
36         printFeatureDeclaration();
37         printInstallHandler();
38         printDescription();
39         printCopyright();
40         printLicense();
41         printURL();
42         printIncludes();
43         printRequires();
44         printPlugins();
45         printData();
46         endTag("feature"); //$NON-NLS-1$
47
super.close();
48     }
49
50     public void printFeatureDeclaration() {
51         parameters.clear();
52         parameters.put("id", feature.getFeatureIdentifier()); //$NON-NLS-1$
53
parameters.put("version", feature.getVersionedIdentifier().getVersion().toString()); //$NON-NLS-1$
54
parameters.put("label", feature.getLabelNonLocalized()); //$NON-NLS-1$
55
parameters.put("provider-name", feature.getProviderNonLocalized()); //$NON-NLS-1$
56
parameters.put("image", feature.getImageURLString()); //$NON-NLS-1$
57
parameters.put("os", feature.getOS()); //$NON-NLS-1$
58
parameters.put("arch", feature.getOSArch()); //$NON-NLS-1$
59
parameters.put("ws", feature.getWS()); //$NON-NLS-1$
60
parameters.put("nl", feature.getNL()); //$NON-NLS-1$
61
parameters.put("colocation-affinity", feature.getAffinityFeature()); //$NON-NLS-1$
62
parameters.put("primary", new Boolean JavaDoc(feature.isPrimary())); //$NON-NLS-1$
63
parameters.put("application", feature.getApplication()); //$NON-NLS-1$
64

65         startTag("feature", parameters, true); //$NON-NLS-1$
66
}
67
68     public void printInstallHandler() {
69         if (feature.getInstallHandlerEntry() == null)
70             return;
71         parameters.clear();
72         parameters.put("library", feature.getInstallHandlerModel().getLibrary()); //$NON-NLS-1$
73
parameters.put("handler", feature.getInstallHandlerModel().getHandlerName()); //$NON-NLS-1$
74
startTag("install-handler", parameters); //$NON-NLS-1$
75
endTag("install-handler"); //$NON-NLS-1$
76
}
77
78     public void printDescription() {
79         if (feature.getDescriptionModel() == null)
80             return;
81         parameters.clear();
82         parameters.put("url", feature.getDescriptionModel().getURLString()); //$NON-NLS-1$
83

84         startTag("description", parameters, true); //$NON-NLS-1$
85
printTabulation();
86         printlnEscaped(feature.getDescriptionModel().getAnnotationNonLocalized());
87         endTag("description"); //$NON-NLS-1$
88
}
89
90     private void printCopyright() {
91         if (feature.getCopyrightModel() == null)
92             return;
93         parameters.clear();
94         parameters.put("url", feature.getCopyrightModel().getURLString()); //$NON-NLS-1$
95
startTag("copyright", parameters, true); //$NON-NLS-1$
96
printTabulation();
97         printlnEscaped(feature.getCopyrightModel().getAnnotationNonLocalized());
98         endTag("copyright"); //$NON-NLS-1$
99
}
100
101     public void printLicense() {
102         if (feature.getLicenseModel() == null)
103             return;
104         parameters.clear();
105         parameters.put("url", feature.getLicenseModel().getURLString()); //$NON-NLS-1$
106
startTag("license", parameters, true); //$NON-NLS-1$
107
printTabulation();
108         printlnEscaped(feature.getLicenseModel().getAnnotationNonLocalized());
109         endTag("license"); //$NON-NLS-1$
110
}
111
112     public void printURL() {
113         if (feature.getUpdateSiteEntryModel() != null || feature.getDiscoverySiteEntryModels().length != 0) {
114             parameters.clear();
115
116             startTag("url", null); //$NON-NLS-1$
117
if (feature.getUpdateSiteEntryModel() != null) {
118                 parameters.clear();
119                 parameters.put("url", feature.getUpdateSiteEntryModel().getURLString()); //$NON-NLS-1$
120
parameters.put("label", feature.getUpdateSiteEntryModel().getAnnotationNonLocalized()); //$NON-NLS-1$
121
printTag("update", parameters, true, true, true); //$NON-NLS-1$
122
}
123
124             URLEntryModel[] siteEntries = feature.getDiscoverySiteEntryModels();
125             for (int i = 0; i < siteEntries.length; i++) {
126                 parameters.clear();
127                 parameters.put("url", siteEntries[i].getURLString()); //$NON-NLS-1$
128
parameters.put("label", siteEntries[i].getAnnotationNonLocalized()); //$NON-NLS-1$
129
printTag("discovery", parameters, true, true, true); //$NON-NLS-1$
130
}
131             endTag("url"); //$NON-NLS-1$
132
}
133     }
134
135     public void printIncludes() throws CoreException {
136         IIncludedFeatureReference[] features = feature.getRawIncludedFeatureReferences();
137         for (int i = 0; i < features.length; i++) {
138             parameters.clear();
139             try {
140                 parameters.put("id", features[i].getVersionedIdentifier().getIdentifier()); //$NON-NLS-1$
141
IFeature tmpFeature = generator.getSite(false).findFeature(features[i].getVersionedIdentifier().getIdentifier(), null, true);
142                 parameters.put("version", tmpFeature.getVersionedIdentifier().getVersion().toString()); //$NON-NLS-1$
143
} catch (CoreException e) {
144                 String JavaDoc message = NLS.bind(Messages.exception_missingFeature, features[i].getVersionedIdentifier().getIdentifier());
145                 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_MISSING, message, null));
146             }
147
148             printTag("includes", parameters, true, true, true); //$NON-NLS-1$
149
}
150     }
151
152     private void printRequires() {
153         if (feature.getImportModels().length == 0)
154             return;
155         startTag("requires", null); //$NON-NLS-1$
156
printImports();
157         endTag("requires"); //$NON-NLS-1$
158
}
159
160     private void printImports() {
161         IImport[] imports = feature.getRawImports();
162         for (int i = 0; i < imports.length; i++) {
163             parameters.clear();
164             if (imports[i].getKind() == IImport.KIND_PLUGIN) {
165                 parameters.put("plugin", imports[i].getVersionedIdentifier().getIdentifier()); //$NON-NLS-1$
166
parameters.put("version", imports[i].getVersionedIdentifier().getVersion().toString()); //$NON-NLS-1$
167
} else {
168                 //The import refers to a feature
169
parameters.put("feature", imports[i].getVersionedIdentifier().getIdentifier()); //$NON-NLS-1$
170
parameters.put("version", imports[i].getVersionedIdentifier().getVersion().toString()); //$NON-NLS-1$
171
}
172             parameters.put("match", getStringForMatchingRule(imports[i].getRule())); //$NON-NLS-1$
173
printTag("import", parameters, true, true, true); //$NON-NLS-1$
174
}
175     }
176
177     /**
178      * Method getStringForMatchingRule.
179      * @param ruleNumber
180      */

181     private String JavaDoc getStringForMatchingRule(int ruleNumber) {
182         switch (ruleNumber) {
183             case 1 :
184                 return "perfect"; //$NON-NLS-1$
185
case 2 :
186                 return "equivalent"; //$NON-NLS-1$
187
case 3 :
188                 return "compatible"; //$NON-NLS-1$
189
case 4 :
190                 return "greaterOrEqual"; //$NON-NLS-1$
191
case 0 :
192             default :
193                 return ""; //$NON-NLS-1$
194
}
195     }
196
197     public void printPlugins() throws CoreException {
198         IPluginEntry[] plugins = feature.getRawPluginEntries();
199         for (int i = 0; i < plugins.length; i++) {
200             parameters.clear();
201             parameters.put("id", plugins[i].getVersionedIdentifier().getIdentifier()); //$NON-NLS-1$
202

203             String JavaDoc versionRequested = plugins[i].getVersionedIdentifier().getVersion().toString();
204             BundleDescription effectivePlugin = null;
205             try {
206                 effectivePlugin = generator.getSite(false).getRegistry().getResolvedBundle(plugins[i].getVersionedIdentifier().getIdentifier(), versionRequested);
207             } catch (CoreException e) {
208                 String JavaDoc message = NLS.bind(Messages.exception_missingPlugin, plugins[i].getVersionedIdentifier());
209                 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PLUGIN_MISSING, message, null));
210             }
211             if (effectivePlugin == null) {
212                 String JavaDoc message = NLS.bind(Messages.exception_missingPlugin, plugins[i].getVersionedIdentifier());
213                 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PLUGIN_MISSING, message, null));
214             }
215             parameters.put("version", effectivePlugin.getVersion()); //$NON-NLS-1$
216
parameters.put("fragment", new Boolean JavaDoc(plugins[i].isFragment())); //$NON-NLS-1$
217
parameters.put("os", plugins[i].getOS()); //$NON-NLS-1$
218
parameters.put("arch", plugins[i].getOSArch()); //$NON-NLS-1$
219
parameters.put("ws", plugins[i].getWS()); //$NON-NLS-1$
220
parameters.put("nl", plugins[i].getNL()); //$NON-NLS-1$
221
if (plugins[i] instanceof PluginEntry && !((PluginEntry)plugins[i]).isUnpack())
222                 parameters.put("unpack", Boolean.FALSE.toString()); //$NON-NLS-1$
223
parameters.put("download-size", new Long JavaDoc(plugins[i].getDownloadSize() != -1 ? plugins[i].getDownloadSize() : 0)); //$NON-NLS-1$
224
parameters.put("install-size", new Long JavaDoc(plugins[i].getInstallSize() != -1 ? plugins[i].getInstallSize() : 0)); //$NON-NLS-1$
225
printTag("plugin", parameters, true, true, true); //$NON-NLS-1$
226
}
227     }
228
229     private void printData() {
230         INonPluginEntry[] entries = feature.getNonPluginEntries();
231         for (int i = 0; i < entries.length; i++) {
232             parameters.put("id", entries[i].getIdentifier()); //$NON-NLS-1$
233
parameters.put("os", entries[i].getOS()); //$NON-NLS-1$
234
parameters.put("arch", entries[i].getOSArch()); //$NON-NLS-1$
235
parameters.put("ws", entries[i].getWS()); //$NON-NLS-1$
236
parameters.put("nl", entries[i].getNL()); //$NON-NLS-1$
237
parameters.put("download-size", new Long JavaDoc(entries[i].getDownloadSize() != -1 ? entries[i].getDownloadSize() : 0)); //$NON-NLS-1$
238
parameters.put("install-size", new Long JavaDoc(entries[i].getInstallSize() != -1 ? entries[i].getInstallSize() : 0)); //$NON-NLS-1$
239
printTag("data", parameters, true, true, true); //$NON-NLS-1$
240
}
241     }
242 }
243
Popular Tags