-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Separating and clearer naming * Add license header * Review feedback
- Loading branch information
Showing
4 changed files
with
75 additions
and
50 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
rewrite-xml/src/main/java/org/openrewrite/xml/trait/XmlReference.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,64 @@ | ||
/* | ||
* 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.xml.trait; | ||
|
||
import org.openrewrite.Cursor; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Tree; | ||
import org.openrewrite.trait.Reference; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
public abstract class XmlReference implements Reference { | ||
|
||
@Override | ||
public Tree getTree() { | ||
return Reference.super.getTree(); | ||
} | ||
|
||
@Override | ||
public String getValue() { | ||
if (getTree() instanceof Xml.Attribute) { | ||
Xml.Attribute attribute = (Xml.Attribute) getTree(); | ||
return attribute.getValueAsString(); | ||
} else if (getTree() instanceof Xml.Tag) { | ||
Xml.Tag tag = (Xml.Tag) getTree(); | ||
if (tag.getValue().isPresent()) { | ||
return tag.getValue().get(); | ||
} | ||
} | ||
throw new IllegalArgumentException("getTree() must be an Xml.Attribute or Xml.Tag: " + getTree().getClass()); | ||
} | ||
|
||
@Override | ||
public boolean supportsRename() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public Tree rename(Renamer renamer, Cursor cursor, ExecutionContext ctx) { | ||
Tree tree = cursor.getValue(); | ||
if (tree instanceof Xml.Attribute) { | ||
Xml.Attribute attribute = (Xml.Attribute) tree; | ||
String renamed = renamer.rename(this); | ||
return attribute.withValue(attribute.getValue().withValue(renamed)); | ||
} else if (tree instanceof Xml.Tag && ((Xml.Tag) tree).getValue().isPresent()) { | ||
String renamed = renamer.rename(this); | ||
return ((Xml.Tag) tree).withValue(renamed); | ||
} | ||
return tree; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
rewrite-xml/src/main/resources/META-INF/services/org.openrewrite.trait.Reference$Provider
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 |
---|---|---|
@@ -1 +1 @@ | ||
org.openrewrite.xml.trait.SpringReference$Provider | ||
org.openrewrite.xml.trait.SpringXmlReference$Provider |
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