Class ReplaceTagger
- java.lang.Object
-
- com.norconex.importer.handler.AbstractImporterHandler
-
- com.norconex.importer.handler.tagger.AbstractDocumentTagger
-
- com.norconex.importer.handler.tagger.impl.ReplaceTagger
-
- All Implemented Interfaces:
IXMLConfigurable
,IImporterHandler
,IDocumentTagger
public class ReplaceTagger extends AbstractDocumentTagger
Replaces an existing metadata value with another one. The "toField" argument is optional.
It is possible to only keep values that changed from a replacement and discard others by setting "discardUnchanged" to
true
.Storing values in an existing field
If a target field with the same name already exists for a document, values will be added to the end of the existing value list. It is possible to change this default behavior by supplying a
PropertySetter
.Not specifying a "toValue" will delete the matches from sources values.
Can be used both as a pre-parse or post-parse handler.
You can specify whether matches should be made against the whole field value or not (default). You can also specify whether replacement should be attempted on first match only (default) or all occurrences. This last option is only applicable when whole value matching is
false
.XML configuration usage:
<handler class="com.norconex.importer.handler.tagger.impl.ReplaceTagger"> <!-- multiple "restrictTo" tags allowed (only one needs to match) --> <restrictTo> <fieldMatcher>(field-matching expression)</fieldMatcher> <valueMatcher>(value-matching expression)</valueMatcher> </restrictTo> <!-- multiple replace tags allowed --> <replace toField="(optional target field name)"> discardUnchanged="[false|true]"> <fieldMatcher> (one or more matching fields to have their values replaced) </fieldMatcher> <valueMatcher>(one or more source values to replace)</valueMatcher> <toValue>(replacement value)</toValue> </replace> </handler>
XML usage example:
<handler class="ReplaceTagger"> <replace> <fieldMatcher>fruit</fieldMatcher> <valueMatcher replaceAll="true"> apple </valueMatcher> <toValue>orange</toValue> </replace> </handler>
The following example replaces occurrences of "apple" to "orange" in the "fruit" field.
- Author:
- Pascal Essiembre
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ReplaceTagger.Replacement
-
Constructor Summary
Constructors Constructor Description ReplaceTagger()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addReplacement(ReplaceTagger.Replacement replacement)
Adds a replacement.boolean
equals(Object other)
List<ReplaceTagger.Replacement>
getReplacements()
int
hashCode()
protected void
loadHandlerFromXML(XML xml)
Loads configuration settings specific to the implementing class.void
removeReplacement(String fromField)
Deprecated.protected void
saveHandlerToXML(XML xml)
Saves configuration settings specific to the implementing class.void
tagApplicableDocument(HandlerDoc doc, InputStream document, ParseState parseState)
String
toString()
-
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
-
tagApplicableDocument
public void tagApplicableDocument(HandlerDoc doc, InputStream document, ParseState parseState) throws ImporterHandlerException
- Specified by:
tagApplicableDocument
in classAbstractDocumentTagger
- Throws:
ImporterHandlerException
-
getReplacements
public List<ReplaceTagger.Replacement> getReplacements()
-
removeReplacement
@Deprecated public void removeReplacement(String fromField)
Deprecated.
-
addReplacement
public void addReplacement(ReplaceTagger.Replacement replacement)
Adds a replacement.- Parameters:
replacement
- the replacement- Since:
- 2.2.0
-
loadHandlerFromXML
protected void loadHandlerFromXML(XML xml)
Description copied from class:AbstractImporterHandler
Loads configuration settings specific to the implementing class.- Specified by:
loadHandlerFromXML
in classAbstractImporterHandler
- Parameters:
xml
- XML configuration
-
saveHandlerToXML
protected void saveHandlerToXML(XML xml)
Description copied from class:AbstractImporterHandler
Saves configuration settings specific to the implementing class.- Specified by:
saveHandlerToXML
in classAbstractImporterHandler
- Parameters:
xml
- the XML
-
equals
public boolean equals(Object other)
- Overrides:
equals
in classAbstractImporterHandler
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classAbstractImporterHandler
-
toString
public String toString()
- Overrides:
toString
in classAbstractImporterHandler
-
-