My favorites
▼
|
Sign in
flexxb
FlexXB - an annotation based serializer for Flex and Air applications with built-in xml support
Project Home
Downloads
Wiki
Issues
Source
Export to GitHub
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
13
attachment: DescriptorStore.as
(6.5 KB)
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
/**
* FlexXB - an annotation based xml serializer for Flex and Air applications
* Copyright (C) 2008-2009 Alex Ciobanu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.googlecode.flexxb {
import com.googlecode.flexxb.annotation.XmlClass;
import flash.utils.Dictionary;
import flash.utils.describeType;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
/**
*
* @author aCiobanu
*
*/
internal final class DescriptorStore implements IDescriptorStore {
private var descriptorCache:Dictionary = new Dictionary();
private var classNamespaceMap:Dictionary;
/**
* Get the class descriptor associated with the object type
* @param object
* @return XmlClass descriptor
*
*/
public function getDescriptor(object:Object):XmlClass {
var className:String = getQualifiedClassName(object);
return getDefinition(object, className).descriptor as XmlClass;
}
/**
* Determine whether the object is custom serializable or not
* @param object
* @return true if the object is custom serialisable, false otherwise
*
*/
public function isCustomSerializable(object:Object):Boolean {
var className:String = getQualifiedClassName(object);
return getDefinition(object, className).reference != null;
}
/**
* Get the reference instance defined for a custom serializable type
* @param clasz
* @return object type instance
*
*/
public function getCustomSerializableReference(clasz:Class):IXmlSerializable {
var className:String = getQualifiedClassName(clasz);
return getDefinition(clasz, className).reference as IXmlSerializable;
}
/**
* @see IDescriptorStore#getXmlName()
*
*/
public final function getXmlName(object:Object):QName {
if (object != null) {
var classDescriptor:XmlClass = getDescriptor(object);
if (classDescriptor) {
return classDescriptor.xmlName;
}
}
return null;
}
/**
*
* @see IDescriptorStore#getClassByNamespace()
*
*/
public function getClassByNamespace(ns:String):Class {
if (classNamespaceMap) {
return classNamespaceMap[ns] as Class;
}
return null;
}
/**
*
* @param name
* @return
*
*/
public function getClassByTagName(name:String):Class {
for each (var store:ResultStore in descriptorCache) {
if (store.descriptor && store.descriptor.alias == name) {
return store.descriptor.fieldType;
}
}
return null;
}
/**
* @see IDescriptorStore#getNamespace()
*
*/
public function getNamespace(object:Object):Namespace {
if (object) {
var desc:XmlClass = getDescriptor(object);
if (desc) {
return desc.nameSpace;
}
}
return null;
}
/**
*
* @param xmlDescriptor
* @param type
*
*/
public function registerDescriptor(xmlDescriptor:XML, type:Class):void {
var className:String = getQualifiedClassName(type);
if (hasDescriptorDefined(className)) {
return;
}
putDescriptorInCache(xmlDescriptor, className, false);
}
private function xmlDescribeType(xmlDescriptor:XML):XmlClass {
var descriptor:XML = xmlDescriptor;
if (descriptor.factory.length() > 0) {
descriptor = descriptor.factory[0];
}
//get class annotation
var classDescriptor:XmlClass = new XmlClass(descriptor);
//signal the class descriptor that no more members are to be added
classDescriptor.memberAddFinished();
//if the class descriptor defines a namespace, register it in the namespace map
if (classDescriptor.nameSpace) {
if (!classNamespaceMap) {
classNamespaceMap = new Dictionary();
}
classNamespaceMap[classDescriptor.nameSpace.uri] = classDescriptor.fieldType;
}
return classDescriptor;
}
private function hasDescriptorDefined(className:String):Boolean {
return descriptorCache && descriptorCache[className] != null;
}
private function getDefinition(object:Object, className:String):ResultStore {
if (!hasDescriptorDefined(className)) {
put(object, className)
}
return descriptorCache[className];
}
private function put(object:Object, className:String):void {
var descriptor:XML = describeType(object);
// var interfaces:XMLList = descriptor.name() == "type" ? descriptor.implementsInterface : descriptor.factory.implementsInterface
var interfaces:XMLList = descriptor.factory.implementsInterface;
var customSerializable:Boolean;
for each (var interf:XML in interfaces) {
if (interf.@type.indexOf("IXmlSerializable") >= 0) {
customSerializable = true;
break;
}
}
putDescriptorInCache(descriptor, className, customSerializable);
}
private function putDescriptorInCache(descriptor:XML, className:String, customSerializable:Boolean):void {
var xmlClass:XmlClass;
var referenceObject:Object;
if (customSerializable) {
var cls:Class = getDefinitionByName(className) as Class;
referenceObject = new cls();
}
else {
xmlClass = xmlDescribeType(descriptor);
}
var result:Object = new ResultStore(xmlClass, customSerializable, referenceObject);
descriptorCache[className] = result;
}
}
}
import com.googlecode.flexxb.annotation.XmlClass;
/**
*
* @author Alexutz
* @private
*/
internal class ResultStore {
/**
* @private
*/
public var descriptor:XmlClass;
/**
* @private
*/
public var customSerializable:Boolean;
/**
* @private
*/
public var reference:Object;
/**
* @private
* @param descriptor
* @param customSerializable
* @param reference
*
*/
public function ResultStore(descriptor:XmlClass, customSerializable:Boolean, reference:Object) {
this.descriptor = descriptor;
this.customSerializable = customSerializable;
this.reference = reference;
}
}
Powered by
Google Project Hosting