Class ScriptCondition

  • All Implemented Interfaces:
    IXMLConfigurable, IImporterCondition

    public class ScriptCondition
    extends AbstractStringCondition

    A condition formulated using a scripting language. The default script engine is JavaScript.

    Refer to ScriptRunner for more information on using a scripting language with Norconex Importer.

    How to create a condition with scripting:

    The following are variables made available to your script for each document:

    • reference: Document unique reference as a string.
    • content: Document content, as a string (of maxReadSize length).
    • metadata: Document metadata as a Properties object.
    • parsed: Whether the document was already parsed, as a boolean.
    • sectionIndex: Content section index (integer) if it had to be split because it was too large.

    The expected return value from your script is a boolean indicating whether the document was matched or not.

    XML configuration usage:

    
    <condition
        class="com.norconex.importer.handler.condition.impl.ScriptCondition"
        maxReadSize="(max characters to read at once)"
        sourceCharset="(character encoding)"
        engineName="(script engine name)">
      (your script)
    </condition>

    XML usage example:

    
    <!-- Javascript: -->
    <condition
        class="ScriptCondition">
      <![CDATA[
         var isAppleDoc = metadata.getString('fruit') == 'apple'
                 || content.indexOf('Apple') > -1;
         // Return value:
         isAppleDoc;
       ]]>
    </condition>
    <!-- Lua: -->
    <condition
        class="ScriptCondition"
        engineName="lua">
      <![CDATA[
         local isAppleDoc = metadata:getString('fruit') == 'apple'
                 and content:find('Apple') ~= nil;
         return isAppleDoc;
       ]]>
    </condition>
    Since:
    3.0.0
    Author:
    Pascal Essiembre
    See Also:
    ScriptRunner