@@ -168,7 +168,7 @@ private static Schema createSchema(ExtensionsSupplier extensionsSupplier) {
168168 }
169169 }
170170
171- private static void validate (InputStream is , IidmVersion version , ExtensionsSupplier extensionsSupplier ) {
171+ public static void validate (InputStream is , IidmVersion version , ExtensionsSupplier extensionsSupplier ) {
172172 Objects .requireNonNull (is );
173173 Objects .requireNonNull (version );
174174 Objects .requireNonNull (extensionsSupplier );
@@ -185,9 +185,6 @@ private static void validate(InputStream is, IidmVersion version, ExtensionsSupp
185185 Schema schema ;
186186 if (extensionsSupplier == DefaultExtensionsSupplier .getInstance ()) {
187187 schema = DEFAULT_SCHEMAS_SUPPLIER .get ().computeIfAbsent (version , v -> createSchema (DefaultExtensionsSupplier .getInstance (), v ));
188- if (schema == null ) {
189- throw new PowsyblException ("Schema not found: version=" + version );
190- }
191188 } else {
192189 schema = createSchema (extensionsSupplier , version );
193190 }
@@ -290,31 +287,35 @@ private static void checkNamespace(byte[] xmlBytes, IidmVersion validationVersio
290287 * @return schema locations found in {@code xs:import}
291288 */
292289 private static List <String > extractSchemaLocations (byte [] xsdBytes ) {
290+ try {
291+ return proceedExtractSchemaLocations (xsdBytes );
292+ } catch (XMLStreamException e ) {
293+ throw new UncheckedXmlStreamException (e );
294+ }
295+ }
296+
297+ private static List <String > proceedExtractSchemaLocations (byte [] xsdBytes ) throws XMLStreamException {
293298 List <String > locations = new ArrayList <>();
294299 XMLStreamReader reader = null ;
295- try {
296- reader = getXMLInputFactory ().createXMLStreamReader (new ByteArrayInputStream ( xsdBytes ) );
300+ try ( ByteArrayInputStream in = new ByteArrayInputStream ( xsdBytes )) {
301+ reader = getXMLInputFactory ().createXMLStreamReader (in );
297302 while (reader .hasNext ()) {
298303 int event = reader .next ();
299304 if (event == XMLStreamConstants .START_ELEMENT
300305 && XMLConstants .W3C_XML_SCHEMA_NS_URI .equals (reader .getNamespaceURI ())
301- && ( "import" .equals (reader .getLocalName () ))) {
306+ && "import" .equals (reader .getLocalName ())) {
302307 String schemaLocation = reader .getAttributeValue (null , "schemaLocation" );
303308 if (schemaLocation != null && !schemaLocation .isBlank () && ALLOWED_IIDM_XSDS .contains (schemaLocation )) {
304309 locations .add (schemaLocation );
305310 }
306311 }
307312 }
308313 return locations ;
309- } catch (XMLStreamException e ) {
314+ } catch (XMLStreamException | IOException e ) {
310315 throw new PowsyblException ("Failed to parse XSD schema" , e );
311316 } finally {
312317 if (reader != null ) {
313- try {
314- reader .close ();
315- } catch (XMLStreamException e ) {
316- LOGGER .error (e .toString (), e );
317- }
318+ reader .close ();
318319 }
319320 }
320321 }
@@ -326,9 +327,16 @@ private static List<String> extractSchemaLocations(byte[] xsdBytes) {
326327 * @return Namespace URI
327328 */
328329 private static String readRootNamespace (byte [] xmlBytes ) {
329- XMLStreamReader reader = null ;
330330 try {
331- reader = getXMLInputFactory ().createXMLStreamReader (new ByteArrayInputStream (xmlBytes ));
331+ return proceedReadRootNamespace (xmlBytes );
332+ } catch (XMLStreamException e ) {
333+ throw new UncheckedXmlStreamException (e );
334+ }
335+ }
336+ private static String proceedReadRootNamespace (byte [] xmlBytes ) throws XMLStreamException {
337+ XMLStreamReader reader = null ;
338+ try (ByteArrayInputStream in = new ByteArrayInputStream (xmlBytes )) {
339+ reader = getXMLInputFactory ().createXMLStreamReader (in );
332340 while (reader .hasNext ()) {
333341 if (reader .next () == XMLStreamConstants .START_ELEMENT ) {
334342 if (!NETWORK_ROOT_ELEMENT_NAME .equals (reader .getLocalName ())) {
@@ -342,15 +350,11 @@ private static String readRootNamespace(byte[] xmlBytes) {
342350 }
343351 }
344352 throw new PowsyblException ("Missing root namespace" );
345- } catch (XMLStreamException e ) {
353+ } catch (XMLStreamException | IOException e ) {
346354 throw new PowsyblException ("Failed to read namespace from XML" , e );
347355 } finally {
348356 if (reader != null ) {
349- try {
350- reader .close ();
351- } catch (XMLStreamException e ) {
352- LOGGER .error (e .toString (), e );
353- }
357+ reader .close ();
354358 }
355359 }
356360 }
0 commit comments