Skip to content

Commit 134bf6e

Browse files
committed
[PDE-Build] Use Path instead of URL to represent local files
This avoids unnecessary conversations that also use deprecated methods like File.toURL()
1 parent 1387bb1 commit 134bf6e

File tree

14 files changed

+95
-132
lines changed

14 files changed

+95
-132
lines changed

build/org.eclipse.pde.build.tests/src/org/eclipse/pde/build/internal/tests/ScriptGenerationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public void testBug187809() throws Exception {
290290
assertResourceFile(buildFolder, "features/sdk/feature.xml");
291291
IFile feature = buildFolder.getFile("features/sdk/feature.xml");
292292
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
293-
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocationURI().toURL());
293+
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocation().toPath());
294294

295295
FeatureEntry[] included = model.getIncludedFeatureReferences();
296296
assertEquals(included.length, 3);
@@ -485,7 +485,7 @@ public void testBug198536() throws Exception {
485485
buildFolder.getLocation().toOSString(), null);
486486

487487
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
488-
BuildTimeFeature feature = factory.parseBuildFeature(featureXML.getLocationURI().toURL());
488+
BuildTimeFeature feature = factory.parseBuildFeature(featureXML.getLocation().toPath());
489489
FeatureEntry[] pluginEntryModels = feature.getPluginEntries();
490490
assertEquals(pluginEntryModels[0].getId(), "foo");
491491
assertEquals(pluginEntryModels[0].getVersion(), "1.0.0.vA");
@@ -532,7 +532,7 @@ public void testBug206679() throws Exception {
532532

533533
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
534534
try {
535-
factory.parseBuildFeature(featureXML.getLocationURI().toURL());
535+
factory.parseBuildFeature(featureXML.getLocation().toPath());
536536
} catch (CoreException e) {
537537
assertTrue(e.getStatus().toString().indexOf(Messages.feature_parse_emptyRequires) > 0);
538538
return;

build/org.eclipse.pde.build.tests/src/org/eclipse/pde/build/internal/tests/SourceTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void testBug206679_247198() throws Exception {
7575
IFolder jdtSource = buildFolder.getFolder("features").getFolder("jdt.source");
7676
IFile featureXML = jdtSource.getFile("feature.xml");
7777
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
78-
BuildTimeFeature feature = factory.parseBuildFeature(featureXML.getLocationURI().toURL());
78+
BuildTimeFeature feature = factory.parseBuildFeature(featureXML.getLocation().toPath());
7979
assertTrue(feature.getDescription() != null);
8080
}
8181

@@ -175,7 +175,7 @@ public void testbug184920() throws Exception {
175175
IFile feature = buildFolder.getFile("features/a.feature.source/feature.xml");
176176

177177
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
178-
factory.parseBuildFeature(feature.getLocationURI().toURL());
178+
factory.parseBuildFeature(feature.getLocation().toPath());
179179
}
180180

181181
// test that source can come before the feature it is based on
@@ -302,7 +302,7 @@ public void testBug228537() throws Exception {
302302
IFile featureFile = buildFolder.getFile("features/source/feature.xml");
303303

304304
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
305-
BuildTimeFeature feature = factory.parseBuildFeature(featureFile.getLocationURI().toURL());
305+
BuildTimeFeature feature = factory.parseBuildFeature(featureFile.getLocation().toPath());
306306
FeatureEntry[] entries = feature.getRawIncludedFeatureReferences();
307307
assertTrue(entries.length == 1);
308308
assertEquals(entries[0].getId(), "org.eclipse.rcp");
@@ -374,7 +374,7 @@ public void testIndividualSourceBundles() throws Exception {
374374

375375
IFile feature = buildFolder.getFile("features/rcp.source/feature.xml");
376376
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
377-
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocationURI().toURL());
377+
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocation().toPath());
378378

379379
FeatureEntry[] included = model.getPluginEntries();
380380
assertEquals(included.length, 2);
@@ -661,7 +661,7 @@ public void testBug290828() throws Exception {
661661
IFile feature = buildFolder.getFile("tmp/eclipse/features/f.source_1.0.0/feature.xml");
662662
assertResourceFile(feature);
663663
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
664-
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocationURI().toURL());
664+
BuildTimeFeature model = factory.parseBuildFeature(feature.getLocation().toPath());
665665
FeatureEntry[] included = model.getPluginEntries();
666666
assertEquals(1, included.length);
667667
for (FeatureEntry element : included) {

build/org.eclipse.pde.build.tests/src/org/eclipse/pde/build/internal/tests/p2/LicenseTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ public void testLicenseFeatureOldP2() throws Exception {
118118
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
119119

120120
IFile licenseFeatureFile = buildFolder.getFile("features/L1/feature.xml");
121-
BuildTimeFeature licenseFeature = factory.parseBuildFeature(licenseFeatureFile.getLocationURI().toURL());
121+
BuildTimeFeature licenseFeature = factory.parseBuildFeature(licenseFeatureFile.getLocation().toPath());
122122

123123
IFile originalFeatureFile = buildFolder.getFile("features/F1/feature.xml");
124-
BuildTimeFeature originalFeature = factory.parseBuildFeature(originalFeatureFile.getLocationURI().toURL());
124+
BuildTimeFeature originalFeature = factory.parseBuildFeature(originalFeatureFile.getLocation().toPath());
125125

126126
assertEquals(licenseFeature.getLicenseURL(), URIUtil.toUnencodedString(actualLicense.getLocation()));
127127
assertEquals(licenseFeature.getLicense(), actualLicense.getBody());
@@ -130,7 +130,7 @@ public void testLicenseFeatureOldP2() throws Exception {
130130
IFile actualFeatureFile = buildFolder.getFile("checkFeature.xml");
131131
Utils.extractFromZip(buildFolder, "I.TestBuild/F1-TestBuild.zip", "eclipse/features/F1_1.0.0/feature.xml",
132132
actualFeatureFile);
133-
BuildTimeFeature actualFeature = factory.parseBuildFeature(actualFeatureFile.getLocationURI().toURL());
133+
BuildTimeFeature actualFeature = factory.parseBuildFeature(actualFeatureFile.getLocation().toPath());
134134

135135
assertNotNull(actualFeature.getLicense());
136136
assertEquals(licenseFeature.getLicense(), actualFeature.getLicense());
@@ -435,13 +435,13 @@ private void checkBuiltFeature(IFolder buildFolder, String errorMessage) throws
435435
IFile actualFeatureFile = buildFolder.getFile("checkFeature.xml");
436436
assertTrue(Utils.extractFromZip(buildFolder, "I.TestBuild/F1-TestBuild.zip",
437437
"eclipse/features/F1_1.0.0/feature.xml", actualFeatureFile));
438-
BuildTimeFeature actualFeature = factory.parseBuildFeature(actualFeatureFile.getLocationURI().toURL());
438+
BuildTimeFeature actualFeature = factory.parseBuildFeature(actualFeatureFile.getLocation().toPath());
439439

440440
IFile licenseFeatureFile = buildFolder.getFile("features/L1/feature.xml");
441-
BuildTimeFeature licenseFeature = factory.parseBuildFeature(licenseFeatureFile.getLocationURI().toURL());
441+
BuildTimeFeature licenseFeature = factory.parseBuildFeature(licenseFeatureFile.getLocation().toPath());
442442

443443
IFile originalFeatureFile = buildFolder.getFile("features/F1/feature.xml");
444-
BuildTimeFeature originalFeature = factory.parseBuildFeature(originalFeatureFile.getLocationURI().toURL());
444+
BuildTimeFeature originalFeature = factory.parseBuildFeature(originalFeatureFile.getLocation().toPath());
445445

446446
assertNotNull(errorMessage + "license was null", actualFeature.getLicense());
447447
assertEquals(errorMessage + "license text not equal", licenseFeature.getLicense(), actualFeature.getLicense());
@@ -529,7 +529,7 @@ public void testBug338835_MissingLicenseSection() throws Exception {
529529
runAntScript(buildXml.getLocation().toOSString(), new String[] { "test" },
530530
buildFolder.getLocation().toOSString(), null);
531531
BuildTimeFeature feature = new BuildTimeFeatureFactory()
532-
.parseBuildFeature(featureFolder.getFile("feature.xml").getLocationURI().toURL());
532+
.parseBuildFeature(featureFolder.getFile("feature.xml").getLocation().toPath());
533533

534534
assertEquals(feature.getLicense().trim(), "This is legal stuff");
535535
}

build/org.eclipse.pde.build.tests/src/org/eclipse/pde/build/internal/tests/p2/PublishingTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public void testPublishFeature_versionReplacement() throws Exception {
309309
assertFalse(hasZipped);
310310

311311
BuildTimeFeatureFactory factory = new BuildTimeFeatureFactory();
312-
BuildTimeFeature model = factory.parseBuildFeature(featureXML.getLocationURI().toURL());
312+
BuildTimeFeature model = factory.parseBuildFeature(featureXML.getLocation().toPath());
313313
assertEquals("1.0.0.12345", model.getVersion());
314314
assertEquals("1.0.0.12345", model.getPluginEntries()[0].getVersion());
315315
}

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/FetchScriptGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ protected void retrieveFeature(String elementName, String elementType, Map<Strin
490490
File featuresFolder = new File(root, DEFAULT_FEATURE_LOCATION);
491491
File featureLocation = new File(featuresFolder, elementName);
492492
try {
493-
feature = factory.createFeature(featureLocation.toURL(), null);
493+
feature = factory.createFeature(featureLocation.toPath(), null);
494494
featureProperties = new Properties();
495495
try (InputStream featureStream = new BufferedInputStream(new FileInputStream(new File(featureLocation, PROPERTIES_FILE)))) {
496496
featureProperties.load(featureStream);
@@ -555,8 +555,8 @@ protected void retrieveFeature(String elementName, String elementType, Map<Strin
555555
}
556556
try {
557557
BuildTimeFeatureFactory factory = BuildTimeFeatureFactory.getInstance();
558-
File featureFolder = new File(destination.toString());
559-
feature = factory.createFeature(featureFolder.toURL(), null);
558+
File featureFolder = destination.toFile();
559+
feature = factory.createFeature(featureFolder.toPath(), null);
560560

561561
//We only delete here, so if an exception is thrown the user can still see the retrieve.xml
562562
target.delete();

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/IPDEBuildConstants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ public interface IPDEBuildConstants {
107107
public final static int EXCEPTION_STATE_PROBLEM = 15;
108108
public final static int EXCEPTION_GENERIC = 16;
109109
public final static int EXCEPTION_FEATURE_PARSE = Constants.EXCEPTION_FEATURE_PARSE;
110-
public final static int WARNING_MISSING_SOURCE = 20;
111110
public final static int WARNING_ELEMENT_NOT_FETCHED = 21;
112111
public final static int EXCEPTION_CONFIG_FORMAT = 22;
113112
public final static int EXCEPTION_PRODUCT_FORMAT = 23;

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/packager/PackageConfigScriptGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ private String getFinalName(BundleDescription bundle, String shape) {
8080

8181
private String getFinalName(BuildTimeFeature feature) {
8282
if (!AbstractScriptGenerator.getPropertyAsBoolean(IBuildPropertiesConstants.PROPERTY_PACKAGER_AS_NORMALIZER)) {
83-
IPath featurePath = IPath.fromOSString(feature.getURL().getPath());
84-
return featurePath.segment(featurePath.segmentCount() - 2);
83+
return feature.getPath().getParent().getFileName().toString();
8584
}
8685
return feature.getId() + "_" + feature.getVersion(); //$NON-NLS-1$
8786
}

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeFeature.java

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@
1414
package org.eclipse.pde.internal.build.site;
1515

1616
import java.io.File;
17-
import java.net.URI;
18-
import java.net.URISyntaxException;
19-
import java.net.URL;
17+
import java.nio.file.Path;
2018
import java.util.ArrayList;
2119

22-
import org.eclipse.core.runtime.URIUtil;
2320
import org.eclipse.equinox.p2.publisher.eclipse.Feature;
2421
import org.eclipse.equinox.p2.publisher.eclipse.FeatureEntry;
2522
import org.eclipse.pde.build.Constants;
@@ -52,7 +49,7 @@ public BuildTimeFeature() {
5249
private int contextQualifierLength = -1;
5350
private BuildTimeSiteContentProvider contentProvider = null;
5451
private BuildTimeSite site = null;
55-
private URL url = null;
52+
private Path path = null;
5653
private String rootLocation = null;
5754

5855
public FeatureEntry[] getRawIncludedFeatureReferences() {
@@ -156,31 +153,28 @@ public BuildTimeSiteContentProvider getFeatureContentProvider() {
156153
return contentProvider;
157154
}
158155

159-
public URL getURL() {
160-
return url;
156+
public Path getPath() {
157+
return path;
161158
}
162159

163-
public void setURL(URL url) {
164-
this.url = url;
160+
public void setPath(Path path) {
161+
this.path = path;
165162
}
166163

167164
/**
168165
* @return the local filesystem location of the directory containing the feature.xml file.
169166
*/
170167
public String getRootLocation() {
171168
if (rootLocation == null) {
172-
URL location = getURL();
173-
if (location == null)
169+
Path location = getPath();
170+
if (location == null) {
174171
return null;
175-
try {
176-
URI locationURI = URIUtil.toURI(location);
177-
rootLocation = URIUtil.toFile(locationURI).getAbsolutePath();
178-
} catch (URISyntaxException e) {
179-
rootLocation = location.getPath();
180172
}
181-
int i = rootLocation.lastIndexOf(Constants.FEATURE_FILENAME_DESCRIPTOR);
182-
if (i != -1)
183-
rootLocation = rootLocation.substring(0, i);
173+
location = location.toAbsolutePath();
174+
if (location.endsWith(Constants.FEATURE_FILENAME_DESCRIPTOR)) {
175+
location = location.getParent();
176+
}
177+
rootLocation = location.toString() + File.separator;
184178
}
185179
return rootLocation;
186180
}
@@ -194,4 +188,12 @@ public FeatureEntry findPluginEntry(String id, String version) {
194188
}
195189
return null;
196190
}
191+
192+
private static final Path FEATURE_XML_PATH = Path.of(FEATURE_XML);
193+
194+
static Path ensureEndsWithFeatureXml(Path path) {
195+
return path != null && !path.endsWith(BuildTimeFeature.FEATURE_XML_PATH) //
196+
? path.resolve(BuildTimeFeature.FEATURE_XML_PATH)
197+
: path;
198+
}
197199
}

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeFeatureFactory.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
package org.eclipse.pde.internal.build.site;
1515

1616
import java.io.IOException;
17-
import java.net.URL;
17+
import java.nio.file.Path;
1818

1919
import org.eclipse.core.runtime.CoreException;
20-
import org.eclipse.core.runtime.IPath;
2120
import org.eclipse.core.runtime.IStatus;
2221
import org.eclipse.core.runtime.Status;
2322
import org.eclipse.osgi.util.NLS;
@@ -28,7 +27,7 @@
2827
import org.eclipse.pde.internal.build.Messages;
2928
import org.xml.sax.SAXException;
3029

31-
public class BuildTimeFeatureFactory /*extends BaseFeatureFactory */implements /*IFeatureFactory,*/IPDEBuildConstants, IBuildPropertiesConstants {
30+
public class BuildTimeFeatureFactory /*extends BaseFeatureFactory */ implements /*IFeatureFactory,*/IPDEBuildConstants, IBuildPropertiesConstants {
3231
public final static String BUILDTIME_FEATURE_FACTORY_ID = PI_PDEBUILD + ".BuildTimeFeatureFactory"; //$NON-NLS-1$
3332

3433
private static BuildTimeFeatureFactory factoryInstance = null;
@@ -43,20 +42,18 @@ public static BuildTimeFeatureFactory getInstance() {
4342
return factoryInstance;
4443
}
4544

46-
public BuildTimeFeature createFeature(URL url, BuildTimeSite site) throws CoreException {
47-
BuildTimeFeature feature = null;
48-
49-
if (url == null) {
50-
if (site != null)
45+
public BuildTimeFeature createFeature(Path featurePath, BuildTimeSite site) throws CoreException {
46+
if (featurePath == null) {
47+
if (site != null) {
5148
return createFeature(site);
49+
}
5250
return null;
5351
}
54-
5552
try {
56-
URL featureURL = new URL(url, BuildTimeFeature.FEATURE_XML);
57-
feature = parseBuildFeature(featureURL);
53+
featurePath = BuildTimeFeature.ensureEndsWithFeatureXml(featurePath);
54+
BuildTimeFeature feature = parseBuildFeature(featurePath);
5855

59-
String qualifier = AbstractScriptGenerator.readProperties(IPath.fromOSString(url.getFile()).removeLastSegments(1).toOSString(), PROPERTIES_FILE, IStatus.OK).getProperty(PROPERTY_QUALIFIER);
56+
String qualifier = AbstractScriptGenerator.readProperties(featurePath.getParent().toString(), PROPERTIES_FILE, IStatus.OK).getProperty(PROPERTY_QUALIFIER);
6057
String newVersion = QualifierReplacer.replaceQualifierInVersion(feature.getVersion(), feature.getId(), qualifier, site != null ? site.getFeatureVersions() : null);
6158
if (newVersion != null) {
6259
//a feature version ending in qualifier using context will be further modified based on its included plugins
@@ -68,36 +65,36 @@ public BuildTimeFeature createFeature(URL url, BuildTimeSite site) throws CoreEx
6865
}
6966

7067
feature.setSite(site);
71-
feature.setURL(featureURL);
68+
feature.setPath(featurePath);
69+
return feature;
7270
} catch (CoreException e) {
73-
String message = NLS.bind(Messages.error_creatingFeature, url);
71+
String message = NLS.bind(Messages.error_creatingFeature, featurePath);
7472
BundleHelper.getDefault().getLog().log(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_PARSE, message, e));
7573
throw e;
7674
} catch (Exception e) {
77-
String message = NLS.bind(Messages.exception_readingFile, url);
75+
String message = NLS.bind(Messages.exception_readingFile, featurePath);
7876
Status status = new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_MISSING, message, e);
7977
BundleHelper.getDefault().getLog().log(status); //Logging here because the caller consumes CoreExceptions.
8078
throw new CoreException(status);
8179
}
82-
return feature;
8380
}
8481

85-
public BuildTimeFeature parseBuildFeature(URL featureURL) throws CoreException {
82+
public BuildTimeFeature parseBuildFeature(Path featurePath) throws CoreException {
8683
BuildTimeFeatureParser parser = new BuildTimeFeatureParser();
8784
BuildTimeFeature feature = null;
8885
try {
89-
feature = (BuildTimeFeature) parser.parse(featureURL);
86+
feature = (BuildTimeFeature) parser.parse(featurePath);
9087
if (parser.getStatus() != null) {
9188
// some internalError were detected
9289
throw new CoreException(parser.getStatus());
9390
}
9491
} catch (SAXException e) {
95-
String message = NLS.bind(Messages.error_creatingFeature, featureURL.toString());
92+
String message = NLS.bind(Messages.error_creatingFeature, featurePath);
9693
Status status = new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_PARSE, message, e);
9794
BundleHelper.getDefault().getLog().log(status);
9895
throw new CoreException(status);
9996
} catch (IOException e) {
100-
String message = NLS.bind(Messages.exception_readingFile, featureURL.toString());
97+
String message = NLS.bind(Messages.exception_readingFile, featurePath);
10198
Status status = new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e);
10299
BundleHelper.getDefault().getLog().log(status); //Logging here because the caller consumes CoreExceptions.
103100
throw new CoreException(status);

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeFeatureParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
import java.io.IOException;
1919
import java.io.InputStream;
20-
import java.net.URL;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
2122

2223
import org.eclipse.equinox.internal.p2.publisher.eclipse.FeatureManifestParser;
2324
import org.eclipse.equinox.p2.publisher.eclipse.Feature;
@@ -30,10 +31,9 @@ protected Feature createFeature(String id, String version) {
3031
return new BuildTimeFeature(id, version);
3132
}
3233

33-
public Feature parse(URL featureURL) throws SAXException, IOException {
34-
InputStream in = featureURL.openStream();
35-
try (in) {
36-
return super.parse(in, featureURL);
34+
public Feature parse(Path featurePath) throws SAXException, IOException {
35+
try (InputStream in = Files.newInputStream(featurePath)) {
36+
return super.parse(in, featurePath.toUri().toURL());
3737
}
3838
}
3939
}

0 commit comments

Comments
 (0)