dimanche 11 décembre 2016

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'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:

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);

Problem 1: If the prefix isn't registered in the schemaManager or there is no prefix 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 type registry has no knowledge of the namespace prefixes and the schema manager has no knowledge of class definitions of QNames. This leads me to believe there is a piece I'm missing.

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.

So pretending I didn't hack a fragile, easily broken system is there an official or better method to store class definitions, their namespaces and prefixes?

Aucun commentaire:

Enregistrer un commentaire