@@ -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 }
@@ -293,29 +290,29 @@ private static List<String> extractSchemaLocations(byte[] xsdBytes) {
293290 List <String > locations = new ArrayList <>();
294291 XMLStreamReader reader = null ;
295292 try {
296- reader = getXMLInputFactory ().createXMLStreamReader (new ByteArrayInputStream (xsdBytes ));
297- while (reader .hasNext ()) {
298- int event = reader .next ();
299- if (event == XMLStreamConstants .START_ELEMENT
300- && XMLConstants .W3C_XML_SCHEMA_NS_URI .equals (reader .getNamespaceURI ())
301- && ("import" .equals (reader .getLocalName ()))) {
302- String schemaLocation = reader .getAttributeValue (null , "schemaLocation" );
303- if (schemaLocation != null && !schemaLocation .isBlank () && ALLOWED_IIDM_XSDS .contains (schemaLocation )) {
304- locations .add (schemaLocation );
293+ try (ByteArrayInputStream in = new ByteArrayInputStream (xsdBytes )) {
294+ reader = getXMLInputFactory ().createXMLStreamReader (in );
295+ while (reader .hasNext ()) {
296+ int event = reader .next ();
297+ if (event == XMLStreamConstants .START_ELEMENT
298+ && XMLConstants .W3C_XML_SCHEMA_NS_URI .equals (reader .getNamespaceURI ())
299+ && ("import" .equals (reader .getLocalName ()))) {
300+ String schemaLocation = reader .getAttributeValue (null , "schemaLocation" );
301+ if (schemaLocation != null && !schemaLocation .isBlank () && ALLOWED_IIDM_XSDS .contains (schemaLocation )) {
302+ locations .add (schemaLocation );
303+ }
305304 }
306305 }
307- }
308- return locations ;
309- } catch (XMLStreamException e ) {
310- throw new PowsyblException ("Failed to parse XSD schema" , e );
311- } finally {
312- if (reader != null ) {
313- try {
306+ return locations ;
307+ } catch (XMLStreamException | IOException e ) {
308+ throw new PowsyblException ("Failed to parse XSD schema" , e );
309+ } finally {
310+ if (reader != null ) {
314311 reader .close ();
315- } catch (XMLStreamException e ) {
316- LOGGER .error (e .toString (), e );
317312 }
318313 }
314+ } catch (XMLStreamException e ) {
315+ throw new UncheckedXmlStreamException (e );
319316 }
320317 }
321318
@@ -328,30 +325,30 @@ private static List<String> extractSchemaLocations(byte[] xsdBytes) {
328325 private static String readRootNamespace (byte [] xmlBytes ) {
329326 XMLStreamReader reader = null ;
330327 try {
331- reader = getXMLInputFactory ().createXMLStreamReader (new ByteArrayInputStream (xmlBytes ));
332- while (reader .hasNext ()) {
333- if (reader .next () == XMLStreamConstants .START_ELEMENT ) {
334- if (!NETWORK_ROOT_ELEMENT_NAME .equals (reader .getLocalName ())) {
335- throw new PowsyblException ("Unexpected root element: " + reader .getLocalName ());
328+ try (ByteArrayInputStream in = new ByteArrayInputStream (xmlBytes )) {
329+ reader = getXMLInputFactory ().createXMLStreamReader (in );
330+ while (reader .hasNext ()) {
331+ if (reader .next () == XMLStreamConstants .START_ELEMENT ) {
332+ if (!NETWORK_ROOT_ELEMENT_NAME .equals (reader .getLocalName ())) {
333+ throw new PowsyblException ("Unexpected root element: " + reader .getLocalName ());
334+ }
335+ String ns = reader .getNamespaceURI ();
336+ if (ns == null || ns .isBlank ()) {
337+ throw new PowsyblException ("Missing root namespace" );
338+ }
339+ return ns ;
336340 }
337- String ns = reader .getNamespaceURI ();
338- if (ns == null || ns .isBlank ()) {
339- throw new PowsyblException ("Missing root namespace" );
340- }
341- return ns ;
342341 }
343- }
344- throw new PowsyblException ("Missing root namespace" );
345- } catch (XMLStreamException e ) {
346- throw new PowsyblException ("Failed to read namespace from XML" , e );
347- } finally {
348- if (reader != null ) {
349- try {
342+ throw new PowsyblException ("Missing root namespace" );
343+ } catch (XMLStreamException | IOException e ) {
344+ throw new PowsyblException ("Failed to read namespace from XML" , e );
345+ } finally {
346+ if (reader != null ) {
350347 reader .close ();
351- } catch (XMLStreamException e ) {
352- LOGGER .error (e .toString (), e );
353348 }
354349 }
350+ } catch (XMLStreamException e ) {
351+ throw new UncheckedXmlStreamException (e );
355352 }
356353 }
357354
0 commit comments