Coverage Report - org.jtheque.core.managers.message.MessageFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageFileReader
0 %
0/28
0 %
0/2
2.667
 
 1  
 package org.jtheque.core.managers.message;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.utils.file.ReadingException;
 20  
 import org.jtheque.core.utils.file.XMLException;
 21  
 import org.jtheque.core.utils.file.XMLReader;
 22  
 import org.jtheque.utils.bean.IntDate;
 23  
 import org.jtheque.utils.io.FileUtils;
 24  
 
 25  
 import java.net.MalformedURLException;
 26  
 import java.net.URL;
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collections;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * @author Baptiste Wicht
 33  
  */
 34  
 final class MessageFileReader {
 35  
     /**
 36  
      * Construct a new MessageFileReader. This constructor is private because
 37  
      * all methods are static.
 38  
      */
 39  
     private MessageFileReader() {
 40  0
         super();
 41  0
     }
 42  
 
 43  
     /**
 44  
      * Read a messages file and return it.
 45  
      *
 46  
      * @param strUrl The URL of the file.
 47  
      * @return The versions file.
 48  
      * @throws ReadingException Thrown when an error occurs during the reading.
 49  
      */
 50  
     public static MessageFile readMessagesFile(String strUrl) throws ReadingException {
 51  
         URL url;
 52  
         try {
 53  0
             url = new URL(strUrl);
 54  0
         } catch (MalformedURLException e) {
 55  0
             throw new ReadingException("Unable to get the messages. ", e);
 56  0
         }
 57  
 
 58  0
         return readMessagesFile(url);
 59  
     }
 60  
 
 61  
     /**
 62  
      * Read a messages file and return it.
 63  
      *
 64  
      * @param url The URL of the file.
 65  
      * @return The messages file.
 66  
      * @throws ReadingException Thrown when an error occurs during the reading.
 67  
      */
 68  
     private static MessageFile readMessagesFile(URL url) throws ReadingException {
 69  0
         MessageFile messageFile = new MessageFile();
 70  
 
 71  0
         XMLReader reader = new XMLReader();
 72  
 
 73  0
         List<Message> messages = new ArrayList<Message>(10);
 74  
 
 75  
         try {
 76  0
             reader.openURL(url);
 77  
 
 78  0
             messageFile.setSource(reader.readString("source", reader.getRootElement()));
 79  
 
 80  0
             for (Object currentNode : reader.getNodes("messages/message", reader.getRootElement())) {
 81  0
                 Message message = new Message();
 82  
 
 83  0
                 message.setId(reader.readInt("id", currentNode));
 84  0
                 message.setDate(new IntDate(reader.readInt("date", currentNode)));
 85  0
                 message.setTitle(reader.readString("title", currentNode));
 86  0
                 message.setMessage(reader.readString("message", currentNode));
 87  0
                 message.setSource(messageFile.getSource());
 88  
 
 89  0
                 messages.add(message);
 90  0
             }
 91  
 
 92  0
             Collections.sort(messages);
 93  
 
 94  0
             messageFile.setMessages(messages);
 95  
 
 96  0
             FileUtils.close(reader);
 97  0
         } catch (XMLException e) {
 98  0
             throw new ReadingException("Unable to read message's file. ", e);
 99  0
         }
 100  
 
 101  0
         return messageFile;
 102  
     }
 103  
 }