Today I was working on a new feature in the CMS. I had to do some flowscript coding in order to implement this feature. By default you can create an instance of a Java class in flowscript by using the following syntax:

cocoon.createObject(Packages.nl.hippo.cms.Class);

Since I did not want to hardcode my class file, but wanted to create a configurable object, I had to find a solution. So first I set the value of the variable in the DefaultsModule. (this can also be a XMLFileInputModule so you can really make this configurable from outside of you Cocoon application.

<component-instance class="org.apache.cocoon.components.modules.input.DefaultsModule"
                    logger="core.modules.input" name="myconstants">
  <values>
    <cms-object>nl.hippo.test.XMLDemo</cms-object>
  </values>
</component-instance>

So now from my flowscript I can fetch the value of the cms-object constant.

var myConstants = cocoon.getComponent(InputModule.ROLE + "Selector").select("myconstants");
var myObject = myConstants.getAttribute("cms-object",null,null);
// Add the "" to the myObject variable, otherwise the interpreter will think that it's a scriptable object instead of a String or Class object.
var myXMLObject = cocoon.createObject(myObject+"");

You will have to declare the import for the Cocoon input package at the top of your flowscript before this will work.

importPackage(Packages.org.apache.cocoon.components.modules.input);

Well that’s it! I could not find any documentation about how to do this. I hope this small chunk of code will help others out in the future.

Usefull links: