001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.file.xml;
020
021import icy.util.XMLUtil;
022
023import java.io.File;
024import java.net.URL;
025
026import org.w3c.dom.Document;
027
028/**
029 * @author Stephane
030 */
031public class XMLPersistentHelper
032{
033    public static boolean loadFromXML(XMLPersistent persistent, String path)
034    {
035        return loadFromXML(persistent, XMLUtil.loadDocument(path, false));
036    }
037
038    public static boolean loadFromXML(XMLPersistent persistent, File file)
039    {
040        return loadFromXML(persistent, XMLUtil.loadDocument(file, false));
041    }
042
043    public static boolean loadFromXML(XMLPersistent persistent, URL xmlUrl)
044    {
045        return loadFromXML(persistent, XMLUtil.loadDocument(xmlUrl, false));
046    }
047
048    public static boolean loadFromXML(XMLPersistent persistent, Document document)
049    {
050        if (document == null)
051            return false;
052
053        // TODO: check if we can really avoid it (it slowdown loading process)
054        // document.normalizeDocument();
055
056        // load from root node
057        return persistent.loadFromXML(document.getDocumentElement());
058    }
059
060    public static boolean saveToXML(XMLPersistent persistent, String path)
061    {
062        final Document document = XMLUtil.createDocument(true);
063
064        if (saveToXML(persistent, document))
065            return XMLUtil.saveDocument(document, path);
066
067        return false;
068    }
069
070    public static boolean saveToXML(XMLPersistent persistent, File file)
071    {
072        final Document document = XMLUtil.createDocument(true);
073
074        if (saveToXML(persistent, document))
075            return XMLUtil.saveDocument(document, file);
076
077        return false;
078    }
079
080    public static boolean saveToXML(XMLPersistent persistent, Document document)
081    {
082        if (document == null)
083            return false;
084
085        // TODO: check if it's ok to do it only on file save operation
086        // document.normalizeDocument();
087
088        // save to root node
089        return persistent.saveToXML(document.getDocumentElement());
090    }
091}