Class ScriptTagger

  • All Implemented Interfaces:
    IXMLConfigurable, IImporterHandler, IDocumentTagger

    public class ScriptTagger
    extends AbstractStringTagger

    Tag incoming documents 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 tag documents 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 an Properties object.
    • parsed: Whether the document was already parsed, as a boolean.
    • sectionIndex: Content section index if it had to be split, as an integer.

    There are no expected return value from your script. Returning one has no effect.

    XML configuration usage:

    
    <handler
        class="com.norconex.importer.handler.tagger.impl.ScriptTagger"
        engineName="(script engine name)"
        maxReadSize="(max characters to read at once)"
        sourceCharset="(character encoding)">
      <!-- multiple "restrictTo" tags allowed (only one needs to match) -->
      <restrictTo>
        <fieldMatcher>(field-matching expression)</fieldMatcher>
        <valueMatcher>(value-matching expression)</valueMatcher>
      </restrictTo>
      <script>(your script)</script>
    </handler>

    Usage example:

    The following examples add new metadata field indicating which fruit is a document about.

    JavaScript:
    
    <handler
        class="ScriptTagger">
      <script>
        <![CDATA[
           metadata.add('fruit', 'apple');
       ]]>
      </script>
    </handler>
    Lua:
    
    <handler
        class="ScriptTagger"
        engineName="lua">
      <script>
        <![CDATA[
           metadata:addString('fruit', {'apple'});
       ]]>
      </script>
    </handler>
    Since:
    2.4.0
    Author:
    Pascal Essiembre
    See Also:
    ScriptRunner