generated from moderneinc/rewrite-recipe-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b113bd
commit 16ebdda
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/org/openrewrite/java/struts/MigrateStrutsDtd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.*; | ||
import org.openrewrite.internal.ListUtils; | ||
import org.openrewrite.java.struts.search.FindStrutsXml; | ||
import org.openrewrite.xml.XmlIsoVisitor; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class MigrateStrutsDtd extends Recipe { | ||
|
||
@Option(displayName = "Struts version", | ||
description = "The Struts version to migrate to.", | ||
example = "6.0", | ||
valid = "2.3, 2.5, 6.0") | ||
String strutsVersion; | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Migrate DTD to a specific Struts version"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Update Struts DTD to reflect the specified version."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check(new FindStrutsXml(), new XmlIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public Xml.DocTypeDecl visitDocTypeDecl(Xml.DocTypeDecl docTypeDecl, ExecutionContext ctx) { | ||
return docTypeDecl.withInternalSubset(ListUtils.map(docTypeDecl.getInternalSubset(), (n, ref) -> { | ||
if (n == 0 && !ref.getName().equals("\"-//Apache Software Foundation//DTD Struts Configuration " + strutsVersion + "//EN\"")) { | ||
return ref.withName("\"-//Apache Software Foundation//DTD Struts Configuration " + strutsVersion + "//EN\""); | ||
} else if (n == 1 && !ref.getName().equals("\"https://struts.apache.org/dtds/struts-" + strutsVersion + ".dtd\"")) { | ||
return ref.withName("\"https://struts.apache.org/dtds/struts-" + strutsVersion + ".dtd\""); | ||
} | ||
return ref; | ||
})); | ||
} | ||
}); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/org/openrewrite/java/struts/MigrateStrutsDtdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.xml.Assertions.xml; | ||
|
||
public class MigrateStrutsDtdTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new MigrateStrutsDtd("6.0")); | ||
} | ||
|
||
@Test | ||
void updateStrutsDtdFrom2_5To6() { | ||
rewriteRun( | ||
//language=xml | ||
xml( | ||
""" | ||
<!DOCTYPE struts PUBLIC | ||
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" | ||
"http://struts.apache.org/dtds/struts-2.5.dtd"> | ||
<struts/> | ||
""", | ||
""" | ||
<!DOCTYPE struts PUBLIC | ||
"-//Apache Software Foundation//DTD Struts Configuration 6.0//EN" | ||
"https://struts.apache.org/dtds/struts-6.0.dtd"> | ||
<struts/> | ||
""" | ||
) | ||
); | ||
} | ||
} |