org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/core/validator/WidgetValidator.java
changeset 50 0560e98b9bf6
child 98 b6d252d808df
equal deleted inserted replaced
43:464130c45935 50:0560e98b9bf6
       
     1 /**
       
     2  * Copyright (c) 2009 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "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  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.wrttools.core.validator;
       
    20 
       
    21 import java.io.File;
       
    22 import java.util.List;
       
    23 import java.util.logging.Level;
       
    24 import java.util.logging.Logger;
       
    25 
       
    26 import org.symbian.tools.wrttools.core.exception.ReportException;
       
    27 import org.symbian.tools.wrttools.core.exception.ValidationException;
       
    28 import org.symbian.tools.wrttools.core.report.IMessageListener;
       
    29 import org.symbian.tools.wrttools.core.report.MessageHandler;
       
    30 import org.symbian.tools.wrttools.core.status.IWRTConstants;
       
    31 import org.symbian.tools.wrttools.core.status.IWRTStatusListener;
       
    32 import org.symbian.tools.wrttools.core.status.WRTStatus;
       
    33 import org.symbian.tools.wrttools.core.status.WRTStatusHandler;
       
    34 import org.symbian.tools.wrttools.core.widgetmodel.WidgetModel;
       
    35 import org.symbian.tools.wrttools.util.Util;
       
    36 
       
    37 /**
       
    38  * The primary class for client to invoke various validation functionalities.  This is the only
       
    39  * class user to interact.  It has public methods to validate individual files html, plist, 
       
    40  * The methods of WidgetValidator class returns boolean to the calling class.   
       
    41  *  
       
    42  * The primary mechanism (class )for user/client  to invoke  various validation functionalities.
       
    43  * This is the only class user should interact with. It has Public methods for validating individual 
       
    44  * files (Plist, html ). This class provides method for validating a widget project and 
       
    45  * also a widget projectzipfile.
       
    46  * 
       
    47  * Instantiate widget model class here.  Iterate through the directory to get the file names, (.html, .plist, .css, .javascript)
       
    48  * parse through these files to validate.  
       
    49  * Validate to check if 1. .html, .plist are present or not.  
       
    50  *  
       
    51  * .html - will look for valid Document Type Definition (DTD) declaration. 
       
    52  *        (<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">)
       
    53  *  
       
    54  * .plist -  This file will be parsed to verify for the Nokia specific DTD 
       
    55  *        
       
    56  *            1. <key >DisplayName</key> - display name should exist
       
    57  *            2. <key>Identifier</key>  - value should be of com.nokia. **
       
    58  *            3. <key>MainHTML</key>    - value should be same as the .html file name
       
    59  * 
       
    60  *  
       
    61  * @author      sduvvuri
       
    62  * @version     1.0
       
    63  */
       
    64 public class WidgetValidator {
       
    65 //	private boolean widgetProjetValidation;
       
    66 	private boolean manFileVal;
       
    67 	private boolean plistVal;
       
    68 	private boolean htmlVal;
       
    69 	private MandatoryFilesValidator manFilevalidator;
       
    70 	private PlistValidator plistValidator;
       
    71 	private HtmlValidator htmlValidator;
       
    72 	
       
    73 	private MessageHandler messageHandler;
       
    74 	private IMessageListener msgListener;
       
    75 	private WRTStatusHandler statusHandler;
       
    76 	private IWRTStatusListener  wrtStatusListener;
       
    77 	
       
    78 	public List<String> filesSelected;
       
    79 	WidgetModel widgetModel;
       
    80 	private Logger log = Logger.getLogger(getClass().getName());
       
    81 //	  
       
    82 	
       
    83 			
       
    84 	/**
       
    85 	 * Class Constructor the packaging process
       
    86 	 */
       
    87 	public WidgetValidator(IWRTStatusListener  wrtStatusListener) {
       
    88 		statusHandler = new WRTStatusHandler();
       
    89 		statusHandler.addListener(wrtStatusListener);
       
    90 		this.wrtStatusListener = wrtStatusListener;
       
    91 	}
       
    92 //	
       
    93 	
       
    94 	/**
       
    95  * Method will validate a widget project.
       
    96  * Accept Variable widgetProjectDirName (Widget Project directory) and enumerate the files, call Individual validation procedures.
       
    97  * Even one validation is failed method will complete all validation and
       
    98  * add message to the message List.
       
    99  * A dummy List<IMessage> is passed as parameter to which the Rule methods will add
       
   100  * messages when a validation rules is failed.
       
   101  * Boolean variable will be returned to indicate the overall status of validation
       
   102  * even if one procedure is failed boolean false will be returned.
       
   103  * If returned value is false then List<IMessage> messageList size will be greater than zero
       
   104  * if returned value is true the List<IMessage> messageList will be empty
       
   105  * creating a dummy list to pass a parameter to the calling method
       
   106  * this list will be used by the rules method to add the validation messages
       
   107  * if any rules validation is failed
       
   108  * 
       
   109  * Passing List <IMessage> messageList will be removed from here.
       
   110  *  
       
   111  * @param fileName
       
   112  * @return
       
   113  * @throws ReportException 
       
   114  * @throws ValidationException 
       
   115  * 
       
   116  */
       
   117 	
       
   118 	WRTStatus status ;
       
   119 	
       
   120 	public WidgetValidator(IMessageListener msgListener,
       
   121 			IWRTStatusListener wrtStatusListener)
       
   122 
       
   123 	{
       
   124 		log.finest(" WidgetValidator>>--->>");
       
   125 		statusHandler = new WRTStatusHandler();
       
   126 		statusHandler.addListener(wrtStatusListener);
       
   127 		this.setWrtStatusListener(wrtStatusListener);
       
   128 		messageHandler = new MessageHandler();
       
   129 		messageHandler.registerListener(msgListener);
       
   130 		status = new WRTStatus();
       
   131 		status.setStatusSource(IWRTConstants.StatusSourceType.VALIDATOR.name());
       
   132 		log.finest(" WidgetValidator<<---<<");
       
   133 		
       
   134 	}
       
   135 
       
   136 	
       
   137 
       
   138 	public boolean validateWidgetProject(File widget) throws ValidationException, ReportException  {
       
   139 		
       
   140 		// Here call the WidgetModel class to return the  file.
       
   141 		log.info(" >>--->> validateWidgetProject");		
       
   142 		
       
   143 		 widgetModel= new WidgetModel();			
       
   144 		File widgetProjFile;
       
   145 //		MessageHandler	messageHandler.setFileTypeZip(false);
       
   146 		
       
   147 		try {
       
   148 						
       
   149 			widgetModel.setMessageHandler(getMessageHandler());
       
   150 			widgetModel.setStatusHandler(statusHandler);
       
   151 			widgetModel.setFilesSelected(getFilesSelected());
       
   152 			
       
   153 			 widgetModel.getWidgetModel(widget);
       
   154 			 widgetProjFile=widgetModel.getWidgetModelFile();
       
   155 			
       
   156 		log.info("widgetProjFile   :"+widgetProjFile.getName());
       
   157 			log.info(" calling ValidateMandatoryFiles ");
       
   158 		
       
   159 		ValidateMandatoryFiles(widgetProjFile);
       
   160 		log.info("ValidateMandatoryFiles  is done");
       
   161 		validatePlist( widgetProjFile);
       
   162 		log.info("Validate plist is done");
       
   163 		validateHtml( widgetProjFile);	
       
   164 		
       
   165 		} catch (ReportException e) {
       
   166 			 Util.logEvent(log, Level.INFO, e);	
       
   167 			 status.setStatusDescription(ValidatorPropMessages.getString("initialize.messagemanager"));
       
   168 			statusHandler.emitStatus(status);
       
   169 			throw(e);
       
   170 		}
       
   171 		log.finest("validateWidgetProject <<---<<");
       
   172 		
       
   173 		if (manFileVal && plistVal && htmlVal) {
       
   174 			return true;
       
   175 		} else {
       
   176 			return false;
       
   177 		}
       
   178 	}
       
   179 	
       
   180 	
       
   181 	public boolean validateWidgetProject(String widgetNameFullPath) throws ValidationException, ReportException  {
       
   182 		
       
   183 		// Here call the WidgetModel class to return the  file.
       
   184 		log.finest(" >>--->> validateWidgetProject");		
       
   185 		 widgetModel= new WidgetModel();			
       
   186 		File widgetProjFile;
       
   187 //		messageHandler.setFileTypeZip(false);	
       
   188 		try {
       
   189 			Util.showData(getFilesSelected(), "selected Files");
       
   190 						
       
   191 			widgetModel.setMessageHandler(getMessageHandler());
       
   192 			widgetModel.setStatusHandler(statusHandler);
       
   193 			widgetModel.setFilesSelected(getFilesSelected());
       
   194 			 widgetModel.getWidgetModel(widgetNameFullPath);
       
   195 			 widgetProjFile=widgetModel.getWidgetModelFile();
       
   196 			
       
   197 		log.info("widgetProjFile   :"+widgetProjFile.getName());
       
   198 		
       
   199 		ValidateMandatoryFiles(widgetProjFile);
       
   200 		validatePlist( widgetProjFile);
       
   201 		log.info("Validate plist is done");
       
   202 		validateHtml( widgetProjFile);	
       
   203 		
       
   204 		} catch (ReportException e) {
       
   205 			 Util.logEvent(log, Level.INFO, e);	
       
   206 //			 status.setStatusDescription(ValidatorPropMessages.getString("initialize.messagemanager"));
       
   207 //			statusHandler.emitStatus(status);
       
   208 			emitStatus(ValidatorPropMessages.getString("initialize.messagemanager"));
       
   209 			throw(e);
       
   210 		}
       
   211 		log.finest("validateWidgetProject <<---<<");
       
   212 		
       
   213 		if (manFileVal && plistVal && htmlVal) {
       
   214 			return true;
       
   215 		} else {
       
   216 			return false;
       
   217 		}
       
   218 	}
       
   219 
       
   220 /*
       
   221  * ReportHandler related function
       
   222  */
       
   223 	
       
   224 	public MessageHandler getMessageHandler() throws ReportException {
       
   225 		log.finest(" >>---<<");
       
   226 		if (messageHandler != null) {
       
   227 			return messageHandler;
       
   228 		} else {			
       
   229 			throw new ReportException(
       
   230 					"Message Manager  must be initialized  ");
       
   231 		}
       
   232 
       
   233 	}
       
   234 
       
   235 
       
   236 
       
   237 	// Creating new Message List.
       
   238 	private boolean ValidateMandatoryFiles(File widgetProjDir) throws ReportException, ValidationException
       
   239 			{
       
   240 		
       
   241 		log.finest("ValidateMandatoryFiles-->>-->>");
       
   242 		emitStatus(ValidatorPropMessages.getString("validate.man.file.started"));
       
   243 //		statusHandler.emitStatus(status);
       
   244 		manFilevalidator = new MandatoryFilesValidator();
       
   245 		manFilevalidator.setWidgetModel(widgetModel);
       
   246 		manFilevalidator.setSelectedFileList(filesSelected);
       
   247 		manFilevalidator.setMessageHandler(getMessageHandler());
       
   248 		manFileVal = manFilevalidator.validate(	widgetProjDir);
       
   249 
       
   250 		
       
   251 		emitStatus(ValidatorPropMessages.getString("validate.man.file.finished"));
       
   252 //		statusHandler.emitStatus(status);
       
   253 		log.finest("ValidateMandatoryFiles <<---<<");
       
   254 			
       
   255 		return manFileVal;
       
   256 	}
       
   257 	private boolean validatePlist(File fileName) throws ReportException, ValidationException{
       
   258 		log.finest("validatePlist-->>-->>");
       
   259 		emitStatus(ValidatorPropMessages.getString("validate.plist.started"));
       
   260 		
       
   261 //		statusHandler.emitStatus(status);
       
   262 		
       
   263 
       
   264 		plistValidator = new PlistValidator();
       
   265 		plistValidator.setWidgetModel(widgetModel);
       
   266 		plistValidator.setMessageHandler(getMessageHandler());
       
   267 		if (widgetModel.isPlistPresent()) {
       
   268 			plistValidator.setFilesToValidate(filesSelected);
       
   269 			plistVal = plistValidator.validate( fileName);
       
   270 		}
       
   271 		emitStatus(ValidatorPropMessages.getString("validate.plist.finished"));
       
   272 //		statusHandler.emitStatus(status);
       
   273 		
       
   274 		
       
   275 		log.finest("validatePlist--<<--<<");
       
   276 		return plistVal;
       
   277 	}
       
   278 	
       
   279 	
       
   280 	private boolean validateHtml(File fileName) throws ReportException {
       
   281 		log.info("validateHtml-->>-->>");
       
   282 		try{
       
   283 		emitStatus(ValidatorPropMessages.getString("validate.html.started"));
       
   284 //		statusHandler.emitStatus(status);		
       
   285 		htmlValidator=new HtmlValidator();
       
   286 		htmlValidator.setWidgetModel(widgetModel);
       
   287 		htmlValidator.setMessageHandler(getMessageHandler());
       
   288 		
       
   289 		if (widgetModel.isHtmlPresent()) {
       
   290 			htmlValidator.setHtmlPlistFileName(widgetModel.getMainHtml());
       
   291 			htmlVal = htmlValidator.validate( fileName);
       
   292 		}
       
   293 		log.info("validateHtml--<<--<<");
       
   294 		emitStatus(ValidatorPropMessages.getString("validate.html.finished"));
       
   295 //		statusHandler.emitStatus(status);
       
   296 		} catch (Exception e) {
       
   297 			 Util.logEvent(log, Level.INFO, e);	
       
   298 			 status.setStatusDescription("Exception "+e.getMessage());
       
   299 				
       
   300 		}
       
   301 		return htmlVal;
       
   302 
       
   303 	}
       
   304 	
       
   305 	protected void emitStatus(String statusDescription) {
       
   306 		WRTStatus status = new WRTStatus();
       
   307 		status.setStatusSource(IWRTConstants.StatusSourceType.VALIDATOR.name());
       
   308 		status.setStatusDescription(statusDescription);		
       
   309 		getWrtStatusListener().emitStatus(status);
       
   310 	}
       
   311 	
       
   312 	public IMessageListener getMsgListener() {
       
   313 		return msgListener;
       
   314 	}
       
   315 
       
   316 
       
   317 	public void setMsgListener(IMessageListener msgListener) {
       
   318 		this.msgListener = msgListener;
       
   319 	}
       
   320 
       
   321 
       
   322 	public IWRTStatusListener getWrtStatusListener() {
       
   323 		return wrtStatusListener;
       
   324 	}
       
   325 
       
   326 
       
   327 	public void setWrtStatusListener(IWRTStatusListener wrtStatusListener) {
       
   328 		this.wrtStatusListener = wrtStatusListener;
       
   329 	}
       
   330 
       
   331 
       
   332 	public List<String> getFilesSelected() {
       
   333 		return filesSelected;
       
   334 	}
       
   335 
       
   336 
       
   337 	public void setFilesSelected(List<String> filesToValidate) {
       
   338 		this.filesSelected = filesToValidate;
       
   339 		
       
   340 		 if (filesSelected != null && filesSelected.size() > 0) {
       
   341 				for (String fileName : filesSelected) {
       
   342 					fileName= Util.replaceChar(fileName, '\\', '/');
       
   343 				}
       
   344 			}
       
   345 	}
       
   346 
       
   347 
       
   348 	public WidgetModel getWidgetModel() {
       
   349 		return widgetModel;
       
   350 	}
       
   351 
       
   352 
       
   353 	public void setWidgetModel(WidgetModel widgetModel) {
       
   354 		this.widgetModel = widgetModel;
       
   355 	}
       
   356 
       
   357 
       
   358 
       
   359 
       
   360 
       
   361 	public void setMessageHandler(MessageHandler messageHandler) {
       
   362 		this.messageHandler = messageHandler;
       
   363 	}
       
   364 	
       
   365 	
       
   366 	
       
   367 
       
   368 	
       
   369 	///**
       
   370 	// * validateWidgetZip function takes zip file as a parameter.
       
   371 	// * Enumerate the files, and parse through the files.
       
   372 	// * 
       
   373 	// * Input parameter List <IMessage> messageList will be removed from here.
       
   374 	// * 
       
   375 	// * @param zipfile
       
   376 	// * @return
       
   377 	// * @throws BaseException 
       
   378 	// * @throws BaseException 
       
   379 	// * @throws ValidatorException 
       
   380 	// */	
       
   381 //		private boolean validateWidgetZip(String fileName) throws BaseException, ValidatorException{
       
   382 //			// HERE need to call the WidgetModel class to return the  file.
       
   383 //			log.finest(" validateWidgetZip >>--->>");
       
   384 //			WidgetModel widgetModel= new WidgetModel();			
       
   385 //			File widgetProjFile;
       
   386 ////			messageManager.setMessageList(null);
       
   387 //			messageManager.setFileTypeZip(true);
       
   388 //			widgetProjFile = widgetModel.getWidgetModelFromZip(fileName, getMessageManager());
       
   389 //			log.info("widgetProjFile   :"+widgetProjFile.getName());
       
   390 //			ValidateMandatoryFiles(widgetProjFile);
       
   391 //			validatePlist( widgetProjFile);
       
   392 //			log.info("Validate plist is done");
       
   393 //			validateHtml( widgetProjFile);	
       
   394 //			FileUtil fileUtil = new FileUtil();		
       
   395 //			fileUtil.deleteDirFile(  widgetProjFile);
       
   396 //			log.finest("validateWidgetZip <<---<<");
       
   397 //			
       
   398 //			if (manFileVal && plistVal && htmlVal) {
       
   399 //				return true;
       
   400 //			} else {
       
   401 //				return false;
       
   402 //			}
       
   403 //		}
       
   404 
       
   405 }