<set>, <use> & Co

There are several elements, which are used to set variables, create and manipulate objects:

The <set> and <use> elements represent "statements" and do not appear inside any of the above elements.

Element Summary

The <set> element uses the var and scope attributes to specify the variable name and scope. The scope attribute is optional. Valid values are local, module and application. The default scope is local.

The <property>, <field> and <method> elements require the name attribute to specify the property, field or method name.

The <object> element requires the class attribute to specify the instantiation class.

The <member> element requires at least one of the value and class attributes. The dynamic value attribute evaluates to the object whose method or field is to be accessed. The class attribute specifies the class used to search for the member to be accessed. If both attributes are specified, the given value must be an instance of the given class. If the class attribute is omitted, the search class defaults to the given value's class. If the value attribute is omitted, only static members of the given class can be accessed.

The <set>, <property> and <arg> elements take their value from the dynamic value attribute or one of:

  • a nested <object> element
  • a nested <member> element

The <object> element may contain a <constructor> as its first child.

The <use> element specifies its value to use either by a dynamic value attribute or a nested <member> element as its first child.

The <use> and <object> elements contain a (mixed) sequence of:

  • nested <property> elements
  • nested <method> elements

The <member> element contains one of:

  • a nested <field> element
  • a nested <method> element

The <method> and <constructor> elements contain any number of nested <arg> elements

Examples

  1. Set a variable using the value attribute:
    <set var="content" value="/WEB-INF/${moduleContext.name}"/>
    
  2. Set a variable to a new object, then use it:
    <set var="jeff">
      <object class="org.foo.bar.Person"/>
    </set>
    
    <use value="${jeff}">
      <property name="name" value="Jefferson"/>
      <method name="addNickname">
        <arg value="Jeff"/>
      </method>
    </use>
    
  3. Semantically the same, but use the new instance inside <object>:
    <set var="jeff">
      <object class="org.foo.bar.Person">
        <property name="name" value="Jefferson"/>
        <method name="addNickname">
          <arg value="Jeff"/>
        </method>
      </object>
    </set>
    
  4. Same as above, but use non-default constructor:
    <set var="jeff">
      <object class="org.foo.bar.Person">
        <constructor>
          <arg value="Jefferson"/>
        </constructor>
        <method name="addNickname">
          <arg value="Jeff"/>
        </method>
      </object>
    </set>
    
  5. Set a variable to a new map and add some associations:
    <set var="map">
      <object class="java.util.HashMap">
        <property name="foo" value="bar"/>
        <property name="foobar">
          <object class="org.foo.bar.Foobar"/>
        </property>
      </object>
    </set>
    
  6. Set a variable to a method result:
    <set var="style">
      <member value="${moduleContext}">
        <method name="getInitParameter">
          <arg value="style"/>
        </method>
      </object>
    </set>
    
  7. Set a variable to a static method result:
    <set var="now">
      <member class="java.lang.System">
        <method name="currentTimeMillis"/>
      </member>
    </set>
    
  8. Set a variable to a static field value:
    <set var="english">
      <member class="java.util.Locale">
        <field name="ENGLISH"/>
      </object>
    </set>
    
  9. Log a message using Commons Logging:
    <use>
      <member class="org.apache.commons.logging.LogFactory">
        <method name="getLog">
          <arg value="de.odysseus.calyxo.base.conf.Test"/>
        </method>
      </member>
      <method name="info">
        <arg value="Hello, world!"/>
      </method>
    </use>
    
  10. Set the server's default locale to java.util.Locale.ENGLISH:
    <use>
      <member class="java.util.Locale">
        <method name="getDefault"/>
      </member>
      <method name="setDefault">
        <arg>
          <member class="java.util.Locale">
            <field name="ENGLISH"/>
          </member>
        </arg>
      </method>
    </use>
    
  11. Create and use a number format:
    <!-- create a number format for german locale -->
    <set var="format">
      <member class="java.text.NumberFormat">
        <method name="getInstance">
          <arg>
            <member class="java.util.Locale">
              <field name="GERMAN"/>
            </member>
          </arg>
        </method>
      </member>
    </set>
      
    <!-- set maximum fraction numbers -->
    <use value="${format}">
      <property name="maximumFractionDigits" value="2"/>
    </use>
    
    <!-- format a number -->
    <set var="result">
      <member value="${format}">
        <method name="format">
          <arg value="${123.456}"/>
        </method>
      </member>
    </set>