Class CharsetTagger
- java.lang.Object
-
- com.norconex.importer.handler.AbstractImporterHandler
-
- com.norconex.importer.handler.tagger.AbstractDocumentTagger
-
- com.norconex.importer.handler.tagger.impl.CharsetTagger
-
- All Implemented Interfaces:
IXMLConfigurable,IImporterHandler,IDocumentTagger
public class CharsetTagger extends AbstractDocumentTagger implements IXMLConfigurable
Converts one or more field values (if needed) from a source character encoding (charset) to a target one. Both the source and target character encodings are optional. If no source character encoding is explicitly provided, it first tries to detect the encoding of the field values before converting them to the target encoding. If the source character encoding cannot be established, the content encoding will remain unchanged. When no target character encoding is specified, UTF-8 is assumed.
Should I use this tagger?
Before using this tagger, you need to know the parsing of documents by the importer (using the default document parser factory) will try to convert and return fields as UTF-8 (for most, if not all content-types). If UTF-8 is your desired target, it only make sense to use this tagger as a pre-parsing handler (for text content-types only) when it is important to work with a specific character encoding before parsing. If on the other hand you wish to convert to a character encoding to a target different than UTF-8, you can use this tagger as a post-parsing handler to do so.
Conversion is not flawless
Because character encoding detection is not always accurate and because documents sometime mix different encoding, there is no guarantee this class will handle ALL character encoding conversions properly.
XML configuration usage:
<handler class="com.norconex.importer.handler.tagger.impl.CharsetTagger" sourceCharset="(character encoding)" targetCharset="(character encoding)"> <!-- multiple "restrictTo" tags allowed (only one needs to match) --> <restrictTo> <fieldMatcher>(field-matching expression)</fieldMatcher> <valueMatcher>(value-matching expression)</valueMatcher> </restrictTo> <fieldMatcher>(expression matching fields to be converted)</fieldMatcher> </handler>XML usage example:
<handler class="CharsetTagger" sourceCharset="ISO-8859-1" targetCharset="UTF-8"> <fieldMatcher>description</fieldMatcher> </handler>The above example converts the characters of a "description" field from "ISO-8859-1" to "UTF-8".
- Since:
- 2.5.0
- Author:
- Pascal Essiembre
- See Also:
CharsetTransformer
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_TARGET_CHARSET
-
Constructor Summary
Constructors Constructor Description CharsetTagger()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanequals(Object other)TextMatchergetFieldMatcher()Gets field matcher.StringgetFieldsRegex()Deprecated.Since 3.0.0, usegetFieldMatcher().StringgetSourceCharset()StringgetTargetCharset()inthashCode()protected voidloadHandlerFromXML(XML xml)Loads configuration settings specific to the implementing class.protected voidsaveHandlerToXML(XML xml)Saves configuration settings specific to the implementing class.voidsetFieldMatcher(TextMatcher fieldMatcher)Set field matcher (copy).voidsetFieldsRegex(String fieldsRegex)Deprecated.Since 3.0.0, usesetFieldMatcher(TextMatcher)voidsetSourceCharset(String sourceCharset)voidsetTargetCharset(String targetCharset)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
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.norconex.commons.lang.xml.IXMLConfigurable
loadFromXML, saveToXML
-
-
-
-
Field Detail
-
DEFAULT_TARGET_CHARSET
public static final String DEFAULT_TARGET_CHARSET
-
-
Method Detail
-
tagApplicableDocument
public void tagApplicableDocument(HandlerDoc doc, InputStream document, ParseState parseState) throws ImporterHandlerException
- Specified by:
tagApplicableDocumentin classAbstractDocumentTagger- Throws:
ImporterHandlerException
-
getFieldsRegex
@Deprecated public String getFieldsRegex()
Deprecated.Since 3.0.0, usegetFieldMatcher().Gets field-matching expression.- Returns:
- expression
-
setFieldsRegex
@Deprecated public void setFieldsRegex(String fieldsRegex)
Deprecated.Since 3.0.0, usesetFieldMatcher(TextMatcher)Sets field-matching regular expression.- Parameters:
fieldsRegex- regular expressiopm
-
getFieldMatcher
public TextMatcher getFieldMatcher()
Gets field matcher.- Returns:
- field matcher
- Since:
- 3.0.0
-
setFieldMatcher
public void setFieldMatcher(TextMatcher fieldMatcher)
Set field matcher (copy).- Parameters:
fieldMatcher- field matcher- Since:
- 3.0.0
-
getTargetCharset
public String getTargetCharset()
-
setTargetCharset
public void setTargetCharset(String targetCharset)
-
getSourceCharset
public String getSourceCharset()
-
setSourceCharset
public void setSourceCharset(String sourceCharset)
-
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
-
-