001    /**
002     * 
003     * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004     *
005     * This file is part of ** M y C o R e **
006     * Visit our homepage at http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, normally in the file license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     *
023     **/
024    package org.mycore.backend.hibernate.types;
025    
026    import java.io.ByteArrayOutputStream;
027    import java.sql.Blob;
028    
029    import org.dom4j.DocumentFactory;
030    import org.dom4j.Element;
031    import org.dom4j.Node;
032    import org.dom4j.io.OutputFormat;
033    import org.dom4j.io.SAXReader;
034    import org.dom4j.io.XMLWriter;
035    import org.hibernate.engine.Mapping;
036    import org.hibernate.engine.SessionFactoryImplementor;
037    import org.hibernate.lob.BlobImpl;
038    import org.hibernate.type.BlobType;
039    
040    import org.mycore.common.MCRException;
041    
042    public class MCRXMLBlobType extends BlobType {
043    
044        private static final long serialVersionUID = -5081036473140936467L;
045    
046        private static final DocumentFactory DOC_FACTORY = new DocumentFactory();
047    
048        @Override
049        public Object fromXMLNode(Node xml, Mapping factory) {
050            if (xml.getNodeType() != Node.ELEMENT_NODE) {
051                throw new UnsupportedOperationException("Blobs can only be read from elements: " + xml.getNodeTypeName());
052            }
053            Element e = (Element) xml;
054            OutputFormat format = OutputFormat.createCompactFormat();
055            format.setTrimText(false);
056            format.setNewLineAfterDeclaration(true);
057            format.setSuppressDeclaration(false);
058            format.setEncoding("UTF-8");
059            ByteArrayOutputStream bout = new ByteArrayOutputStream();
060            try {
061                XMLWriter xmlWriter = new XMLWriter(bout, format);
062                xmlWriter.write(e.content());
063            } catch (Exception e1) {
064                throw new MCRException("Cannot write xml elements to Blob.", e1);
065            }
066            BlobImpl blob = new BlobImpl(bout.toByteArray());
067            return blob;
068        }
069    
070        @Override
071        public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) {
072            Blob blob = (Blob) value;
073            if (node.getNodeType() != Node.ELEMENT_NODE) {
074                throw new UnsupportedOperationException("Blobs can only be written to elements.");
075            }
076            Element e;
077            SAXReader xmlReader = new SAXReader(DOC_FACTORY);
078            xmlReader.setValidation(false);
079            try {
080                e = (Element) node;
081                e.appendContent(xmlReader.read(blob.getBinaryStream()));
082            } catch (Exception e1) {
083                throw new MCRException("Cannot build xml elements from Blob.", e1);
084            }
085        }
086    
087    }