+++ /dev/null
-<?php\r
-/**\r
- * Name: ConversionManager class\r
- * Author: Antonio Gamba Bari - antonio.gambabari@utoronto.ca. ARTC, University of Toronto.\r
- * Last Update: July 22, 2009\r
- * \r
- * Description: This class performs all the conversion functions\r
- * 1. Start a conversion\r
- * 2. Retrieve a caption file\r
- * 3. Load all caption formats inc files\r
- * 3. Autodetect caption format\r
- * 4. Build a Collection of Captions\r
- * 5. Call export method from the proper CaptionFormat sub-class \r
- */\r
-\r
-class ConversionManager\r
-{\r
- private $ccStringTarget; // Caption Target file as String\r
- private $ccSourceFileName; // File Name of the Source caption \r
- private $ccTargetExt; // File Extension of the caption file \r
- private $ccTypeSource; // Class name of the source caption format\r
- private $ccTypeTarget; // Class name of the target caption format\r
- private $ccFormats = Array(); // array containing all the class names of the supported caption formats\r
- private $ccTargetUrl; // relative path to target caption file\r
- public $ccImportedCollection; // The target CaptionCollection Object\r
- \r
- \r
- /**\r
- * Class Constructor: Receives a caption string, parameters and returns the target caption as string\r
- *\r
- * @param String $theCaption The Caption File as String\r
- * @param String $theTarget The target Type = Class name of target sub-class\r
- * @param String $theFileName The name of the source caption file \r
- */\r
- public function __construct($theCaption,$theTarget,$theFileName)\r
- {\r
- global $rosettaCCSettings;\r
- /*\r
- * start conversion \r
- */\r
- // assign parameters to class members \r
- $this->ccTypeTarget = $theTarget;\r
- $this->ccSourceFileName = $theFileName;\r
- $this->ccTypeSource = ''; // initialize $ccTypeSource = ''\r
- \r
- // load caption formats \r
- $this->ccFormats = CcUtilVital::ccFormatsLoad();\r
-\r
- /*\r
- * run detection function on imported caption string\r
- * Note: the type of the source caption is not a parametter. This means that for stability \r
- * purposes the conversion tool MUST identify the format. Otherwhise, many unexpected and \r
- * incorrect conversions may occur \r
- */ \r
- $this->ccTypeSource = $this->_ccAutoDetect($theCaption); \r
- \r
- // proceed only if caption format is detected \r
- if($this->ccTypeSource!='')\r
- {\r
- //echo '<br/><br/> Caption format Detected: <b>'.$this->ccTypeSource.'</b>';\r
-\r
- /*\r
- * Invoques import function in caption format subclass\r
- * Note: here we know the name of the Caption format class from the $this->ccTypeSource (the value assigned by the autodetection function) \r
- */\r
- $ccSourceObj = new $this->ccTypeSource();\r
- \r
- // get a CaptionCollection Object \r
- $this->ccImportedCollection = $ccSourceObj->importCC($theCaption); \r
- \r
- //echo '<br/><br/>Uploaded Caption file was loaded into a CaptionCollection';\r
- \r
- // Initialize exported collection string\r
- $this->ccStringTarget = '';\r
- \r
- // run export function\r
- $this->ccStringTarget = $this->_ccExport();\r
- \r
- // print target format in a form for debuging purposes only\r
- //echo '<form><textarea name="textarea" cols="120" rows="10">'. $this->ccStringTarget . '</textarea></form>';\r
- \r
- //$this->_ccExport();\r
- \r
- // verify if the Target format has been provided \r
- if ($this->ccStringTarget!='')\r
- {\r
- // Save collection in DB as serialized string\r
- //DbTools::collectionInBdSave($this->ccImportedCollection, $theFileName);\r
- \r
- // FINALLY !! if all goes right save exported caption into a file \r
- $theConvertedFile = $this->_saveExported();\r
- // display player test option\r
- //PlayerTools::displayPlayerOp($theConvertedFile);\r
- \r
- // force downlaod: only when no comments or debuging mode \r
- //TxtFileTools::downloadFile($theConvertedFile);\r
- \r
- // set the relative path of the target caption file\r
- $this->ccTargetUrl = $theConvertedFile;\r
-\r
- }\r
-\r
- \r
- } else {\r
- echo 'The format of source Caption was not recognized.';\r
- };\r
- \r
- // return $ccSourceFormat\r
- \r
- }// end __construct() \r
-\r
- public function getCcTargetUrl()\r
- {\r
- return $this->ccTargetUrl; \r
- }\r
- \r
- /**\r
- * Gets the file name without extension of a file\r
- *\r
- * @param String $theFilename Name of the file\r
- * @return String $targetFileName\r
- */\r
- private function _buildExportFileName()\r
- {\r
- $targetFileName = substr($this->ccSourceFileName, 0, -3);\r
- return $targetFileName;\r
- }\r
- \r
- private function _saveExported()\r
- {\r
- //global $rosettaCCSettings;\r
- // build the proper name to save converted caption\r
- $theCCname = $this->_buildExportFileName().$this->ccTargetExt;\r
- \r
- //$fullFileUrl = $rosettaCCSettings['uploadDir'].'/'.$theCCname;\r
- \r
- // save exported collection in a file \r
- TxtFileTools::stringToFile($theCCname,$this->ccStringTarget);\r
-\r
- // return the generated file name\r
- return $theCCname;\r
- \r
- // download the generated caption file \r
- //CcUtil::downloadFile($fullFileUrl);\r
- \r
- }\r
-\r
- /**\r
- * Gets a CaptionCollection object and return the target caption file as string\r
- *\r
- * @return unknown\r
- */\r
- private function _ccExport()\r
- {\r
- $ccTargetString = '';\r
-\r
- //echo '<br/><br/>...creating an object of the target format: '.$this->ccTypeTarget;\r
- //echo '<br/><br/>I AM a '.$ccObjExp->getName().' Object';\r
-\r
- if ($this->ccTypeTarget!='0')\r
- {\r
- // create an instance of the target Caption Format\r
- $ccObjExp = new $this->ccTypeTarget();\r
- \r
- // get the extension of the target Caption\r
- $this->ccTargetExt = $ccObjExp->getFileExtension();\r
- //echo '<br/>The extension of the Target Caption is: '. $this->ccTargetExt; \r
- \r
- // call export method in caption format instance\r
- $ccTargetString = $ccObjExp->exportCC($this->ccImportedCollection);\r
-\r
- } // end if\r
- \r
- //echo '<br/>Target Collection: <br/>'.$ccTargetString;\r
- return $ccTargetString;\r
- } // end _ccExport()\r
- \r
- /**\r
- * Import caption as a sting and return a Caption Collection\r
- * @param String $theCaption Caption file as a String \r
- * @param String $theCcFormatClassName The class name of the caption source\r
- * @return CaptionCollection $importedCollection Return a Caption Collection Object \r
- */\r
- private function _ccImport($theCaption,$theCcFormatClassName)\r
- {\r
- // Create an instance of the source Caption Format\r
- $myCcSourceObj = new $theCcFormatClassName();\r
- \r
- // import the caption into a CaptionCollection Object\r
- return $myCcSourceObj->importCC($theCaption);\r
- \r
- } // _ccImport() end\r
-\r
- /**\r
- * Auto detect format of a caption string using the unique pattern provided by each caption format \r
- * @param String $theCaption Caption file as a String \r
- * @return String $formatfound Return detected caption format or '' if not found \r
- */\r
- private function _ccAutoDetect($theCaption)\r
- {\r
- //echo '<br/>Total Formats to Auto-detect: '.count($this->ccFormats);\r
- \r
- $formatfound = '';\r
- \r
- // instanciate each caption format sub-class and call getIdPattern() \r
- foreach ($this->ccFormats as $ccId)\r
- { \r
- // testing here a polymorphic behaviour \r
- //echo '<br/>.....Detecting format: '.$ccId;\r
- $theCcIdPattern = '';\r
- $ccObj = new $ccId(); // Create an instance of a caption format\r
- $theCcIdPattern = $ccObj->getIdPattern(); // get the pattern identifying the caption format\r
- //echo '<br/>***** '.$ccId.' = '.$theCcIdPattern;\r
- \r
- // look for the pattern in the caption string\r
- if (preg_match($theCcIdPattern,$theCaption)==1)\r
- {\r
- //echo '<br/>!! Caption Format Dettected!! ***** '.$ccId.' = '.$theCcIdPattern; \r
- //echo '<br/>Caption Format Dettected: <b>'.$ccId.'</b>';\r
- \r
- // set the detected format\r
- $formatfound = $ccId;\r
- \r
- return $formatfound;\r
- break; // stop detecting caption format\r
- }\r
-\r
- } // foreach end\r
-\r
- return $formatfound;\r
- \r
- } // _ccAutoDetect() end\r
- \r
- public function getCaptionFormats()\r
- {\r
- return $this->ccFormats;\r
- }\r
- \r
- public function getImportedCollection()\r
- {\r
- return $this->ccImportedCollection;\r
- }\r
-\r
- \r
-} // class end \r
-?>
\ No newline at end of file