001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.util;
022    
023    import java.io.StringReader;
024    import java.io.StringWriter;
025    
026    import javax.xml.parsers.DocumentBuilder;
027    import javax.xml.parsers.DocumentBuilderFactory;
028    import javax.xml.transform.Transformer;
029    import javax.xml.transform.TransformerFactory;
030    import javax.xml.transform.dom.DOMSource;
031    import javax.xml.transform.stream.StreamResult;
032    
033    import org.w3c.dom.Document;
034    import org.xml.sax.InputSource;
035    
036    /**
037     * @author Franck WOLFF
038     */
039    public class XMLUtil {
040    
041        private DocumentBuilderFactory documentBuilderFactory = null;
042        private TransformerFactory transformerFactory = null;
043    
044        public Document buildDocument(String xml) {
045            try {
046                DocumentBuilder builder = getDocumentBuilderFactory().newDocumentBuilder();
047                return builder.parse(new InputSource(new StringReader(xml)));
048            } catch (Exception e) {
049                throw new RuntimeException("Could not parse XML string", e);
050            }
051        }
052    
053        public String toString(Document doc) {
054            try {
055                Transformer transformer = getTransformerFactory().newTransformer();
056                StringWriter writer = new StringWriter();
057                transformer.transform(new DOMSource(doc), new StreamResult(writer));
058                return writer.toString();
059            } catch (Exception e) {
060                throw new RuntimeException("Could not serialize document", e);
061            }
062        }
063    
064        private TransformerFactory getTransformerFactory() {
065            if (transformerFactory == null)
066                transformerFactory = TransformerFactory.newInstance();
067            return transformerFactory;
068        }
069    
070        private DocumentBuilderFactory getDocumentBuilderFactory() {
071            if (documentBuilderFactory == null)
072                documentBuilderFactory = DocumentBuilderFactory.newInstance();
073            return documentBuilderFactory;
074        }
075    }