View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.dao.xml;
22  
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Node;
25  import org.waarp.common.file.FileUtils;
26  import org.waarp.common.logging.WaarpLogger;
27  import org.waarp.common.logging.WaarpLoggerFactory;
28  
29  import javax.xml.XMLConstants;
30  import javax.xml.transform.OutputKeys;
31  import javax.xml.transform.Transformer;
32  import javax.xml.transform.TransformerException;
33  import javax.xml.transform.TransformerFactory;
34  import javax.xml.transform.dom.DOMSource;
35  import javax.xml.transform.stream.StreamResult;
36  import java.io.File;
37  import java.io.FileNotFoundException;
38  import java.io.FileOutputStream;
39  
40  public final class XMLUtils {
41  
42    private static final WaarpLogger logger =
43        WaarpLoggerFactory.getLogger(XMLUtils.class);
44  
45    private XMLUtils() {
46    }
47  
48    public static Node createNode(final Document document, final String tag,
49                                  final String text) {
50      final Node res = document.createElement(tag);
51      res.setTextContent(text);
52      return res;
53    }
54  
55    public static void writeToFile(final File file, final Document document) {
56      final TransformerFactory factory =//NOSONAR
57          TransformerFactory.newInstance();//NOSONAR
58      FileOutputStream outputStream = null;
59      try {
60        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
61        final Transformer transformer = factory.newTransformer();
62        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
63        final DOMSource domSource = new DOMSource(document);
64        outputStream = new FileOutputStream(file);
65        final StreamResult streamResult = new StreamResult(outputStream);
66        transformer.transform(domSource, streamResult);
67      } catch (final TransformerException e) {
68        logger.error("Error while writing document to file: {}", e.getMessage());
69      } catch (final FileNotFoundException e) {
70        logger.error("Error while writing document to file: {}", e.getMessage());
71      } catch (final Exception e) {
72        logger.error("Error while writing document to file", e);
73      } finally {
74        if (outputStream != null) {
75          FileUtils.close(outputStream);
76        }
77      }
78    }
79  }