Coverage Report - org.jtheque.core.managers.core.application.XMLApplicationReader
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLApplicationReader
0 %
0/89
0 %
0/36
2.917
 
 1  
 package org.jtheque.core.managers.core.application;
 2  
 
 3  
 import org.jdom.Element;
 4  
 import org.jtheque.core.managers.resource.ImageDescriptor;
 5  
 import org.jtheque.core.managers.resource.ImageType;
 6  
 import org.jtheque.core.utils.SystemProperty;
 7  
 import org.jtheque.core.utils.file.XMLException;
 8  
 import org.jtheque.core.utils.file.XMLReader;
 9  
 import org.jtheque.utils.StringUtils;
 10  
 import org.jtheque.utils.bean.InternationalString;
 11  
 import org.jtheque.utils.bean.Version;
 12  
 import org.jtheque.utils.io.FileUtils;
 13  
 
 14  
 import java.io.File;
 15  
 import java.util.ArrayList;
 16  
 import java.util.Collection;
 17  
 
 18  
 /*
 19  
  * This file is part of JTheque.
 20  
  *
 21  
  * JTheque is free software: you can redistribute it and/or modify
 22  
  * it under the terms of the GNU General Public License as published by
 23  
  * the Free Software Foundation, either version 3 of the License.
 24  
  *
 25  
  * JTheque is distributed in the hope that it will be useful,
 26  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 27  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 28  
  * GNU General Public License for more details.
 29  
  *
 30  
  * You should have received a copy of the GNU General Public License
 31  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 32  
  */
 33  
 
 34  
 /**
 35  
  * An XML application reader.
 36  
  *
 37  
  * @author Baptiste Wicht
 38  
  */
 39  0
 public final class XMLApplicationReader {
 40  
     private XMLReader reader;
 41  
 
 42  
     /**
 43  
      * Read the application file.
 44  
      *
 45  
      * @param filePath The path to the application file.
 46  
      *
 47  
      * @return The builded Application.
 48  
      */
 49  
     public Application readApplication(String filePath){
 50  0
         XMLApplication application = new XMLApplication();
 51  
 
 52  0
         reader = new XMLReader();
 53  
 
 54  0
         openFile(filePath);
 55  
 
 56  
         try {
 57  0
             readFile(application);
 58  0
         } catch (XMLException e){
 59  0
             throw new IllegalArgumentException("Unable to read the file " + filePath, e);
 60  
         } finally {
 61  0
             FileUtils.close(reader);
 62  0
         }
 63  
 
 64  0
         return application;
 65  
     }
 66  
 
 67  
     /**
 68  
      * Open the file.
 69  
      *
 70  
      * @param filePath The path to the file.
 71  
      */
 72  
     private void openFile(String filePath){
 73  
         try {
 74  0
             reader.openFile(filePath);
 75  0
         } catch (XMLException e){
 76  0
             throw new IllegalArgumentException("Unable to read the file " + filePath, e);
 77  0
         }
 78  0
     }
 79  
 
 80  
     /**
 81  
      * Read the file.
 82  
      *
 83  
      * @param application The application to fill.
 84  
      *
 85  
      * @throws XMLException if an error occurs during the XML processing.
 86  
      */
 87  
     private void readFile(XMLApplication application) throws XMLException{
 88  0
         readVersion(application);
 89  0
         readApplicationValues(application);
 90  0
         readInternationalization(application);
 91  0
         application.setImages(readImageDescriptor("logo"), readImageDescriptor("icon"));
 92  0
         readOptions(application);
 93  0
         readProperties(application);
 94  0
     }
 95  
 
 96  
     /**
 97  
      * Read the version of the application from the file.
 98  
      *
 99  
      * @param application The application to fill.
 100  
      *
 101  
      * @throws XMLException if an error occurs during the XML processing.
 102  
      */
 103  
     private void readVersion(XMLApplication application) throws XMLException{
 104  0
         String versionStr = reader.readString("@version", reader.getRootElement());
 105  
 
 106  0
         application.setVersion(new Version(versionStr));
 107  0
     }
 108  
 
 109  
     /**
 110  
      * Read the application values (repository, messages file) from the file.
 111  
      *
 112  
      * @param application The application to fill.
 113  
      *
 114  
      * @throws XMLException if an error occurs during the XML processing.
 115  
      */
 116  
     private void readApplicationValues(XMLApplication application) throws XMLException{
 117  0
         String folder = reader.readString("folder", reader.getRootElement());
 118  
 
 119  0
         if (StringUtils.isEmpty(folder) || !new File(folder).exists()){
 120  0
             application.setProperty("application.folder.path", new File(SystemProperty.USER_DIR.get()).getParentFile().getAbsolutePath());
 121  
         } else {
 122  0
             application.setProperty("application.folder.path", new File(folder).getAbsolutePath());
 123  
         }
 124  
 
 125  0
         application.setProperty("application.repository", reader.readString("repository", reader.getRootElement()));
 126  0
         application.setProperty("application.messages", reader.readString("messages", reader.getRootElement()));
 127  0
     }
 128  
 
 129  
     /**
 130  
      * Read all the internationalized values of the application from the file.
 131  
      *
 132  
      * @param application The application to fill.
 133  
      *
 134  
      * @throws XMLException if an error occurs during the XML processing.
 135  
      */
 136  
     private void readInternationalization(XMLApplication application) throws XMLException{
 137  0
         Object i18nElement = reader.getNode("i18n", reader.getRootElement());
 138  
 
 139  0
         readLanguages(i18nElement, application);
 140  0
         readApplicationProperties(i18nElement, application);
 141  0
     }
 142  
 
 143  
     /**
 144  
      * Read all the supported languages of the application.
 145  
      *
 146  
      * @param application The application to fill.
 147  
      * @param i18nElement The i18n XML element.
 148  
      *
 149  
      * @throws XMLException If an errors occurs during the XML processing.
 150  
      */
 151  
     private void readLanguages(Object i18nElement, XMLApplication application) throws XMLException{
 152  0
         if(reader.existsNode("languages", i18nElement)){
 153  0
             Collection<Element> nodes = reader.getNodes("languages/language", i18nElement);
 154  
 
 155  0
             Collection<String> languages = new ArrayList<String>(nodes.size());
 156  
 
 157  0
             for (Element languageElement : nodes){
 158  0
                 languages.add(languageElement.getText());
 159  
             }
 160  
 
 161  0
             nodes = reader.getNodes("languages/*", i18nElement);
 162  
 
 163  0
             for (Element languageElement : nodes){
 164  0
                 languages.add(languageElement.getName());
 165  
             }
 166  
 
 167  0
             application.setSupportedLanguages(languages.toArray(new String[languages.size()]));
 168  
         }
 169  0
     }
 170  
 
 171  
     /**
 172  
      * Read the application internationalisation properties.
 173  
      *
 174  
      * @param application The application to fill.
 175  
      * @param i18nElement The i18n XML element.
 176  
      *
 177  
      * @throws XMLException If an errors occurs during the XML processing.
 178  
      */
 179  
     private void readApplicationProperties(Object i18nElement, XMLApplication application) throws XMLException{
 180  0
         if (reader.getNode("files", i18nElement) != null || reader.getNode("name", i18nElement) == null){
 181  0
             application.setApplicationProperties(new I18nAplicationProperties());
 182  
         } else {
 183  0
             DirectValuesApplicationProperties props = new DirectValuesApplicationProperties();
 184  
 
 185  0
             props.setName(readInternationalString("name", i18nElement));
 186  0
             props.setAuthor(readInternationalString("author", i18nElement));
 187  0
             props.setSite(readInternationalString("site", i18nElement));
 188  0
             props.setEmail(readInternationalString("email", i18nElement));
 189  0
             props.setCopyright(readInternationalString("copyright", i18nElement));
 190  
 
 191  0
             application.setApplicationProperties(props);
 192  
         }
 193  0
     }
 194  
 
 195  
     /**
 196  
      * Read an international string from the file.
 197  
      *
 198  
      * @param path The path the international string element.
 199  
      * @param parentElement The parent element.
 200  
      *
 201  
      * @return The internationalized string.
 202  
      *
 203  
      * @throws XMLException if an error occurs during the XML processing.
 204  
      */
 205  
     private InternationalString readInternationalString(String path, Object parentElement) throws XMLException{
 206  0
         InternationalString internationalString = new InternationalString();
 207  
 
 208  0
         Collection<Element> elements = reader.getNodes(path + "/*", parentElement);
 209  
 
 210  0
         for (Element child : elements){
 211  0
             internationalString.put(child.getName(), child.getText());
 212  
         }
 213  
 
 214  0
         return internationalString;
 215  
     }
 216  
 
 217  
     /**
 218  
      * Read the window icon information from the file.
 219  
      *
 220  
      * @param node The node to read the image from.
 221  
      *
 222  
      * @throws XMLException if an error occurs during the XML processing.
 223  
      *
 224  
      * @return Return the read image descriptor. If the node doesn't exists a default ImageDescriptor with
 225  
      * the name of the node as the image and PNG type is returned.
 226  
      */
 227  
     private ImageDescriptor readImageDescriptor(String node) throws XMLException{
 228  0
         if(reader.existsNode(node, reader.getRootElement())){
 229  0
             Object iconElement = reader.getNode(node, reader.getRootElement());
 230  
 
 231  0
             StringBuilder path = new StringBuilder(SystemProperty.USER_DIR.get());
 232  0
                         path.append("images/");
 233  0
                         path.append(reader.readString("image", iconElement));
 234  
 
 235  
 
 236  0
             if(reader.existsValue("@image", iconElement)){
 237  0
                 path.append(reader.readString("@image", iconElement));
 238  
             } else {
 239  0
                 path.append(reader.readString("image", iconElement));
 240  
             }
 241  
 
 242  
             ImageType type;
 243  
 
 244  0
             if(reader.existsNode("type", iconElement)){
 245  0
                 String typeStr = reader.readString("type", iconElement);
 246  0
                 type = StringUtils.isEmpty(typeStr) ? ImageType.PNG : ImageType.resolve(typeStr);
 247  0
             } else if(reader.existsValue("@type", iconElement)){
 248  0
                 String typeStr = reader.readString("@type", iconElement);
 249  0
                 type = StringUtils.isEmpty(typeStr) ? ImageType.PNG : ImageType.resolve(typeStr);
 250  0
             } else {
 251  0
                 type = ImageType.PNG;
 252  
             }
 253  
 
 254  0
             return new ImageDescriptor(path.toString(), type);
 255  
         }
 256  
 
 257  0
         return new ImageDescriptor(node, ImageType.PNG);
 258  
     }
 259  
 
 260  
     /**
 261  
      * Read the application options from the file.
 262  
      *
 263  
      * @param application The application to fill.
 264  
      *
 265  
      * @throws XMLException if an error occurs during the XML processing.
 266  
      */
 267  
     private void readOptions(XMLApplication application) throws XMLException{
 268  0
         if(reader.existsNode("options", reader.getRootElement())){
 269  0
             Object optionsElement = reader.getNode("options", reader.getRootElement());
 270  
 
 271  0
             if(reader.existsValue("licence", optionsElement) && StringUtils.isNotEmpty(reader.readString("licence", optionsElement))){
 272  0
                 application.displayLicence();
 273  0
                 application.setProperty("application.licence", SystemProperty.USER_DIR.get() + reader.readString("licence", optionsElement));
 274  
             }
 275  
         }
 276  0
     }
 277  
 
 278  
     /**
 279  
      * Read the application properties from the file.
 280  
      *
 281  
      * @param application The application to fill.
 282  
      *
 283  
      * @throws XMLException if an error occurs during the XML processing.
 284  
      */
 285  
     private void readProperties(XMLApplication application) throws XMLException{
 286  0
         Collection<Element> nodes = reader.getNodes("properties/*", reader.getRootElement());
 287  
 
 288  0
         for (Element propertyElement : nodes){
 289  0
             application.setProperty(propertyElement.getName(), propertyElement.getText());
 290  
         }
 291  0
     }
 292  
 }