Search code examples
xmlactionscript-3flashapache-flexflex4

How to get class definition from prefix and node name using SchemaTypeRegistry and SchemaManager?


I'm trying to register class definitions dynamically so that I can parse an XML document later get the correct class in the correct namespace. I want to be able to do this:

var classObject:Object = typeRegistry.getClassDefinition("s:WindowedApplication");

But I don't have that. Here's where I'm at. I'm able to register the types using an instance or qualified class name string using SchemaTypeRegistry:

typeRegistry = SchemaTypeRegistry.getInstance();

var windowedApplicationQName:QName = new QName(MXMLDocumentConstants.sparkNamespaceURI, "WindowedApplication");
//typeRegistry.registerClass(windowedApplicationQName, WindowedApplication);
typeRegistry.registerClass(windowedApplicationQName, "spark.components::WindowedApplication");

I can then get the class and create an instance of it easily enough:

var className:Object = typeRegistry.getClass(windowedApplicationQName);
var classInstance:Object;
classInstance  = new className();

But later on, I'm inside a text document, don't ask why, OK I'm in an XML text editor, and I can get the XML node name, in this case that value is "s:WindowedApplication".

So now when I'm trying to register known namespace URI's and I'm doing that with the SchemaManager (not found in documentation extends QualifiedResourceManager):

schemaManager = new SchemaManager();
schemaManager.addNamespaces({s:MXMLDocumentConstants.sparkNamespace});
var qname:QName = schemaManager.getQNameForPrefixedName("s:WindowedApplication");

This is great because I can get the QName object and then I can search for it using the type registry like so:

qname = schemaManager.getQNameForPrefixedName("s:WindowedApplication");
var classObject:Object = typeRegistry.getClass(qname);

But I have encountered problems with this.

Problem 1:
If the prefix, "s" isn't registered in the schemaManager or there is no prefix, "WindowedApplication" vs "s:WindowedApplication", the schemaManager throws an error. This is because it is expecting a schema but the data types are dynamic so there isn't one.

Problem 2:
The SchemaTypeRegistry has no knowledge of the namespace prefixes and the SchemaManager has no knowledge of class definitions of the namespaces it holds. This leads me to believe there is a piece I'm missing.

So pretending I don't have a fragile, disconnected system is there an official or better method to store class definitions, their namespaces and prefixes?

Update:
If nothing else I could extend either class and add code from either class to connect the class definitions with the namespace and namespace prefixes. But again, there seems like there's a piece missing.


Solution

  • I had to create my own class to map the relationships.

    There's two ClassLoader and ClassRegistry.

    ClassLoader loads the flex-config.xml. That contains a list of namespaces and classes in those namespaces. ClassRegistry can then register each class.

    There is an example of it's use here.