mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-21 09:37:50 -05:00
Brought up to date with the private repository (feea4c7)
This commit is contained in:
1 parent
1aa738d692
commit
1b1df1ae48
7925 files changed
+2617237
-20
No files matched your search
@@ -0,0 +1,34 @@
|
||||
package org.osmf.captioning
|
||||
{
|
||||
import org.osmf.captioning.media.CaptioningProxyElement;
|
||||
import org.osmf.media.MediaElement;
|
||||
import org.osmf.media.MediaFactoryItem;
|
||||
import org.osmf.media.MediaFactoryItemType;
|
||||
import org.osmf.media.PluginInfo;
|
||||
import org.osmf.net.NetLoader;
|
||||
|
||||
public class CaptioningPluginInfo extends PluginInfo
|
||||
{
|
||||
|
||||
public static const CAPTIONING_METADATA_NAMESPACE:String = "http://www.osmf.org/captioning/1.0";
|
||||
|
||||
public static const CAPTIONING_METADATA_KEY_URI:String = "uri";
|
||||
|
||||
public static const CAPTIONING_TEMPORAL_METADATA_NAMESPACE:String = "http://www.osmf.org/temporal/captioning";
|
||||
|
||||
public function CaptioningPluginInfo()
|
||||
{
|
||||
var items:Vector.<MediaFactoryItem> = new Vector.<MediaFactoryItem>();
|
||||
var loader:NetLoader = new NetLoader();
|
||||
var item:MediaFactoryItem = new MediaFactoryItem("org.osmf.captioning.CaptioningPluginInfo",loader.canHandleResource,this.createCaptioningProxyElement,MediaFactoryItemType.PROXY);
|
||||
items.push(item);
|
||||
super(items);
|
||||
}
|
||||
|
||||
private function createCaptioningProxyElement() : MediaElement
|
||||
{
|
||||
return new CaptioningProxyElement();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package org.osmf.captioning.loader
|
||||
{
|
||||
import org.osmf.captioning.model.CaptioningDocument;
|
||||
import org.osmf.media.MediaResourceBase;
|
||||
import org.osmf.traits.LoadTrait;
|
||||
import org.osmf.traits.LoaderBase;
|
||||
|
||||
public class CaptioningLoadTrait extends LoadTrait
|
||||
{
|
||||
|
||||
private var _document:CaptioningDocument;
|
||||
|
||||
public function CaptioningLoadTrait(loader:LoaderBase, resource:MediaResourceBase)
|
||||
{
|
||||
super(loader,resource);
|
||||
}
|
||||
|
||||
public function get document() : CaptioningDocument
|
||||
{
|
||||
return this._document;
|
||||
}
|
||||
|
||||
public function set document(value:CaptioningDocument) : void
|
||||
{
|
||||
this._document = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package org.osmf.captioning.loader
|
||||
{
|
||||
import org.osmf.captioning.model.CaptioningDocument;
|
||||
import org.osmf.captioning.parsers.DFXPParser;
|
||||
import org.osmf.captioning.parsers.ICaptioningParser;
|
||||
import org.osmf.events.LoaderEvent;
|
||||
import org.osmf.events.MediaErrorEvent;
|
||||
import org.osmf.media.MediaResourceBase;
|
||||
import org.osmf.traits.LoadState;
|
||||
import org.osmf.traits.LoadTrait;
|
||||
import org.osmf.traits.LoaderBase;
|
||||
import org.osmf.utils.HTTPLoadTrait;
|
||||
import org.osmf.utils.HTTPLoader;
|
||||
|
||||
public class CaptioningLoader extends LoaderBase
|
||||
{
|
||||
|
||||
private var httpLoader:HTTPLoader;
|
||||
|
||||
public function CaptioningLoader(httpLoader:HTTPLoader = null)
|
||||
{
|
||||
super();
|
||||
this.httpLoader = httpLoader != null ? httpLoader : new HTTPLoader();
|
||||
}
|
||||
|
||||
override public function canHandleResource(resource:MediaResourceBase) : Boolean
|
||||
{
|
||||
return this.httpLoader.canHandleResource(resource);
|
||||
}
|
||||
|
||||
override protected function executeLoad(loadTrait:LoadTrait) : void
|
||||
{
|
||||
var httpLoadTrait:HTTPLoadTrait = null;
|
||||
var onHTTPLoaderStateChange:Function = null;
|
||||
var onLoadError:Function = null;
|
||||
onHTTPLoaderStateChange = function(event:LoaderEvent):void
|
||||
{
|
||||
var parser:ICaptioningParser = null;
|
||||
var captioningDocument:CaptioningDocument = null;
|
||||
if(event.newState == LoadState.READY)
|
||||
{
|
||||
httpLoader.removeEventListener(LoaderEvent.LOAD_STATE_CHANGE,onHTTPLoaderStateChange);
|
||||
httpLoadTrait.removeEventListener(MediaErrorEvent.MEDIA_ERROR,onLoadError);
|
||||
parser = createCaptioningParser();
|
||||
try
|
||||
{
|
||||
captioningDocument = parser.parse(httpLoadTrait.urlLoader.data.toString());
|
||||
}
|
||||
catch(e:Error)
|
||||
{
|
||||
updateLoadTrait(loadTrait,LoadState.LOAD_ERROR);
|
||||
}
|
||||
CaptioningLoadTrait(loadTrait).document = captioningDocument;
|
||||
updateLoadTrait(loadTrait,LoadState.READY);
|
||||
}
|
||||
else if(event.newState == LoadState.LOAD_ERROR)
|
||||
{
|
||||
httpLoader.removeEventListener(LoaderEvent.LOAD_STATE_CHANGE,onHTTPLoaderStateChange);
|
||||
updateLoadTrait(loadTrait,event.newState);
|
||||
}
|
||||
};
|
||||
onLoadError = function(event:MediaErrorEvent):void
|
||||
{
|
||||
httpLoadTrait.removeEventListener(MediaErrorEvent.MEDIA_ERROR,onLoadError);
|
||||
loadTrait.dispatchEvent(event.clone());
|
||||
};
|
||||
updateLoadTrait(loadTrait,LoadState.LOADING);
|
||||
this.httpLoader.addEventListener(LoaderEvent.LOAD_STATE_CHANGE,onHTTPLoaderStateChange);
|
||||
httpLoadTrait = new HTTPLoadTrait(this.httpLoader,loadTrait.resource);
|
||||
httpLoadTrait.addEventListener(MediaErrorEvent.MEDIA_ERROR,onLoadError);
|
||||
this.httpLoader.load(httpLoadTrait);
|
||||
}
|
||||
|
||||
override protected function executeUnload(loadTrait:LoadTrait) : void
|
||||
{
|
||||
updateLoadTrait(loadTrait,LoadState.UNLOADING);
|
||||
updateLoadTrait(loadTrait,LoadState.UNINITIALIZED);
|
||||
}
|
||||
|
||||
protected function createCaptioningParser() : ICaptioningParser
|
||||
{
|
||||
return new DFXPParser();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
package org.osmf.captioning.media
|
||||
{
|
||||
import org.osmf.captioning.CaptioningPluginInfo;
|
||||
import org.osmf.captioning.loader.CaptioningLoadTrait;
|
||||
import org.osmf.captioning.loader.CaptioningLoader;
|
||||
import org.osmf.captioning.model.Caption;
|
||||
import org.osmf.captioning.model.CaptioningDocument;
|
||||
import org.osmf.elements.ProxyElement;
|
||||
import org.osmf.events.LoadEvent;
|
||||
import org.osmf.events.MediaError;
|
||||
import org.osmf.events.MediaErrorEvent;
|
||||
import org.osmf.media.MediaElement;
|
||||
import org.osmf.media.MediaResourceBase;
|
||||
import org.osmf.media.URLResource;
|
||||
import org.osmf.metadata.Metadata;
|
||||
import org.osmf.metadata.TimelineMetadata;
|
||||
import org.osmf.traits.LoadState;
|
||||
import org.osmf.traits.LoadTrait;
|
||||
import org.osmf.traits.MediaTraitType;
|
||||
|
||||
public class CaptioningProxyElement extends ProxyElement
|
||||
{
|
||||
|
||||
public static const MEDIA_ERROR_INVALID_PROXIED_ELEMENT:int = 2201;
|
||||
|
||||
private static const ERROR_MISSING_CAPTION_METADATA:String = "Media Element is missing Captioning metadata";
|
||||
|
||||
private static const ERROR_MISSING_RESOURCE:String = "Media Element is missing a valid resource";
|
||||
|
||||
private var loadTrait:CaptioningLoadTrait;
|
||||
|
||||
private var _continueLoadOnFailure:Boolean;
|
||||
|
||||
public function CaptioningProxyElement(proxiedElement:MediaElement = null, continueLoadOnFailure:Boolean = true)
|
||||
{
|
||||
super(proxiedElement);
|
||||
this._continueLoadOnFailure = continueLoadOnFailure;
|
||||
}
|
||||
|
||||
public function get continueLoadOnFailure() : Boolean
|
||||
{
|
||||
return this._continueLoadOnFailure;
|
||||
}
|
||||
|
||||
override public function set proxiedElement(value:MediaElement) : void
|
||||
{
|
||||
var mediaElement:MediaElement = null;
|
||||
var tempResource:MediaResourceBase = null;
|
||||
var metadata:Metadata = null;
|
||||
var timedTextURL:String = null;
|
||||
super.proxiedElement = value;
|
||||
if(value != null)
|
||||
{
|
||||
mediaElement = super.proxiedElement;
|
||||
tempResource = Boolean(mediaElement) && mediaElement.resource != null ? mediaElement.resource : resource;
|
||||
if(tempResource == null)
|
||||
{
|
||||
dispatchEvent(new MediaErrorEvent(MediaErrorEvent.MEDIA_ERROR,false,false,new MediaError(MEDIA_ERROR_INVALID_PROXIED_ELEMENT)));
|
||||
}
|
||||
else
|
||||
{
|
||||
metadata = tempResource.getMetadataValue(CaptioningPluginInfo.CAPTIONING_METADATA_NAMESPACE) as Metadata;
|
||||
if(metadata == null)
|
||||
{
|
||||
if(!this._continueLoadOnFailure)
|
||||
{
|
||||
dispatchEvent(new MediaErrorEvent(MediaErrorEvent.MEDIA_ERROR,false,false,new MediaError(MEDIA_ERROR_INVALID_PROXIED_ELEMENT)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
timedTextURL = metadata.getValue(CaptioningPluginInfo.CAPTIONING_METADATA_KEY_URI);
|
||||
if(timedTextURL != null)
|
||||
{
|
||||
this.loadTrait = new CaptioningLoadTrait(new CaptioningLoader(),new URLResource(timedTextURL));
|
||||
this.loadTrait.addEventListener(LoadEvent.LOAD_STATE_CHANGE,this.onLoadStateChange,false,99);
|
||||
addTrait(MediaTraitType.LOAD,this.loadTrait);
|
||||
}
|
||||
else if(!this._continueLoadOnFailure)
|
||||
{
|
||||
dispatchEvent(new MediaErrorEvent(MediaErrorEvent.MEDIA_ERROR,false,false,new MediaError(MEDIA_ERROR_INVALID_PROXIED_ELEMENT)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function onLoadStateChange(event:LoadEvent) : void
|
||||
{
|
||||
var document:CaptioningDocument = null;
|
||||
var mediaElement:MediaElement = null;
|
||||
var captionMetadata:TimelineMetadata = null;
|
||||
var i:int = 0;
|
||||
var caption:Caption = null;
|
||||
if(event.loadState == LoadState.READY)
|
||||
{
|
||||
document = this.loadTrait.document;
|
||||
if(Boolean(document))
|
||||
{
|
||||
mediaElement = super.proxiedElement;
|
||||
captionMetadata = proxiedElement.getMetadata(CaptioningPluginInfo.CAPTIONING_TEMPORAL_METADATA_NAMESPACE) as TimelineMetadata;
|
||||
if(captionMetadata == null)
|
||||
{
|
||||
captionMetadata = new TimelineMetadata(proxiedElement);
|
||||
proxiedElement.addMetadata(CaptioningPluginInfo.CAPTIONING_TEMPORAL_METADATA_NAMESPACE,captionMetadata);
|
||||
}
|
||||
for(i = 0; i < document.numCaptions; i++)
|
||||
{
|
||||
caption = document.getCaptionAt(i);
|
||||
captionMetadata.addMarker(caption);
|
||||
}
|
||||
}
|
||||
this.cleanUp();
|
||||
}
|
||||
else if(event.loadState == LoadState.LOAD_ERROR)
|
||||
{
|
||||
if(!this._continueLoadOnFailure)
|
||||
{
|
||||
dispatchEvent(event.clone());
|
||||
}
|
||||
else
|
||||
{
|
||||
this.cleanUp();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function cleanUp() : void
|
||||
{
|
||||
removeTrait(MediaTraitType.LOAD);
|
||||
var loadTrait:LoadTrait = getTrait(MediaTraitType.LOAD) as LoadTrait;
|
||||
if(loadTrait != null && loadTrait.loadState == LoadState.UNINITIALIZED)
|
||||
{
|
||||
loadTrait.load();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package org.osmf.captioning.model
|
||||
{
|
||||
import flash.errors.IllegalOperationError;
|
||||
import org.osmf.metadata.TimelineMarker;
|
||||
import org.osmf.utils.OSMFStrings;
|
||||
|
||||
public class Caption extends TimelineMarker
|
||||
{
|
||||
|
||||
private var _id:uint;
|
||||
|
||||
private var _captionText:String;
|
||||
|
||||
private var _formats:Vector.<CaptionFormat>;
|
||||
|
||||
public function Caption(id:uint, start:Number, end:Number, captionText:String)
|
||||
{
|
||||
var duration:Number = end > 0 ? end - start : NaN;
|
||||
super(start,duration);
|
||||
this._id = id;
|
||||
this._captionText = captionText;
|
||||
}
|
||||
|
||||
public function addCaptionFormat(value:CaptionFormat) : void
|
||||
{
|
||||
if(this._formats == null)
|
||||
{
|
||||
this._formats = new Vector.<CaptionFormat>();
|
||||
}
|
||||
this._formats.push(value);
|
||||
}
|
||||
|
||||
public function get numCaptionFormats() : int
|
||||
{
|
||||
var num:int = 0;
|
||||
if(this._formats != null)
|
||||
{
|
||||
num = int(this._formats.length);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public function getCaptionFormatAt(index:int) : CaptionFormat
|
||||
{
|
||||
if(this._formats == null || index >= this._formats.length || index < 0)
|
||||
{
|
||||
throw new IllegalOperationError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
|
||||
}
|
||||
return this._formats[index];
|
||||
}
|
||||
|
||||
public function get text() : String
|
||||
{
|
||||
return this._captionText;
|
||||
}
|
||||
|
||||
public function get clearText() : String
|
||||
{
|
||||
var clrTxt:String = "";
|
||||
if(this._captionText != null && this._captionText.length > 0)
|
||||
{
|
||||
clrTxt = this._captionText.replace(/<(.|\n)*?>/g,"");
|
||||
}
|
||||
return clrTxt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.osmf.captioning.model
|
||||
{
|
||||
public class CaptionFormat
|
||||
{
|
||||
|
||||
public static const UNDEFINED_INDEX:int = -1;
|
||||
|
||||
private static const MISSING_STYLE_ERROR_MSG:String = "CaptionStyle is required for CaptionFormat objects";
|
||||
|
||||
private var _startIndex:int;
|
||||
|
||||
private var _endIndex:int;
|
||||
|
||||
private var _style:CaptionStyle;
|
||||
|
||||
public function CaptionFormat(style:CaptionStyle, start:int = -1, end:int = -1)
|
||||
{
|
||||
super();
|
||||
if(style == null)
|
||||
{
|
||||
throw new ArgumentError(MISSING_STYLE_ERROR_MSG);
|
||||
}
|
||||
this._startIndex = start;
|
||||
this._endIndex = end;
|
||||
this._style = style;
|
||||
}
|
||||
|
||||
public function get startIndex() : int
|
||||
{
|
||||
return this._startIndex;
|
||||
}
|
||||
|
||||
public function get endIndex() : int
|
||||
{
|
||||
return this._endIndex;
|
||||
}
|
||||
|
||||
public function get style() : CaptionStyle
|
||||
{
|
||||
return this._style;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package org.osmf.captioning.model
|
||||
{
|
||||
public class CaptionStyle
|
||||
{
|
||||
|
||||
private var _id:String;
|
||||
|
||||
private var _backgroundColor:Object;
|
||||
|
||||
private var _backgroundColorAlpha:Object;
|
||||
|
||||
private var _textColor:Object;
|
||||
|
||||
private var _textColorAlpha:Object;
|
||||
|
||||
private var _fontFamily:String;
|
||||
|
||||
private var _fontSize:int;
|
||||
|
||||
private var _fontStyle:String;
|
||||
|
||||
private var _fontWeight:String;
|
||||
|
||||
private var _textAlign:String;
|
||||
|
||||
private var _wrapOption:Boolean;
|
||||
|
||||
public function CaptionStyle(id:String)
|
||||
{
|
||||
super();
|
||||
this._id = id;
|
||||
}
|
||||
|
||||
public function get id() : String
|
||||
{
|
||||
return this._id;
|
||||
}
|
||||
|
||||
public function get backgroundColor() : Object
|
||||
{
|
||||
return this._backgroundColor;
|
||||
}
|
||||
|
||||
public function set backgroundColor(value:Object) : void
|
||||
{
|
||||
this._backgroundColor = value;
|
||||
}
|
||||
|
||||
public function get backgroundColorAlpha() : Object
|
||||
{
|
||||
return this._backgroundColorAlpha;
|
||||
}
|
||||
|
||||
public function set backgroundColorAlpha(value:Object) : void
|
||||
{
|
||||
this._backgroundColorAlpha = value;
|
||||
}
|
||||
|
||||
public function get textColor() : Object
|
||||
{
|
||||
return this._textColor;
|
||||
}
|
||||
|
||||
public function set textColor(value:Object) : void
|
||||
{
|
||||
this._textColor = value;
|
||||
}
|
||||
|
||||
public function get textColorAlpha() : Object
|
||||
{
|
||||
return this._textColorAlpha;
|
||||
}
|
||||
|
||||
public function set textColorAlpha(value:Object) : void
|
||||
{
|
||||
this._textColorAlpha = value;
|
||||
}
|
||||
|
||||
public function get fontFamily() : String
|
||||
{
|
||||
return this._fontFamily;
|
||||
}
|
||||
|
||||
public function set fontFamily(value:String) : void
|
||||
{
|
||||
this._fontFamily = value;
|
||||
}
|
||||
|
||||
public function get fontSize() : int
|
||||
{
|
||||
return this._fontSize;
|
||||
}
|
||||
|
||||
public function set fontSize(value:int) : void
|
||||
{
|
||||
this._fontSize = value;
|
||||
}
|
||||
|
||||
public function get fontStyle() : String
|
||||
{
|
||||
return this._fontStyle;
|
||||
}
|
||||
|
||||
public function set fontStyle(value:String) : void
|
||||
{
|
||||
this._fontStyle = value;
|
||||
}
|
||||
|
||||
public function get fontWeight() : String
|
||||
{
|
||||
return this._fontWeight;
|
||||
}
|
||||
|
||||
public function set fontWeight(value:String) : void
|
||||
{
|
||||
this._fontWeight = value;
|
||||
}
|
||||
|
||||
public function get textAlign() : String
|
||||
{
|
||||
return this._textAlign;
|
||||
}
|
||||
|
||||
public function set textAlign(value:String) : void
|
||||
{
|
||||
this._textAlign = value;
|
||||
}
|
||||
|
||||
public function get wrapOption() : Boolean
|
||||
{
|
||||
return this._wrapOption;
|
||||
}
|
||||
|
||||
public function set wrapOption(value:Boolean) : void
|
||||
{
|
||||
this._wrapOption = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package org.osmf.captioning.model
|
||||
{
|
||||
import flash.errors.IllegalOperationError;
|
||||
import org.osmf.utils.OSMFStrings;
|
||||
|
||||
public class CaptioningDocument
|
||||
{
|
||||
|
||||
private var _title:String;
|
||||
|
||||
private var _desc:String;
|
||||
|
||||
private var _copyright:String;
|
||||
|
||||
private var _captions:Vector.<Caption>;
|
||||
|
||||
private var _styles:Vector.<CaptionStyle>;
|
||||
|
||||
public function CaptioningDocument()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public function get title() : String
|
||||
{
|
||||
return this._title;
|
||||
}
|
||||
|
||||
public function set title(value:String) : void
|
||||
{
|
||||
this._title = value;
|
||||
}
|
||||
|
||||
public function get description() : String
|
||||
{
|
||||
return this._desc;
|
||||
}
|
||||
|
||||
public function set description(value:String) : void
|
||||
{
|
||||
this._desc = value;
|
||||
}
|
||||
|
||||
public function get copyright() : String
|
||||
{
|
||||
return this._copyright;
|
||||
}
|
||||
|
||||
public function set copyright(value:String) : void
|
||||
{
|
||||
this._copyright = value;
|
||||
}
|
||||
|
||||
public function addStyle(style:CaptionStyle) : void
|
||||
{
|
||||
if(this._styles == null)
|
||||
{
|
||||
this._styles = new Vector.<CaptionStyle>();
|
||||
}
|
||||
this._styles.push(style);
|
||||
}
|
||||
|
||||
public function get numStyles() : int
|
||||
{
|
||||
var num:int = 0;
|
||||
if(this._styles != null)
|
||||
{
|
||||
num = int(this._styles.length);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public function getStyleAt(index:int) : CaptionStyle
|
||||
{
|
||||
if(this._styles == null || index >= this._styles.length)
|
||||
{
|
||||
throw new IllegalOperationError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
|
||||
}
|
||||
return this._styles[index];
|
||||
}
|
||||
|
||||
public function addCaption(caption:Caption) : void
|
||||
{
|
||||
if(this._captions == null)
|
||||
{
|
||||
this._captions = new Vector.<Caption>();
|
||||
}
|
||||
this._captions.push(caption);
|
||||
}
|
||||
|
||||
public function get numCaptions() : int
|
||||
{
|
||||
var num:int = 0;
|
||||
if(this._captions != null)
|
||||
{
|
||||
num = int(this._captions.length);
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
public function getCaptionAt(index:int) : Caption
|
||||
{
|
||||
if(this._captions == null || index >= this._captions.length)
|
||||
{
|
||||
throw new IllegalOperationError(OSMFStrings.getString(OSMFStrings.INVALID_PARAM));
|
||||
}
|
||||
return this._captions[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,500 @@
|
||||
package org.osmf.captioning.parsers
|
||||
{
|
||||
import flash.utils.Dictionary;
|
||||
import org.osmf.captioning.model.Caption;
|
||||
import org.osmf.captioning.model.CaptionFormat;
|
||||
import org.osmf.captioning.model.CaptionStyle;
|
||||
import org.osmf.captioning.model.CaptioningDocument;
|
||||
import org.osmf.utils.TimeUtil;
|
||||
|
||||
public class DFXPParser implements ICaptioningParser
|
||||
{
|
||||
|
||||
private var ns:Namespace;
|
||||
|
||||
private var ttm:Namespace;
|
||||
|
||||
private var tts:Namespace;
|
||||
|
||||
private var rootNamespace:Namespace;
|
||||
|
||||
private var namedColorMap:Dictionary;
|
||||
|
||||
public function DFXPParser()
|
||||
{
|
||||
super();
|
||||
this.namedColorMap = new Dictionary();
|
||||
this.namedColorMap["transparent"] = 0;
|
||||
this.namedColorMap["black"] = 0;
|
||||
this.namedColorMap["silver"] = 12632256;
|
||||
this.namedColorMap["gray"] = 8421504;
|
||||
this.namedColorMap["white"] = 16777215;
|
||||
this.namedColorMap["maroon"] = 8388608;
|
||||
this.namedColorMap["red"] = 16711680;
|
||||
this.namedColorMap["purple"] = 8388736;
|
||||
this.namedColorMap["fuchsia"] = 16711935;
|
||||
this.namedColorMap["magenta"] = 16711935;
|
||||
this.namedColorMap["green"] = 32768;
|
||||
this.namedColorMap["lime"] = 65280;
|
||||
this.namedColorMap["olive"] = 8421376;
|
||||
this.namedColorMap["yellow"] = 16776960;
|
||||
this.namedColorMap["navy"] = 128;
|
||||
this.namedColorMap["blue"] = 255;
|
||||
this.namedColorMap["teal"] = 32896;
|
||||
this.namedColorMap["aqua"] = 65535;
|
||||
this.namedColorMap["cyan"] = 65535;
|
||||
}
|
||||
|
||||
public function parse(rawData:String) : CaptioningDocument
|
||||
{
|
||||
var xml:XML = null;
|
||||
var document:CaptioningDocument = new CaptioningDocument();
|
||||
var saveXMLIgnoreWhitespace:Boolean = Boolean(XML.ignoreWhitespace);
|
||||
var saveXMLPrettyPrinting:Boolean = Boolean(XML.prettyPrinting);
|
||||
var xmlStr:String = rawData.replace(/\s+$/,"");
|
||||
xmlStr = xmlStr.replace(/>\s+</g,"><");
|
||||
XML.ignoreWhitespace = false;
|
||||
XML.prettyPrinting = false;
|
||||
try
|
||||
{
|
||||
xml = new XML(xmlStr);
|
||||
}
|
||||
catch(e:Error)
|
||||
{
|
||||
debugLog("Unhandled exception in DFXPParser : " + e.message);
|
||||
throw e;
|
||||
}
|
||||
finally
|
||||
{
|
||||
XML.ignoreWhitespace = saveXMLIgnoreWhitespace;
|
||||
XML.prettyPrinting = saveXMLPrettyPrinting;
|
||||
}
|
||||
this.rootNamespace = xml.namespace();
|
||||
this.ns = xml.namespace();
|
||||
this.ttm = xml.namespace("ttm");
|
||||
this.tts = xml.namespace("tts");
|
||||
try
|
||||
{
|
||||
this.parseHead(document,xml);
|
||||
this.parseBody(document,xml);
|
||||
}
|
||||
catch(err:Error)
|
||||
{
|
||||
debugLog("Unhandled exception in DFXPParser : " + err.message);
|
||||
throw err;
|
||||
}
|
||||
return document;
|
||||
}
|
||||
|
||||
private function parseHead(doc:CaptioningDocument, xml:XML) : void
|
||||
{
|
||||
var styling:XMLList;
|
||||
var styles:XMLList = null;
|
||||
var i:uint = 0;
|
||||
var styleNode:XML = null;
|
||||
var styleObj:CaptionStyle = null;
|
||||
try
|
||||
{
|
||||
doc.title = xml..this.ttm::title.text();
|
||||
doc.description = xml..this.ttm::desc.text();
|
||||
doc.copyright = xml..this.ttm::copyright.text();
|
||||
}
|
||||
catch(err:Error)
|
||||
{
|
||||
if(err.errorID != 1080)
|
||||
{
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
styling = xml..this.ns::styling;
|
||||
if(Boolean(styling.length()))
|
||||
{
|
||||
styles = styling.children();
|
||||
for(i = 0; i < styles.length(); i++)
|
||||
{
|
||||
styleNode = styles[i];
|
||||
styleObj = this.createStyleObject(styleNode);
|
||||
doc.addStyle(styleObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function createStyleObject(styleNode:XML) : CaptionStyle
|
||||
{
|
||||
var colorValue:Object = null;
|
||||
var id:String = styleNode.@id;
|
||||
var styleObj:CaptionStyle = new CaptionStyle(id);
|
||||
var color:String = styleNode.this.tts::@backgroundColor;
|
||||
if(color != "")
|
||||
{
|
||||
colorValue = this.parseColor(color);
|
||||
styleObj.backgroundColor = colorValue.color;
|
||||
styleObj.backgroundColorAlpha = colorValue.alpha;
|
||||
}
|
||||
styleObj.textAlign = styleNode.this.tts::@textAlign;
|
||||
color = styleNode.this.tts::@color;
|
||||
if(color != "")
|
||||
{
|
||||
colorValue = this.parseColor(color);
|
||||
styleObj.textColor = colorValue.color;
|
||||
styleObj.textColorAlpha = colorValue.alpha;
|
||||
}
|
||||
styleObj.fontFamily = this.parseFontFamily(styleNode.this.tts::@fontFamily);
|
||||
var fontSize:String = this.parseFontSize(styleNode.this.tts::@fontSize);
|
||||
styleObj.fontSize = parseInt(fontSize);
|
||||
styleObj.fontStyle = styleNode.this.tts::@fontStyle;
|
||||
styleObj.fontWeight = styleNode.this.tts::@fontWeight;
|
||||
styleObj.wrapOption = String(styleNode.this.tts::@wrapOption).toLowerCase() == "nowrap" ? false : true;
|
||||
return styleObj;
|
||||
}
|
||||
|
||||
private function parseColor(colorStr:String) : Object
|
||||
{
|
||||
var colorValue:Object = null;
|
||||
var alphaValue:Object = null;
|
||||
var pattern:RegExp = /^\s*#([\dA-Fa-f][\dA-Fa-f][\dA-Fa-f][\dA-Fa-f][\dA-Fa-f][\dA-Fa-f])([\dA-Fa-f][\dA-Fa-f])?\s*$/;
|
||||
var result:Array = colorStr.match(pattern);
|
||||
if(result != null)
|
||||
{
|
||||
colorValue = parseInt(result[1],16);
|
||||
if(result.length == 3)
|
||||
{
|
||||
alphaValue = Number(parseInt(result[2]) / 255);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pattern = /^\s*rgb(a)?\((\d+),(\d+),(\d+)(\)|(,(\d+)\)))\s*$/i;
|
||||
result = colorStr.match(pattern);
|
||||
if(result != null && result.length >= 5)
|
||||
{
|
||||
colorValue = (parseInt(result[2]) << 16) + (parseInt(result[3]) << 8) + parseInt(result[4]);
|
||||
if(result.length == 8 && result[7] != undefined)
|
||||
{
|
||||
alphaValue = Number(parseInt(result[7]) / 255);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
colorValue = this.namedColorMap[colorStr.toLowerCase()];
|
||||
if(colorValue == null)
|
||||
{
|
||||
colorValue = parseInt(colorStr);
|
||||
if(isNaN(int(colorValue)))
|
||||
{
|
||||
colorValue = null;
|
||||
this.debugLog("Invalid DFXP document: invalid color value of " + colorStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
"color":colorValue,
|
||||
"alpha":alphaValue
|
||||
};
|
||||
}
|
||||
|
||||
private function parseFontSize(rawFontSize:String) : String
|
||||
{
|
||||
var incRegExp:RegExp = null;
|
||||
var incResult:Object = null;
|
||||
var regExp:RegExp = null;
|
||||
var result:Object = null;
|
||||
if(!rawFontSize || rawFontSize == "")
|
||||
{
|
||||
return "";
|
||||
}
|
||||
var returnValue:String = "";
|
||||
var perRegExp:RegExp = new RegExp(/^\s*\d+%.*$/);
|
||||
var perResult:Object = perRegExp.exec(rawFontSize);
|
||||
if(Boolean(perResult))
|
||||
{
|
||||
this.debugLog("Invalid DFXP document: percentages are not supported for font size.");
|
||||
returnValue = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
incRegExp = new RegExp(/^\s*[\+\-]\d.*/);
|
||||
incResult = incRegExp.exec(rawFontSize);
|
||||
if(Boolean(incResult))
|
||||
{
|
||||
this.debugLog("Invalid DFXP document: increment values are not supported for font size.");
|
||||
returnValue = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
regExp = new RegExp(/^\s*(\d+).*$/);
|
||||
result = regExp.exec(rawFontSize);
|
||||
if(Boolean(result) && result[1] != undefined)
|
||||
{
|
||||
returnValue = result[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
private function parseFontFamily(rawFontFamily:String) : String
|
||||
{
|
||||
var result:Object = null;
|
||||
if(!rawFontFamily || rawFontFamily == "")
|
||||
{
|
||||
return "";
|
||||
}
|
||||
var retVal:String = "";
|
||||
var regExp:RegExp = new RegExp(/^\s*([^,\s]+)\s*((,\s*([^,\s]+)\s*)*)$/);
|
||||
var done:Boolean = false;
|
||||
do
|
||||
{
|
||||
result = regExp.exec(rawFontFamily);
|
||||
if(!result)
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(retVal.length > 0)
|
||||
{
|
||||
retVal += ",";
|
||||
}
|
||||
switch(result[1])
|
||||
{
|
||||
case "default":
|
||||
case "serif":
|
||||
case "proportionalSerif":
|
||||
retVal += "_serif";
|
||||
break;
|
||||
case "monospace":
|
||||
case "monospaceSansSerif":
|
||||
case "monospaceSerif":
|
||||
retVal += "_typewriter";
|
||||
break;
|
||||
case "sansSerif":
|
||||
case "proportionalSansSerif":
|
||||
retVal += "_sans";
|
||||
break;
|
||||
default:
|
||||
retVal += result[1];
|
||||
}
|
||||
if(result[2] != undefined && result[2] != "")
|
||||
{
|
||||
rawFontFamily = result[2].slice(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
while(!done);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
private function parseBody(doc:CaptioningDocument, xml:XML) : void
|
||||
{
|
||||
var div:XMLList = null;
|
||||
var p:XMLList = null;
|
||||
var i:uint = 0;
|
||||
var pNode:XML = null;
|
||||
var body:XMLList = xml..this.ns::body;
|
||||
if(body.length() <= 0)
|
||||
{
|
||||
this.debugLog("Invalid DFXP document: <body> tag is required.");
|
||||
}
|
||||
else
|
||||
{
|
||||
div = xml..this.ns::div;
|
||||
p = Boolean(div.length()) ? div.children() : (Boolean(body.length()) ? body.children() : new XMLList());
|
||||
for(i = 0; i < p.length(); i++)
|
||||
{
|
||||
pNode = p[i];
|
||||
if(this.rootNamespace == pNode.namespace())
|
||||
{
|
||||
this.parsePTag(doc,pNode,i);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.debugLog("Ignoring this tag, foreign namespaces not supported: \"" + pNode + "\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function parsePTag(doc:CaptioningDocument, pNode:XML, index:uint) : void
|
||||
{
|
||||
var text:String = null;
|
||||
var child:XML = null;
|
||||
var styleStartIndex:uint = 0;
|
||||
var spanText:String = null;
|
||||
var stylesList:Array = null;
|
||||
var styleEndIndex:uint = 0;
|
||||
var styleObject:CaptionStyle = null;
|
||||
var fmtObj:CaptionFormat = null;
|
||||
var formatObj:CaptionFormat = null;
|
||||
var begin:String = pNode.@begin;
|
||||
var end:String = pNode.@end;
|
||||
var dur:String = pNode.@dur;
|
||||
if(begin == "")
|
||||
{
|
||||
begin = "0s";
|
||||
}
|
||||
var beginSecs:Number = TimeUtil.parseTime(begin);
|
||||
var endSecs:Number = 0;
|
||||
if(end != "")
|
||||
{
|
||||
endSecs = TimeUtil.parseTime(end);
|
||||
}
|
||||
else if(dur != "")
|
||||
{
|
||||
endSecs = beginSecs + TimeUtil.parseTime(dur);
|
||||
}
|
||||
var captionFormatList:Array = new Array();
|
||||
text = new String("<p>");
|
||||
var children:XMLList = pNode.children();
|
||||
for(var i:uint = 0; i < children.length(); i++)
|
||||
{
|
||||
child = children[i];
|
||||
switch(child.nodeKind())
|
||||
{
|
||||
case "text":
|
||||
text += this.formatCCText(child.toString());
|
||||
break;
|
||||
case "element":
|
||||
switch(child.localName())
|
||||
{
|
||||
case "set":
|
||||
case "metadata":
|
||||
break;
|
||||
case "span":
|
||||
styleStartIndex = uint(this.calcClearTextLength(text));
|
||||
stylesList = new Array();
|
||||
text += this.parseSpanTag(doc,child,stylesList);
|
||||
styleEndIndex = uint(this.calcClearTextLength(text));
|
||||
for each(styleObject in stylesList)
|
||||
{
|
||||
fmtObj = new CaptionFormat(styleObject,styleStartIndex,styleEndIndex);
|
||||
captionFormatList.push(fmtObj);
|
||||
}
|
||||
break;
|
||||
case "br":
|
||||
text += "<br />";
|
||||
break;
|
||||
default:
|
||||
text += this.formatCCText(child.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
text += "</p>";
|
||||
var captionItem:Caption = new Caption(index,beginSecs,endSecs,text);
|
||||
var styleObj:CaptionStyle = this.parseStyleAttrib(doc,pNode);
|
||||
if(Boolean(styleObj))
|
||||
{
|
||||
formatObj = new CaptionFormat(styleObj);
|
||||
captionItem.addCaptionFormat(formatObj);
|
||||
}
|
||||
for(i = 0; i < captionFormatList.length; i++)
|
||||
{
|
||||
captionItem.addCaptionFormat(captionFormatList[i]);
|
||||
}
|
||||
doc.addCaption(captionItem);
|
||||
}
|
||||
|
||||
private function parseStyleAttrib(doc:CaptioningDocument, node:XML) : CaptionStyle
|
||||
{
|
||||
var i:uint = 0;
|
||||
var attributes:XMLList = null;
|
||||
var attrib:XML = null;
|
||||
var localName:String = null;
|
||||
var styleId:String = node.@style;
|
||||
var captionStyle:CaptionStyle = null;
|
||||
if(styleId != "")
|
||||
{
|
||||
while(i < doc.numStyles)
|
||||
{
|
||||
if(doc.getStyleAt(i).id == styleId)
|
||||
{
|
||||
captionStyle = doc.getStyleAt(i);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
attributes = node.this.tts::@*;
|
||||
}
|
||||
catch(err:Error)
|
||||
{
|
||||
if(err.errorID != 1080)
|
||||
{
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
i = 0;
|
||||
while(attributes != null && i < attributes.length())
|
||||
{
|
||||
attrib = attributes[i];
|
||||
localName = attrib.localName();
|
||||
if(localName != "inherit")
|
||||
{
|
||||
captionStyle = this.createStyleObject(node);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return captionStyle;
|
||||
}
|
||||
|
||||
private function parseSpanTag(doc:CaptioningDocument, spanNode:XML, styles:Array) : String
|
||||
{
|
||||
var child:XML = null;
|
||||
var styleObj:CaptionStyle = this.parseStyleAttrib(doc,spanNode);
|
||||
if(styleObj != null)
|
||||
{
|
||||
styles.push(styleObj);
|
||||
}
|
||||
var ccText:String = new String();
|
||||
var children:XMLList = spanNode.children();
|
||||
for(var i:uint = 0; i < children.length(); i++)
|
||||
{
|
||||
child = children[i];
|
||||
switch(child.nodeKind())
|
||||
{
|
||||
case "text":
|
||||
ccText += this.formatCCText(child.toString());
|
||||
break;
|
||||
case "element":
|
||||
switch(child.localName())
|
||||
{
|
||||
case "set":
|
||||
case "metadata":
|
||||
break;
|
||||
case "br":
|
||||
ccText += "<br/>";
|
||||
break;
|
||||
default:
|
||||
ccText += child.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return ccText;
|
||||
}
|
||||
|
||||
private function formatCCText(txt:String) : String
|
||||
{
|
||||
return txt.replace(/\s+/g," ");
|
||||
}
|
||||
|
||||
private function calcClearTextLength(txt:String) : int
|
||||
{
|
||||
var clrTxt:String = txt.replace(/<(.|\n)*?>/g,"");
|
||||
return clrTxt.length;
|
||||
}
|
||||
|
||||
private function debugLog(msg:String) : void
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.osmf.captioning.parsers
|
||||
{
|
||||
import org.osmf.captioning.model.CaptioningDocument;
|
||||
|
||||
public interface ICaptioningParser
|
||||
{
|
||||
|
||||
function parse(param1:String) : CaptioningDocument;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user