|
56 | 56 | import java.time.ZonedDateTime; |
57 | 57 | import java.time.format.DateTimeFormatter; |
58 | 58 | import java.util.*; |
| 59 | +import java.util.concurrent.ConcurrentHashMap; |
| 60 | +import java.util.concurrent.ConcurrentMap; |
59 | 61 | import java.util.concurrent.ExecutorService; |
60 | 62 | import java.util.concurrent.ForkJoinPool; |
61 | 63 | import java.util.function.Supplier; |
@@ -89,7 +91,9 @@ public final class NetworkSerDe { |
89 | 91 | static final byte[] BIIDM_MAGIC_NUMBER = {0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x49, 0x49, 0x44, 0x4d}; |
90 | 92 |
|
91 | 93 | private static final Supplier<Schema> DEFAULT_SCHEMA_SUPPLIER = Suppliers.memoize(() -> NetworkSerDe.createSchema(DefaultExtensionsSupplier.getInstance())); |
92 | | - private static final Supplier<Map<IidmVersion, Schema>> DEFAULT_SCHEMAS_SUPPLIER = Suppliers.memoize(() -> NetworkSerDe.createDefaultSchemas(DefaultExtensionsSupplier.getInstance())); |
| 94 | + private static final Supplier<ConcurrentMap<IidmVersion, Schema>> DEFAULT_SCHEMAS_SUPPLIER = Suppliers.memoize(ConcurrentHashMap::new); |
| 95 | + private static final Supplier<Map<IidmVersion, Schema>> DEFAULT_SCHEMAS_SUPPLIER_V_TEST = Suppliers.memoize(() -> NetworkSerDe.createDefaultSchemas(DefaultExtensionsSupplier.getInstance())); |
| 96 | + |
93 | 97 | private static final int MAX_NAMESPACE_PREFIX_NUM = 100; |
94 | 98 | private static final String XSD_RESOURCE_DIR = "/xsd/"; |
95 | 99 |
|
@@ -177,7 +181,7 @@ private static void validate(InputStream is, IidmVersion version, ExtensionsSupp |
177 | 181 | // XSD validation |
178 | 182 | Schema schema; |
179 | 183 | if (extensionsSupplier == DefaultExtensionsSupplier.getInstance()) { |
180 | | - schema = DEFAULT_SCHEMAS_SUPPLIER.get().get(version); |
| 184 | + schema = DEFAULT_SCHEMAS_SUPPLIER.get().computeIfAbsent(version, v -> createSchema(DefaultExtensionsSupplier.getInstance(), v)); |
181 | 185 | if (schema == null) { |
182 | 186 | throw new PowsyblException("Schema not found: version=" + version); |
183 | 187 | } |
@@ -301,7 +305,7 @@ private static List<String> extractSchemaLocations(byte[] xsdBytes) { |
301 | 305 | && XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(reader.getNamespaceURI()) |
302 | 306 | && ("import".equals(reader.getLocalName()))) { |
303 | 307 | String schemaLocation = reader.getAttributeValue(null, "schemaLocation"); |
304 | | - if (schemaLocation != null && !schemaLocation.isBlank()) { |
| 308 | + if (schemaLocation != null && !schemaLocation.isBlank() && isValidXsdFile(schemaLocation)) { |
305 | 309 | locations.add(schemaLocation); |
306 | 310 | } |
307 | 311 | } |
@@ -340,6 +344,15 @@ private static String readRootNamespace(byte[] xmlBytes) { |
340 | 344 | } |
341 | 345 | } |
342 | 346 |
|
| 347 | + private static boolean isValidXsdFile(String shemaLocation) { |
| 348 | + for (IidmVersion version :IidmVersion.values()) { |
| 349 | + if(shemaLocation.equals("iidm_V" + version.toString("_") + ".xsd")){ |
| 350 | + return true; |
| 351 | + } |
| 352 | + } |
| 353 | + return false; |
| 354 | + } |
| 355 | + |
343 | 356 | private static void throwExceptionIfOption(AbstractOptions<?> options, String message) { |
344 | 357 | if (options.isThrowExceptionIfExtensionNotFound()) { |
345 | 358 | throw new PowsyblException(message); |
|
0 commit comments