@@ -117,19 +117,31 @@ private static String dto2ddi(DatasetDTO datasetDto) throws XMLStreamException {
117117 }
118118
119119 private static void dtoddi (DatasetDTO datasetDto , OutputStream outputStream ) throws XMLStreamException {
120- XMLStreamWriter xmlw = XMLOutputFactory .newInstance ().createXMLStreamWriter (outputStream );
121- xmlw .writeStartElement ("codeBook" );
122- xmlw .writeDefaultNamespace ("ddi:codebook:2_5" );
123- xmlw .writeAttribute ("xmlns:xsi" , "http://www.w3.org/2001/XMLSchema-instance" );
124- xmlw .writeAttribute ("xsi:schemaLocation" , DDIExporter .DEFAULT_XML_NAMESPACE + " " + DDIExporter .DEFAULT_XML_SCHEMALOCATION );
125- xmlw .writeAttribute ("version" , DDIExporter .DEFAULT_XML_VERSION );
126- if (DvObjectContainer .isMetadataLanguageSet (datasetDto .getMetadataLanguage ())) {
127- xmlw .writeAttribute ("xml:lang" , datasetDto .getMetadataLanguage ());
128- }
129- createStdyDscr (xmlw , datasetDto );
130- createOtherMats (xmlw , datasetDto .getDatasetVersion ().getFiles ());
131- xmlw .writeEndElement (); // codeBook
132- xmlw .flush ();
120+ XMLStreamWriter xmlw = null ;
121+ try {
122+ xmlw = XMLOutputFactory .newInstance ().createXMLStreamWriter (outputStream );
123+ xmlw .writeStartElement ("codeBook" );
124+ xmlw .writeDefaultNamespace ("ddi:codebook:2_5" );
125+ xmlw .writeAttribute ("xmlns:xsi" , "http://www.w3.org/2001/XMLSchema-instance" );
126+ xmlw .writeAttribute ("xsi:schemaLocation" , DDIExporter .DEFAULT_XML_NAMESPACE + " " + DDIExporter .DEFAULT_XML_SCHEMALOCATION );
127+ xmlw .writeAttribute ("version" , DDIExporter .DEFAULT_XML_VERSION );
128+ if (DvObjectContainer .isMetadataLanguageSet (datasetDto .getMetadataLanguage ())) {
129+ xmlw .writeAttribute ("xml:lang" , datasetDto .getMetadataLanguage ());
130+ }
131+ createStdyDscr (xmlw , datasetDto );
132+ createOtherMats (xmlw , datasetDto .getDatasetVersion ().getFiles ());
133+ xmlw .writeEndElement (); // codeBook
134+ xmlw .flush ();
135+ } finally {
136+ if (xmlw != null ) {
137+ try {
138+ xmlw .close ();
139+ } catch (XMLStreamException e ) {
140+ // Log this exception, but don't rethrow as it's in finally block
141+ logger .log (Level .WARNING , "Error closing XMLStreamWriter" , e );
142+ }
143+ }
144+ }
133145 }
134146
135147
@@ -139,21 +151,34 @@ public static void datasetJson2ddi(JsonObject datasetDtoAsJson, JsonArray fileDe
139151 Gson gson = new Gson ();
140152 DatasetDTO datasetDto = gson .fromJson (datasetDtoAsJson .toString (), DatasetDTO .class );
141153
142- XMLStreamWriter xmlw = XMLOutputFactory .newInstance ().createXMLStreamWriter (outputStream );
143- xmlw .writeStartElement ("codeBook" );
144- xmlw .writeDefaultNamespace ("ddi:codebook:2_5" );
145- xmlw .writeAttribute ("xmlns:xsi" , "http://www.w3.org/2001/XMLSchema-instance" );
146- xmlw .writeAttribute ("xsi:schemaLocation" , DDIExporter .DEFAULT_XML_NAMESPACE + " " + DDIExporter .DEFAULT_XML_SCHEMALOCATION );
147- xmlw .writeAttribute ("version" , DDIExporter .DEFAULT_XML_VERSION );
148- if (DvObjectContainer .isMetadataLanguageSet (datasetDto .getMetadataLanguage ())) {
149- xmlw .writeAttribute ("xml:lang" , datasetDto .getMetadataLanguage ());
150- }
151- createStdyDscr (xmlw , datasetDto );
152- createFileDscr (xmlw , fileDetails );
153- createDataDscr (xmlw , fileDetails );
154- createOtherMatsFromFileMetadatas (xmlw , fileDetails );
155- xmlw .writeEndElement (); // codeBook
156- xmlw .flush ();
154+ XMLStreamWriter xmlw = null ;
155+ try {
156+ xmlw = XMLOutputFactory .newInstance ().createXMLStreamWriter (outputStream );
157+
158+ xmlw .writeStartElement ("codeBook" );
159+ xmlw .writeDefaultNamespace ("ddi:codebook:2_5" );
160+ xmlw .writeAttribute ("xmlns:xsi" , "http://www.w3.org/2001/XMLSchema-instance" );
161+ xmlw .writeAttribute ("xsi:schemaLocation" , DDIExporter .DEFAULT_XML_NAMESPACE + " " + DDIExporter .DEFAULT_XML_SCHEMALOCATION );
162+ xmlw .writeAttribute ("version" , DDIExporter .DEFAULT_XML_VERSION );
163+ if (DvObjectContainer .isMetadataLanguageSet (datasetDto .getMetadataLanguage ())) {
164+ xmlw .writeAttribute ("xml:lang" , datasetDto .getMetadataLanguage ());
165+ }
166+ createStdyDscr (xmlw , datasetDto );
167+ createFileDscr (xmlw , fileDetails );
168+ createDataDscr (xmlw , fileDetails );
169+ createOtherMatsFromFileMetadatas (xmlw , fileDetails );
170+ xmlw .writeEndElement (); // codeBook
171+ xmlw .flush ();
172+ } finally {
173+ if (xmlw != null ) {
174+ try {
175+ xmlw .close ();
176+ } catch (XMLStreamException e ) {
177+ // Log this exception, but don't rethrow as it's in finally block
178+ logger .log (Level .WARNING , "Error closing XMLStreamWriter" , e );
179+ }
180+ }
181+ }
157182 }
158183
159184 /**
0 commit comments