|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.metadata.schema; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.UncheckedIOException; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Comparator; |
| 25 | +import java.util.HashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.regex.Pattern; |
| 29 | + |
| 30 | +/** |
| 31 | + * Registry-driven lint: classifies a metadata key against the committed registries rather than a |
| 32 | + * hand-coded rule. A key is legitimate iff it is an enumerated closed key |
| 33 | + * ({@code metadata-keys.json}), sits under a registered passthrough prefix |
| 34 | + * ({@code metadata-open-namespaces.json}), or is a documented template instance. Anything else is |
| 35 | + * {@link Classification#UNKNOWN} — a typo, an unregistered namespace, or a key someone forgot to |
| 36 | + * declare. |
| 37 | + * |
| 38 | + * <p>Reads the JSON snapshots (which {@code MetadataSchemaTest} gates against the live declarations), |
| 39 | + * so it needs no parser classes loaded and stays dependency-free. |
| 40 | + */ |
| 41 | +public final class MetadataKeyValidator { |
| 42 | + |
| 43 | + public enum Classification { CLOSED, OPEN, TEMPLATE, UNKNOWN } |
| 44 | + |
| 45 | + private static final String KEYS = "/org/apache/tika/metadata/metadata-keys.json"; |
| 46 | + private static final String OPEN = "/org/apache/tika/metadata/metadata-open-namespaces.json"; |
| 47 | + // Legacy closed keys declared as bare String constants (not Property), so absent from KEYS. |
| 48 | + private static final String STRING_KEYS = "/org/apache/tika/metadata/metadata-string-keys.json"; |
| 49 | + |
| 50 | + // Conservative BCP-47 subset for the XMP lang-alt template suffix (<closed-key>:<lang>). |
| 51 | + private static final Pattern LANG_TAG = |
| 52 | + Pattern.compile("x-default|[A-Za-z]{2,3}(-[A-Za-z0-9]{1,8})*"); |
| 53 | + |
| 54 | + private final Set<String> closedKeys; |
| 55 | + private final List<String> openPrefixes; // longest-first: most specific prefix wins |
| 56 | + |
| 57 | + MetadataKeyValidator(Set<String> closedKeys, List<String> openPrefixes) { |
| 58 | + this.closedKeys = Set.copyOf(closedKeys); |
| 59 | + List<String> sorted = new ArrayList<>(openPrefixes); |
| 60 | + sorted.sort(Comparator.comparingInt(String::length).reversed()); |
| 61 | + this.openPrefixes = List.copyOf(sorted); |
| 62 | + } |
| 63 | + |
| 64 | + /** Loads the validator from the committed registries on the classpath. */ |
| 65 | + public static MetadataKeyValidator fromClasspath() { |
| 66 | + Set<String> closed = new HashSet<>(readValues(KEYS, "key")); |
| 67 | + closed.addAll(readValues(STRING_KEYS, "key")); |
| 68 | + return new MetadataKeyValidator(closed, readValues(OPEN, "prefix")); |
| 69 | + } |
| 70 | + |
| 71 | + public Classification classify(String key) { |
| 72 | + if (key == null || key.isEmpty()) { |
| 73 | + return Classification.UNKNOWN; |
| 74 | + } |
| 75 | + if (closedKeys.contains(key)) { |
| 76 | + return Classification.CLOSED; |
| 77 | + } |
| 78 | + for (String prefix : openPrefixes) { |
| 79 | + if (key.length() > prefix.length() && key.startsWith(prefix)) { |
| 80 | + return Classification.OPEN; |
| 81 | + } |
| 82 | + } |
| 83 | + if (isLangAltInstance(key)) { |
| 84 | + return Classification.TEMPLATE; |
| 85 | + } |
| 86 | + return Classification.UNKNOWN; |
| 87 | + } |
| 88 | + |
| 89 | + public boolean isLegitimate(String key) { |
| 90 | + return classify(key) != Classification.UNKNOWN; |
| 91 | + } |
| 92 | + |
| 93 | + /** The unregistered keys among {@code names}, in encounter order. */ |
| 94 | + public List<String> findUnknown(Iterable<String> names) { |
| 95 | + List<String> unknown = new ArrayList<>(); |
| 96 | + for (String name : names) { |
| 97 | + if (classify(name) == Classification.UNKNOWN) { |
| 98 | + unknown.add(name); |
| 99 | + } |
| 100 | + } |
| 101 | + return unknown; |
| 102 | + } |
| 103 | + |
| 104 | + /** {@code <closed-key>:<lang>} — the XMP rdf:Alt language variants (dc:title:fr, dc:title:x-default). */ |
| 105 | + private boolean isLangAltInstance(String key) { |
| 106 | + int i = key.lastIndexOf(':'); |
| 107 | + if (i <= 0 || i == key.length() - 1) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + return closedKeys.contains(key.substring(0, i)) && LANG_TAG.matcher(key.substring(i + 1)).matches(); |
| 111 | + } |
| 112 | + |
| 113 | + private static List<String> readValues(String resource, String field) { |
| 114 | + String json = readResource(resource); |
| 115 | + List<String> out = new ArrayList<>(); |
| 116 | + String marker = '"' + field + "\":\""; |
| 117 | + int i = 0; |
| 118 | + while ((i = json.indexOf(marker, i)) >= 0) { |
| 119 | + i += marker.length(); |
| 120 | + StringBuilder sb = new StringBuilder(); |
| 121 | + while (i < json.length()) { |
| 122 | + char c = json.charAt(i++); |
| 123 | + if (c == '\\' && i < json.length()) { |
| 124 | + sb.append(json.charAt(i++)); // unescape \" and \\ |
| 125 | + } else if (c == '"') { |
| 126 | + break; |
| 127 | + } else { |
| 128 | + sb.append(c); |
| 129 | + } |
| 130 | + } |
| 131 | + out.add(sb.toString()); |
| 132 | + } |
| 133 | + return out; |
| 134 | + } |
| 135 | + |
| 136 | + private static String readResource(String resource) { |
| 137 | + try (InputStream in = MetadataKeyValidator.class.getResourceAsStream(resource)) { |
| 138 | + if (in == null) { |
| 139 | + throw new IllegalStateException("missing registry resource " + resource); |
| 140 | + } |
| 141 | + return new String(in.readAllBytes(), StandardCharsets.UTF_8); |
| 142 | + } catch (IOException e) { |
| 143 | + throw new UncheckedIOException(e); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments