public class ScriptTransformer extends AbstractStringTransformer implements IXMLConfigurable
Transform 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.
The following are variables made available to your script for each document:
maxReadSize
length).ImporterMetadata
object.The expected return value from your script is a string holding the modified content.
<transformer class="com.norconex.importer.handler.transformer.impl.ScriptTransformer" engineName="(script engine name)" sourceCharset="(character encoding)" maxReadSize="(max content characters to read at once)" > <restrictTo caseSensitive="[false|true]" field="(name of header/metadata field name to match)"> (regular expression of value to match) </restrictTo> <!-- multiple "restrictTo" tags allowed (only one needs to match) --> <script>(your script)</script> </transformer>
The following example replaces all occurences of "Alice" with "Roger" in a document content.
<transformer class="com.norconex.importer.handler.transformer.impl.ScriptTransformer"> <script><![CDATA[ modifiedContent = content.replace(/Alice/g, 'Roger'); /*return*/ modifiedContent; ]]></script> </transformer>
<transformer class="com.norconex.importer.handler.transformer.impl.ScriptTransformer" engineName="lua"> <script><![CDATA[ modifiedContent = content:gsub('Alice', 'Roger'); return modifiedContent; ]]></script> </transformer>
ScriptRunner
Constructor and Description |
---|
ScriptTransformer() |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other) |
String |
getEngineName() |
String |
getScript() |
int |
hashCode() |
protected void |
loadStringTransformerFromXML(org.apache.commons.configuration.XMLConfiguration xml)
Loads configuration settings specific to the implementing class.
|
protected void |
saveStringTransformerToXML(EnhancedXMLStreamWriter writer)
Saves configuration settings specific to the implementing class.
|
void |
setEngineName(String engineName) |
void |
setScript(String script) |
String |
toString() |
protected void |
transformStringContent(String reference,
StringBuilder content,
ImporterMetadata metadata,
boolean parsed,
int sectionIndex) |
getMaxReadSize, loadCharStreamTransformerFromXML, saveCharStreamTransformerToXML, setMaxReadSize, transformTextDocument
getSourceCharset, loadHandlerFromXML, saveHandlerToXML, setSourceCharset, transformApplicableDocument
transformDocument
addRestriction, addRestriction, addRestrictions, clearRestrictions, detectCharsetIfBlank, getRestrictions, isApplicable, loadFromXML, removeRestriction, removeRestriction, saveToXML
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
loadFromXML, saveToXML
public String getEngineName()
public void setEngineName(String engineName)
public String getScript()
public void setScript(String script)
protected void transformStringContent(String reference, StringBuilder content, ImporterMetadata metadata, boolean parsed, int sectionIndex) throws ImporterHandlerException
transformStringContent
in class AbstractStringTransformer
ImporterHandlerException
protected void saveStringTransformerToXML(EnhancedXMLStreamWriter writer) throws XMLStreamException
AbstractStringTransformer
saveStringTransformerToXML
in class AbstractStringTransformer
writer
- the xml writerXMLStreamException
- could not save to XMLprotected void loadStringTransformerFromXML(org.apache.commons.configuration.XMLConfiguration xml) throws IOException
AbstractStringTransformer
loadStringTransformerFromXML
in class AbstractStringTransformer
xml
- xml configurationIOException
- could not load from XMLpublic boolean equals(Object other)
equals
in class AbstractStringTransformer
public int hashCode()
hashCode
in class AbstractStringTransformer
public String toString()
toString
in class AbstractStringTransformer
Copyright © 2009–2021 Norconex Inc.. All rights reserved.