Class ExternalTagger
- java.lang.Object
-
- com.norconex.importer.handler.AbstractImporterHandler
-
- com.norconex.importer.handler.tagger.AbstractDocumentTagger
-
- com.norconex.importer.handler.tagger.impl.ExternalTagger
-
- All Implemented Interfaces:
IXMLConfigurable,IImporterHandler,IDocumentTagger
public class ExternalTagger extends AbstractDocumentTagger
Extracts metadata from a document using an external application to do so.
This class relies on
ExternalHandlerfor most of the work. Refer toExternalHandlerfor full documentation, except for the following differences this class has:-
There is no
${OUTPUT}token (since taggers do not modify content). -
You can chose not to send any input at all to save some processing
with
setInputDisabled(boolean).
To use an external application to change a file content consider using
ExternalTransformerinstead.To parse/extract raw text from files, it is recommended to use
ExternalParserinstead.XML configuration usage:
<handler class="com.norconex.importer.handler.tagger.impl.ExternalTagger"> <!-- multiple "restrictTo" tags allowed (only one needs to match) --> <restrictTo> <fieldMatcher>(field-matching expression)</fieldMatcher> <valueMatcher>(value-matching expression)</valueMatcher> </restrictTo> <command inputDisabled="[false|true]"> c:\Apps\myapp.exe ${INPUT} ${INPUT_META} ${OUTPUT_META} ${REFERENCE} </command> <metadata inputFormat="[json|xml|properties]" outputFormat="[json|xml|properties]"> <!-- Pattern only used when no output format is specified. Repeat as needed. --> <pattern>(regular expression)</pattern> </metadata> <environment> <!-- repeat variable tag as needed --> <variable name="(environment variable name)"> (environment variable value) </variable> </environment> <tempDir> (Optional directory where to store temporary files used for transformation.) </tempDir> </handler>XML usage example:
<handler class="ExternalTagger"> <command>/path/tag/app ${INPUT} ${OUTPUT_META}</command> </handler>The above example invokes an external application that accepts a document to transform and outputs a file containing the new metadata information.
- Since:
- 2.8.0
- Author:
- Pascal Essiembre
- See Also:
ExternalHandler
-
-
Constructor Summary
Constructors Constructor Description ExternalTagger()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddEnvironmentVariable(String name, String value)Adds an environment variables to the list of previously assigned variables (if any).voidaddEnvironmentVariables(Map<String,String> environmentVariables)Adds the environment variables, keeping environment variables previously assigned.voidaddMetadataExtractionPattern(String field, String pattern)Adds a metadata extraction pattern that will extract the whole text matched into the given field.voidaddMetadataExtractionPattern(String field, String pattern, int valueGroup)Adds a metadata extraction pattern, which will extract the value from the specified group index upon matching.voidaddMetadataExtractionPatterns(RegexFieldValueExtractor... patterns)Adds a metadata extraction pattern that will extract matching field names/values.booleanequals(Object other)StringgetCommand()Gets the command to execute.Map<String,String>getEnvironmentVariables()Gets environment variables.List<RegexFieldValueExtractor>getMetadataExtractionPatterns()Gets metadata extraction patterns.StringgetMetadataInputFormat()Gets the format of the metadata input file sent to the external application.StringgetMetadataOutputFormat()Gets the format of the metadata output file from the external application.PropertySettergetOnSet()Gets the property setter to use when a metadata value is set.PathgetTempDir()Gets directory where to store temporary files used for transformation.inthashCode()booleanisInputDisabled()Gets whether to send the document content or not, regardless whether ${INPUT} token is part of the command or not.protected voidloadHandlerFromXML(XML xml)Loads configuration settings specific to the implementing class.protected voidsaveHandlerToXML(XML xml)Saves configuration settings specific to the implementing class.voidsetCommand(String command)Sets the command to execute.voidsetEnvironmentVariables(Map<String,String> environmentVariables)Sets the environment variables.voidsetInputDisabled(boolean inputDisabled)Sets whether to send the document content or not, regardless whether ${INPUT} token is part of the command or not.voidsetMetadataExtractionPatterns(RegexFieldValueExtractor... patterns)Sets metadata extraction patterns.voidsetMetadataInputFormat(String metadataInputFormat)Sets the format of the metadata input file sent to the external application.voidsetMetadataOutputFormat(String metadataOutputFormat)Sets the format of the metadata output file from the external application.voidsetOnSet(PropertySetter onSet)Sets the property setter to use when a metadata value is set.voidsetTempDir(Path tempDir)Sets directory where to store temporary files used for transformation.voidtagApplicableDocument(HandlerDoc doc, InputStream document, ParseState parseState)StringtoString()-
Methods inherited from class com.norconex.importer.handler.tagger.AbstractDocumentTagger
tagDocument
-
Methods inherited from class com.norconex.importer.handler.AbstractImporterHandler
addRestriction, addRestriction, addRestrictions, clearRestrictions, detectCharsetIfBlank, getRestrictions, isApplicable, loadFromXML, removeRestriction, removeRestriction, saveToXML
-
-
-
-
Method Detail
-
isInputDisabled
public boolean isInputDisabled()
Gets whether to send the document content or not, regardless whether ${INPUT} token is part of the command or not.- Returns:
trueto prevent sending the input content
-
setInputDisabled
public void setInputDisabled(boolean inputDisabled)
Sets whether to send the document content or not, regardless whether ${INPUT} token is part of the command or not.- Parameters:
inputDisabled-trueto prevent sending the input content
-
getCommand
public String getCommand()
Gets the command to execute.- Returns:
- the command
-
setCommand
public void setCommand(String command)
Sets the command to execute. Make sure to escape spaces in executable path and its arguments as well as other special command line characters.- Parameters:
command- the command
-
getMetadataExtractionPatterns
public List<RegexFieldValueExtractor> getMetadataExtractionPatterns()
Gets metadata extraction patterns. See class documentation.- Returns:
- map of patterns and field names
-
addMetadataExtractionPattern
public void addMetadataExtractionPattern(String field, String pattern)
Adds a metadata extraction pattern that will extract the whole text matched into the given field.- Parameters:
field- target field to store the matching pattern.pattern- the pattern
-
addMetadataExtractionPattern
public void addMetadataExtractionPattern(String field, String pattern, int valueGroup)
Adds a metadata extraction pattern, which will extract the value from the specified group index upon matching.- Parameters:
field- target field to store the matching pattern.pattern- the patternvalueGroup- which pattern group to return.
-
addMetadataExtractionPatterns
public void addMetadataExtractionPatterns(RegexFieldValueExtractor... patterns)
Adds a metadata extraction pattern that will extract matching field names/values.- Parameters:
patterns- extraction pattern
-
setMetadataExtractionPatterns
public void setMetadataExtractionPatterns(RegexFieldValueExtractor... patterns)
Sets metadata extraction patterns. Clears any previously assigned patterns.- Parameters:
patterns- extraction pattern
-
getEnvironmentVariables
public Map<String,String> getEnvironmentVariables()
Gets environment variables.- Returns:
- environment variables or
nullif using the current process environment variables
-
setEnvironmentVariables
public void setEnvironmentVariables(Map<String,String> environmentVariables)
Sets the environment variables. Clearing any prevously assigned environment variables. Setnullto use the current process environment variables (default).- Parameters:
environmentVariables- environment variables
-
addEnvironmentVariables
public void addEnvironmentVariables(Map<String,String> environmentVariables)
Adds the environment variables, keeping environment variables previously assigned. Existing variables of the same name will be overwritten. To clear all previously assigned variables and use the current process environment variables, passnulltosetEnvironmentVariables(Map).- Parameters:
environmentVariables- environment variables
-
addEnvironmentVariable
public void addEnvironmentVariable(String name, String value)
Adds an environment variables to the list of previously assigned variables (if any). Existing variables of the same name will be overwritten. Setting a variable with anullname has no effect whilenullvalues are converted to empty strings.- Parameters:
name- environment variable namevalue- environment variable value
-
getMetadataInputFormat
public String getMetadataInputFormat()
Gets the format of the metadata input file sent to the external application. One of "json" (default), "xml", or "properties" is expected. Only applicable when the${INPUT}token is part of the command.- Returns:
- metadata input format
-
setMetadataInputFormat
public void setMetadataInputFormat(String metadataInputFormat)
Sets the format of the metadata input file sent to the external application. One of "json" (default), "xml", or "properties" is expected. Only applicable when the${INPUT}token is part of the command.- Parameters:
metadataInputFormat- format of the metadata input file
-
getMetadataOutputFormat
public String getMetadataOutputFormat()
Gets the format of the metadata output file from the external application. By default no format is set, and metadata extraction patterns are used to extract metadata information. One of "json", "xml", or "properties" is expected. Only applicable when the${OUTPUT}token is part of the command.- Returns:
- metadata output format
-
setMetadataOutputFormat
public void setMetadataOutputFormat(String metadataOutputFormat)
Sets the format of the metadata output file from the external application. One of "json" (default), "xml", or "properties" is expected. Set tonullfor relying metadata extraction patterns instead. Only applicable when the${OUTPUT}token is part of the command.- Parameters:
metadataOutputFormat- format of the metadata output file
-
getOnSet
public PropertySetter getOnSet()
Gets the property setter to use when a metadata value is set.- Returns:
- property setter
- Since:
- 3.0.0
-
setOnSet
public void setOnSet(PropertySetter onSet)
Sets the property setter to use when a metadata value is set.- Parameters:
onSet- property setter- Since:
- 3.0.0
-
getTempDir
public Path getTempDir()
Gets directory where to store temporary files used for transformation.- Returns:
- temporary directory
-
setTempDir
public void setTempDir(Path tempDir)
Sets directory where to store temporary files used for transformation.- Parameters:
tempDir- temporary directory
-
tagApplicableDocument
public void tagApplicableDocument(HandlerDoc doc, InputStream document, ParseState parseState) throws ImporterHandlerException
- Specified by:
tagApplicableDocumentin classAbstractDocumentTagger- Throws:
ImporterHandlerException
-
loadHandlerFromXML
protected void loadHandlerFromXML(XML xml)
Description copied from class:AbstractImporterHandlerLoads configuration settings specific to the implementing class.- Specified by:
loadHandlerFromXMLin classAbstractImporterHandler- Parameters:
xml- XML configuration
-
saveHandlerToXML
protected void saveHandlerToXML(XML xml)
Description copied from class:AbstractImporterHandlerSaves configuration settings specific to the implementing class.- Specified by:
saveHandlerToXMLin classAbstractImporterHandler- Parameters:
xml- the XML
-
equals
public boolean equals(Object other)
- Overrides:
equalsin classAbstractImporterHandler
-
hashCode
public int hashCode()
- Overrides:
hashCodein classAbstractImporterHandler
-
toString
public String toString()
- Overrides:
toStringin classAbstractImporterHandler
-
-