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).Properties
object.The expected return value from your script is a string holding the modified content.
<handler
class="com.norconex.importer.handler.transformer.impl.ScriptTransformer"
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
method="[basic|csv|wildcard|regex]"
ignoreCase="[false|true]"
ignoreDiacritic="[false|true]"
partial="[false|true]">
(field-matching expression)
</fieldMatcher>
<valueMatcher
method="[basic|csv|wildcard|regex]"
ignoreCase="[false|true]"
ignoreDiacritic="[false|true]"
partial="[false|true]">
(value-matching expression)
</valueMatcher>
</restrictTo>
<script>(your script)</script>
</handler>
The following example replaces all occurences of "Alice" with "Roger" in a document content.
<handler
class="ScriptTransformer">
<script>
<![CDATA[
modifiedContent = content.replace(/Alice/g, 'Roger');
/*return*/ modifiedContent;
]]>
</script>
</handler>
<handler
class="ScriptTransformer"
engineName="lua">
<script>
<![CDATA[
modifiedContent = content:gsub('Alice', 'Roger');
return modifiedContent;
]]>
</script>
</handler>
ScriptRunner
Constructor and Description |
---|
ScriptTransformer() |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other) |
String |
getEngineName() |
String |
getScript() |
int |
hashCode() |
protected void |
loadStringTransformerFromXML(XML xml)
Loads configuration settings specific to the implementing class.
|
protected void |
saveStringTransformerToXML(XML xml)
Saves configuration settings specific to the implementing class.
|
void |
setEngineName(String engineName) |
void |
setScript(String script) |
String |
toString() |
protected void |
transformStringContent(HandlerDoc doc,
StringBuilder content,
ParseState parseState,
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(HandlerDoc doc, StringBuilder content, ParseState parseState, int sectionIndex) throws ImporterHandlerException
transformStringContent
in class AbstractStringTransformer
ImporterHandlerException
protected void saveStringTransformerToXML(XML xml)
AbstractStringTransformer
saveStringTransformerToXML
in class AbstractStringTransformer
xml
- the XMLprotected void loadStringTransformerFromXML(XML xml)
AbstractStringTransformer
loadStringTransformerFromXML
in class AbstractStringTransformer
xml
- XML configurationpublic boolean equals(Object other)
equals
in class AbstractStringTransformer
public int hashCode()
hashCode
in class AbstractStringTransformer
public String toString()
toString
in class AbstractStringTransformer
Copyright © 2009–2023 Norconex Inc.. All rights reserved.