Project

General

Profile

How to create a macro

The following example will describe how to create a macro to realize several calls to merge processor.

1. Create your view in a classic way

 <view>
        <connector type="XMLConnector">
            <parameter name="content"><![CDATA[
                    <root>
                        <parent>
                            <child1>data1</child1>
                            <child2>data2</child2>
                            <child3>data3</child3>
                        </parent>
                    </root>
            ]]></parameter>
        </connector>
        <processors>
            <merge nodes="/root/parent/*"/>
            <merge nodes="/root/parent"/>
            <merge nodes="/root/parent"/>
        </processors>
    </view>

2. Copy-paste code in a dedicated XSL file

Copy the block dedicated to be transformed as a macro into a template of the 2-macros.xsl file in the lavoisier-configuration module.

    <xsl:template match="lav:processor[@type='MergeMacro']">
        <merge nodes="/root/parent"/>
        <merge nodes="/root/parent"/>
    </xsl:template>

NB : to debug you can just move | copy 2-macros.xsl in /etc

TEST : check priority

3. Implement variables and process

The aim is now to finalize your macro making it generic and configurable.

   <xsl:template match="lav:processor[@type='MergeMacro']">
        <xsl:variable name="nodes" select="lav:parameter[@name='nodes']/text()"/>
        <xsl:param name="count" select="number(lav:parameter[@name='count']/text())"/>
        <xsl:if test="$count > 0">
            <merge nodes="{$nodes}"/>
            <xsl:apply-templates select=".">
                <xsl:with-param name="count" select="$count - 1"/>
            </xsl:apply-templates>
        </xsl:if>
    </xsl:template> 

4. Call the macro in the view

<view>
  <connector type="XMLConnector">
    <parameter name="content"><![CDATA[
      <root>
        <parent>
          <child1>data1</child1>
          <child2>data2</child2>
          <child3>data3</child3>
        </parent>
      </root>]]>
    </parameter>
  </connector>
  <processors>
    <merge nodes="/root/parent/*"/>
    <processor type="MergeMacro">
      <parameter name="nodes">/root/parent</parameter>
      <parameter name="count">2</parameter>
    </processor>
  </processors>
</view>

5. Create a plugin associated to this macro

Create src/fr/in2p3/lavoisier/adaptor/MergeMacro.java file in lavoisier-configuration module.
This class will hold the plugin code to auto-completion and auto-documentations features.

public class MergeMacro implements Processor {
    public String getDescription() {
        return "This adaptor merges selected nodes with the N first children";
    }

    public Parameter[] getUsage() {
        return new Parameter[]{
                Parameter.xpath("nodes", "The selected nodes to be merged"),
                Parameter.integer("count", "The number of children to merge with selected nodes")
        };
    }

    public void init(Configuration parameters) throws Exception {}
}