Skip to content

Commit

Permalink
Improve property substitution for code-style importer (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshiell committed Sep 15, 2024
1 parent fd00770 commit 2a2f5dc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# CheckStyle-IDEA Changelog

* **5.95.0** Fixed: Improved property substitution for code-style importer (#651).
* **5.95.0** New: Added CheckStyle 10.18.1.
* **5.94.1** Fixed: Error on case-sensitive FSs due to change in plugin directory name with new build plugin (#650).
* **5.94.0** New: Added CheckStyle 10.18.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,20 @@
import org.infernus.idea.checkstyle.CheckstyleProjectService;
import org.infernus.idea.checkstyle.csapi.CheckstyleInternalObject;
import org.infernus.idea.checkstyle.exception.CheckStylePluginException;
import org.infernus.idea.checkstyle.model.ConfigurationLocation;
import org.infernus.idea.checkstyle.model.ConfigurationLocationFactory;
import org.infernus.idea.checkstyle.model.ConfigurationType;
import org.infernus.idea.checkstyle.model.NamedScopeHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import static java.util.function.Function.identity;

/**
* Imports code style settings from check style configuration file.
* Registered as {@code schemeImporter} in <em>plugin.xml</em>.
Expand Down Expand Up @@ -67,6 +78,10 @@ private CheckstyleProjectService checkstyleProjectService(@NotNull final Project
return project.getService(CheckstyleProjectService.class);
}

private ConfigurationLocationFactory configurationLocationFactory(@NotNull final Project project) {
return project.getService(ConfigurationLocationFactory.class);
}

@Nullable
@Override
public String getAdditionalImportInfo(@NotNull final CodeStyleScheme scheme) {
Expand All @@ -79,9 +94,30 @@ private CheckstyleInternalObject loadConfiguration(@NotNull final Project projec
@NotNull final VirtualFile selectedFile) {
return checkstyleProjectService(project)
.getCheckstyleInstance()
.loadConfiguration(selectedFile, null);
.loadConfiguration(selectedFile, defaultPropertiesForFile(project, selectedFile));
}

@NotNull
private Map<String, String> defaultPropertiesForFile(@NotNull final Project project,
@NotNull final VirtualFile selectedFile) {
try {
ConfigurationLocation codeImportHolder = configurationLocationFactory(project).create(
project,
UUID.randomUUID().toString(),
ConfigurationType.LOCAL_FILE,
selectedFile.getPath(),
"Code Import Holder",
NamedScopeHelper.getDefaultScope(project));
codeImportHolder.resolve(checkstyleProjectService(project).underlyingClassLoader());
final Map<String, String> properties = codeImportHolder.getProperties();
return properties.keySet().stream().
collect(Collectors.toMap(identity(), k -> properties.getOrDefault(k, "")));

} catch (Exception e) {
LOG.error("Unable to construct defaulted properties", e);
return Collections.emptyMap();
}
}

void importConfiguration(@NotNull final CheckstyleProjectService checkstyleProjectService,
@NotNull final CheckstyleInternalObject configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import org.infernus.idea.checkstyle.checker.CheckerFactoryCache;
import org.infernus.idea.checkstyle.util.CheckStyleEntityResolver;
import org.infernus.idea.checkstyle.util.Objects;
import org.infernus.idea.checkstyle.util.Pair;
import org.infernus.idea.checkstyle.util.ProjectPaths;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import java.io.File;
Expand Down Expand Up @@ -171,11 +171,11 @@ public synchronized void reset() {
unblock();
}

private List<String> extractProperties(@Nullable final InputStream inputStream,
private Map<String, String> extractProperties(@Nullable final InputStream inputStream,
@NotNull final ClassLoader checkstyleClassLoader) {
if (inputStream != null) {
try {
final List<String> propertyNames = new ArrayList<>();
final Map<String, String> propertiesAndDefaults = new HashMap<>();

final XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setXMLResolver(new CheckStyleEntityResolver(this, checkstyleClassLoader));
Expand All @@ -185,32 +185,38 @@ private List<String> extractProperties(@Nullable final InputStream inputStream,
final XMLEvent event = eventReader.nextEvent();

if (event.isStartElement()) {
final String propertyName = extractNameIfPropertyElement((StartElement) event);
if (propertyName != null) {
propertyNames.add(propertyName);
final var property = extractNameAndDefaultIfPropertyElement((StartElement) event);
if (property != null) {
propertiesAndDefaults.put(property.first(), property.second());
}
}
}

return propertyNames;
return propertiesAndDefaults;

} catch (Exception e) {
LOG.warn("CheckStyle file could not be parsed for properties.", e);
}
}

return new ArrayList<>();
return Collections.emptyMap();
}

private static String extractNameIfPropertyElement(final StartElement startElement) {
private static Pair<String, String> extractNameAndDefaultIfPropertyElement(final StartElement startElement) {
if ("property".equals(startElement.getName().getLocalPart())) {
final Attribute valueAttribute = startElement.getAttributeByName(new QName("value"));
final var valueAttribute = startElement.getAttributeByName(new QName("value"));
if (valueAttribute != null) {
final String value = valueAttribute.getValue();
final int propertyStart = value.indexOf("${");
final int propertyEnd = value.indexOf('}');
if (propertyStart >= 0 && propertyEnd >= 0) {
return value.substring(propertyStart + 2, propertyEnd);
final String propertyName = value.substring(propertyStart + 2, propertyEnd);

final var defaultAttribute = startElement.getAttributeByName(new QName("default"));
if (defaultAttribute != null) {
return Pair.of(propertyName, defaultAttribute.getValue());
}
return Pair.of(propertyName, "");
}
}
}
Expand All @@ -230,15 +236,15 @@ public synchronized InputStream resolve(@NotNull final ClassLoader checkstyleCla
InputStream is = resolveFile(checkstyleClassLoader);

if (!propertiesCheckedThisSession) {
final List<String> propertiesInFile = extractProperties(is, checkstyleClassLoader);
final Map<String, String> propertiesInFile = extractProperties(is, checkstyleClassLoader);

for (final String propertyName : propertiesInFile) {
for (final String propertyName : propertiesInFile.keySet()) {
if (!properties.containsKey(propertyName)) {
properties.put(propertyName, "");
properties.put(propertyName, propertiesInFile.getOrDefault(propertyName, ""));
}
}

properties.keySet().removeIf(propertyName -> !propertiesInFile.contains(propertyName));
properties.keySet().removeIf(propertyName -> !propertiesInFile.keySet().contains(propertyName));

try {
is.reset();
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/infernus/idea/checkstyle/util/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.infernus.idea.checkstyle.util;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public record Pair<K, V>(K first, V second) {

public static <K, V> Pair<K, V> of(@NotNull final K first, @Nullable final V second) {
return new Pair<>(first, second);
}

}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<change-notes>
<![CDATA[
<ul>
<li>5.95.0: Fixed: Improved property substitution for code-style importer (#651).</li>
<li>5.95.0: New: Added Checkstyle 10.18.1.</li>
<li>5.94.1: Fixed: Error on case-sensitive FSs due to change in plugin directory name with new build plugin (#650).</li>
<li>5.94.0: New: Added Checkstyle 10.18.0.</li>
Expand Down

0 comments on commit 2a2f5dc

Please sign in to comment.