|
| 1 | +/* |
| 2 | + * Copyright 2024 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.spotless.rdf; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.lang.reflect.InvocationTargetException; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +import com.diffplug.spotless.FormatterFunc; |
| 26 | +import com.diffplug.spotless.LineEnding; |
| 27 | + |
| 28 | +public class RdfFormatterFunc implements FormatterFunc { |
| 29 | + private static final Set<String> TURTLE_EXTENSIONS = Set.of("ttl", "turtle"); |
| 30 | + private static final Set<String> TRIG_EXTENSIONS = Set.of("trig"); |
| 31 | + private static final Set<String> NTRIPLES_EXTENSIONS = Set.of("n-triples", "ntriples", "nt"); |
| 32 | + private static final Set<String> NQUADS_EXTENSIONS = Set.of("n-quads", "nquads", "nq"); |
| 33 | + |
| 34 | + private final RdfFormatterStep.State state; |
| 35 | + private final ReflectionHelper reflectionHelper; |
| 36 | + |
| 37 | + public RdfFormatterFunc(RdfFormatterStep.State state) |
| 38 | + throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 39 | + this.state = state; |
| 40 | + this.reflectionHelper = new ReflectionHelper(state); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String apply(String input) throws Exception { |
| 45 | + throw new UnsupportedOperationException("We need to know the filename so we can guess the RDF format. Use apply(String, File) instead!"); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String apply(String rawUnix, File file) throws Exception { |
| 50 | + String filename = file.getName().toLowerCase(Locale.US); |
| 51 | + int lastDot = filename.lastIndexOf('.'); |
| 52 | + if (lastDot < 0) { |
| 53 | + throw new IllegalArgumentException( |
| 54 | + String.format("File %s has no file extension, cannot determine RDF format", file.getAbsolutePath())); |
| 55 | + } |
| 56 | + if (lastDot + 1 >= filename.length()) { |
| 57 | + throw new IllegalArgumentException( |
| 58 | + String.format("File %s has no file extension, cannot determine RDF format", file.getAbsolutePath())); |
| 59 | + } |
| 60 | + String extension = filename.substring(lastDot + 1); |
| 61 | + |
| 62 | + try { |
| 63 | + if (TURTLE_EXTENSIONS.contains(extension)) { |
| 64 | + return formatTurtle(rawUnix, file, reflectionHelper); |
| 65 | + } |
| 66 | + if (TRIG_EXTENSIONS.contains(extension)) { |
| 67 | + return formatTrig(rawUnix, file); |
| 68 | + } |
| 69 | + if (NTRIPLES_EXTENSIONS.contains(extension)) { |
| 70 | + return formatNTriples(rawUnix, file); |
| 71 | + } |
| 72 | + if (NQUADS_EXTENSIONS.contains(extension)) { |
| 73 | + return formatNQuads(rawUnix, file); |
| 74 | + } |
| 75 | + throw new IllegalArgumentException(String.format("Cannot handle file with extension %s", extension)); |
| 76 | + } catch (InvocationTargetException e) { |
| 77 | + throw new RuntimeException("Error formatting file " + file.getPath(), e.getCause()); |
| 78 | + } catch (Exception e) { |
| 79 | + throw new RuntimeException("Error formatting file " + file.getPath(), e); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private String formatNQuads(String rawUnix, File file) { |
| 84 | + throw new UnsupportedOperationException("NQUADS formatting not supported yet"); |
| 85 | + } |
| 86 | + |
| 87 | + private String formatNTriples(String rawUnix, File file) { |
| 88 | + throw new UnsupportedOperationException("NTRIPLES formatting not supported yet"); |
| 89 | + } |
| 90 | + |
| 91 | + private String formatTrig(String rawUnix, File file) { |
| 92 | + throw new UnsupportedOperationException("TRIG formatting not supported yet"); |
| 93 | + } |
| 94 | + |
| 95 | + private String formatTurtle(String rawUnix, File file, ReflectionHelper reflectionHelper) |
| 96 | + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, |
| 97 | + NoSuchFieldException, InstantiationException { |
| 98 | + String formatted; |
| 99 | + Object lang = reflectionHelper.getLang("TTL"); |
| 100 | + formatted = reflectionHelper.formatWithTurtleFormatter(rawUnix); |
| 101 | + if (state.getConfig().isVerify()) { |
| 102 | + veryfyResult(rawUnix, file, reflectionHelper, lang, formatted); |
| 103 | + } |
| 104 | + return LineEnding.toUnix(formatted); |
| 105 | + } |
| 106 | + |
| 107 | + private static void veryfyResult(String rawUnix, File file, ReflectionHelper reflectionHelper, Object lang, |
| 108 | + String formatted) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 109 | + Object modelBefore = reflectionHelper.parseToModel(rawUnix, file, lang); |
| 110 | + Object modelAfter = reflectionHelper.parseToModel(formatted, file, lang); |
| 111 | + if (!reflectionHelper.areModelsIsomorphic(modelBefore, modelAfter)) { |
| 112 | + long beforeSize = reflectionHelper.modelSize(modelBefore); |
| 113 | + long afterSize = reflectionHelper.modelSize(modelAfter); |
| 114 | + String diffResult; |
| 115 | + if (beforeSize != afterSize) { |
| 116 | + diffResult = String.format("< %,d triples", beforeSize); |
| 117 | + diffResult += String.format("> %,d triples", afterSize); |
| 118 | + } else { |
| 119 | + diffResult = calculateDiff(reflectionHelper, modelBefore, modelAfter); |
| 120 | + } |
| 121 | + throw new IllegalStateException( |
| 122 | + "Formatted RDF is not isomorphic with original, which means that formatting changed the data.\n" |
| 123 | + + "This could be a bug in the formatting system leading to data corruption and should be reported. \n" |
| 124 | + + "If you are not scared to lose data, you can disable this check by setting the config option 'verify' to 'false'" |
| 125 | + + "\n\nDiff:\n" |
| 126 | + + diffResult); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static String calculateDiff(ReflectionHelper reflectionHelper, Object modelBefore, Object modelAfter) |
| 131 | + throws InvocationTargetException, IllegalAccessException { |
| 132 | + String diffResult; |
| 133 | + Object graphBefore = reflectionHelper.getGraph(modelBefore); |
| 134 | + Object graphAfter = reflectionHelper.getGraph(modelAfter); |
| 135 | + |
| 136 | + List<Object> onlyInBeforeContent = reflectionHelper.streamGraph(graphBefore) |
| 137 | + .filter(triple -> { |
| 138 | + try { |
| 139 | + return !reflectionHelper.graphContainsSameTerm(graphAfter, triple); |
| 140 | + } catch (InvocationTargetException | IllegalAccessException e) { |
| 141 | + throw new RuntimeException(e); |
| 142 | + } |
| 143 | + }) |
| 144 | + .collect(Collectors.toList()); |
| 145 | + |
| 146 | + List<Object> onlyInAfterContent = reflectionHelper.streamGraph(graphAfter) |
| 147 | + .filter(triple -> { |
| 148 | + try { |
| 149 | + return !reflectionHelper.graphContainsSameTerm(graphBefore, triple); |
| 150 | + } catch (InvocationTargetException | IllegalAccessException e) { |
| 151 | + throw new RuntimeException(e); |
| 152 | + } |
| 153 | + }) |
| 154 | + .collect(Collectors.toList()); |
| 155 | + if (!(onlyInBeforeContent.isEmpty() && onlyInAfterContent.isEmpty())) { |
| 156 | + diffResult = onlyInBeforeContent.stream().map(s -> String.format("< %s", s)) |
| 157 | + .collect(Collectors.joining("\n")); |
| 158 | + diffResult += "\n" + onlyInAfterContent.stream().map(s -> String.format("> %s", s)).collect(Collectors.joining("\n")); |
| 159 | + } else { |
| 160 | + diffResult = "'before' and 'after' content differs, but we don't know why. This is probably a bug."; |
| 161 | + } |
| 162 | + return diffResult; |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments