Skip to content

Commit

Permalink
[MENFORCER-507] Add xsltLocation parameter to ExternalRules
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Jul 1, 2024
1 parent baf6d08 commit 3a7e286
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Objects;

Expand All @@ -58,6 +59,8 @@ public final class ExternalRules extends AbstractEnforcerRuleConfigProvider {
/**
* The external rules location. If it starts with <code>classpath:</code> the resource is read from the classpath.
* Otherwise, it is handled as a filesystem path, either absolute, or relative to <code>${project.basedir}</code>
*
* @since 3.2.0
*/
private String location;

Expand Down Expand Up @@ -100,7 +103,8 @@ public final class ExternalRules extends AbstractEnforcerRuleConfigProvider {
* </xsl:template>
*
* <!-- An empty template will effectively remove the matching nodes -->
* <xsl:template match="//bannedDependencies/excludes/exclude[contains(text(), 'com.google.code.findbugs:jsr305')]"/>
* <xsl:template match=
* "//bannedDependencies/excludes/exclude[contains(text(), 'com.google.code.findbugs:jsr305')]"/>
* </xsl:stylesheet>
* }</pre>
*
Expand Down Expand Up @@ -183,19 +187,25 @@ private InputStream resolveDescriptor(String path) throws EnforcerRuleError {

@Override
public String toString() {
return String.format("ExternalRules[location=%s]", location);
return String.format("ExternalRules[location=%s, xsltLocation=%s]", location, xsltLocation);
}

InputStream transform(String sourceLocation, InputStream sourceXml, String xsltLocation) {
if (xsltLocation == null || !xsltLocation.trim().isEmpty()) {
if (xsltLocation == null || xsltLocation.trim().isEmpty()) {
return sourceXml;
}

try (InputStream in = resolveDescriptor(xsltLocation);
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(in));
transformer.transform(new StreamSource(sourceXml), new StreamResult(baos));
return new ByteArrayInputStream(baos.toByteArray());
} catch (IOException | EnforcerRuleException | TransformerConfigurationException
final byte[] bytes = baos.toByteArray();
getLog().info(() -> (CharSequence) ("Rules transformed by " + xsltLocation + " from " + location + ":\n\n"
+ new String(bytes, StandardCharsets.UTF_8)));
return new ByteArrayInputStream(bytes);
} catch (IOException
| EnforcerRuleException
| TransformerConfigurationException
| TransformerFactoryConfigurationError e) {
throw new RuntimeException("Could not open resource " + xsltLocation);
} catch (TransformerException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ void shouldLoadRulesFromClassPath() throws EnforcerRuleException {
assertEquals(2, rulesConfig.getChildCount());
}


@Test
void shouldFilterRules() throws EnforcerRuleException {
MojoDescriptor mojoDescriptor = new MojoDescriptor();
Expand All @@ -116,6 +115,8 @@ void shouldFilterRules() throws EnforcerRuleException {
assertEquals("excludes", rulesConfig.getChild(0).getChild(0).getName());
assertEquals(1, rulesConfig.getChild(0).getChild(0).getChildCount());
assertEquals("exclude", rulesConfig.getChild(0).getChild(0).getChild(0).getName());
assertEquals("com.google.guava:listenablefuture", rulesConfig.getChild(0).getChild(0).getChild(0).getValue());
assertEquals(
"com.google.guava:listenablefuture",
rulesConfig.getChild(0).getChild(0).getChild(0).getValue());
}
}

0 comments on commit 3a7e286

Please sign in to comment.