Merge branch 'localfile'
[potlatch2.git] / net / systemeD / halcyon / NestedXMLLoader.as
1 package net.systemeD.halcyon {
2
3         /** A class permitting you to load XML files containing 'include' elements (for example,
4         *   <include file="cuisine.xml" />, which will be automatically replaced with the contents of the file.
5         *
6         *   Typical usage:
7         *
8         *               xmlLoader=new NestedXMLLoader();
9         *               xmlLoader.addEventListener(Event.COMPLETE, onFeatureLoad);
10         *               xmlLoader.load("root.xml");
11         *
12         *       onFeatureLoad can then access the XML via event.target.xml.
13         */
14
15     import net.systemeD.halcyon.FileBank;
16
17         import flash.events.*;
18
19         public class NestedXMLLoader extends EventDispatcher {
20                 public var xml:XML = null;
21                 private var count:int;
22
23                 public function NestedXMLLoader() {
24                 }
25                 
26                 public function load(url:String):void {
27             FileBank.getInstance().addFromFile(url, fileLoaded);
28                 }
29                 
30                 private function fileLoaded(fileBank:FileBank, filename:String):void {
31             count=1;
32                         xml = new XML(fileBank.getAsString(filename));
33                         for each (var inc:XML in xml.descendants('include')) {
34                                 count++;
35                                 replaceXML(inc);
36                         }
37             decreaseCount();
38                 }
39
40                 private function replaceXML(inc:XML):void {
41                         var xmlLoader:NestedXMLLoader=new NestedXMLLoader();
42                         var includeElement:XML=inc;
43                         xmlLoader.addEventListener(Event.COMPLETE, function(event:Event):void {
44                                 includeElement.parent().replace(findChildIndex(includeElement),event.target.xml);
45                                 decreaseCount();
46                         });
47                         xmlLoader.load(inc.@file);
48                 }
49
50                 private function findChildIndex(child:XML):int {
51                         var i:uint=0;
52                         for each (var sibling:XML in child.parent().children()) {
53                                 if (sibling==child) return i;
54                                 i++;
55                         }
56                         return -1;
57                 }
58                 
59                 private function decreaseCount():void {
60                         count--; if (count>0) return;
61                         fireComplete();
62                 }
63                 
64                 private function fireComplete():void {
65                         var event:Event=new Event(Event.COMPLETE);
66                         dispatchEvent(event);
67                 }
68         }
69 }