I couldn't figure out how to do what I want to do using the existing rules,
so I wrote my own (it was simple enough).  The existing property setter rule
has to be told what property name you want to set.  But, I want one of the
attributes of the element to contain the property name and another attribute
to contain the value to be set (pardon the formatting)...

public class SetPropertyRule extends BaseRule
{
    public static final String ATTR_TRANSLATOR = "translator";
    public static final String DEFAULT_TRANSLATOR = "smart";
    public static final String ATTR_VALUE = "value";
    public static final String ATTR_NAME = "name";

    public void begin( SchemaProcessor schemaProcessor, Element element )
    {
        final Translator translator = element.getAttributeValue(
ATTR_TRANSLATOR ) == null ? schemaProcessor.getTranslator(
DEFAULT_TRANSLATOR ) : schemaProcessor.getTranslator(
element.getAttributeValue( ATTR_TRANSLATOR ) );
        final String propertyName = element.getAttributeValue( ATTR_NAME );
        final Object propertyValue = translator.translate(
schemaProcessor.getContributingModule(), PropertyUtils.getPropertyType(
schemaProcessor.peek() , propertyName ), element.getAttributeValue(
ATTR_VALUE ) );
        PropertyUtils.write( schemaProcessor.peek(), propertyName,
propertyValue );
    }
}

So, in my SDL I have...

bean (class="test.impl.MyPlugin")
{
  property (name="name" value="Yo Mama!")
  property (name="clazz" value="java.lang.String" translator="class")
}

I have defined a schema called "bean" which looks like this...

schema (id="bean")
    {
      element (name="bean")
      {
        attribute (name="class" required="true" translator="object")
        rules
        {
          push-attribute (attribute="class")
          invoke-parent (method="addElement")
        }
        element (name="property")
        {
          attribute (name="name" required="true")
          attribute (name="value" required="true")
          attribute (name="translator" required="false")
          rules
          {
            custom (class="test.rules.SetPropertyRule" )
          }
        }
      }
    }

Did I miss something or is this sort of structure not allowed by the current
core library?



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to