Package kameleon.gui.util

Source Code of kameleon.gui.util.GuiWorkSpaceManager

/*
* Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*      * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*      * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*      * The names of its contributors may not be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package kameleon.gui.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

import kameleon.exception.FileReadingException;
import kameleon.exception.FileWritingException;
import kameleon.exception.KameleonException;
import kameleon.util.IOFile;
import kameleon.util.WorkSpaceManager;

/**
* Utility class for creating and maintaining the work space of
* the Kameleon software.
*
* @author    Schnell Michaël
* @version    1.0
*/
public class GuiWorkSpaceManager extends WorkSpaceManager
implements FileConstants {

  /**
   * Checks if the default languages files are present. If not attempts
   * to create them.
   *
   * @throws  KameleonException
   *       if the languages are missing and could not be created
   */
  public static void ensureBaseLanguage() throws KameleonException {
    ensureWorkSpace() ;
    // Check the language files
    File languageFile = new File(LANGUAGE_FOLDER) ;
    String[] foundLanguageFiles = languageFile.list(new FilenameFilter () {
      /**
       * A file is accepted if its name matches the regular
       * expression {@value #LANGUAGE_FILE_NAMES_REGEXP} and is
       * not a directory.
       */
      @Override
      public boolean accept(File file, String name) {
        File target = new File(String.format(PATH_EXTENSION,
            file.getAbsolutePath(), name)) ;
        return !target.isDirectory()
            && name.matches(LANGUAGE_FILE_NAMES_REGEXP) ;
      }// accept(File, String)
    }) ;
    if (foundLanguageFiles.length == 0) {
      // No language file found, create default ones
      Locale[] DEFAULT_LOCALES = new Locale[]{Locale.ENGLISH, Locale.FRENCH} ;
      for(Locale locale : DEFAULT_LOCALES) {
        String localeLanguage = locale.getLanguage() ;
        String targetFile = String.format(
            LANGUAGE_FILE_TEMPLATE, localeLanguage) ;
        String sourceFile = String.format(
            LANGUAGE_FILE_JAR_TEMPLATE, localeLanguage) ;
       
        // Open the streams
        BufferedOutputStream destination = null ;
        BufferedInputStream source = null ;
        InputStream src =
            GuiWorkSpaceManager.class.getResourceAsStream(sourceFile) ;
        if (src == null) {
          throw new FileReadingException(sourceFile) ;
        }// if
        source = new BufferedInputStream(src) ;
        try {
          destination = new BufferedOutputStream(
              new FileOutputStream(targetFile)) ;
        } catch(FileNotFoundException e) {
          throw new FileWritingException(new File(targetFile)) ;
        }// try
       
        // Do the actual copying
        IOFile.copyFile(source, destination) ;
       
        // Close the streams
        try {
          source.close() ;
        } catch(IOException ioe) {
          /* Do nothing. */
        }// try
        try {
          destination.close() ;
        } catch(IOException ioe) {
          throw new FileWritingException(new File(targetFile)) ;
        }// try
      }// for
    } else {
      //TODO Check found language files
      // OR build an array with the found language files 
    }// if
  }// ensureBaseLanguage()

}// class GuiWorkSpaceManager
TOP

Related Classes of kameleon.gui.util.GuiWorkSpaceManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.