Archive for the 'Actionscript' Category

Support CSS state selector with ID selector for Spark SkinnableContainer

Wednesday, August 18th, 2010

Main.mxml

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
< ?xml version="1.0" encoding="utf-8"?>
<s :WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx" 
					   xmlns:local="*"
					   creationComplete="creationCompleteHandler(event)">
	<fx :Style>
		@namespace s "library://ns.adobe.com/flex/spark";
		@namespace mx "library://ns.adobe.com/flex/mx";
		@namespace local "*";
 
		local|MyTestContainer #textDisplay
		{
			color:#0099FF;
		}
 
		local|MyTestContainer:green #textDisplay
		{
			color:#00FF99;
		}
 
		local|MyTestContainer:red #textDisplay
		{
			color:#FF9900;
		}				
	</fx>
 
	<fx :Script>
		< ![CDATA[
			import mx.collections.ArrayCollection;
			import mx.collections.IList;
			import mx.events.FlexEvent;
 
			import spark.events.IndexChangeEvent;
 
			[Bindable]
			private var stateSelectorDataProvider:IList;
 
			protected function stateSelector_changeHandler(event:IndexChangeEvent):void
			{
				myTestContainer.currentState = event.currentTarget.selectedItem.name;
			}
 
 
			protected function creationCompleteHandler(event:FlexEvent):void
			{
				stateSelectorDataProvider = new ArrayCollection(myTestContainer.states);
			}
 
		]]>
	</fx>
	<fx :Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx>
	</s><s :layout>
		<s :VerticalLayout/>
	</s>
 
	<s :DropDownList id="stateSelector" dataProvider="{stateSelectorDataProvider}" 
					change="stateSelector_changeHandler(event)"
					selectedIndex="-1" 
					labelField="name"/>
	<local :MyTestContainer id="myTestContainer" skinClass="MyTestContainerSkin"/>

MyTestContainer.mxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
< ?xml version="1.0" encoding="utf-8"?>
<components :MySkinnableContainer xmlns:fx="http://ns.adobe.com/mxml/2009" 
													 xmlns:s="library://ns.adobe.com/flex/spark" 
													 xmlns:mx="library://ns.adobe.com/flex/mx" 
													 xmlns:components="com.experiment.framework.components.*" 
													 width="400" height="300"
													 currentState="normal">
	<fx :Metadata>
		[SkinState("normal")]
		[SkinState("green")]
		[SkinState("red")]
	</fx>
	<fx :Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx>
	</components><components :states>
		<s :State name="normal"/>
		<s :State name="green"/>
		<s :State name="red"/>
	</components>
	<s :Label id="textDisplay" text="This is Css State Selector Support"/>

MySkinnableTestContainer.as

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.experiment.framework.components
{
	import mx.styles.IStyleClient;
 
	import spark.components.SkinnableContainer;
 
	public class MySkinnableContainer extends SkinnableContainer
	{
		public function MySkinnableContainer()
		{
			super();
		}
 
		override public function setCurrentState(stateName:String, playTransition:Boolean=true):void
		{
			super.setCurrentState(stateName,playTransition);
			invalidateSkinState();
		}
 
		override public function invalidateSkinState():void
		{
			super.invalidateSkinState();
			var styleClient:IStyleClient = this as IStyleClient;
			var styleName:Object;
 
			if(styleClient)
			{
				if(styleClient.styleName == null)
				{
					styleClient.styleName = {};
					styleClient.styleName = null;
				}
				else
				{
					styleName = styleClient.styleName;
					styleClient.styleName = null;
					styleClient.styleName = styleName;
				}
			}
		}
 
		override protected function getCurrentSkinState():String
		{
			return currentState;
		}
 
	}
}

MyTestContainerSkin.mxml

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
< ?xml version="1.0" encoding="utf-8"?>
 
<!--
 
    ADOBE SYSTEMS INCORPORATED
    Copyright 2008 Adobe Systems Incorporated
    All Rights Reserved.
 
    NOTICE: Adobe permits you to use, modify, and distribute this file
    in accordance with the terms of the license agreement accompanying it.
 
-->
 
<!--- The default skin class for a Spark SkinnableContainer container.  
 
     @see spark.components.SkinnableContainer
 
      @langversion 3.0
      @playerversion Flash 10
      @playerversion AIR 1.5
      @productversion Flex 4
-->
<s :Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:fb="http://ns.adobe.com/flashbuilder/2009" alpha.disabled="0.5">
 
    <fx :Metadata>
    < ![CDATA[ 
        /** 
         * @copy spark.skins.spark.ApplicationSkin#hostComponent
         */
        [HostComponent("spark.components.SkinnableContainer")]
    ]]>
    </fx> 
 
    </s><s :states>
        <s :State name="normal" />
        <s :State name="disabled" />
		<s :State name="green"/>
		<s :State name="red"/>
    </s>
 
    <!--- Defines the appearance of the SkinnableContainer class's background. -->
    <s :Rect id="background" left="0" right="0" top="0" bottom="0">
        </s><s :fill>
            <!--- @private -->
            <s :SolidColor id="bgFill" color="#FFFFFF"/>
        </s>
 
 
    <!--
        Note: setting the minimum size to 0 here so that changes to the host component's
        size will not be thwarted by this skin part's minimum size.   This is a compromise,
        more about it here: http://bugs.adobe.com/jira/browse/SDK-21143
    -->
    <!--- @copy spark.components.SkinnableContainer#contentGroup -->
    <s :Group id="contentGroup" left="0" right="0" top="0" bottom="0" minWidth="0" minHeight="0">
        </s><s :layout>
            <s :BasicLayout/>
        </s>

Disable a Tab in Spark TabBar

Wednesday, August 18th, 2010

Use the class below as the itemRenderer of the Spark TabBar

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
30
31
import flash.events.Event;
 
import mx.core.INavigatorContent;
 
import spark.components.ButtonBarButton;
 
public class ButtonBarButton extends spark.components.ButtonBarButton
{
	public function ButtonBarButton()
	{
		super();
	}
 
	override public function set data(value:Object):void
	{
		if(data is INavigatorContent)
			INavigatorContent(data).removeEventListener("enabledChanged",enabledChangedHandler);
 
		super.data = value;
 
		if(data is INavigatorContent)
		{
			INavigatorContent(data).addEventListener("enabledChanged",enabledChangedHandler);
		}
	}
 
	private function enabledChangedHandler(event:Event):void
	{
		enabled = INavigatorContent(data).enabled;
	}
}

“Type(var)” vs “var as Type”

Friday, April 23rd, 2010

Description:
I have done an experiment to test the performance of data type conversions in actionscript 3. Create a new flex 4 based application with the code below, then compile and run it:


CODE:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   minWidth="955" minHeight="600"
			   xmlns:controls="com.wds.framework.controls.*"
			   initialize="application1_initializeHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.containers.Canvas;
			import mx.core.Container;
			import mx.core.FlexSprite;
			import mx.core.IContainer;
			import mx.core.IUIComponent;
			import mx.core.UIComponent;
			import mx.events.FlexEvent;
			import mx.utils.ObjectProxy;
 
			protected function application1_initializeHandler(event:FlexEvent):void
			{
				var n:int = 1000000;
				var i:int = 0;
				var timer:int = 0;
				var obj:*;
 
				trace("--------------------------");
				////////////////////////////////////////////////////
				trace("Basic Types");
				trace("-----------");
				obj = {};
				trace("obj = {}");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Object(obj);
				}
				trace("Object(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Object;
				}
				trace("obj as Object",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = "str";
				trace("obj = \"str\"");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					String(obj);
				}
				trace("String(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as String;
				}
				trace("obj as String",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = 0.5;
				trace("obj = 0.5");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Number(obj);
				}
				trace("Number(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Number;
				}
				trace("obj as Number",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = -12;
				trace("obj = -12");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					int(obj);
				}
				trace("int(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as int;
				}
				trace("obj as int",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = 12;
				trace("obj = 12");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					uint(obj);
				}
				trace("uint(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as uint;
				}
				trace("obj as uint",getTimer() - timer);
				trace("-----------");
 
				////////////////////////////////////////////////////
				trace("Dynamic Classes");
				trace("-----------");
				obj = new Array();
				trace("obj = new Array()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Array(obj);
				}
				trace("Array(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Array;
				}
				trace("obj as Array",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new ObjectProxy();
				trace("obj = new ObjectProxy()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					ObjectProxy(obj);
				}
				trace("ObjectProxy(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as ObjectProxy;
				}
				trace("obj as ObjectProxy",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new MyDynamicClass();
				trace("obj = new MyDynamicClass()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					MyDynamicClass(obj);
				}
				trace("MyDynamicClass(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as MyDynamicClass;
				}
				trace("obj as MyDynamicClass",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new MyProxyClass();
				trace("obj = new MyProxyClass()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					MyProxyClass(obj);
				}
				trace("MyProxyClass(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as MyProxyClass;
				}
				trace("obj as MyProxyClass",getTimer() - timer);
				trace("-----------");
				////////////////////////////////////////////////////
				trace("Typed Classes");
				trace("-----------");
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Canvas(obj);
				}
				trace("Canvas(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Canvas;
				}
				trace("obj as Canvas",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Container(obj);
				}
				trace("Container(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Container;
				}
				trace("obj as Container",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					UIComponent(obj);
				}
				trace("UIComponent(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as UIComponent;
				}
				trace("obj as UIComponent",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Sprite(obj);
				}
				trace("Sprite(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Sprite;
				}
				trace("obj as Sprite",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					DisplayObject(obj);
				}
				trace("DisplayObject(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as DisplayObject;
				}
				trace("obj as DisplayObject",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					EventDispatcher(obj);
				}
				trace("EventDispatcher(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as EventDispatcher;
				}
				trace("obj as EventDispatcher",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Object(obj);
				}
				trace("Object(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Object;
				}
				trace("obj as Object",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Container();
				trace("obj = new Container()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Container(obj);
				}
				trace("Container(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Container;
				}
				trace("obj as Container",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new UIComponent();
				trace("obj = new UIComponent()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					UIComponent(obj);
				}
				trace("UIComponent(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as UIComponent;
				}
				trace("obj as UIComponent",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new FlexSprite();
				trace("obj = new FlexSprite()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					FlexSprite(obj);
				}
				trace("FlexSprite(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as FlexSprite;
				}
				trace("obj as FlexSprite",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Sprite();
				trace("obj = new Sprite()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					Sprite(obj);
				}
				trace("Sprite(obj)",getTimer() - timer);
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as Sprite;
				}
				trace("obj as Sprite",getTimer() - timer);
				trace("-----------");
 
				////////////////////////////////////////////////////
				trace("Interfaces");
				trace("-----------");
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					IContainer(obj);
				}
				trace("IContainer(obj)",getTimer() - timer);				
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as IContainer;
				}
				trace("obj as IContainer",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					IUIComponent(obj);
				}
				trace("IUIComponent(obj)",getTimer() - timer);				
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as IUIComponent;
				}
				trace("obj as IUIComponent",getTimer() - timer);
				trace();
 
				//////////////////////////
				obj = new Canvas();
				trace("obj = new Canvas()");
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					IEventDispatcher(obj);
				}
				trace("IEventDispatcher(obj)",getTimer() - timer);				
 
				timer = getTimer();
				for(i = 0;i < n;i++)
				{
					obj as IEventDispatcher;
				}
				trace("obj as IEventDispatcher",getTimer() - timer);
				trace();
			}
 
		]]>
	</fx:Script>
</s:Application>

RESULTS:

--------------------------
Basic Types
-----------
obj = {}
Object(obj) 150
obj as Object 109

obj = "str"
String(obj) 110
obj as String 110

obj = 0.5
Number(obj) 65
obj as Number 107

obj = -12
int(obj) 67
obj as int 110

obj = 12
uint(obj) 67
obj as uint 108
-----------
Dynamic Classes
-----------
obj = new Array()
Array(obj) 1563
obj as Array 113

obj = new ObjectProxy()
ObjectProxy(obj) 126
obj as ObjectProxy 134

obj = new MyDynamicClass()
MyDynamicClass(obj) 128
obj as MyDynamicClass 138

obj = new MyProxyClass()
MyProxyClass(obj) 133
obj as MyProxyClass 135
-----------
Typed Classes
-----------
obj = new Canvas()
Canvas(obj) 131
obj as Canvas 132

obj = new Canvas()
Container(obj) 136
obj as Container 144

obj = new Canvas()
UIComponent(obj) 141
obj as UIComponent 148

obj = new Canvas()
Sprite(obj) 176
obj as Sprite 124

obj = new Canvas()
DisplayObject(obj) 167
obj as DisplayObject 122

obj = new Canvas()
EventDispatcher(obj) 170
obj as EventDispatcher 125

obj = new Canvas()
Object(obj) 150
obj as Object 108

obj = new Container()
Container(obj) 132
obj as Container 138

obj = new UIComponent()
UIComponent(obj) 134
obj as UIComponent 138

obj = new FlexSprite()
FlexSprite(obj) 133
obj as FlexSprite 134

obj = new Sprite()
Sprite(obj) 170
obj as Sprite 120
-----------
Interfaces
-----------
obj = new Canvas()
IContainer(obj) 136
obj as IContainer 147

obj = new Canvas()
IUIComponent(obj) 139
obj as IUIComponent 148

obj = new Canvas()
IEventDispatcher(obj) 120
obj as IEventDispatcher 121

Conclusion

  • To convert an Object instance to String, uint, int or Number, “Type(var)” is faster than “var as Type”
  • To convert an Object  instance to Array,  ”Type(var)” is much slower than “var as Type”, you can receive a warning “Array(x) behaves the same as new Array(x). To cast a value to type Array use the expression x as Array instead of Array(x)” with Flash Builder 4.
  • To convert an Object  instance of a sub-class to its super class, “var as Type” is faster than “Type(var)” if the super class is not Sprite or its super classes.
  • To convert an Object instance of a class to that class instance, ”var as Type” is faster than “Type(var)” sometimes if the super class is not Sprite or its super classes.
  • To convert an Object instance of a class as an interface, ”Type(var)” is faster than “var as Type”.

as3 DateParser

Friday, April 23rd, 2010
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
////////////////////////////////////////////////////////////////////////////////
// DateParser.as
//
// An as3 implementation of the java SimpleDateFormat like class based on Flex 
// SDK which can be used to parse a String with specified format to as3 Date 
// object.
//
// This code is a modification version of Daniel Wabyick's as2 
// SimpleDateFormatter(http://osflash.org/simpledateformatter) which 
// is directly adapted from Matt Kruse's Javascript class implementation.
//
// @author edison.guo(http://www.hydra1983.com)
// @author Daniel Wabyick(http://www.fluid.com)
// @author Matt Kruse (http://www.JavascriptToolbox.com)
 
// The following notice is maintained from Matt Kruse's 
// original Javascript code. 
// 
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// USAGE
// ----------------------------------------------------------------------
// These functions use the same pattern letters used by the DateFormatter 
// class provided by Flex SDK. You can find the detailed description of 
// these letter in the Adobe Flex Language Reference.
// Examples:
//		var dateString:String = "9/10/2008 GMT+0800 08:30:41";
// 		var parser:DateParser = new DateParser();
//		parser.formatString = "M/D/Y HH:NN:SS";
//		trace(parser.parse(dateString));
//		//Wed Sep 10 08:30:41 GMT+0800 2008
//
// ----------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
 
package
{
	import mx.formatters.Formatter;
	import mx.utils.StringUtil;
 
	public class DateParser
	{
		private static const VALID_PATTERN_CHARS:String = "Y,M,D,A,E,H,J,K,L,N,S,Q";
 
		public var error:String;
 
		private var _formatString:String = "YYYY-MM-DD HH:NN";
 
		public function get formatString():String
		{
			return _formatString;
		}
 
		public function set formatString(value:String):void
		{
			if(value) _formatString = value;
		}
 
		private var _defaultInvalidFormatError:String = Formatter.defaultInvalidFormatError;
 
		public function get defaultInvalidFormatError():String
		{
			return _defaultInvalidFormatError;
		}
 
		public function set defaultInvalidFormatError(value:String):void
		{
			_defaultInvalidFormatError = value;
		}
 
		private var _defaultInvalidValueError:String = Formatter.defaultInvalidValueError;
 
		public function get defaultInvalidValueError():String
		{
			return _defaultInvalidValueError;
		}
 
		public function set defaultInvalidValueError(value:String):void
		{
			_defaultInvalidValueError = value;
		}
 
		public function parse( value:Object):Date 
		{
			if (error)
				error = null;
 
			if(!(value is String))
			{
				error = defaultInvalidValueError;
				return null;
			}
 
			if(!value || value == "")
			{
				error = defaultInvalidValueError;
				return null;
			}
 
			var timezoneRegEx:RegExp = /(GMT|UTC)((\+|-)\d\d\d\d(\s)?)?/ig;
			value = StringUtil.trim(String(value).replace(timezoneRegEx, ""));
 
			var letter:String;
			var nTokens:int = 0;
			var tokens:String = "";
 
			var n:int = formatString.length;
			for (var i:int = 0; i < n; i++)
			{
				letter = formatString.charAt(i);
				if (VALID_PATTERN_CHARS.indexOf(letter) != -1 && letter != ",")
				{
					nTokens++;
					if (tokens.indexOf(letter) == -1)
					{
						tokens += letter;
					}
					else
					{
						if (letter != formatString.charAt(Math.max(i - 1, 0)))
						{
							error = defaultInvalidFormatError;
							return null;
						}
					}
				}
			}
 
			if (nTokens < 1)
			{
				error = defaultInvalidFormatError;
				return null;
			}
 
			var dataParser:DateStringParser = new DateStringParser(
				formatString, VALID_PATTERN_CHARS);
 
			return dataParser.parseValue(value);	
		}
	}
}
import mx.formatters.DateBase;
 
class DateStringParser
{
	private static var digits:String = "1234567890";
 
	private static var dayNames:Array = DateBase.dayNamesLong.concat(DateBase.dayNamesShort);
 
	private static var monthNames:Array = DateBase.monthNamesLong.concat(DateBase.monthNamesShort);
 
	public function DateStringParser(format:String, tokens:String)
	{
		super();
 
		this.format = format;
		this.tokens = tokens;
	}
 
	private var format:String;
 
	private var tokens:String;
 
	public function parseValue(value:Object):Date
	{		
		var valueString:String = String(value);
 
		var i_value:int = 0;
		var i_format:int = 0;
		var char_token:String = "";
		var token:String = "";
		var start:int = 0;
		var end:int = 0;
 
		var now:Date = new Date();
		var years:Object = now.getFullYear();
		var months:Object = now.getMonth() + 1;
		var dates:Object = now.getDate();	
		var days:Object = now.getDay();
		var hours:Object = now.getHours();
		var minutes:Object = now.getMinutes();
		var seconds:Object = now.getSeconds();
		var milliseconds:Object = now.getMilliseconds();
		var ampm:String = "";
 
		var max:int = 0;
		var min:int = 0;
 
		while(i_format < format.length)
		{
			char_token = format.charAt(i_format);
			if(tokens.indexOf(char_token) > -1)
			{
				start = format.indexOf(char_token);
				end = format.lastIndexOf(char_token);
				end = end >= 0 ? end + 1 : start + 1;
				token = format.substring(start,end);			
				i_format = end;
 
				if(token == "YYYY" || token == "YY" || token == "Y")
				{					
					if(token == "YYYY") {max = min = 4;}
					if(token == "YY") {max = min = 2;}
					if(token == "Y"){max = 4;min = 2;}
 
					years = getIntegerValue(valueString,i_value,min,max);
					if(isNaN(Number(years))) return null;
					i_value += String(years).length;
					years = Number(years);
				}
				else if(token == "MM" || token == "M")
				{
					months = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(months)) || months < 1 || months > 12) return null;
					i_value += String(months).length;
					months = Number(months);
				}
				else if(token == "MMMM" || token == "MMM")
				{
					var monthName:String = getMonthName(valueString,i_value);
					if(!monthName || monthName.length == 0) return null;
					months = getMonths(monthName);
					if(isNaN(Number(months)) || months < 1 || months > 12) return null;
					i_value += monthName.length;
					months = Number(months);
				}
				else if(token == "EE" || token == "E")
				{
					days = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(days)) || days < 1 || days > 7) return null;
					i_value += String(days).length;
					days = Number(days);
				}
				else if(token == "EEEE" || token == "EEE")
				{
					var dayName:String = getDayName(valueString,i_value);
					if(!dayName || dayName.length == 0) return null;
					days = getDays(dayName);
					if(isNaN(Number(days)) || days < 1 || days > 7) return null;
					i_value += dayName.length;
					days = Number(days);
				}
				else if(token == "DD" || token == "D")
				{
					dates = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(dates)) || dates < 1 || dates > 31) return null;
					i_value += String(dates).length;
					dates = Number(dates);
				}
				else if(token == "AA" || token == "A")
				{
					ampm = valueString.substr(i_value,2).toUpperCase();
					if(ampm != "AM" && ampm != "PM") return null;
					i_value += 2;
				}
				else if(token == "JJ" || token == "J")
				{
					hours = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(hours)) || hours < 0 || hours > 23) return null;
					i_value += String(hours).length;
					hours = Number(hours);
				}
				else if(token == "HH" || token == "H")
				{
					hours = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(hours)) || hours < 1 || hours > 24) return null;
					i_value += String(hours).length;
					hours = Number(hours);
				}				
				else if(token == "KK" || token == "K")
				{
					hours = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(hours)) || hours < 0 || hours > 11) return null;
					i_value += String(hours).length;
					hours = Number(hours);
				}
				else if(token == "LL" || token == "L")
				{
					hours = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(hours)) || hours < 1 || hours > 12) return null;
					i_value += String(hours).length;
					hours = Number(hours);
				}
				else if(token == "NN" || token == "N")
				{
					minutes = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(minutes)) || minutes < 0 || minutes > 59) return null;
					i_value += String(minutes).length;
					minutes = Number(minutes);
				}
				else if(token == "SS" || token == "S")
				{
					seconds = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(seconds)) || seconds < 0 || seconds > 59) return null;
					i_value += String(seconds).length;
					seconds = Number(seconds);
				}
				else if(token == "QQQ" || token == "QQ" || token == "Q")
				{
					milliseconds = getIntegerValue(valueString,i_value,token.length,2);
					if(isNaN(Number(milliseconds)) || milliseconds < 0 || milliseconds > 999) return null;
					i_value += String(milliseconds).length;
					milliseconds = Number(milliseconds);
				}
			}
			else
			{
				i_format ++;
				i_value ++;
			}	
		}
 
		if (i_value != valueString.length) { return null; }
 
		if (months == 2) 
		{
			var isLeapYear:Boolean = isLeapYear(Number(years));
			if(isLeapYear && dates > 28) return null;
 
			if(!isLeapYear && dates > 29) return null;
		}
 
		if (((months == 4)||(months == 6)||(months == 9)||(months == 11)) && dates > 30) 
			return null;
 
		if(hours < 12 && ampm == "PM") {hours = (Number(hours)) + 12;}
		else if(hours > 11 && ampm == "AM") {hours = (Number(hours)) - 12};
 
		return new Date(years,(Number(months)) - 1,dates,hours,minutes,seconds,milliseconds);
	}
 
	public function getIntegerValue(dateString:String,index:int,min:int,max:int):Object
	{
		var valueString:String;
		for(var i:int = max;i >= min;i--)
		{
			valueString = dateString.substring(index,index + i);
			if(valueString.length < min) return NaN;
			if(isInteger(valueString)) return valueString;
		}
		return NaN;
	}
 
	private function isInteger(valueString:String):Boolean
	{
		var n:int = valueString.length;
		for(var i:int = 0;i < n;i++)
		{
			if(!isDigit(valueString.charAt(i)))
				return false;
		}
		return true;
	}
 
	private function isDigit(char:String):Boolean
	{
		return digits.indexOf(char) > -1;
	}
 
	private function getDayName(valueString:String,index:int):String
	{		
		var n:int = dayNames.length;
		var dayName:String;
		for(var i:int = 0;i < n;i++)
		{
			dayName = dayNames[i];
			if(valueString.substr(index,dayName.length) == dayName)
				return dayName;
		}	
		return "";
	}
 
	private function getDays(dayName:String):Number
	{		
		var day:Number = dayNames.indexOf(dayName) + 1;
		day = day > 12? day - 12:day;
		day = day == 0?NaN:day;
		return day;
	}
 
	private function getMonthName(valueString:String,index:int):String
	{
		var n:int = monthNames.length;
		var monthName:String;
		for(var i:int = 0;i < n;i++)
		{
			monthName = monthNames[i];
			if(valueString.substr(index,monthName.length) == monthName)
				return monthName;
		}	
		return "";
	}
 
	private function getMonths(monthName:String):Number
	{
		var month:Number = monthNames.indexOf(monthName) + 1;
		month = month > 12? month - 12:month;
		month = month == 0?NaN:month;
		return month;
	}
 
	private function isLeapYear(year:Number):Boolean
	{
		return ((year % 4 == 0) && (year % 100 != 0)) || ( year % 400 == 0);
	}
}

Count attributes amount within one E4X expression

Wednesday, February 10th, 2010
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="1024" minHeight="768"
				creationComplete="application1_creationCompleteHandler(event)">
	<mx:Script>
		<![CDATA[
            import mx.events.FlexEvent;
            import mx.utils.ObjectUtil;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                var myXml:XML =
                    <deals name="bbs" type="C">
                        <deal name="sf" id="3" type="D"/>
                        <deal name="fdsf" id="1" type="E" description="testtest">
                            <deal name="basdf" id="2" type="A"/>
                        </deal>
                    </deals>;
                var names:Object = {};

                var attributes:XMLList = myXml..@*.(names[localName()] == null ?
                                                    names[localName()] = 1 :
                                                    names[localName()]++);
                trace(ObjectUtil.toString(names));
            }
        ]]>
    </mx:Script>
</mx:Application>

The output should be:

(Object)#0
  description = 1
  id = 3
  name = 4
  type = 4