PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/src/away3d/library/naming/NumSuffixConflictStrategy.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 63 lines | 47 code | 11 blank | 5 comment | 7 complexity | 4fdf216d52e126743f6a1aed770fe746 MD5 | raw file
 1package away3d.library.naming
 2{
 3	import away3d.arcane;
 4	import away3d.library.assets.IAsset;
 5	
 6	use namespace arcane;
 7	
 8	public class NumSuffixConflictStrategy extends ConflictStrategyBase
 9	{
10		private var _separator:String;
11		private var _next_suffix:Object;
12		
13		public function NumSuffixConflictStrategy(separator:String = '.')
14		{
15			super();
16			
17			_separator = separator;
18			_next_suffix = {};
19		}
20		
21		public override function resolveConflict(changedAsset:IAsset, oldAsset:IAsset, assetsDictionary:Object, precedence:String):void
22		{
23			var orig:String;
24			var new_name:String;
25			var base:String, suffix:int;
26			
27			orig = changedAsset.name;
28			if (orig.indexOf(_separator) >= 0) {
29				// Name has an ocurrence of the separator, so get base name and suffix,
30				// unless suffix is non-numerical, in which case revert to zero and 
31				// use entire name as base
32				base = orig.substring(0, orig.lastIndexOf(_separator));
33				suffix = parseInt(orig.substring(base.length - 1));
34				if (isNaN(suffix)) {
35					base = orig;
36					suffix = 0;
37				}
38			} else {
39				base = orig;
40				suffix = 0;
41			}
42			
43			if (suffix == 0 && _next_suffix.hasOwnProperty(base))
44				suffix = _next_suffix[base];
45			
46			// Find the first suffixed name that does
47			// not collide with other names.
48			do {
49				suffix++;
50				new_name = base.concat(_separator, suffix);
51			} while (assetsDictionary.hasOwnProperty(new_name));
52			
53			_next_suffix[base] = suffix;
54			
55			updateNames(oldAsset.assetNamespace, new_name, oldAsset, changedAsset, assetsDictionary, precedence);
56		}
57		
58		public override function create():ConflictStrategyBase
59		{
60			return new NumSuffixConflictStrategy(_separator);
61		}
62	}
63}