All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openrewrite.maven.AddPropertyVisitor Maven / Gradle / Ivy

There is a newer version: 8.40.3
Show newest version
/*
 * Copyright 2022 the original author or authors.
 * 

* 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 *

* https://www.apache.org/licenses/LICENSE-2.0 *

* 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.maven; import lombok.EqualsAndHashCode; import lombok.Value; import org.jspecify.annotations.Nullable; import org.openrewrite.ExecutionContext; import org.openrewrite.xml.AddToTagVisitor; import org.openrewrite.xml.ChangeTagValueVisitor; import org.openrewrite.xml.TagNameComparator; import org.openrewrite.xml.tree.Xml; import java.util.Optional; @Value @EqualsAndHashCode(callSuper = false) public class AddPropertyVisitor extends MavenIsoVisitor { String key; String value; @Nullable Boolean preserveExistingValue; @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { Xml.Document d = super.visitDocument(document, ctx); Xml.Tag root = d.getRoot(); Optional properties = root.getChild("properties"); if (!properties.isPresent()) { Xml.Tag propertiesTag = Xml.Tag.build("\n<" + key + ">" + value + "\n"); d = (Xml.Document) new AddToTagVisitor(root, propertiesTag, new MavenTagInsertionComparator(root.getChildren())).visitNonNull(d, ctx); } else if (!properties.get().getChildValue(key).isPresent()) { Xml.Tag propertyTag = Xml.Tag.build("<" + key + ">" + value + ""); d = (Xml.Document) new AddToTagVisitor<>(properties.get(), propertyTag, new TagNameComparator()).visitNonNull(d, ctx); } if (d != document) { maybeUpdateModel(); } return d; } @Override public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { if (!Boolean.TRUE.equals(preserveExistingValue) && isPropertyTag() && key.equals(tag.getName()) && !value.equals(tag.getValue().orElse(null))) { doAfterVisit(new ChangeTagValueVisitor<>(tag, value)); } return super.visitTag(tag, ctx); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy