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.domain.assets |
30 | |
{ |
31 | |
import org.as3collections.IIterator; |
32 | |
import org.as3collections.IMap; |
33 | |
import org.as3collections.maps.HashMap; |
34 | |
import org.as3utils.StringUtil; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class UrlAssetParser |
42 | |
{ |
43 | |
private var _typeRegexpMap:IMap; |
44 | |
|
45 | |
public function UrlAssetParser() |
46 | 1 | { |
47 | 1 | _typeRegexpMap = new HashMap(); |
48 | |
|
49 | 1 | var aacExt:Array = getAACExtensions(); |
50 | 1 | var cssExt:Array = getCSSExtensions(); |
51 | 1 | var imgExt:Array = getImageExtensions(); |
52 | 1 | var jsonExt:Array = getJsonExtensions(); |
53 | 1 | var mp3Ext:Array = getMp3Extensions(); |
54 | 1 | var swfExt:Array = getSWFExtensions(); |
55 | 1 | var txtExt:Array = getTxtExtensions(); |
56 | 1 | var videoExt:Array = getVideoExtensions(); |
57 | 1 | var xmlExt:Array = getXMLExtensions(); |
58 | |
|
59 | 1 | _typeRegexpMap.put(AssetType.AAC, getRegexp(aacExt)); |
60 | 1 | _typeRegexpMap.put(AssetType.CSS, getRegexp(cssExt)); |
61 | 1 | _typeRegexpMap.put(AssetType.IMAGE, getRegexp(imgExt)); |
62 | 1 | _typeRegexpMap.put(AssetType.JSON, getRegexp(jsonExt)); |
63 | 1 | _typeRegexpMap.put(AssetType.MP3, getRegexp(mp3Ext)); |
64 | 1 | _typeRegexpMap.put(AssetType.SWF, getRegexp(swfExt)); |
65 | 1 | _typeRegexpMap.put(AssetType.TXT, getRegexp(txtExt)); |
66 | 1 | _typeRegexpMap.put(AssetType.VIDEO, getRegexp(videoExt)); |
67 | 1 | _typeRegexpMap.put(AssetType.XML, getRegexp(xmlExt)); |
68 | 1 | } |
69 | |
|
70 | |
public function getAssetType(src:String):AssetType |
71 | |
{ |
72 | 1 | if (StringUtil.isBlank(src)) throw new ArgumentError("Argument <src> must not be null nor an empty String."); |
73 | |
|
74 | |
var type:AssetType; |
75 | |
var regexp:RegExp; |
76 | 1 | var it:IIterator = _typeRegexpMap.iterator(); |
77 | |
|
78 | 1 | while (it.hasNext()) |
79 | |
{ |
80 | 1 | regexp = it.next(); |
81 | 1 | type = it.pointer(); |
82 | |
|
83 | 1 | if (src.search(regexp) != -1) return type; |
84 | |
} |
85 | |
|
86 | 1 | return null; |
87 | |
} |
88 | |
|
89 | |
private function getAACExtensions(): Array |
90 | |
{ |
91 | 1 | var ext:Array = []; |
92 | 1 | ext.push("aac"); |
93 | 1 | ext.push("m4a"); |
94 | 1 | ext.push("m4b"); |
95 | 1 | ext.push("m4p"); |
96 | 1 | ext.push("m4v"); |
97 | 1 | ext.push("m4r"); |
98 | |
|
99 | 1 | return ext; |
100 | |
} |
101 | |
|
102 | |
private function getCSSExtensions(): Array |
103 | |
{ |
104 | 1 | var ext:Array = []; |
105 | 1 | ext.push("css"); |
106 | |
|
107 | 1 | return ext; |
108 | |
} |
109 | |
|
110 | |
private function getImageExtensions(): Array |
111 | |
{ |
112 | 1 | var ext:Array = []; |
113 | 1 | ext.push("jpg"); |
114 | 1 | ext.push("png"); |
115 | |
|
116 | 1 | return ext; |
117 | |
} |
118 | |
|
119 | |
private function getJsonExtensions(): Array |
120 | |
{ |
121 | 1 | var ext:Array = []; |
122 | 1 | ext.push("json"); |
123 | |
|
124 | 1 | return ext; |
125 | |
} |
126 | |
|
127 | |
private function getMp3Extensions(): Array |
128 | |
{ |
129 | 1 | var ext:Array = []; |
130 | 1 | ext.push("mp3"); |
131 | |
|
132 | 1 | return ext; |
133 | |
} |
134 | |
|
135 | |
private function getSWFExtensions(): Array |
136 | |
{ |
137 | 1 | var ext:Array = []; |
138 | 1 | ext.push("swf"); |
139 | |
|
140 | 1 | return ext; |
141 | |
} |
142 | |
|
143 | |
private function getTxtExtensions(): Array |
144 | |
{ |
145 | 1 | var ext:Array = []; |
146 | 1 | ext.push("asp"); |
147 | 1 | ext.push("aspx"); |
148 | 1 | ext.push("jsp"); |
149 | 1 | ext.push("php"); |
150 | 1 | ext.push("txt"); |
151 | |
|
152 | 1 | return ext; |
153 | |
} |
154 | |
|
155 | |
private function getVideoExtensions(): Array |
156 | |
{ |
157 | 1 | var ext:Array = []; |
158 | 1 | ext.push("flv"); |
159 | 1 | ext.push("f4v"); |
160 | 1 | ext.push("mov"); |
161 | 1 | ext.push("mpg"); |
162 | 1 | ext.push("mpeg"); |
163 | 1 | ext.push("mp4"); |
164 | 1 | ext.push("mp4v"); |
165 | 1 | ext.push("3gp"); |
166 | 1 | ext.push("3g2"); |
167 | |
|
168 | 1 | return ext; |
169 | |
} |
170 | |
|
171 | |
private function getXMLExtensions(): Array |
172 | |
{ |
173 | 1 | var ext:Array = []; |
174 | 1 | ext.push("xml"); |
175 | |
|
176 | 1 | return ext; |
177 | |
} |
178 | |
|
179 | |
private function getRegexp(ext:Array): RegExp |
180 | |
{ |
181 | 1 | var l:int = ext.length; |
182 | 1 | var r:String = "^.+\.("; |
183 | |
|
184 | 1 | for (var i:int = 0; i < l; i++) |
185 | |
{ |
186 | 1 | r += "(" + ext[i] + ")"; |
187 | 1 | if (i + 1 < l) r += "|"; |
188 | |
} |
189 | |
|
190 | 1 | r += ")"; |
191 | |
|
192 | 1 | var regexp:RegExp = new RegExp(r, "i"); |
193 | |
|
194 | 1 | return regexp; |
195 | |
} |
196 | |
|
197 | |
} |
198 | |
|
199 | |
} |