Provide some documentation as to what each of the 4 Connection classes are for.
[potlatch2.git] / net / systemeD / halcyon / connection / XMLBaseConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.*;
4
5         import flash.system.Security;
6         import flash.net.*;
7     import org.iotashan.oauth.*;
8
9         import net.systemeD.halcyon.Globals;
10
11     /**
12     * XMLBaseConnection is the common code between connecting to an OSM server
13     * (i.e. XMLConnection) and connecting to a standalone .osm file (i.e. OSMConnection)
14     * and so mainly concerns itself with /map -call-ish related matters
15     */
16         public class XMLBaseConnection extends Connection {
17
18                 public function XMLBaseConnection() {
19                 }
20                 
21         protected function loadedMap(event:Event):void {
22             dispatchEvent(new Event(LOAD_COMPLETED));
23
24             var map:XML = new XML(URLLoader(event.target).data);
25             var id:Number;
26             var version:uint;
27             var tags:Object;
28
29             for each(var relData:XML in map.relation) {
30                 id = Number(relData.@id);
31                 version = uint(relData.@version);
32                 
33                 var rel:Relation = getRelation(id);
34                 if ( rel == null || !rel.loaded ) {
35                     tags = parseTags(relData.tag);
36                     var members:Array = [];
37                     for each(var memberXML:XML in relData.member) {
38                         var type:String = memberXML.@type.toLowerCase();
39                         var role:String = memberXML.@role;
40                         var memberID:Number = Number(memberXML.@ref);
41                         var member:Entity = null;
42                         if ( type == "node" ) {
43                             member = getNode(memberID);
44                             if ( member == null ) {
45                                 member = new Node(memberID,0,{},false,0,0);
46                                 setNode(Node(member),true);
47                             }
48                         } else if ( type == "way" ) {
49                             member = getWay(memberID);
50                             if (member == null) {
51                                 member = new Way(memberID,0,{},false,[]);
52                                 setWay(Way(member),true);
53                             }
54                         } else if ( type == "relation" ) {
55                             member = getRelation(memberID);
56                             if (member == null) {
57                                 member = new Relation(memberID,0,{},false,[]);
58                                 setRelation(Relation(member),true);
59                             }
60                         }
61                         
62                         if ( member != null )
63                             members.push(new RelationMember(member, role));
64                     }
65                     
66                     if ( rel == null )
67                         setRelation(new Relation(id, version, tags, true, members), false);
68                     else {
69                         rel.update(version,tags,true,members);
70                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
71                     }
72                 }
73             }
74             
75             for each(var nodeData:XML in map.node) {
76                 id = Number(nodeData.@id);
77                 version = uint(nodeData.@version);
78
79                 var node:Node = getNode(id);
80                 if ( node == null || !node.loaded ) {
81                     var lat:Number = Number(nodeData.@lat);
82                     var lon:Number = Number(nodeData.@lon);
83                     tags = parseTags(nodeData.tag);
84                     if ( node == null )
85                         setNode(new Node(id, version, tags, true, lat, lon),false);
86                     else {
87                         node.update(version, tags, true, lat, lon);
88                         sendEvent(new EntityEvent(NEW_NODE, node), false);
89                     }
90                 }
91             }
92
93             for each(var data:XML in map.way) {
94                 id = Number(data.@id);
95                 version = uint(data.@version);
96
97                 var way:Way = getWay(id);
98                 if ( way == null || !way.loaded ) {
99                     var nodes:Array = [];
100                     for each(var nd:XML in data.nd)
101                         nodes.push(getNode(Number(nd.@ref)));
102                     tags = parseTags(data.tag);
103                     if ( way == null )
104                         setWay(new Way(id, version, tags, true, nodes),false);
105                     else {
106                         way.update(version, tags, true, nodes);
107                         sendEvent(new EntityEvent(NEW_WAY, way), false);
108                     }
109                 }
110             }
111             
112             registerPOINodes();
113         }
114         
115         protected function registerPOINodes():void {
116             for each (var nodeID:Number in getAllNodeIDs()) {
117                 var node:Node = getNode(nodeID);
118                 if (!node.hasParentWays)
119                     registerPOI(node);
120             }
121         }
122
123         private function parseTags(tagElements:XMLList):Object {
124             var tags:Object = {};
125             for each (var tagEl:XML in tagElements)
126                 tags[tagEl.@k] = tagEl.@v;
127             return tags;
128         }
129
130         }
131 }