/
ManifestParser.java
100 lines (82 loc) · 3.26 KB
/
ManifestParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.remainsoftware.tycho.pombuilder.core.internal;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.ManifestElement;
import com.remainsoftware.tycho.pombuilder.core.Constants;
import com.remainsoftware.tycho.pombuilder.core.IParser;
import com.remainsoftware.tycho.pombuilder.core.IPom;
import com.remainsoftware.tycho.pombuilder.core.PomBuilderException;
public class ManifestParser implements IParser {
private static final String PARENT_PROJECT = "Parent-Project";
private static final String ECLIPSE_PLUGIN = "eclipse-plugin";
private static final String BUNDLE_VERSION = "Bundle-Version";
private static final String BUNDLE_SYMBOLIC_NAME = "Bundle-SymbolicName";
@Override
public void parse(IResource resource) throws CoreException {
// remove previous markers
IMarker[] markers = resource.findMarkers(Constants.POM_PROBLEM_MARKER,
false, IResource.DEPTH_ZERO);
for (IMarker marker : markers) {
marker.delete();
}
FileInputStream fisManifest = null;
boolean fisManifestOpen = false;
try {
fisManifest = new FileInputStream(resource.getLocation().toFile());
HashMap<String, String> map = new HashMap<String, String>();
ManifestElement.parseBundleManifest(fisManifest, map);
ManifestElement[] header = ManifestElement.parseHeader(
BUNDLE_SYMBOLIC_NAME, map.get(BUNDLE_SYMBOLIC_NAME));
String symbolicName = header[0].getValueComponents()[0];
fisManifestOpen = true;
IPom pom = new Pom(resource);
pom.setModelVersion("4.0.0");
pom.setVersion(map.get(BUNDLE_VERSION));
pom.setArtifactId(symbolicName);
pom.setPackaging(ECLIPSE_PLUGIN);
if (map.get(PARENT_PROJECT) == null
|| map.get(PARENT_PROJECT).trim().length() == 0) {
IMarker marker = resource
.createMarker(Constants.POM_PROBLEM_MARKER);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
marker.setAttribute(IMarker.MESSAGE, "Variable '"
+ Constants.PARENT_PROJECT + "' has not been defined.");
marker.setAttribute(IMarker.LINE_NUMBER, 1);
} else {
try {
pom.setParentProject(map.get(PARENT_PROJECT));
} catch (PomBuilderException e) {
IMarker marker = resource
.createMarker(Constants.POM_PROBLEM_MARKER);
marker.setAttribute(IMarker.SEVERITY,
IMarker.SEVERITY_WARNING);
marker.setAttribute(IMarker.MESSAGE, e.getMessage());
marker.setAttribute(
IMarker.LINE_NUMBER,
calculateLine(resource.getLocation().toFile(),
PARENT_PROJECT));
}
}
pom.write();
pom.getResource().refreshLocal(IResource.DEPTH_ZERO, null);
} catch (Exception e) {
throw new CoreException(new Status(IStatus.ERROR,
Constants.BUNDLE_ID, e.getMessage(), e));
}
finally {
if (fisManifestOpen) {
FileUtil.closeInputStream(fisManifest);
}
}
}
private Object calculateLine(File file, String string) {
int line = FileUtil.calculateLine(file, string);
return line == -1 ? 1 : line;
}
}