public class ReplaceTagger extends AbstractDocumentTagger
Replaces an existing metadata value with another one. The "toField"
argument is optional. The same field will be used for the replacement if no
"toField" is specified. If there are no matches and a "toField" is
specified, no value will be added to the "toField". To have the original
value copied in the "toField" when there are no matches, first copy the
original value using CopyTagger
to your target field then use this
class on that new field without a "toField".
Can be used both as a pre-parse or post-parse handler.
Since 2.6.1, 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
.
<tagger class="com.norconex.importer.handler.tagger.impl.ReplaceTagger"> <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) --> <replace fromField="sourceFieldName" toField="targetFieldName" caseSensitive="[false|true]" regex="[false|true]" wholeMatch="[false|true]" replaceAll="[false|true]"> <fromValue>Source Value</fromValue> <toValue>Target Value</toValue> </replace> <!-- multiple replace tags allowed --> </tagger>
Note: To preserve white space add xml:space="preserve"
to the "toValue" tag, like this:
<toValue xml:space="preserve"> </toValue>
The following example replaces occurrences of "apple" to "orange" in the "fruit" field.
<tagger class="com.norconex.importer.handler.tagger.impl.ReplaceTagger"> <replace fromField="fruit" replaceAll="true"> <fromValue>apple</fromValue> <toValue>orange</toValue> </replace> </tagger>
Modifier and Type | Class and Description |
---|---|
static class |
ReplaceTagger.Replacement |
Constructor and Description |
---|
ReplaceTagger() |
Modifier and Type | Method and Description |
---|---|
void |
addReplacement(ReplaceTagger.Replacement replacement)
Adds a replacement.
|
void |
addReplacement(String fromValue,
String toValue,
String fromField)
Deprecated.
Since 2.2.0, use
addReplacement(Replacement)
instead. |
void |
addReplacement(String fromValue,
String toValue,
String fromField,
boolean regex)
Deprecated.
Since 2.2.0, use
addReplacement(Replacement)
instead. |
void |
addReplacement(String fromValue,
String toValue,
String fromField,
String toField)
Deprecated.
Since 2.2.0, use
addReplacement(Replacement)
instead. |
void |
addReplacement(String fromValue,
String toValue,
String fromField,
String toField,
boolean regex)
Deprecated.
Since 2.2.0, use
addReplacement(Replacement)
instead. |
boolean |
equals(Object other) |
List<ReplaceTagger.Replacement> |
getReplacements() |
int |
hashCode() |
protected void |
loadHandlerFromXML(org.apache.commons.configuration.XMLConfiguration xml)
Loads configuration settings specific to the implementing class.
|
void |
removeReplacement(String fromField) |
protected void |
saveHandlerToXML(EnhancedXMLStreamWriter writer)
Saves configuration settings specific to the implementing class.
|
void |
tagApplicableDocument(String reference,
InputStream document,
ImporterMetadata metadata,
boolean parsed) |
String |
toString() |
tagDocument
addRestriction, addRestriction, addRestrictions, clearRestrictions, detectCharsetIfBlank, getRestrictions, isApplicable, loadFromXML, removeRestriction, removeRestriction, saveToXML
public void tagApplicableDocument(String reference, InputStream document, ImporterMetadata metadata, boolean parsed) throws ImporterHandlerException
tagApplicableDocument
in class AbstractDocumentTagger
ImporterHandlerException
public List<ReplaceTagger.Replacement> getReplacements()
public void removeReplacement(String fromField)
@Deprecated public void addReplacement(String fromValue, String toValue, String fromField)
addReplacement(Replacement)
instead.fromValue
- value to replacetoValue
- replacement valuefromField
- field holding the value to replace@Deprecated public void addReplacement(String fromValue, String toValue, String fromField, boolean regex)
addReplacement(Replacement)
instead.fromValue
- value to replacetoValue
- replacement valuefromField
- field holding the value to replaceregex
- true
if a regular expression@Deprecated public void addReplacement(String fromValue, String toValue, String fromField, String toField)
addReplacement(Replacement)
instead.fromValue
- value to replacetoValue
- replacement valuefromField
- field holding the value to replacetoField
- field to store the replaced value@Deprecated public void addReplacement(String fromValue, String toValue, String fromField, String toField, boolean regex)
addReplacement(Replacement)
instead.fromValue
- value to replacetoValue
- replacement valuefromField
- field holding the value to replacetoField
- field to store the replaced valueregex
- true
if a regular expressionpublic void addReplacement(ReplaceTagger.Replacement replacement)
replacement
- the replacementprotected void loadHandlerFromXML(org.apache.commons.configuration.XMLConfiguration xml) throws IOException
AbstractImporterHandler
loadHandlerFromXML
in class AbstractImporterHandler
xml
- xml configurationIOException
- could not load from XMLprotected void saveHandlerToXML(EnhancedXMLStreamWriter writer) throws XMLStreamException
AbstractImporterHandler
saveHandlerToXML
in class AbstractImporterHandler
writer
- the xml writerXMLStreamException
- could not save to XMLpublic boolean equals(Object other)
equals
in class AbstractImporterHandler
public int hashCode()
hashCode
in class AbstractImporterHandler
public String toString()
toString
in class AbstractImporterHandler
Copyright © 2009–2021 Norconex Inc.. All rights reserved.