Coverage Report - org.jtheque.core.managers.update.versions.VersionsFileReader
 
Classes in this File Line Coverage Branch Coverage Complexity
VersionsFileReader
0 %
0/97
0 %
0/22
2.118
 
 1  
 package org.jtheque.core.managers.update.versions;
 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.core.ICore;
 22  
 import org.jtheque.core.managers.log.ILoggingManager;
 23  
 import org.jtheque.core.managers.module.beans.ModuleContainer;
 24  
 import org.jtheque.core.managers.patch.OnlinePatch;
 25  
 import org.jtheque.core.managers.update.Updatable;
 26  
 import org.jtheque.core.managers.update.actions.AbstractUpdateAction;
 27  
 import org.jtheque.core.managers.update.actions.DeleteAction;
 28  
 import org.jtheque.core.managers.update.actions.DownloadAction;
 29  
 import org.jtheque.core.managers.update.actions.MoveAction;
 30  
 import org.jtheque.core.managers.update.actions.UpdateAction;
 31  
 import org.jtheque.core.utils.file.XMLException;
 32  
 import org.jtheque.core.utils.file.XMLReader;
 33  
 import org.jtheque.utils.StringUtils;
 34  
 import org.jtheque.utils.bean.Version;
 35  
 import org.jtheque.utils.io.FileUtils;
 36  
 
 37  
 import java.net.MalformedURLException;
 38  
 import java.net.URL;
 39  
 import java.util.ArrayList;
 40  
 import java.util.Collection;
 41  
 import java.util.Collections;
 42  
 import java.util.List;
 43  
 
 44  
 /**
 45  
  * A reader for the versions file.
 46  
  *
 47  
  * @author Baptiste Wicht
 48  
  */
 49  0
 public final class VersionsFileReader {
 50  0
     private final XMLReader reader = new XMLReader();
 51  
     private VersionsFile versionsFile;
 52  
 
 53  
     /**
 54  
      * Read a versions file and return it.
 55  
      *
 56  
      * @param object The URL of the file.
 57  
      * @return The versions file.
 58  
      */
 59  
     public VersionsFile read(Object object) {
 60  0
         if (isModule(object)) {
 61  0
             ModuleContainer module = (ModuleContainer) object;
 62  
 
 63  0
             if (StringUtils.isNotEmpty(module.getInfos().updateURL())) {
 64  0
                 return read(module.getInfos().updateURL());
 65  
             }
 66  0
         } else if (isUpdatable(object)) {
 67  0
             Updatable updatable = (Updatable) object;
 68  
 
 69  0
             return read(updatable.getVersionsFileURL());
 70  0
         } else if (isCore(object)) {
 71  0
             return read(Managers.getCore().getFiles().getVersionsFileURL());
 72  
         }
 73  
 
 74  0
         return null;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Indicate if the object is a module or not.
 79  
      *
 80  
      * @param object The object to test.
 81  
      * @return true if the object is a module else false.
 82  
      */
 83  
     private static boolean isModule(Object object) {
 84  0
         return object instanceof ModuleContainer;
 85  
     }
 86  
 
 87  
     /**
 88  
      * Indicate if the object is an updatable or not.
 89  
      *
 90  
      * @param object The object to test.
 91  
      * @return true if the object is an updatable else false.
 92  
      */
 93  
     private static boolean isUpdatable(Object object) {
 94  0
         return object instanceof Updatable;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Indicate if the object is the core or not.
 99  
      *
 100  
      * @param object The object to test.
 101  
      * @return true if the object is the core else false.
 102  
      */
 103  
     private static boolean isCore(Object object) {
 104  0
         return object instanceof ICore;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Read the VersionsFile at the url.
 109  
      *
 110  
      * @param strUrl the url string value.
 111  
      * @return the read versions file.
 112  
      */
 113  
     private VersionsFile read(String strUrl) {
 114  
         try {
 115  0
             return readVersionsFile(new URL(strUrl));
 116  0
         } catch (MalformedURLException e) {
 117  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e, "Unable to read versions file");
 118  
         }
 119  
 
 120  0
         return null;
 121  
     }
 122  
 
 123  
     /**
 124  
      * Read the the version's file at the specified URL.
 125  
      *
 126  
      * @param versionsFileURL The URL of the versions file.
 127  
      * @return The version's file or null if the URL is invalid.
 128  
      */
 129  
     public VersionsFile readURL(String versionsFileURL) {
 130  
         try {
 131  0
             return readVersionsFile(new URL(versionsFileURL));
 132  0
         } catch (MalformedURLException e) {
 133  0
             return null;
 134  
         }
 135  
     }
 136  
 
 137  
     /**
 138  
      * Read a versions file and return it.
 139  
      *
 140  
      * @param url The URL of the file.
 141  
      * @return The versions file.
 142  
      */
 143  
     private VersionsFile readVersionsFile(URL url) {
 144  0
         versionsFile = new VersionsFile();
 145  0
         List<OnlineVersion> onlineVersions = new ArrayList<OnlineVersion>(10);
 146  
 
 147  
         try {
 148  0
             reader.openURL(url);
 149  
 
 150  0
             readInstallVersion();
 151  
 
 152  0
             for (Object currentNode : reader.getNodes("jt-version", reader.getRootElement())) {
 153  0
                 readOnlineVersion(currentNode, onlineVersions);
 154  
             }
 155  
 
 156  0
             Collections.sort(onlineVersions);
 157  
 
 158  0
             versionsFile.setVersions(onlineVersions);
 159  0
         } catch (XMLException e) {
 160  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e, "Unable to read versions file");
 161  
         } finally {
 162  0
             FileUtils.close(reader);
 163  0
         }
 164  
 
 165  0
         return versionsFile;
 166  
     }
 167  
 
 168  
     /**
 169  
      * Read the install version.
 170  
      *
 171  
      * @throws XMLException If an error occurs during the xml reading.
 172  
      */
 173  
     private void readInstallVersion() throws XMLException {
 174  0
         InstallVersion installVersion = new InstallVersion();
 175  
 
 176  0
         Object installVersionNode = reader.getNode("install", reader.getRootElement());
 177  
 
 178  0
         readOnlineVersion(installVersionNode, installVersion);
 179  
 
 180  0
         installVersion.setJarFile(reader.readString("jar-file", installVersionNode));
 181  0
         installVersion.setTitle(reader.readString("title", installVersionNode));
 182  
 
 183  0
         versionsFile.setInstallVersion(installVersion);
 184  0
     }
 185  
 
 186  
     /**
 187  
      * Read the patches of the current node.
 188  
      *
 189  
      * @param currentNode The current node.
 190  
      * @param version     The version to read the patches for.
 191  
      * @throws XMLException If an errors occurs during the reading process.
 192  
      */
 193  
     private void readPatches(Object currentNode, OnlineVersion version) throws XMLException {
 194  0
         Collection<Element> nodes = reader.getNodes("patchs/patch", currentNode);
 195  
 
 196  0
         Collection<OnlinePatch> patches = new ArrayList<OnlinePatch>(nodes.size());
 197  
 
 198  0
         for (Object node : nodes) {
 199  0
             OnlinePatch patch = new OnlinePatch();
 200  0
             patch.setClassName(reader.readString("@class", node));
 201  
 
 202  0
             patches.add(patch);
 203  0
         }
 204  
 
 205  0
         version.setPatches(patches);
 206  0
     }
 207  
 
 208  
     /**
 209  
      * Fill an online version.
 210  
      *
 211  
      * @param currentNode    The current node.
 212  
      * @param onlineVersions The list of all online versions.
 213  
      * @throws XMLException If an error occurs during the reading process.
 214  
      */
 215  
     private void readOnlineVersion(Object currentNode, Collection<OnlineVersion> onlineVersions) throws XMLException {
 216  0
         OnlineVersion onlineVersion = new OnlineVersion();
 217  
 
 218  0
         readOnlineVersion(currentNode, onlineVersion);
 219  
 
 220  0
         onlineVersions.add(onlineVersion);
 221  0
     }
 222  
 
 223  
     /**
 224  
      * Read the online version.
 225  
      *
 226  
      * @param currentNode   The node to read from.
 227  
      * @param onlineVersion The online version to fill.
 228  
      * @throws XMLException If an error occurs during the reading process.
 229  
      */
 230  
     private void readOnlineVersion(Object currentNode, OnlineVersion onlineVersion) throws XMLException {
 231  0
         readPatches(currentNode, onlineVersion);
 232  0
         readVersionNumber(currentNode, onlineVersion);
 233  0
         readCoreVersion(currentNode, onlineVersion);
 234  0
         readActions(currentNode, onlineVersion);
 235  0
     }
 236  
 
 237  
     /**
 238  
      * Read the version number.
 239  
      *
 240  
      * @param currentNode   The node to read from.
 241  
      * @param onlineVersion The online version to fill.
 242  
      * @throws XMLException If an error occurs during the reading process.
 243  
      */
 244  
     private void readVersionNumber(Object currentNode, OnlineVersion onlineVersion) throws XMLException {
 245  0
         onlineVersion.setVersion(new Version(reader.readString("nom", currentNode)));
 246  0
     }
 247  
 
 248  
     /**
 249  
      * Read the actions of the online version.
 250  
      *
 251  
      * @param currentNode   The node to read from.
 252  
      * @param onlineVersion The online version to fill.
 253  
      * @throws XMLException If an error occurs during the reading process.
 254  
      */
 255  
     private void readActions(Object currentNode, OnlineVersion onlineVersion) throws XMLException {
 256  0
         Collection<Element> nodes = reader.getNodes("actions/*", currentNode);
 257  
 
 258  0
         onlineVersion.setActions(new ArrayList<UpdateAction>(nodes.size()));
 259  
 
 260  0
         for (Element node : nodes) {
 261  0
             String name = node.getName();
 262  
 
 263  0
             if ("add".equals(name)) {
 264  0
                 AbstractUpdateAction action = readDownloadAction(node);
 265  
 
 266  0
                 onlineVersion.getActions().add(action);
 267  0
             } else if ("delete".equals(name)) {
 268  0
                 AbstractUpdateAction action = readDeleteAction(node);
 269  
 
 270  0
                 onlineVersion.getActions().add(action);
 271  0
             } else if ("move".equals(name)) {
 272  0
                 AbstractUpdateAction action = readMoveAction(node);
 273  
 
 274  0
                 onlineVersion.getActions().add(action);
 275  
             }
 276  0
         }
 277  0
     }
 278  
 
 279  
     /**
 280  
      * Read the download action.
 281  
      *
 282  
      * @param node The node to read from.
 283  
      * @return the read DownloadAction.
 284  
      * @throws XMLException If an error occurs during the reading process.
 285  
      */
 286  
     private AbstractUpdateAction readDownloadAction(Object node) throws XMLException {
 287  0
         DownloadAction action = new DownloadAction();
 288  
 
 289  0
         action.setOs(reader.readString("@os", node));
 290  0
         action.setFolder(reader.readString("folder", node));
 291  0
         action.setFile(reader.readString("file", node));
 292  0
         action.setUrl(reader.readString("url", node));
 293  
 
 294  0
         return action;
 295  
     }
 296  
 
 297  
     /**
 298  
      * Read a delete action.
 299  
      *
 300  
      * @param node The node to read from.
 301  
      * @return The delete action.
 302  
      * @throws XMLException If an error occurs during the reading process.
 303  
      */
 304  
     private AbstractUpdateAction readDeleteAction(Object node) throws XMLException {
 305  0
         AbstractUpdateAction action = new DeleteAction();
 306  
 
 307  0
         action.setFolder(reader.readString("folder", node));
 308  0
         action.setFile(reader.readString("file", node));
 309  
 
 310  0
         return action;
 311  
     }
 312  
 
 313  
     /**
 314  
      * Read a move action.
 315  
      *
 316  
      * @param node The node to read from.
 317  
      * @return The move action.
 318  
      * @throws XMLException If an error occurs during the reading process.
 319  
      */
 320  
     private AbstractUpdateAction readMoveAction(Object node) throws XMLException {
 321  0
         MoveAction action = new MoveAction();
 322  
 
 323  0
         action.setSourceFile(reader.readString("src-file", node));
 324  0
         action.setSourceFolder(reader.readString("src-folder", node));
 325  0
         action.setFile(reader.readString("dest-file", node));
 326  0
         action.setFolder(reader.readString("dest-folder", node));
 327  
 
 328  0
         return action;
 329  
     }
 330  
 
 331  
     /**
 332  
      * Read the needed version of the core.
 333  
      *
 334  
      * @param currentNode   The node to read from.
 335  
      * @param onlineVersion The online version to fill.
 336  
      * @throws XMLException If an error occurs during the reading process.
 337  
      */
 338  
     private void readCoreVersion(Object currentNode, OnlineVersion onlineVersion) throws XMLException {
 339  0
         if (StringUtils.isEmpty(reader.readString("core", currentNode))) {
 340  0
             onlineVersion.setCoreVersion(Managers.getCore().getCoreCurrentVersion());
 341  
         } else {
 342  0
             onlineVersion.setCoreVersion(new Version(reader.readString("core", currentNode)));
 343  
         }
 344  0
     }
 345  
 }