sftemplateswizard/com.nokia.s60tools.symbianfoundationtemplates/src/com/nokia/s60tools/symbianfoundationtemplates/util/Util.java
changeset 0 61163b28edca
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 package com.nokia.s60tools.symbianfoundationtemplates.util;
       
    18 
       
    19 import java.io.File;
       
    20 import java.util.AbstractList;
       
    21 import java.util.ArrayList;
       
    22 import java.util.Calendar;
       
    23 import java.util.GregorianCalendar;
       
    24 
       
    25 import org.eclipse.core.resources.IFile;
       
    26 import org.eclipse.core.resources.IFolder;
       
    27 import org.eclipse.core.resources.IProject;
       
    28 import org.eclipse.core.resources.ResourcesPlugin;
       
    29 import org.eclipse.core.runtime.IStatus;
       
    30 import org.eclipse.core.runtime.Path;
       
    31 import org.eclipse.core.runtime.Status;
       
    32 
       
    33 import com.nokia.s60tools.symbianfoundationtemplates.SymbianFoundationTemplates;
       
    34 
       
    35 /**
       
    36  * Utility class.
       
    37  *
       
    38  */
       
    39 public class Util {
       
    40 	/**
       
    41 	 * Get the status of the given file name (empty, exists already, discouraged extension...)
       
    42 	 * 
       
    43 	 * @param fileName the file name
       
    44 	 * @param templates template files used for checking extensions
       
    45 	 * @param folder folder where file resides
       
    46 	 * @return status
       
    47 	 */
       
    48 	public static IStatus getFileNameStatus(String fileName, String templates, String folder) {
       
    49 		IStatus fileStatus = new Status(IStatus.OK, SymbianFoundationTemplates.PLUGIN_ID, 0, "", null);
       
    50 		
       
    51 		if(fileName.length() == 0)
       
    52 			fileStatus = new Status(IStatus.ERROR, SymbianFoundationTemplates.PLUGIN_ID, -1, "File name is empty.", null);
       
    53 		else {
       
    54 			AbstractList<String> approvedExtensions = getFileExtensions(templates);
       
    55 		
       
    56 			boolean endsWithCorrect = false;
       
    57 
       
    58 			for(String extension : approvedExtensions)
       
    59 				if(fileName.endsWith(extension))
       
    60 					endsWithCorrect = true;
       
    61 			
       
    62 			if(!endsWithCorrect)
       
    63 				fileStatus = new Status(IStatus.WARNING, SymbianFoundationTemplates.PLUGIN_ID, 1, "File name is discouraged. File extension does not correspond to file types " + approvedExtensions + ".", null);
       
    64 			
       
    65 			if(folder!="" && getSelectedFile(fileName, folder).exists())
       
    66 				fileStatus = new Status(IStatus.ERROR, SymbianFoundationTemplates.PLUGIN_ID, -1, "File already exists.", null);
       
    67 		}
       
    68 		
       
    69 		return fileStatus;
       
    70 	}
       
    71 
       
    72 	/**
       
    73 	 * Get the status of the given folder name (empty, exists already...)
       
    74 	 * 
       
    75 	 * @param folderName folder name
       
    76 	 * @param folderType folder type used for creating a correct status message	 
       
    77 	 * @return status
       
    78 	 */
       
    79 	public static IStatus getFolderNameStatus(String folderName, String folderType) {
       
    80 		IStatus folderStatus = new Status(IStatus.OK, SymbianFoundationTemplates.PLUGIN_ID, 0, "", null);
       
    81 		
       
    82 		if(folderName.equals(""))
       
    83 			folderStatus = new Status(IStatus.ERROR, SymbianFoundationTemplates.PLUGIN_ID, -1, folderType + " is empty.", null);
       
    84 		else if(!selectedFolderExists(folderName))
       
    85 			folderStatus = new Status(IStatus.ERROR, SymbianFoundationTemplates.PLUGIN_ID, -1, folderType + " does not exist.", null);
       
    86 		
       
    87 		return folderStatus;
       
    88 	}
       
    89 	
       
    90 	/**
       
    91 	 * Helper method for getting the selected file
       
    92 	 * 
       
    93 	 * @param file file name
       
    94 	 * @param folder folder name
       
    95 	 * @return IFile representing the selected file
       
    96 	 */
       
    97 	public static IFile getSelectedFile(String file, String folder) {
       
    98 		return ResourcesPlugin.getWorkspace().getRoot().getFile(
       
    99 			new Path(folder 
       
   100 					+ File.separator 
       
   101 					+ file));
       
   102 	}
       
   103 	
       
   104 	/**
       
   105 	 * Get the module name.
       
   106 	 * 
       
   107 	 * @param folder folder to determine the module from
       
   108 	 * @return the module name
       
   109 	 */
       
   110 	public static String getModuleName(String folder) {
       
   111 		String[] folderParts = folder.split("/");
       
   112 		
       
   113 		if(folderParts.length > 0)
       
   114 			return folderParts[0];
       
   115 		else
       
   116 			return "";
       
   117 	}
       
   118 	
       
   119 	/**
       
   120 	 * Get the subsystem name.
       
   121 	 * 
       
   122 	 * @param folder the folder to determine the subsystem from
       
   123 	 * @return the subsystem name
       
   124 	 */
       
   125 	public static String getSubSystemName(String folder) {
       
   126 		String[] folderParts = folder.split("/");
       
   127 		
       
   128 		if(folderParts.length > 1)
       
   129 			return folderParts[1];
       
   130 		else
       
   131 			return "";
       
   132 	}
       
   133 	
       
   134 	public static int getCopyrightYear() {
       
   135 		return new GregorianCalendar().get(Calendar.YEAR);
       
   136 	}
       
   137 	
       
   138 	/**
       
   139 	 * Helper method for getting template file extensions
       
   140 	 * 
       
   141 	 * @param templates templates
       
   142 	 * @return extensions
       
   143 	 */
       
   144 	public static AbstractList<String> getFileExtensions(String templates) {
       
   145 		String[] templateFiles = templates.split(";");
       
   146 		
       
   147 		AbstractList<String> extensions = new ArrayList<String>();
       
   148 		
       
   149 		for(int i = 0; i < templateFiles.length; i++)
       
   150 			if(templateFiles[i].contains(".")) {
       
   151 				String extension = templateFiles[i].substring(
       
   152 						templateFiles[i].lastIndexOf('.'),
       
   153 						templateFiles[i].length());
       
   154 				
       
   155 				if(!extensions.contains(extension))
       
   156 					extensions.add(extension);
       
   157 			}
       
   158 	
       
   159 		return extensions;
       
   160 	}
       
   161 	
       
   162 	/**
       
   163 	 * Helper method for checking whether the selected directory already exists.
       
   164 	 * 
       
   165 	 * @return true if exists, false otherwise
       
   166 	 */
       
   167 	private static boolean selectedFolderExists(String folderName) {
       
   168 		String[] pathParts = folderName.split("/");
       
   169 		IProject project=null;
       
   170 		if(pathParts!=null && folderName.indexOf("\\")==-1 && !folderName.startsWith(".") && !folderName.startsWith("/"))
       
   171 		{
       
   172 			project = ResourcesPlugin.getWorkspace().getRoot().getProject(pathParts[0]);
       
   173 		}
       
   174 		else
       
   175 			return false;
       
   176 		
       
   177 		String directory = pathParts[0] + File.separator;
       
   178 		
       
   179 		if(pathParts.length > 1)
       
   180 			for(int i = 1; i < pathParts.length; i++)
       
   181 				directory += pathParts[i] + File.separator;
       
   182 		
       
   183 		IFolder folder = null;
       
   184 		
       
   185 		if(pathParts.length > 1)
       
   186 			folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path(directory));
       
   187 		
       
   188 		if(folder == null)
       
   189 			return project.exists();
       
   190 		else
       
   191 			return folder.exists();
       
   192 	}
       
   193 	/**
       
   194 	 * Helper method for checking whether the given file name is valid or not.
       
   195 	 * @param filename filename
       
   196 	 * @return true if it is valid.
       
   197 	 */
       
   198 	public static boolean checkFileName(String filename)
       
   199 	{
       
   200 		String[] restricted_chars={"&", "^", "+", "-", "@", "$", "%", "*", "(", ")", "|", "\\", "/", "[", "]", "{", "}", "<", ">", "?", ";", ":", ",", "'"};
       
   201 		
       
   202 		for (int i = 0; i < restricted_chars.length; i++) {
       
   203 			if(filename.indexOf(restricted_chars[i])!=-1)
       
   204 				return false;
       
   205 		}
       
   206 		if(filename.indexOf(".") != filename.lastIndexOf("."))
       
   207 			return false;
       
   208 			
       
   209 		return true;
       
   210 	}
       
   211 
       
   212 	/**
       
   213 	 * Helper method for getting default license.
       
   214 	 * @return License string
       
   215 	 */
       
   216 	public static String[] getDefaultLicence() {
       
   217 		return new String[] {"Symbian Foundation License v1.0","Eclipse Public License v1.0"};
       
   218 	}
       
   219 
       
   220 	/**
       
   221 	 * Helper method for getting the default company name.
       
   222 	 * @return Company name string.
       
   223 	 */
       
   224 	public static String getDefaultCompanyName() {
       
   225 		return "Nokia Corporation";
       
   226 	}
       
   227 
       
   228 	/**
       
   229 	 * Helper method for getting the default company copytight.
       
   230 	 * @return Company copyright string.
       
   231 	 */
       
   232 	public static String getDefaultCompanyCopyright() {
       
   233 		return "Nokia Corporation and/or its subsidiary(-ies)";
       
   234 	}
       
   235 }