Is there a way to get the DefaultProperty metadata at runtime? - actionscript-3

I'm trying to get the DefaultProperty metadata for a class in ActionScript but I can't find any mention of the DefaultProperty in the metadata XML.
I'm using the following code:
var describedTypeRecord:DescribeTypeCacheRecord = mx.utils.DescribeTypeCache.describeType(BorderContainer);
var typeDescription:* = describedTypeRecord.typeDescription;
The XML is long but here the first node it returns:
<type name="spark.components::BorderContainer" base="spark.components::SkinnableContainer" isDynamic="false" isFinal="false" isStatic="false">
The default property for BorderContainer or SkinnableContainer is mxmlContentFactory:
[DefaultProperty("mxmlContentFactory")]
I've also tried RichText. The default property for this is "content":
[DefaultProperty("content")]
It doesn't seem to show anything about it in the metadata.
In the compiler options I have included the compiler arguments:
-keep-as3-metadata+=DefaultProperty

It is not set on some classes but if it is defined it will be defined on the super classes.
I was checking BorderContainer but it was defined on a SkinnableContainer:
<metadata name="DefaultProperty">
<arg key="" value="mxmlContentFactory"/>
</metadata>
The code I have posted works when it's defined on the main class. If a class doesn't have a default property I would get a list of all the super classes and check each one until I got to object or UIComponent.

Related

Dynamic Class Initiation AS3

I´m trying to initialize a class, based on a concatenation of a string and a number.
All my classes are public.
This is my code:
public function setCurrentPath(pathNumber:String)
{
var pth_class:Class = getDefinitionByName('Pth'+pathNumber) as Class;
var pth:MovieClip = new pth_class();
addChild(pth)
pth.getXY();
}
So I´m getting Error #1065.
Any help?
Yes I have up on my class file import flash.utils.*
Is your pth_class variable null?
If so, there are a couple of reasons this might be the case:
1) You haven't input the correct fully qualified class name of your class. E.g com.myClasses.Pth1
or
2)
If you're instanciating classes dynamically like this and there is no other "regular" reference to the class (such as blah = new Pth1()) then the "Pth1" class won't be included in the compilation process.
To get around this I think you can supply arguments to the compiler to force it to compile those classes OR you can manually include references to them in your existing code:
p1:Pth1;
p2:Pth2;

Actionscript 3.0 type downcast issue

I have implemented a new class that extends MovieClip. It's name is base.MovieClipWithDelays ("base" here is a package name).
My scene contains such an object named Blah.
In Symbol Properties I checked Export for ActionScript and Export in first frame checkboxes.
I set Class Name as T_Idle_0.
And I specified it's Base class as base.MovieClipWithDelays.
The problem is that following code leads to the Type Error:
var dob:DisplayObject = getChild("Blah");
trace("SuperClass = " + getQualifiedSuperclassName(dob));
return MovieClipWithDelays(dob);
it outputs:
SuperClass = base::MovieClipWithDelays
TypeError: Error #1034: Type Coercion failed: cannot convert T_Idle_0#1ec59e9 to base.MovieClipWithDelays.
As you can see, it's superclass name is OK. Nevertheless, it fails to downcast it. How is it possible and how do I workaround it?
You cannot set the base class of a library MovieClip to a custom class. You can either set it to Sprite or MovieClip. To do what you want to do, you have two solutions:
1 . Manage everything (drawing, etc.) from your MovieClipWithDelays class. i.e. don't make it depend on a library object.
Or:
2 . Make your MovieClipWithDelays wraps a MovieClip instance.
var libraryMC:MovieClip = new SomeLibraryMovieClip();
var customMc:MovieClipWithDelays = new MovieClipWithDelays(libraryMC);
Then within MovieClipWithDelays, you'll need to have some function and properties to handle the wrapped MovieClip.

namespaces in nested xml elements

I have seen afew answers to the problem i am having now but mine is that of a nested one.
I have an xml looking like this:
>
<em>
<type xmlns="http://www.sitcom-project.org/sitcom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<model>
<implemented_with>comoco</implemented_with>
<implemented_in>perl</implemented_in>
<cash_flow>casual</cash_flow>
<interaction>conventional</interaction>
</model>
</type>
</em>
now, how do i access the element of the implemented_with node?
ofc i could access the xmlList this way:
namespace ns = www.sitcom-project.org/sitcom;
type.ns::model;
but now, how do i access the implemented_with node in the model xmlList?
i tried type.ns::model.implemented_with, but didn't work. Anyone got any idea?
thanks
There are a couple ways to do this, but the best way is to use a namespace prefix before each dot access. In your case the first thing you want to do is to isolate the namespace. You can do this by hard coding the namespace into a new namespace object... ie;
var ns:Namespace = new Namespace( "http://www.sitcom-project.org/sitcom" );
Or a better way is to just extract it from the appropriate node. In the following code I am getting all the namespaces (as an array) declared on the type node, and just targeting the first one in the list. Because I don't know the namespace beforehand, I have to retrieve it using the children() method.
var emNode:XML = _yourXML.em[0];
var typeRoot:XML = emNode.children()[0];
var ns:Namespace = typeRoot.namespaceDeclarations()[0];
Once you have accomplished this, you can use namespace syntax to dig into your model.
var impWith:String = typeRoot.ns::model.ns::implemented_with;
This can be a little verbose, so you can set the default namespace using the following syntax. I don't like this personally, but it does work.
default xml namespace = ns;
var impWith:String = typeRoot.model.implemented_with;
default xml namespace = null;
A simple one-liner could be.
var ns:Namespace = new Namespace("http://www.sitcom-project.org/sitcom");
var imp:String = _yourXML.em[0].ns::type.ns::model.ns::implemented_with;
Using the default syntax
default xml namespace = new Namespace("http://www.sitcom-project.org/sitcom");
var imp:String = _yourXML.em[0].type.model.implemented_with;
default xml namespace = null;
Hope this helps.

Jibx always gives "Error during validation: null"

I'm really stumped on this incredibly simple mapping. It looks just like one of the examples even. If I comment out the internal structure, it'll run the binding compiler successfully. If I put the internal structure back in, it fails. Note that the internal structure is just defining the XML. This is basically example5 of the JIBX tutorial examples.
<binding>
<mapping name="RequestTransaction" class="TransactionRequest">
<value name="version" set-method="setVersion" get-method="getVersion" style="attribute" />
<structure name="transHeader">
<value name="requestCount" set-method="setRequestCount" get-method="getRequestCount"/>
</structure>
</mapping>
<binding>
Then I get the following error on the jibx compile:
Error: Error during validation: null; on mapping element at (line 2, col 97, in jibx-binding.xml)
I'm absolutely stumped and out of ideas. Google shows nothing useful.
The <structure> is arguably the most important concept in JiBX binding because it allows you to map arbitrary XML to your Java classes without forcing you to create bloated and ugly layers of nested Java objects and classes to match the XML design.
In this case your binding declares that you have an XML element named <transHeader> that will not be present in your Java class.
With some slight fixes to your XML format, your binding works perfectly. I assume the fact that your binding has two <binding> open tags rather than and open and close <binding></binding> is a typo, because you said you got it to work without the structure. Also add <?xml version="1.0"?> at the top of your binding file. Those two XML mods allow the JiBX 1.2 binding compiler to work with the following Java class:
(Note: you didn't provide the Java class this binding is for so I had to reconstruct it from the info you put in the binding file. The obvious side effect of this is that I reconstructed a class that will work with this binding. But the simple fact is that a JiBX binding by design contains all the info you need to know about the class and the XML.)
public class TransactionRequest {
private String version;
private int requestCount;
public void setVersion(String ver) {
version = ver;
}
public String getVersion() {
return version;
}
public void setRequestCount(int count) {
requestCount = count;
}
public int getRequestCount() {
return requestCount;
}
}
compile the class then run the binding compiler with:
>java -jar jibx-bind.jar jibx-binding.xml
To test it I used the following sample.xml:
(Note: you also didn't provide the XML you are trying to map so again I created a sample based on what you did provide)
<?xml version="1.0"?>
<RequestTransaction version="0.1">
<transHeader>
<requestCount>3</requestCount>
</transHeader>
</RequestTransaction>
Running the test uses the following code:
public static void main(String[] argz) {
String fileName = "./sample.xml";
IBindingFactory bfact = null;
IUnmarshallingContext uctx = null;
TransactionRequest sample = null;
try {
bfact = BindingDirectory.getFactory(TransactionRequest.class);
uctx = bfact.createUnmarshallingContext();
InputStream in = new FileInputStream(fileName);
sample = (TransactionRequest)uctx.unmarshalDocument(in, null);
System.out.println(sample.getRequestCount());
System.out.println(sample.getVersion());
}
catch (Exception e) {
e.printStackTrace();
}
}
And it runs successfully.
It's been a while now, but I found it was related to inheritance. I needed to give mappings for everything in the inheritance tree, including interfaces as I recall.
I ended up creating a wrapper object, which I've found seems to be the easiest way to use JIBX in general. Trying to map a true domain class causes tendrils into every class that class touches and I have to unjar everything so JIBX can find the classes, including 3rd party libs.

Map inheritance from generic class in Linq To SQL

I'm trying to map my inheritance hierarchy to DB using Linq to SQL:
Inheritance is like this, classes are POCO, without any LINQ to SQL attributes:
public interface IStage
{ ... }
public abstract class SimpleStage<T> : IStage where T : Process
{ ... }
public class ConcreteStage : SimpleStage<ConcreteProcess>
{ ... }
Here is the mapping:
<Database Name="NNN" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">
<Table Name="dbo.Stage" Member="Stage">
<Type Name="BusinessLogic.Domain.IStage">
<Column Name="ID" Member="ID" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" />
<Column Name="StageType" Member="StageType" IsDiscriminator="true" />
<Type Name="BusinessLogic.Domain.SimpleStage" IsInheritanceDefault="true">
<Type Name="BusinessLogic.Domain.ConcreteStage" IsInheritanceDefault="true" InheritanceCode="1"/>
</Type>
</Type>
</Table>
</Database>
In the runtime I get error:
System.InvalidOperationException was unhandled
Message="Mapping Problem: Cannot find runtime type for type mapping 'BusinessLogic.Domain.SimpleStage'."
Neither specifying SimpleStage, nor SimpleStage<T> in mapping file helps - runtime keeps producing different types of errors.
DC is created like this:
StreamReader sr = new StreamReader(#"MappingFile.map");
XmlMappingSource mapping = XmlMappingSource.FromStream(sr.BaseStream);
DataContext dc = new DataContext(#"connection string", mapping);
If Linq to SQL doesn't support this, could you, please, advise some other ORM, which does.
Thanks in advance,
Regards!
Ksenia
I found the answer myself, after I looked into IL of my generic class.
In IL its name looks like SimpleStage`1<...> so the issue with mapping file was fixed when I wrote
<Type Name="BusinessLogic.Domain.SimpleStage`1" ...
Have you added the reference to the files and also are you importing the references with using statements? OR using the fully qualified class names?
I think it's because of the generic type T in SimpleStage: it can't produce internally a mapping lookup table for SimpleStage as T is generic.