001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM - Initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build;
011:
012: import java.io.IOException;
013: import java.io.OutputStream;
014: import java.util.LinkedHashMap;
015: import java.util.Map;
016: import org.eclipse.core.runtime.*;
017: import org.eclipse.osgi.service.resolver.BundleDescription;
018: import org.eclipse.osgi.util.NLS;
019: import org.eclipse.pde.internal.build.builder.FeatureBuildScriptGenerator;
020: import org.eclipse.update.core.*;
021: import org.eclipse.update.core.model.URLEntryModel;
022:
023: public class FeatureWriter extends XMLWriter implements
024: IPDEBuildConstants {
025: protected Feature feature;
026: protected FeatureBuildScriptGenerator generator;
027: private Map parameters = new LinkedHashMap(10);
028:
029: public FeatureWriter(OutputStream out, Feature feature,
030: FeatureBuildScriptGenerator generator) throws IOException {
031: super (out);
032: this .feature = feature;
033: this .generator = generator;
034: }
035:
036: public void printFeature() throws CoreException {
037: printFeatureDeclaration();
038: printInstallHandler();
039: printDescription();
040: printCopyright();
041: printLicense();
042: printURL();
043: printIncludes();
044: printRequires();
045: printPlugins();
046: printData();
047: endTag("feature"); //$NON-NLS-1$
048: super .close();
049: }
050:
051: public void printFeatureDeclaration() {
052: parameters.clear();
053: parameters.put("id", feature.getFeatureIdentifier()); //$NON-NLS-1$
054: parameters
055: .put(
056: "version", feature.getVersionedIdentifier().getVersion().toString()); //$NON-NLS-1$
057: parameters.put("label", feature.getLabelNonLocalized()); //$NON-NLS-1$
058: parameters.put(
059: "provider-name", feature.getProviderNonLocalized()); //$NON-NLS-1$
060: parameters.put("image", feature.getImageURLString()); //$NON-NLS-1$
061: parameters.put("os", feature.getOS()); //$NON-NLS-1$
062: parameters.put("arch", feature.getOSArch()); //$NON-NLS-1$
063: parameters.put("ws", feature.getWS()); //$NON-NLS-1$
064: parameters.put("nl", feature.getNL()); //$NON-NLS-1$
065: parameters.put(
066: "colocation-affinity", feature.getAffinityFeature()); //$NON-NLS-1$
067: parameters.put("primary", new Boolean(feature.isPrimary())); //$NON-NLS-1$
068: parameters.put("application", feature.getApplication()); //$NON-NLS-1$
069:
070: startTag("feature", parameters, true); //$NON-NLS-1$
071: }
072:
073: public void printInstallHandler() {
074: if (feature.getInstallHandlerEntry() == null)
075: return;
076: parameters.clear();
077: parameters
078: .put(
079: "library", feature.getInstallHandlerModel().getLibrary()); //$NON-NLS-1$
080: parameters
081: .put(
082: "handler", feature.getInstallHandlerModel().getHandlerName()); //$NON-NLS-1$
083: startTag("install-handler", parameters); //$NON-NLS-1$
084: endTag("install-handler"); //$NON-NLS-1$
085: }
086:
087: public void printDescription() {
088: if (feature.getDescriptionModel() == null)
089: return;
090: parameters.clear();
091: parameters.put(
092: "url", feature.getDescriptionModel().getURLString()); //$NON-NLS-1$
093:
094: startTag("description", parameters, true); //$NON-NLS-1$
095: printTabulation();
096: printlnEscaped(feature.getDescriptionModel()
097: .getAnnotationNonLocalized());
098: endTag("description"); //$NON-NLS-1$
099: }
100:
101: private void printCopyright() {
102: if (feature.getCopyrightModel() == null)
103: return;
104: parameters.clear();
105: parameters.put(
106: "url", feature.getCopyrightModel().getURLString()); //$NON-NLS-1$
107: startTag("copyright", parameters, true); //$NON-NLS-1$
108: printTabulation();
109: printlnEscaped(feature.getCopyrightModel()
110: .getAnnotationNonLocalized());
111: endTag("copyright"); //$NON-NLS-1$
112: }
113:
114: public void printLicense() {
115: if (feature.getLicenseModel() == null)
116: return;
117: parameters.clear();
118: parameters.put("url", feature.getLicenseModel().getURLString()); //$NON-NLS-1$
119: startTag("license", parameters, true); //$NON-NLS-1$
120: printTabulation();
121: printlnEscaped(feature.getLicenseModel()
122: .getAnnotationNonLocalized());
123: endTag("license"); //$NON-NLS-1$
124: }
125:
126: public void printURL() {
127: if (feature.getUpdateSiteEntryModel() != null
128: || feature.getDiscoverySiteEntryModels().length != 0) {
129: parameters.clear();
130:
131: startTag("url", null); //$NON-NLS-1$
132: if (feature.getUpdateSiteEntryModel() != null) {
133: parameters.clear();
134: parameters
135: .put(
136: "url", feature.getUpdateSiteEntryModel().getURLString()); //$NON-NLS-1$
137: parameters
138: .put(
139: "label", feature.getUpdateSiteEntryModel().getAnnotationNonLocalized()); //$NON-NLS-1$
140: printTag("update", parameters, true, true, true); //$NON-NLS-1$
141: }
142:
143: URLEntryModel[] siteEntries = feature
144: .getDiscoverySiteEntryModels();
145: for (int i = 0; i < siteEntries.length; i++) {
146: parameters.clear();
147: parameters.put("url", siteEntries[i].getURLString()); //$NON-NLS-1$
148: parameters
149: .put(
150: "label", siteEntries[i].getAnnotationNonLocalized()); //$NON-NLS-1$
151: printTag("discovery", parameters, true, true, true); //$NON-NLS-1$
152: }
153: endTag("url"); //$NON-NLS-1$
154: }
155: }
156:
157: public void printIncludes() throws CoreException {
158: IIncludedFeatureReference[] features = feature
159: .getRawIncludedFeatureReferences();
160: for (int i = 0; i < features.length; i++) {
161: parameters.clear();
162: try {
163: parameters
164: .put(
165: "id", features[i].getVersionedIdentifier().getIdentifier()); //$NON-NLS-1$
166: IFeature tmpFeature = generator.getSite(false)
167: .findFeature(
168: features[i].getVersionedIdentifier()
169: .getIdentifier(), null, true);
170: parameters
171: .put(
172: "version", tmpFeature.getVersionedIdentifier().getVersion().toString()); //$NON-NLS-1$
173: } catch (CoreException e) {
174: String message = NLS.bind(
175: Messages.exception_missingFeature, features[i]
176: .getVersionedIdentifier()
177: .getIdentifier());
178: throw new CoreException(new Status(IStatus.ERROR,
179: PI_PDEBUILD, EXCEPTION_FEATURE_MISSING,
180: message, null));
181: }
182:
183: printTag("includes", parameters, true, true, true); //$NON-NLS-1$
184: }
185: }
186:
187: private void printRequires() {
188: if (feature.getImportModels().length == 0)
189: return;
190: startTag("requires", null); //$NON-NLS-1$
191: printImports();
192: endTag("requires"); //$NON-NLS-1$
193: }
194:
195: private void printImports() {
196: IImport[] imports = feature.getRawImports();
197: for (int i = 0; i < imports.length; i++) {
198: parameters.clear();
199: if (imports[i].getKind() == IImport.KIND_PLUGIN) {
200: parameters
201: .put(
202: "plugin", imports[i].getVersionedIdentifier().getIdentifier()); //$NON-NLS-1$
203: parameters
204: .put(
205: "version", imports[i].getVersionedIdentifier().getVersion().toString()); //$NON-NLS-1$
206: } else {
207: //The import refers to a feature
208: parameters
209: .put(
210: "feature", imports[i].getVersionedIdentifier().getIdentifier()); //$NON-NLS-1$
211: parameters
212: .put(
213: "version", imports[i].getVersionedIdentifier().getVersion().toString()); //$NON-NLS-1$
214: }
215: parameters
216: .put(
217: "match", getStringForMatchingRule(imports[i].getRule())); //$NON-NLS-1$
218: printTag("import", parameters, true, true, true); //$NON-NLS-1$
219: }
220: }
221:
222: /**
223: * Method getStringForMatchingRule.
224: * @param ruleNumber
225: */
226: private String getStringForMatchingRule(int ruleNumber) {
227: switch (ruleNumber) {
228: case 1:
229: return "perfect"; //$NON-NLS-1$
230: case 2:
231: return "equivalent"; //$NON-NLS-1$
232: case 3:
233: return "compatible"; //$NON-NLS-1$
234: case 4:
235: return "greaterOrEqual"; //$NON-NLS-1$
236: case 0:
237: default:
238: return ""; //$NON-NLS-1$
239: }
240: }
241:
242: public void printPlugins() throws CoreException {
243: IPluginEntry[] plugins = feature.getRawPluginEntries();
244: for (int i = 0; i < plugins.length; i++) {
245: parameters.clear();
246: parameters
247: .put(
248: "id", plugins[i].getVersionedIdentifier().getIdentifier()); //$NON-NLS-1$
249:
250: String versionRequested = plugins[i]
251: .getVersionedIdentifier().getVersion().toString();
252: BundleDescription effectivePlugin = null;
253: try {
254: effectivePlugin = generator.getSite(false)
255: .getRegistry().getResolvedBundle(
256: plugins[i].getVersionedIdentifier()
257: .getIdentifier(),
258: versionRequested);
259: } catch (CoreException e) {
260: String message = NLS.bind(
261: Messages.exception_missingPlugin, plugins[i]
262: .getVersionedIdentifier());
263: throw new CoreException(new Status(IStatus.ERROR,
264: PI_PDEBUILD, EXCEPTION_PLUGIN_MISSING, message,
265: null));
266: }
267: if (effectivePlugin == null) {
268: String message = NLS.bind(
269: Messages.exception_missingPlugin, plugins[i]
270: .getVersionedIdentifier());
271: throw new CoreException(new Status(IStatus.ERROR,
272: PI_PDEBUILD, EXCEPTION_PLUGIN_MISSING, message,
273: null));
274: }
275: parameters.put("version", effectivePlugin.getVersion()); //$NON-NLS-1$
276: parameters.put(
277: "fragment", new Boolean(plugins[i].isFragment())); //$NON-NLS-1$
278: parameters.put("os", plugins[i].getOS()); //$NON-NLS-1$
279: parameters.put("arch", plugins[i].getOSArch()); //$NON-NLS-1$
280: parameters.put("ws", plugins[i].getWS()); //$NON-NLS-1$
281: parameters.put("nl", plugins[i].getNL()); //$NON-NLS-1$
282: if (plugins[i] instanceof PluginEntry
283: && !((PluginEntry) plugins[i]).isUnpack())
284: parameters.put("unpack", Boolean.FALSE.toString()); //$NON-NLS-1$
285: parameters
286: .put(
287: "download-size", new Long(plugins[i].getDownloadSize() != -1 ? plugins[i].getDownloadSize() : 0)); //$NON-NLS-1$
288: parameters
289: .put(
290: "install-size", new Long(plugins[i].getInstallSize() != -1 ? plugins[i].getInstallSize() : 0)); //$NON-NLS-1$
291: printTag("plugin", parameters, true, true, true); //$NON-NLS-1$
292: }
293: }
294:
295: private void printData() {
296: INonPluginEntry[] entries = feature.getNonPluginEntries();
297: for (int i = 0; i < entries.length; i++) {
298: parameters.put("id", entries[i].getIdentifier()); //$NON-NLS-1$
299: parameters.put("os", entries[i].getOS()); //$NON-NLS-1$
300: parameters.put("arch", entries[i].getOSArch()); //$NON-NLS-1$
301: parameters.put("ws", entries[i].getWS()); //$NON-NLS-1$
302: parameters.put("nl", entries[i].getNL()); //$NON-NLS-1$
303: parameters
304: .put(
305: "download-size", new Long(entries[i].getDownloadSize() != -1 ? entries[i].getDownloadSize() : 0)); //$NON-NLS-1$
306: parameters
307: .put(
308: "install-size", new Long(entries[i].getInstallSize() != -1 ? entries[i].getInstallSize() : 0)); //$NON-NLS-1$
309: printTag("data", parameters, true, true, true); //$NON-NLS-1$
310: }
311: }
312: }
|