/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

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