Skip to content

Commit

Permalink
FindStrutsActions
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Mar 1, 2024
1 parent 5951d2f commit f9811c9
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@
@NonNullFields
package org.openrewrite.java.struts;

// We annotate the package to indicate that fields and methods in this package are non-null by default.
import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.struts.search;

import org.openrewrite.*;
import org.openrewrite.java.struts.table.StrutsActions;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.xml.XPathMatcher;
import org.openrewrite.xml.XmlIsoVisitor;
import org.openrewrite.xml.tree.Xml;

import java.util.stream.Collectors;

public class FindStrutsActions extends Recipe {
private final transient StrutsActions actions = new StrutsActions(this);

@Override
public String getDisplayName() {
return "Find Struts actions";
}

@Override
public String getDescription() {
return "Find actions and their associated definitions.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
XPathMatcher actionMatcher = new XPathMatcher("//action");
return Preconditions.check(new FindSourceFiles("**/struts.xml"), new XmlIsoVisitor<ExecutionContext>() {
@Override
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (actionMatcher.matches(getCursor())) {
String pkg = getCursor().getPathAsStream(Xml.Tag.class::isInstance)
.map(Xml.Tag.class::cast)
.filter(t -> t.getName().equals("package"))
.map(t -> getAttribute(t, "name", ""))
.collect(Collectors.joining("."));
actions.insertRow(ctx, new StrutsActions.Row(
getCursor().firstEnclosingOrThrow(SourceFile.class).getSourcePath().toString(),
pkg,
getAttribute(tag, "name", "unknown"),
getAttribute(tag, "class", "unknown"),
getAttribute(tag, "method", "unknown")));
return SearchResult.found(tag);
}
return super.visitTag(tag, ctx);
}
});
}

private String getAttribute(Xml.Tag tag, String name, String defaultValue) {
return tag.getAttributes().stream()
.filter(a -> a.getKey().getName().equals(name))
.findFirst()
.map(a -> a.getValue().getValue())
.orElse(defaultValue);
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/openrewrite/java/struts/search/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@NonNullApi
@NonNullFields
package org.openrewrite.java.struts.search;

import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
55 changes: 55 additions & 0 deletions src/main/java/org/openrewrite/java/struts/table/StrutsActions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.struts.table;

import com.fasterxml.jackson.annotation.JsonIgnoreType;
import lombok.Value;
import org.openrewrite.Column;
import org.openrewrite.DataTable;
import org.openrewrite.Recipe;

@JsonIgnoreType
public class StrutsActions extends DataTable<StrutsActions.Row> {

public StrutsActions(Recipe recipe) {
super(recipe,
"Struts actions",
"Definition of struts action.");
}

@Value
public static class Row {
@Column(displayName = "Source file",
description = "The source file that the action is defined in.")
String sourceFile;

@Column(displayName = "Package",
description = "The package of the action.")
String pkg;

@Column(displayName = "Action name",
description = "The name of the action.")
String name;

@Column(displayName = "Class",
description = "The action class.")
String className;

@Column(displayName = "Method name",
description = "The method name of the action method.")
String methodName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.struts.search;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.srcMainResources;
import static org.openrewrite.xml.Assertions.xml;

public class FindStrutsActionTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new FindStrutsActions());
}

@Test
void findActions() {
rewriteRun(
srcMainResources(
xml(
//language=xml
"""
<struts>
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
""",
//language=xml
"""
<struts>
<package name="basicstruts2" extends="struts-default">
<!--~~>--><action name="index">
<result>/index.jsp</result>
</action>
<!--~~>--><action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
""",
spec -> spec.path("struts.xml")
)
)
);
}
}

0 comments on commit f9811c9

Please sign in to comment.