Coverage Report - org.jtheque.core.managers.update.repository.RepositoryReader
 
Classes in this File Line Coverage Branch Coverage Complexity
RepositoryReader
0 %
0/43
0 %
0/8
1.833
 
 1  
 package org.jtheque.core.managers.update.repository;
 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.jdom.Element;
 20  
 import org.jtheque.core.managers.Managers;
 21  
 import org.jtheque.core.managers.log.ILoggingManager;
 22  
 import org.jtheque.core.utils.file.XMLException;
 23  
 import org.jtheque.core.utils.file.XMLReader;
 24  
 import org.jtheque.utils.StringUtils;
 25  
 import org.jtheque.utils.bean.InternationalString;
 26  
 import org.jtheque.utils.bean.Version;
 27  
 import org.jtheque.utils.io.FileUtils;
 28  
 
 29  
 /**
 30  
  * A reader for repository XML file.
 31  
  *
 32  
  * @author Baptiste Wicht
 33  
  */
 34  
 public final class RepositoryReader {
 35  
     private final Repository repository;
 36  
     private final XMLReader reader;
 37  
 
 38  
     /**
 39  
      * Construct a new RepositoryReader.
 40  
      */
 41  
     public RepositoryReader() {
 42  0
         super();
 43  
 
 44  0
         repository = new Repository();
 45  0
         reader = new XMLReader();
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Read a repository file.
 50  
      *
 51  
      * @param strUrl The URL of the repository file.
 52  
      * @return The repository.
 53  
      */
 54  
     public Repository read(String strUrl) {
 55  
         try {
 56  0
             reader.openURL(strUrl);
 57  
 
 58  0
             read();
 59  0
         } catch (XMLException e) {
 60  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e, "Unable to get information from repository");
 61  
         } finally {
 62  0
             FileUtils.close(reader);
 63  0
         }
 64  
 
 65  0
         return repository;
 66  
     }
 67  
 
 68  
     /**
 69  
      * Read all the informations from the reader.
 70  
      *
 71  
      * @throws XMLException If an error occurs during the XML reading process.
 72  
      */
 73  
     private void read() throws XMLException {
 74  0
         readTitle(reader);
 75  0
         readApplication(reader);
 76  0
         readModules(reader);
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Read the application informations from the reader.
 81  
      *
 82  
      * @param reader The reader to use.
 83  
      * @throws XMLException If an error occurs during the XML reading process.
 84  
      */
 85  
     private void readApplication(XMLReader reader) throws XMLException {
 86  0
         repository.setApplication(reader.readString("application", reader.getRootElement()));
 87  0
     }
 88  
 
 89  
     /**
 90  
      * Read the modules informations from the reader.
 91  
      *
 92  
      * @param reader The reader to use.
 93  
      * @throws XMLException If an error occurs during the XML reading process.
 94  
      */
 95  
     private void readModules(XMLReader reader) throws XMLException {
 96  0
         for (Object currentNode : reader.getNodes("modules/module", reader.getRootElement())) {
 97  0
             ModuleDescription module = new ModuleDescription();
 98  
 
 99  0
             module.setId(reader.readString("id", currentNode));
 100  0
             module.setName(reader.readString("name", currentNode));
 101  
 
 102  0
             if (StringUtils.isEmpty(reader.readString("core", currentNode))) {
 103  0
                 module.setCoreVersion(Managers.getCore().getCoreCurrentVersion());
 104  
             } else {
 105  0
                 module.setCoreVersion(new Version(reader.readString("core", currentNode)));
 106  
             }
 107  
 
 108  0
             module.setVersionsFileURL(reader.readString("versions", currentNode));
 109  
 
 110  0
             Element descriptionElement = reader.getNode("description", currentNode);
 111  
 
 112  0
             InternationalString description = new InternationalString();
 113  
 
 114  0
             for (Object child : descriptionElement.getChildren()) {
 115  0
                 Element childElement = (Element) child;
 116  
 
 117  0
                 description.put(childElement.getName(), childElement.getValue());
 118  0
             }
 119  
 
 120  0
             module.setDescription(description);
 121  
 
 122  0
             repository.getModules().add(module);
 123  0
         }
 124  0
     }
 125  
 
 126  
     /**
 127  
      * Read the title of the repository from the reader.
 128  
      *
 129  
      * @param reader The reader to use.
 130  
      * @throws XMLException If an error occurs during the XML reading process.
 131  
      */
 132  
     private void readTitle(XMLReader reader) throws XMLException {
 133  0
         InternationalString title = new InternationalString();
 134  
 
 135  0
         Element titleElement = reader.getNode("title", reader.getRootElement());
 136  
 
 137  0
         for (Object child : titleElement.getChildren()) {
 138  0
             Element childElement = (Element) child;
 139  
 
 140  0
             title.put(childElement.getName(), childElement.getValue());
 141  0
         }
 142  
 
 143  0
         repository.setTitle(title);
 144  0
     }
 145  
 }