Copy link to clipboard
Copied
Is there any way of casting between these two? I'm trying to write a component in cfscript which involves validating an Xml document against a schema. All the validation itself works fine, and xmlValidate returns a struct which contains two array properties; "Errors" and "FatalErrors".
Much as they look like arrays, isArray() returns true, cfdump shows them as arrays, they're not; it's this old gem where behind the scenes CF has created them in some crazy internal way which means they're different.
var myArr = ["value1", "value2"] ;
writeoutput("myArr.getClass(): " & myArr.getClass()) ;
myArr.getClass(): class coldfusion.runtime.Array
var ValidationErrors = xmlValidate(TextContent, SchemaFile) ;
writeoutput("ValidationErrors.Errors.getClass(): " & ValidationErrors.Errors.getClass()) ;
ValidationErrors.Errors.getClass(): class [Ljava.lang.Object;
The upshot being that I cannot use the new 9.01 syntax for looping over an array, ie the "for ( var i in ValidationErrors.Errors )". I hate having to write my code around bugs, so does anyone know if there's a way round? Or am I doomed?
Ta.
Copy link to clipboard
Copied
And to save anyone looking yes it's already been logged as a bug with Adobe, I'm just asking to see if anyone knows any fixes other than using the old script syntax.
Copy link to clipboard
Copied
I can't test this in a CF9 environment as I don't have one here. However I can test the issue as raised in the bugtracker, and this might work for you:
<cfscript>
a = getComponentMetadata("path.to.some.component").functions;
writeOutput(a.getClass().getName() & "<br />");
a2 = createObject("java", "java.util.Arrays");
writeOutput(a2.getClass().getName() & "<br />");
a3 = a2.asList(a);
writeOutput(a3.getClass().getName() & "<br />");
a4 = createObject("java", "java.util.Vector").init(a3);
writeOutput(a4.getClass().getName() & "<br />");
</cfscript>
<cfdump var="#variables#">
I googled "java convert array to vector", and found this page: http://www.roseindia.net/java/collection/array-vector-collections.shtml, upon which I based the code above.
Can you test:
for (stFunction in a4){
/// some stuff here
}
?
--
Adam
Copy link to clipboard
Copied
Yup, that certainly works. However tidy it is not
Looks like I'll be doing the old style loops then. Shame really, this is what happens when you try and be clever and cutting-edge I guess...