Skip to content

Commit 465f6bc

Browse files
authored
Merge pull request #1051 from amvanbaren/string-literal-constants
Use constants for string literals
2 parents 2c82308 + b8b4fd9 commit 465f6bc

15 files changed

+284
-246
lines changed

server/src/main/java/org/eclipse/openvsx/ExtensionProcessor.java

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public class ExtensionProcessor implements AutoCloseable {
4444
private static final String[] README = { "extension/README.md", "extension/README", "extension/README.txt" };
4545
private static final String[] CHANGELOG = { "extension/CHANGELOG.md", "extension/CHANGELOG", "extension/CHANGELOG.txt" };
4646

47+
private static final String MANIFEST_METADATA = "Metadata";
48+
private static final String MANIFEST_IDENTITY = "Identity";
49+
private static final String MANIFEST_PROPERTIES = "Properties";
50+
private static final String MANIFEST_PROPERTY = "Property";
51+
private static final String MANIFEST_VALUE = "Value";
52+
4753
protected final Logger logger = LoggerFactory.getLogger(ExtensionProcessor.class);
4854

4955
private final TempFile extensionFile;
@@ -91,7 +97,7 @@ private void loadPackageJson() {
9197
// Read package.json
9298
try (var entryFile = ArchiveUtil.readEntry(zipFile, PACKAGE_JSON)) {
9399
if (entryFile == null) {
94-
throw new ErrorResultException("Entry not found: " + PACKAGE_JSON);
100+
throw new ErrorResultException(entryNotFoundMessage(PACKAGE_JSON));
95101
}
96102

97103
var mapper = new ObjectMapper();
@@ -114,7 +120,7 @@ private void loadVsixManifest() {
114120
// Read extension.vsixmanifest
115121
try (var entryFile = ArchiveUtil.readEntry(zipFile, VSIX_MANIFEST)) {
116122
if (entryFile == null) {
117-
throw new ErrorResultException("Entry not found: " + VSIX_MANIFEST);
123+
throw new ErrorResultException(entryNotFoundMessage(VSIX_MANIFEST));
118124
}
119125

120126
var mapper = new XmlMapper();
@@ -127,6 +133,10 @@ private void loadVsixManifest() {
127133
}
128134
}
129135

136+
private String entryNotFoundMessage(String file) {
137+
return "Entry not found: " + file;
138+
}
139+
130140
private JsonNode findByIdInArray(Iterable<JsonNode> iter, String id) {
131141
for(JsonNode node : iter){
132142
var idNode = node.get("Id");
@@ -139,84 +149,84 @@ private JsonNode findByIdInArray(Iterable<JsonNode> iter, String id) {
139149

140150
public String getExtensionName() {
141151
loadVsixManifest();
142-
return vsixManifest.path("Metadata").path("Identity").path("Id").asText();
152+
return vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_IDENTITY).path("Id").asText();
143153
}
144154

145155
public String getNamespace() {
146156
loadVsixManifest();
147-
return vsixManifest.path("Metadata").path("Identity").path("Publisher").asText();
157+
return vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_IDENTITY).path("Publisher").asText();
148158
}
149159

150160
public List<String> getExtensionDependencies() {
151161
loadVsixManifest();
152-
var extDepenNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.ExtensionDependencies");
153-
return asStringList(extDepenNode.path("Value").asText(), ",");
162+
var extDepenNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.ExtensionDependencies");
163+
return asStringList(extDepenNode.path(MANIFEST_VALUE).asText(), ",");
154164
}
155165

156166
public List<String> getBundledExtensions() {
157167
loadVsixManifest();
158-
var extPackNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.ExtensionPack");
159-
return asStringList(extPackNode.path("Value").asText(), ",");
168+
var extPackNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.ExtensionPack");
169+
return asStringList(extPackNode.path(MANIFEST_VALUE).asText(), ",");
160170
}
161171

162172
public List<String> getExtensionKinds() {
163173
loadVsixManifest();
164-
var extKindNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.ExtensionKind");
165-
return asStringList(extKindNode.path("Value").asText(), ",");
174+
var extKindNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.ExtensionKind");
175+
return asStringList(extKindNode.path(MANIFEST_VALUE).asText(), ",");
166176
}
167177

168178
public String getHomepage() {
169179
loadVsixManifest();
170-
var extKindNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Links.Learn");
171-
return extKindNode.path("Value").asText();
180+
var extKindNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Links.Learn");
181+
return extKindNode.path(MANIFEST_VALUE).asText();
172182
}
173183

174184
public String getRepository() {
175185
loadVsixManifest();
176-
var sourceNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Links.Source");
177-
return sourceNode.path("Value").asText();
186+
var sourceNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Links.Source");
187+
return sourceNode.path(MANIFEST_VALUE).asText();
178188
}
179189

180190
public String getBugs() {
181191
loadVsixManifest();
182-
var supportNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Links.Support");
183-
return supportNode.path("Value").asText();
192+
var supportNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Links.Support");
193+
return supportNode.path(MANIFEST_VALUE).asText();
184194
}
185195

186196
public String getGalleryColor() {
187197
loadVsixManifest();
188-
var colorNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Branding.Color");
189-
return colorNode.path("Value").asText();
198+
var colorNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Branding.Color");
199+
return colorNode.path(MANIFEST_VALUE).asText();
190200
}
191201

192202
public String getGalleryTheme() {
193203
loadVsixManifest();
194-
var themeNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Services.Branding.Theme");
195-
return themeNode.path("Value").asText();
204+
var themeNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Services.Branding.Theme");
205+
return themeNode.path(MANIFEST_VALUE).asText();
196206
}
197207

198208
public List<String> getLocalizedLanguages() {
199209
loadVsixManifest();
200-
var languagesNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.LocalizedLanguages");
201-
return asStringList(languagesNode.path("Value").asText(), ",");
210+
var languagesNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.LocalizedLanguages");
211+
return asStringList(languagesNode.path(MANIFEST_VALUE).asText(), ",");
202212
}
203213

204214
public boolean isPreview() {
205215
loadVsixManifest();
206-
var galleryFlags = vsixManifest.path("Metadata").path("GalleryFlags");
216+
var galleryFlags = vsixManifest.path(MANIFEST_METADATA).path("GalleryFlags");
207217
return asStringList(galleryFlags.asText(), " ").contains("Preview");
208218
}
209219

210220
public boolean isPreRelease() {
211221
loadVsixManifest();
212-
var preReleaseNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.PreRelease");
213-
return preReleaseNode.path("Value").asBoolean(false);
222+
var preReleaseNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.PreRelease");
223+
return preReleaseNode.path(MANIFEST_VALUE).asBoolean(false);
214224
}
215225

216226
public String getSponsorLink() {
217227
loadVsixManifest();
218-
var sponsorLinkNode = findByIdInArray(vsixManifest.path("Metadata").path("Properties").path("Property"), "Microsoft.VisualStudio.Code.SponsorLink");
219-
return sponsorLinkNode.path("Value").asText();
228+
var sponsorLinkNode = findByIdInArray(vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_PROPERTIES).path(MANIFEST_PROPERTY), "Microsoft.VisualStudio.Code.SponsorLink");
229+
return sponsorLinkNode.path(MANIFEST_VALUE).asText();
220230
}
221231

222232
public ExtensionVersion getMetadata() {
@@ -227,10 +237,10 @@ public ExtensionVersion getMetadata() {
227237
extVersion.setTargetPlatform(getTargetPlatform());
228238
extVersion.setPreview(isPreview());
229239
extVersion.setPreRelease(isPreRelease());
230-
extVersion.setDisplayName(vsixManifest.path("Metadata").path("DisplayName").asText());
231-
extVersion.setDescription(vsixManifest.path("Metadata").path("Description").path("").asText());
240+
extVersion.setDisplayName(vsixManifest.path(MANIFEST_METADATA).path("DisplayName").asText());
241+
extVersion.setDescription(vsixManifest.path(MANIFEST_METADATA).path("Description").path("").asText());
232242
extVersion.setEngines(getEngines(packageJson.path("engines")));
233-
extVersion.setCategories(asStringList(vsixManifest.path("Metadata").path("Categories").asText(), ","));
243+
extVersion.setCategories(asStringList(vsixManifest.path(MANIFEST_METADATA).path("Categories").asText(), ","));
234244
extVersion.setExtensionKind(getExtensionKinds());
235245
extVersion.setTags(getTags());
236246
extVersion.setLicense(packageJson.path("license").textValue());
@@ -248,11 +258,11 @@ public ExtensionVersion getMetadata() {
248258
}
249259

250260
public String getVersion() {
251-
return vsixManifest.path("Metadata").path("Identity").path("Version").asText();
261+
return vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_IDENTITY).path("Version").asText();
252262
}
253263

254264
private String getTargetPlatform() {
255-
var targetPlatform = vsixManifest.path("Metadata").path("Identity").path("TargetPlatform").asText();
265+
var targetPlatform = vsixManifest.path(MANIFEST_METADATA).path(MANIFEST_IDENTITY).path("TargetPlatform").asText();
256266
if (targetPlatform.isEmpty()) {
257267
targetPlatform = TargetPlatform.NAME_UNIVERSAL;
258268
}
@@ -261,7 +271,7 @@ private String getTargetPlatform() {
261271
}
262272

263273
private List<String> getTags() {
264-
var tags = vsixManifest.path("Metadata").path("Tags").asText();
274+
var tags = vsixManifest.path(MANIFEST_METADATA).path("Tags").asText();
265275
return asStringList(tags, ",").stream()
266276
.collect(Collectors.groupingBy(String::toLowerCase))
267277
.entrySet().stream()
@@ -356,7 +366,7 @@ protected TempFile getManifest(ExtensionVersion extVersion) throws IOException {
356366
readInputStream();
357367
var entryFile = ArchiveUtil.readEntry(zipFile, PACKAGE_JSON);
358368
if (entryFile == null) {
359-
throw new ErrorResultException("Entry not found: " + PACKAGE_JSON);
369+
throw new ErrorResultException(entryNotFoundMessage(PACKAGE_JSON));
360370
}
361371
var manifest = new FileResource();
362372
manifest.setExtension(extVersion);
@@ -403,7 +413,7 @@ public TempFile getLicense(ExtensionVersion extVersion) throws IOException {
403413

404414
var entryFile = ArchiveUtil.readEntry(zipFile, assetPath);
405415
if(entryFile == null) {
406-
throw new ErrorResultException("Entry not found: " + assetPath);
416+
throw new ErrorResultException(entryNotFoundMessage(assetPath));
407417
}
408418

409419
var lastSegmentIndex = assetPath.lastIndexOf('/');
@@ -418,7 +428,7 @@ private TempFile readFromVsixPackage(String assetType, String[] alternateNames)
418428
if(StringUtils.isNotEmpty(assetPath)) {
419429
var entryFile = ArchiveUtil.readEntry(zipFile, assetPath);
420430
if(entryFile == null) {
421-
throw new ErrorResultException("Entry not found: " + assetPath);
431+
throw new ErrorResultException(entryNotFoundMessage(assetPath));
422432
}
423433

424434
var lastSegmentIndex = assetPath.lastIndexOf('/');
@@ -451,7 +461,7 @@ private TempFile readFromAlternateNames(String[] names) throws IOException {
451461

452462
private String tryGetLicensePath() {
453463
loadVsixManifest();
454-
var licensePath = vsixManifest.path("Metadata").path("License").asText();
464+
var licensePath = vsixManifest.path(MANIFEST_METADATA).path("License").asText();
455465
return licensePath.isEmpty()
456466
? tryGetAssetPath(ExtensionQueryResult.ExtensionFile.FILE_LICENSE)
457467
: licensePath;
@@ -486,7 +496,7 @@ protected TempFile getIcon(ExtensionVersion extVersion) throws IOException {
486496

487497
var entryFile = ArchiveUtil.readEntry(zipFile, iconPath);
488498
if (entryFile == null) {
489-
throw new ErrorResultException("Entry not found: " + iconPath);
499+
throw new ErrorResultException(entryNotFoundMessage(iconPath));
490500
}
491501

492502
var icon = new FileResource();
@@ -511,7 +521,7 @@ public TempFile getVsixManifest(ExtensionVersion extVersion) throws IOException
511521

512522
var entryFile = ArchiveUtil.readEntry(zipFile, VSIX_MANIFEST);
513523
if(entryFile == null) {
514-
throw new ErrorResultException("Entry not found: " + VSIX_MANIFEST);
524+
throw new ErrorResultException(entryNotFoundMessage(VSIX_MANIFEST));
515525
}
516526

517527
entryFile.setResource(vsixManifest);

server/src/main/java/org/eclipse/openvsx/ExtensionValidator.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,27 @@ public Optional<Issue> validateNamespace(String namespace) {
4949
return Optional.of(new Issue("Invalid namespace name: " + namespace));
5050
}
5151
if (namespace.length() > DEFAULT_STRING_SIZE) {
52-
return Optional.of(new Issue("The namespace name exceeds the current limit of " + DEFAULT_STRING_SIZE + " characters."));
52+
return Optional.of(new Issue(charactersExceededMessage("namespace name", DEFAULT_STRING_SIZE)));
5353
}
5454
return Optional.empty();
5555
}
5656

57+
private void validateDisplayName(String displayName, int limit, List<Issue> issues) {
58+
var field = "displayName";
59+
checkCharacters(displayName, field, issues);
60+
checkFieldSize(displayName, limit, field, issues);
61+
}
62+
63+
private void validateDescription(String description, int limit, List<Issue> issues) {
64+
var field = "description";
65+
checkCharacters(description, field, issues);
66+
checkFieldSize(description, limit, field, issues);
67+
}
68+
5769
public List<Issue> validateNamespaceDetails(NamespaceDetailsJson json) {
5870
var issues = new ArrayList<Issue>();
59-
checkCharacters(json.getDisplayName(), "displayName", issues);
60-
checkFieldSize(json.getDisplayName(), 32, "displayName", issues);
61-
checkCharacters(json.getDescription(), "description", issues);
62-
checkFieldSize(json.getDescription(), DEFAULT_STRING_SIZE, "description", issues);
71+
validateDisplayName(json.getDisplayName(), 32, issues);
72+
validateDescription(json.getDescription(), DEFAULT_STRING_SIZE, issues);
6373
checkURL(json.getWebsite(), "website", issues);
6474
checkURL(json.getSupportLink(), "supportLink", issues);
6575

@@ -87,7 +97,7 @@ public Optional<Issue> validateExtensionName(String name) {
8797
return Optional.of(new Issue("Invalid extension name: " + name));
8898
}
8999
if (name.length() > DEFAULT_STRING_SIZE) {
90-
return Optional.of(new Issue("The extension name exceeds the current limit of " + DEFAULT_STRING_SIZE + " characters."));
100+
return Optional.of(new Issue(charactersExceededMessage("extension name", DEFAULT_STRING_SIZE)));
91101
}
92102
return Optional.empty();
93103
}
@@ -104,10 +114,8 @@ public List<Issue> validateMetadata(ExtensionVersion extVersion) {
104114
var issues = new ArrayList<Issue>();
105115
checkVersion(extVersion.getVersion(), issues);
106116
checkTargetPlatform(extVersion.getTargetPlatform(), issues);
107-
checkCharacters(extVersion.getDisplayName(), "displayName", issues);
108-
checkFieldSize(extVersion.getDisplayName(), DEFAULT_STRING_SIZE, "displayName", issues);
109-
checkCharacters(extVersion.getDescription(), "description", issues);
110-
checkFieldSize(extVersion.getDescription(), DESCRIPTION_SIZE, "description", issues);
117+
validateDisplayName(extVersion.getDisplayName(), DEFAULT_STRING_SIZE, issues);
118+
validateDescription(extVersion.getDescription(), DESCRIPTION_SIZE, issues);
111119
checkCharacters(extVersion.getCategories(), "categories", issues);
112120
checkFieldSize(extVersion.getCategories(), DEFAULT_STRING_SIZE, "categories", issues);
113121
checkCharacters(extVersion.getTags(), "keywords", issues);
@@ -181,10 +189,14 @@ private void checkCharacters(List<String> values, String field, List<Issue> issu
181189

182190
private void checkFieldSize(String value, int limit, String field, List<Issue> issues) {
183191
if (value != null && value.length() > limit) {
184-
issues.add(new Issue("The field '" + field + "' exceeds the current limit of " + limit + " characters."));
192+
issues.add(new Issue(charactersExceededMessage("field '" + field + "'", limit)));
185193
}
186194
}
187195

196+
private String charactersExceededMessage(String name, int limit) {
197+
return "The " + name + " exceeds the current limit of " + limit + " characters.";
198+
}
199+
188200
private void checkFieldSize(List<String> values, int limit, String field, List<Issue> issues) {
189201
if (values == null) {
190202
return;

0 commit comments

Comments
 (0)