001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle-sdk-ext.
005 *
006 * Dicoogle/dicoogle-sdk-ext 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 * Dicoogle/dicoogle-sdk-ext 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 Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.sdk.p2p.Messages;
020
021import java.io.ByteArrayInputStream;
022import org.dom4j.Document;
023import org.dom4j.DocumentException;
024import org.dom4j.Element;
025import org.dom4j.io.SAXReader;
026
027/**
028 *
029 * @author Carlos Ferreira
030 * @author Pedro Bento
031 */
032public class MessageXML implements MessageI<byte[]>
033{
034    private String Message;
035
036    public MessageXML(byte[] message)
037    {
038        this.Message = new String(message);
039    }
040
041    public String getType()
042    {
043        SAXReader saxReader = new SAXReader();
044        ByteArrayInputStream input = new ByteArrayInputStream(Message.getBytes());
045        Document document = null;
046        try
047        {
048            document = saxReader.read(input);
049        } catch (DocumentException ex)
050        {
051            ex.printStackTrace(System.out);
052        }
053        Element root = document.getRootElement();
054        Element tmp = root.element(MessageFields.MESSAGE_TYPE);
055        return tmp.getText();
056    }
057
058    public byte[] getMessage()
059    {
060        return this.Message.getBytes();
061    }
062
063    @Override
064    public String toString()
065    {
066        return this.Message;
067    }
068}