Class DOMDeleteTransformer
- java.lang.Object
-
- com.norconex.importer.handler.AbstractImporterHandler
-
- com.norconex.importer.handler.transformer.AbstractDocumentTransformer
-
- com.norconex.importer.handler.transformer.impl.DOMDeleteTransformer
-
- All Implemented Interfaces:
IXMLConfigurable,IImporterHandler,IDocumentTransformer
public class DOMDeleteTransformer extends AbstractDocumentTransformer
Enables deletion of one or more elements matching a given selector from a document content. Applies to HTML, XHTML, or XML document. To deal with DOM elements in metadata fields, use
DOMTaggerinstead.This class constructs a DOM tree from the document content. That DOM tree is loaded entirely into memory. Use this transformer with caution if you know you'll need to parse huge files. It may be preferable to use
ReplaceTransformerif this is a concern. Also, to help performance and avoid re-creating DOM tree before every DOM operations you want to perform, try to combine multiple extractions in a single instance of this transformer.The jsoup parser library is used to load a document content into a DOM tree. Elements are referenced using a CSS or JQuery-like syntax.
Should be used as a pre-parse handler.
Content-types
By default, this filter is restricted to (applies only to) documents matching the restrictions returned by
CommonRestrictions.domContentTypes(String). You can specify your own content types if you know they represent a file with HTML or XML-like markup tags.When used as a pre-parse handler, this class attempts to detect the content character encoding unless the character encoding was specified using
setSourceCharset(String).You can specify which parser to use when reading documents. The default is "html" and will normalize the content as HTML. This is generally a desired behavior, but this can sometimes have your selector fail. If you encounter this problem, try switching to "xml" parser, which does not attempt normalization on the content. The drawback with "xml" is you may not get all HTML-specific selector options to work. If you know you are dealing with XML to begin with, specifying "xml" should be a good option.
XML configuration usage:
<handler class="com.norconex.importer.handler.transformer.impl.DOMDeleteTransformer" parser="[html|xml]" sourceCharset="(character encoding)"> <!-- multiple "restrictTo" tags allowed (only one needs to match) --> <restrictTo> <fieldMatcher>(field-matching expression)</fieldMatcher> <valueMatcher>(value-matching expression)</valueMatcher> </restrictTo> <!-- multiple "dom" tags allowed --> <dom selector="(selector syntax)"/> </handler>XML usage example:
<handler class="DOMDeleteTransformer"> <dom selector="div.firstName"/> </handler>Given this HTML snippet...
<div class="firstName">Joe</div> <div class="lastName">Dalton</div>
... the above example will delete "Joe" but keep "Dalton".
- Since:
- 3.0.0
- Author:
- Pascal Essiembre
- See Also:
DOMTagger,DOMPreserveTransformer
-
-
Constructor Summary
Constructors Constructor Description DOMDeleteTransformer()Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddSelector(String selector)booleanequals(Object other)StringgetParser()Gets the parser to use when creating the DOM-tree.List<String>getSelectors()StringgetSourceCharset()Gets the assumed source character encoding.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.voidsetParser(String parser)Sets the parser to use when creating the DOM-tree.voidsetSelectors(List<String> selectors)voidsetSourceCharset(String sourceCharset)Sets the assumed source character encoding.StringtoString()protected voidtransformApplicableDocument(HandlerDoc doc, InputStream document, OutputStream output, ParseState parseState)-
Methods inherited from class com.norconex.importer.handler.transformer.AbstractDocumentTransformer
transformDocument
-
Methods inherited from class com.norconex.importer.handler.AbstractImporterHandler
addRestriction, addRestriction, addRestrictions, clearRestrictions, detectCharsetIfBlank, getRestrictions, isApplicable, loadFromXML, removeRestriction, removeRestriction, saveToXML
-
-
-
-
Method Detail
-
getSourceCharset
public String getSourceCharset()
Gets the assumed source character encoding.- Returns:
- character encoding of the source to be transformed
-
setSourceCharset
public void setSourceCharset(String sourceCharset)
Sets the assumed source character encoding.- Parameters:
sourceCharset- character encoding of the source to be transformed
-
getParser
public String getParser()
Gets the parser to use when creating the DOM-tree.- Returns:
html(default) orxml.
-
setParser
public void setParser(String parser)
Sets the parser to use when creating the DOM-tree.- Parameters:
parser-htmlorxml.
-
transformApplicableDocument
protected void transformApplicableDocument(HandlerDoc doc, InputStream document, OutputStream output, ParseState parseState) throws ImporterHandlerException
- Specified by:
transformApplicableDocumentin classAbstractDocumentTransformer- Throws:
ImporterHandlerException
-
addSelector
public void addSelector(String selector)
-
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
-
-