Skip to content

Commit 66a95fd

Browse files
Neha Burnwalgireeshpunathil
authored andcommitted
quick fix for invalid characters in the MANIFEST header
1 parent e7b608f commit 66a95fd

File tree

6 files changed

+102
-4
lines changed

6 files changed

+102
-4
lines changed

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/JarManifestErrorReporter.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2017 IBM Corporation and others.
2+
* Copyright (c) 2000, 2017, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import org.eclipse.core.resources.IFile;
22+
import org.eclipse.core.resources.IMarker;
2223
import org.eclipse.core.runtime.IProgressMonitor;
2324
import org.eclipse.core.runtime.OperationCanceledException;
2425
import org.eclipse.jface.text.BadLocationException;
@@ -61,6 +62,15 @@ private String getHeaderName(String line) {
6162
return null;
6263
}
6364

65+
private boolean isInvalidHeader(String line) {
66+
int colonInd = line.indexOf(':');
67+
if (colonInd > 0) {
68+
String headerPart = line.substring(0, colonInd);
69+
return headerPart.matches(".*[^a-zA-Z0-9-_].*"); //$NON-NLS-1$
70+
}
71+
return false;
72+
}
73+
6474
protected int getPackageLine(IHeader header, ManifestElement element) {
6575
String packageName = element.getValue();
6676
if (element.getDirectiveKeys() != null || element.getKeys() != null) {
@@ -174,7 +184,15 @@ protected void parseManifest(IDocument document, IProgressMonitor monitor) {
174184
}
175185
String headerName = getHeaderName(line);
176186
if (headerName == null) {
177-
report(PDECoreMessages.BundleErrorReporter_invalidHeaderName, lineNumber, CompilerFlags.ERROR, PDEMarkerFactory.CAT_FATAL);
187+
boolean hasInvalidChars = isInvalidHeader(line);
188+
if (hasInvalidChars) {
189+
VirtualMarker marker = report(PDECoreMessages.BundleErrorReporter_invalidHeaderName, lineNumber,
190+
CompilerFlags.ERROR, PDEMarkerFactory.M_HEADER_INCORRECT, PDEMarkerFactory.CAT_FATAL);
191+
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
192+
return;
193+
}
194+
report(PDECoreMessages.BundleErrorReporter_invalidHeaderName, lineNumber, CompilerFlags.ERROR,
195+
PDEMarkerFactory.CAT_FATAL);
178196
return;
179197
}
180198
if (line.length() < colon + 2 || line.charAt(colon + 1) != ' ') {

ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/builders/PDEMarkerFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public class PDEMarkerFactory {
7474
public static final int M_ONLY_CONFIG_SEV = 0x1027; // other problem
7575
public static final int M_NO_AUTOMATIC_MODULE = 0x1028; // other problem
7676
public static final int M_EXEC_ENV_TOO_LOW = 0x1029; // other problem
77-
public static final int M_CONFLICTING_AUTOMATIC_MODULE = 0x1030; // other
78-
// problem
77+
public static final int M_CONFLICTING_AUTOMATIC_MODULE = 0x1030; // other problem
78+
public static final int M_HEADER_INCORRECT = 0x1031; // other problem
7979
public static final int M_SINGLETON_DIR_CHANGE = 0x1033; // other problem
8080

8181
// build properties fixes

ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/PDEUIMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ public class PDEUIMessages extends NLS {
695695

696696
public static String UpdateClasspathResolution_label;
697697
public static String UpdateExecutionEnvironment_label;
698+
public static String RemoveInvalidCharacters;
698699

699700
//
700701
// PDE resource strings

ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/ResolutionGenerator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ public IMarkerResolution[] getNonConfigSevResolutions(IMarker marker) {
149149
case PDEMarkerFactory.M_CONFLICTING_AUTOMATIC_MODULE:
150150
return new IMarkerResolution[] {
151151
new RemoveRedundantAutomaticModuleHeader(AbstractPDEMarkerResolution.REMOVE_TYPE, marker) };
152+
case PDEMarkerFactory.M_HEADER_INCORRECT:
153+
return new IMarkerResolution[] {
154+
new UpdateCorrectHeaderName(AbstractPDEMarkerResolution.RENAME_TYPE, marker) };
152155
}
153156
return NO_RESOLUTIONS;
154157
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 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.logging.Level;
17+
import java.util.logging.Logger;
18+
19+
import org.eclipse.core.resources.IMarker;
20+
import org.eclipse.jface.text.BadLocationException;
21+
import org.eclipse.jface.text.IDocument;
22+
import org.eclipse.jface.text.IRegion;
23+
import org.eclipse.pde.internal.core.text.bundle.BundleModel;
24+
import org.eclipse.pde.internal.ui.PDEUIMessages;
25+
26+
/**
27+
* Resolution to remove invalid characters from header in MANIFEST. Remove all
28+
* the invalid characters (characters other than alpha numeric characters, '-'
29+
* and '_') Also adding a space after 'Require-Bundle' header(before the colon)
30+
* when not present.
31+
*/
32+
public class UpdateCorrectHeaderName extends AbstractManifestMarkerResolution {
33+
34+
private static final Logger logger = Logger.getLogger(UpdateCorrectHeaderName.class.getName());
35+
36+
public UpdateCorrectHeaderName(int type, IMarker marker) {
37+
super(type, marker);
38+
}
39+
40+
@Override
41+
public String getLabel() {
42+
return PDEUIMessages.RemoveInvalidCharacters;
43+
}
44+
45+
private String removeInvalidChars(String userHeader) {
46+
return userHeader.replaceAll("[^a-zA-Z0-9-_]", ""); //$NON-NLS-1$ //$NON-NLS-2$
47+
}
48+
49+
@Override
50+
protected void createChange(BundleModel model) {
51+
model.getBundle();
52+
IDocument doc = model.getDocument();
53+
try {
54+
int lineNum = marker.getAttribute(IMarker.LINE_NUMBER, -1);
55+
IRegion lineInfo = doc.getLineInformation(lineNum - 1);
56+
int offset = lineInfo.getOffset();
57+
int length = lineInfo.getLength();
58+
59+
String getLine = doc.get(offset, length);
60+
int colonInd = getLine.indexOf(':');
61+
62+
if (colonInd > 0) {
63+
String userHeader = getLine.substring(0, colonInd);
64+
String correctHeader = removeInvalidChars(userHeader);
65+
doc.replace(offset, colonInd, correctHeader);
66+
}
67+
68+
} catch (BadLocationException e) {
69+
logger.log(Level.SEVERE,
70+
"Failed to apply UpdateCorrectHeaderName quick fix, unexpected location in the doc", //$NON-NLS-1$
71+
e);
72+
}
73+
}
74+
75+
}

ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/pderesources.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,7 @@ LocationSection_0=&Locations
24252425
AppendSeperatorBuildEntryResolution_label=Append a trailing separator to {0}.
24262426
AddExportPackageResolution_Label=Add missing packages
24272427
AddExportPackageInternalDirectiveResolution_Label=Add missing packages with x-internal directive
2428+
RemoveInvalidCharacters=Remove invalid characters from header
24282429

24292430
DependencyManagementSection_jobName=Analyzing Code
24302431
DescriptionSection_nameLabel=Name:

0 commit comments

Comments
 (0)