com.google.code.maven_replacer_plugin.JsonPathReplacer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of replacer Show documentation
Show all versions of replacer Show documentation
Maven plugin to replace tokens in a given file with a value
The newest version!
/*
Copyright (c) 2019 Isaias Arellano - [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.google.code.maven_replacer_plugin;
import com.google.gson.*;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ReadContext;
import java.util.*;
public class JsonPathReplacer implements Replacer {
private final TokenReplacer tokenReplacer;
private final Configuration jsonConfig;
private final Gson gson;
public JsonPathReplacer(TokenReplacer tokenReplacer) {
try {
if (tokenReplacer == null) {
throw new IllegalArgumentException("Must supply a tokenReplacer to change the node's content.");
}
this.tokenReplacer = tokenReplacer;
jsonConfig = Configuration.builder()
.options(Option.AS_PATH_LIST).build();
gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
} catch (Exception e) {
throw new IllegalStateException("Unable to initialise JSON processing: " + e.getMessage(), e);
}
}
public String replace(String content, Replacement replacement, boolean regex, int regexFlags) {
try {
ReadContext doc = parseJson(content);
List replacementTargets = findReplacementNodes(doc, replacement.getJsonpath());
if (replacementTargets.size() > 0) {
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(content);
replaceContent(replacementTargets, replacement, regex, regexFlags, element);
return gson.toJson(element);
}
return content;
} catch (Exception e) {
String cause = e.getMessage() != null ? e.getMessage() : e.getCause().getMessage();
throw new RuntimeException("Error during JSON replacement: " + cause, e);
}
}
List