Custom Schemas in Coldspring 2

One of the most powerful new features in Coldspring 2 is the ability to create custom xml schemas that
can be used within your XML configuration.

Rather than go into the details of how to write and configure custom schemas, here is an example of one that comes
bundled with CS2 out of the box.

In Coldspring 1.x, if you ever wanted to create a standalone structure inside your XML configuration, you would quite
commonly create it through a MapFactoryBean like
so:


<bean id="myMap" class="coldspring.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
    <map>
        <entry key="keyA" value="keyA value"
/>
        <entry key="keyB" value="keyB value"
/>
        <entry key="keyC" value="keyC value"
/>
    </map>
</property>
</bean>

This is all well and good, but the issue here is that you specifically need to know the api of the MapFactoryBean.
 If you get any part if the configuration of the MapFactoryBean wrong, you tend to get errors that may not be
clear, or potentially no error at all, simply a result that is not desirable.

For example, if you misspell the sourceMap property, Coldspring will attempt to set the property, but it doesn't
throw an error when it fails.


<bean id="myMap" class="coldspring.beans.factory.config.MapFactoryBean">
<property name="sourcMap">
    <map>
        <entry key="keyA" value="keyA value"
/>
        <entry key="keyB" value="keyB value"
/>
        <entry key="keyC" value="keyC value"
/>
    </map>
</property>
</bean>

Therefore, all you will get back is an empty struct, and quite possibly a headache trying to debug exactly why this
is happening in your code.

In ColdSpring 2, there is a nice, convenient util
 custom schema
, that exists to do things like create arrays, lists and other data structures for you.
 It essentially is a simple wrapper around things like the MapFactoryBean, but the custom schema gives you a
code completion and hinting when using an XML editor (that support XML Schemas), as well as better error messages
when things go wrong.

So, for example, if I wanted to do exactly the same configuration as above, I would add the xml namespace for util in
the head of my xml file, like so:


<beans xmlns="http://www.coldspringframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.coldspringframework.org/schema/util"

        xsi:schemaLocation="http://www.coldspringframework.org/schema/beans
http://coldspringframework.org/schema/coldspring-beans-2.0.xsd

        http://www.coldspringframework.org/schema/util
http://coldspringframework.org/schema/coldspring-util-2.0.xsd ">

Which would enable the <util:> namespace in my XML editor, and I could quickly type out:


<util:map id="myMap">
        <entry key="keyA" value="keyA value"
/>
        <entry key="keyB" value="keyB value"
/>
        <entry key="keyC" value="keyC value"
/>
</util:map>

And we would have the same result as above, but if something went wrong with your syntax, the IDE should show you
what it was, and if it didn’t, you would get a nice error from the XML validation letting you know exactly
what it was.

Of course, the whole point of this post, is due to the extensible nature of ColdSpring, you can write your own custom
XML namespaces, and register them with your XmlBeanFactory, to do things in very concise and easy to use manner
inside your IDEs as well.

This is a very simple example of what can be done with Custom xml schemas. You can drastically change the nature of
the BeanFactory as well as the contained beans through this mechanism, but this should hopefully give you a little
bit of a taste of what can be done with this new functionality.

Leave a Comment