1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 1 | package org.vostokframework.configuration.parsers.xml |
30 | |
{ |
31 | |
import org.as3collections.IList; |
32 | |
import org.as3collections.IMap; |
33 | |
import org.as3collections.lists.ArrayList; |
34 | |
import org.as3collections.maps.HashMap; |
35 | |
import org.as3collections.utils.ListUtil; |
36 | |
import org.as3collections.utils.MapUtil; |
37 | |
import org.vostokframework.configuration.AssetConfiguration; |
38 | |
import org.vostokframework.configuration.parsers.xml.errors.XMLConfigurationParserError; |
39 | |
import org.vostokframework.domain.assets.AssetType; |
40 | |
import org.vostokframework.domain.loading.settings.LoadingSettings; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class AssetXMLNodeParser |
48 | |
{ |
49 | |
|
50 | |
private static const ASSET_ID_ATTRIBUTE_NAME:String = "id"; |
51 | |
private static const ASSET_SRC_ATTRIBUTE_NAME:String = "src"; |
52 | |
private static const ASSET_TYPE_ATTRIBUTE_NAME:String = "type"; |
53 | |
private static const SETTINGS_NODE_NAME:String = "settings"; |
54 | |
|
55 | |
private var _settingsParser:LoadingSettingsXMLNodeParser; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public function AssetXMLNodeParser(settingsParser:LoadingSettingsXMLNodeParser) |
61 | 1 | { |
62 | 1 | _settingsParser = settingsParser; |
63 | 1 | } |
64 | |
|
65 | |
public function parse(assets:XMLList):IList |
66 | |
{ |
67 | 1 | if (!assets) return null; |
68 | |
|
69 | 1 | var $assets:IList = parseAssets(assets); |
70 | 1 | return $assets; |
71 | |
} |
72 | |
|
73 | |
private function parseAssets(assetsXmlList:XMLList):IList |
74 | |
{ |
75 | 1 | if (!assetsXmlList) return null; |
76 | |
|
77 | 1 | var assets:IList = ListUtil.getTypedList(new ArrayList(), AssetConfiguration); |
78 | |
var assetConfiguration:AssetConfiguration; |
79 | |
var assetAttributesMap:IMap; |
80 | |
var id:String; |
81 | |
var src:String; |
82 | |
var type:AssetType; |
83 | 1 | var settingsNode:XML = <settings></settings>; |
84 | |
var settingsXMLList:XMLList; |
85 | |
var settings:LoadingSettings; |
86 | |
|
87 | 1 | for each (var assetNode:XML in assetsXmlList) |
88 | |
{ |
89 | 1 | assetAttributesMap = new HashMap(); |
90 | 1 | MapUtil.feedMapWithXmlList(assetAttributesMap, assetNode.attributes()); |
91 | |
|
92 | 1 | if (!assetAttributesMap.containsKey(ASSET_SRC_ATTRIBUTE_NAME)) |
93 | |
{ |
94 | 1 | var errorMessage:String = "Asset node must have < " + ASSET_SRC_ATTRIBUTE_NAME + "> attribute."; |
95 | 1 | throw new XMLConfigurationParserError(errorMessage); |
96 | |
} |
97 | |
|
98 | 1 | id = assetAttributesMap.getValue(ASSET_ID_ATTRIBUTE_NAME); |
99 | 1 | src = assetAttributesMap.getValue(ASSET_SRC_ATTRIBUTE_NAME); |
100 | 1 | if (assetAttributesMap.containsKey(ASSET_TYPE_ATTRIBUTE_NAME)) |
101 | |
{ |
102 | 1 | type = AssetType.getByName(assetAttributesMap.getValue(ASSET_TYPE_ATTRIBUTE_NAME)); |
103 | |
} |
104 | |
|
105 | |
|
106 | 1 | settingsXMLList = assetNode.attributes(); |
107 | 1 | for each(var att:XML in settingsXMLList) |
108 | |
{ |
109 | 1 | settingsNode.appendChild(<{att.localName()}>{att.toString()}</{att.localName()}>); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | 1 | settingsXMLList = assetNode.children().(name().localName == SETTINGS_NODE_NAME).children(); |
115 | 1 | settingsNode.appendChild(settingsXMLList); |
116 | |
|
117 | |
|
118 | 1 | settings = _settingsParser.parse(settingsNode); |
119 | |
|
120 | 1 | assetConfiguration = new AssetConfiguration(src, type, settings, id); |
121 | 1 | assets.add(assetConfiguration); |
122 | |
} |
123 | |
|
124 | 1 | return assets; |
125 | |
} |
126 | |
|
127 | |
} |
128 | |
|
129 | |
} |