Coverage Report - org.jtheque.core.utils.file.XMLUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLUtils
0 %
0/22
0 %
0/4
3.5
 
 1  
 package org.jtheque.core.utils.file;
 2  
 
 3  
 import org.jdom.Document;
 4  
 import org.jdom.output.Format;
 5  
 import org.jdom.output.XMLOutputter;
 6  
 import org.jtheque.core.managers.Managers;
 7  
 import org.jtheque.core.managers.log.ILoggingManager;
 8  
 import org.jtheque.utils.io.FileUtils;
 9  
 
 10  
 import java.io.File;
 11  
 import java.io.FileNotFoundException;
 12  
 import java.io.FileOutputStream;
 13  
 import java.io.IOException;
 14  
 import java.io.OutputStream;
 15  
 
 16  
 /*
 17  
  * This file is part of JTheque.
 18  
  *
 19  
  * JTheque is free software: you can redistribute it and/or modify
 20  
  * it under the terms of the GNU General Public License as published by
 21  
  * the Free Software Foundation, either version 3 of the License.
 22  
  *
 23  
  * JTheque is distributed in the hope that it will be useful,
 24  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 25  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 26  
  * GNU General Public License for more details.
 27  
  *
 28  
  * You should have received a copy of the GNU General Public License
 29  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 30  
  */
 31  
 
 32  
 /**
 33  
  * An utility class for XML.
 34  
  *
 35  
  * @author Baptiste Wicht
 36  
  */
 37  
 public final class XMLUtils {
 38  
     /**
 39  
      * This is an utility class, not instanciable.
 40  
      */
 41  
     private XMLUtils() {
 42  0
         super();
 43  0
     }
 44  
 
 45  
     /**
 46  
      * Save a XML file.
 47  
      *
 48  
      * @param doc  The XML document to save.
 49  
      * @param path The path to the file.
 50  
      */
 51  
     public static void writeXml(Document doc, String path) {
 52  0
         XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
 53  
 
 54  0
         boolean fileOk = true;
 55  
 
 56  0
         File f = new File(path);
 57  
 
 58  0
         if (!f.exists()) {
 59  
             try {
 60  0
                 fileOk = f.createNewFile();
 61  0
             } catch (IOException e) {
 62  0
                 fileOk = false;
 63  0
                 Managers.getManager(ILoggingManager.class).getLogger(XMLUtils.class).error(e);
 64  0
             }
 65  
         }
 66  
 
 67  0
         if (fileOk) {
 68  0
             OutputStream stream = null;
 69  
             try {
 70  0
                 stream = new FileOutputStream(path);
 71  
 
 72  0
                 sortie.output(doc, stream);
 73  0
             } catch (FileNotFoundException e) {
 74  0
                 Managers.getManager(ILoggingManager.class).getLogger(XMLUtils.class).error(e);
 75  0
             } catch (IOException e) {
 76  0
                 Managers.getManager(ILoggingManager.class).getLogger(XMLUtils.class).error(e);
 77  
             } finally {
 78  0
                 FileUtils.close(stream);
 79  0
             }
 80  
         }
 81  0
     }
 82  
 }