1 /*******************************************************************************
2  *  Copyright (c) 2017, 2020 IBM Corporation and others.
3  *
4  *  This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License 2.0
6  *  which accompanies this distribution, and is available at
7  *  https://www.eclipse.org/legal/epl-2.0/
8  *
9  *  SPDX-License-Identifier: EPL-2.0
10  *
11  *  Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.pde.internal.ui.correction;
15 
16 import java.util.HashSet;
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.pde.internal.core.ICoreConstants;
19 import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
20 import org.eclipse.pde.internal.core.ibundle.IBundle;
21 import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
22 import org.eclipse.pde.internal.core.text.bundle.Bundle;
23 import org.eclipse.pde.internal.core.text.bundle.BundleModel;
24 import org.eclipse.pde.internal.ui.PDEUIMessages;
25 import org.eclipse.pde.internal.ui.wizards.plugin.NewProjectCreationOperation;
26 import org.osgi.framework.Constants;
27 
28 public class AddAutomaticModuleResolution extends AbstractManifestMarkerResolution {
29 	private IMarker mark;
30 
AddAutomaticModuleResolution(int type, IMarker marker)31 	public AddAutomaticModuleResolution(int type, IMarker marker) {
32 		super(type, marker);
33 		mark = marker;
34 	}
35 
36 	@Override
findOtherMarkers(IMarker[] markers)37 	public IMarker[] findOtherMarkers(IMarker[] markers) {
38 		HashSet<IMarker> mset = new HashSet<>(markers.length);
39 		for (IMarker iMarker : markers) {
40 			if (iMarker.equals(mark))
41 				continue;
42 			if (getProblemId(iMarker) == PDEMarkerFactory.M_NO_AUTOMATIC_MODULE)
43 				mset.add(iMarker);
44 		}
45 		int size = mset.size();
46 		return mset.toArray(new IMarker[size]);
47 	}
48 	@Override
createChange(BundleModel model)49 	protected void createChange(BundleModel model) {
50 		IBundle bundle = model.getBundle();
51 		if (bundle instanceof Bundle) {
52 			Bundle bun = (Bundle) bundle;
53 			IManifestHeader header = bun.getManifestHeader(ICoreConstants.AUTOMATIC_MODULE_NAME);
54 			if (header == null) {
55 				IManifestHeader headerName = bun.getManifestHeader(Constants.BUNDLE_SYMBOLICNAME);
56 				String val = headerName.getValue();
57 				try {
58 					val = val.substring(0, val.indexOf(';'));
59 				}
60 				catch (Exception e) {
61 					// for cases where ; not present
62 				}
63 				bundle.setHeader(ICoreConstants.AUTOMATIC_MODULE_NAME,
64 						NewProjectCreationOperation.determineAutomaticModuleNameFromBSN(val));
65 			}
66 		}
67 	}
68 
69 	@Override
getDescription()70 	public String getDescription() {
71 		return PDEUIMessages.AddAutomaticModuleResolution_desc;
72 	}
73 
74 	@Override
getLabel()75 	public String getLabel() {
76 		return PDEUIMessages.AddAutomaticModuleResolution_label;
77 	}
78 
getProblemId(IMarker marker)79 	int getProblemId(IMarker marker) {
80 		int problemID = marker.getAttribute(PDEMarkerFactory.PROBLEM_ID, PDEMarkerFactory.NO_RESOLUTION);
81 		if (problemID != PDEMarkerFactory.NO_RESOLUTION) {
82 			return problemID;
83 		}
84 		return marker.getAttribute("id", PDEMarkerFactory.NO_RESOLUTION); //$NON-NLS-1$
85 	}
86 
87 
88 }
89