HtmlCleaner API

阅读更多

HtmlCleaner API

Create cleaner instance:

Constructor or method Purpose
HtmlCleaner() Create cleaner with default tag information provider.
HtmlCleaner(ITagInfoProvider) Create cleaner with custom tag information provider.

Set cleaner properties in order to tune its behavior:

Constructor or method Purpose
HtmlCleaner.getProperties() Get properties instance of the cleaner.
class CleanerProperties :

setAdvancedXmlEscape(boolean)
setUseCdataForScriptAndStyle(boolean)
setTranslateSpecialEntities(boolean)
setRecognizeUnicodeChars(boolean)
setOmitUnknownTags(boolean)
setTreatUnknownTagsAsContent(boolean)
setOmitDeprecatedTags(boolean)
setTreatDeprecatedTagsAsContent(boolean)
setOmitComments(boolean)
setOmitXmlDeclaration(boolean)
setOmitDoctypeDeclaration(boolean)
setOmitHtmlEnvelope(boolean)
setUseEmptyElementTags(boolean)
setAllowMultiWordAttributes(boolean)
setAllowHtmlInsideAttributes(boolean)
setIgnoreQuestAndExclam(boolean)
setNamespacesAware(boolean)
setHyphenReplacementInComment(String)
setPruneTags(String)
setBooleanAttributeValues(String)
Property setters
class CleanerProperties :

getTagInfoProvider()
isAdvancedXmlEscape()
isUseCdataForScriptAndStyle()
isTranslateSpecialEntities()
isRecognizeUnicodeChars()
isOmitUnknownTags()
isOmitDeprecatedTags()
isTreatUnknownTagsAsContent()
isTreatDeprecatedTagsAsContent()
isOmitComments()
isOmitXmlDeclaration()
isOmitDoctypeDeclaration()
isOmitHtmlEnvelope()
isUseEmptyElementTags()
isAllowMultiWordAttributes()
isAllowHtmlInsideAttributes()
isIgnoreQuestAndExclam()
isNamespacesAware()
getHyphenReplacementInComment()
getPruneTags()
getBooleanAttributeValues()
Property getters

Set cleaner transformations:new!

Constructor or method Purpose
CleanerTransformations() Create collection of transformations.
TagTransformation(String, String, boolean) Create single tag transformation.
CleanerTransformations.
addTransformation(TagTransformation)
Add tag transormation to transformations collection.
TagTransformation.
addAttributeTransformation(String, String)
Specify attribute transformation for the tag transformation.
HtmlCleaner.
setTransformations(CleanerTransformations)
Set cleaner transformations.

Clean HTML with instance of HtmlCleaner:

Constructor or method Purpose
class HtmlCleaner :

clean(String)
clean(File)
clean(File, String)
clean(URL)
clean(URL, String)
clean(InputStream)
clean(InputStream, String)
clean(Reader)
Clean HTML that comes from verious sources.

Search cleaned DOM and modify its structure:

Constructor or method Purpose
class TagNode :

getAttributeByName(String)
getAttributes()
hasAttribute(String)
addAttribute(String, String)
removeAttribute(String)
Work with node (tag) attributes
class TagNode :

getChildTagList()
getChildTags()
getText()
getParent()
addChild(Object)
addChildren(List)
removeFromTree()
removeChild(Object)
getAllElementsList(boolean)
getAllElements(boolean)
findElementByName(String, boolean)
getElementListByName(s, b)
getElementsByName(s, b)
findElementHavingAttribute(s, b)
getElementListHavingAttribute(s, b)
getElementsHavingAttribute(s, b)
findElementByAttValue(s, s, b, b)
getElementListByAttValue(s, s, b, b)
getElementsByAttValue(s, s, b, b)
evaluateXPath(String)
Find and modify nodes.
HtmlCleaner.setInnerHtml(TagNode, String)
Cleans given portion of HTML and stores it in specified tag node.

Serialize DOM nodes:

Constructor or method Purpose
SimpleXmlSerializer(CleanerProperties)
CompactXmlSerializer(CleanerProperties)
BrowserCompactXmlSerializer(CleanerProperties)
PrettyXmlSerializer(CleanerProperties)
DomSerializer(CleanerProperties, boolean)
JDomSerializer(CleanerProperties, boolean)
Create various kinds of XML serializers.
class XmlSerializer :

writeXmlToStream(TagNode, OutputStream, String)
writeXmlToStream(TagNode, OutputStream)
writeXmlToFile(TagNode, String, String)
writeXmlToFile(TagNode, String)
getXmlAsString(TagNode, String)
writeXml(TagNode, Writer, String)
Serialize node to different outputs.
DomSerializer.createDOM(TagNode)
JDomSerializer.createJDom(TagNode)
Create common DOM objects out of cleaned HTML.

Providing custom tag info set

HtmlCleaner implements default HTML tag set and rules for their balancing, that is similar to the browsers' behavior. However, user is free to implement interface ITagInfoProvider or extend some of its imlementations in order to provide custom tag info set. The easiest way to do that is to write XML configuration file which describes all tags and their dependacies and use ConfigFileTagProvider like:

HtmlCleaner cleaner = 
    new
 HtmlCleaner(
 new
 ConfigFileTagProvider(
myConfigFile)
 )
;

 

Perhaps the best starting point is default tag ruleset description file . It is the basis for DefaultTagProvider .

For example, someone may not like the rule that implicit TBODY is inserted before TR in the HTML table. To remove it, find element in the XML and remove tbody from req-enclosing-tags section.

Setting cleaner transformations

Following code snippet demonstrates how to set tranformations from the example :

...
HtmlCleaner
 cleaner = new
 HtmlCleaner(
...)
;

...
CleanerTransformations
 transformations = 
    new
 CleanerTransformations(
)
;

 
TagTransformation tt = new
 TagTransformation(
"cfoutput"
)
;

transformations.addTransformation
(
tt)
;

 
tt = new
 TagTransformation(
"c:block"
, "div"
, false
)
;

transformations.addTransformation
(
tt)
;

 
tt = new
 TagTransformation(
"font"
, "span"
, true
)
;

tt.addAttributeTransformation
(
"size"
)
;

tt.addAttributeTransformation
(
"face"
)
;

tt.addAttributeTransformation
(

    "style"
, 
    "${style};font-family=${face};font-size=${size};"

)
;

transformations.addTransformation
(
tt)
;

...
cleaner
.setTransformations
(
transformations)
;

...
TagNode
 node = cleaner.clean
(
...)
;
 

你可能感兴趣的:(XML,HTML)