Class XHTMLWhitespaceXMLFilter
- java.lang.Object
-
- org.xml.sax.helpers.XMLFilterImpl
-
- org.xwiki.rendering.wikimodel.xhtml.filter.DefaultXMLFilter
-
- org.xwiki.rendering.wikimodel.xhtml.filter.XHTMLWhitespaceXMLFilter
-
- All Implemented Interfaces:
ContentHandler,DTDHandler,EntityResolver,ErrorHandler,LexicalHandler,XMLFilter,XMLReader
public class XHTMLWhitespaceXMLFilter extends DefaultXMLFilter
Removes non-semantic whitespaces in XML elements. See http://www.w3.org/TR/html4/struct/text.html#h-9.1 for more details. Possible use cases:- UC1: Any white spaces group is removed if it's before a non inline (see NONINLINE_ELEMENTS) element or at the beginning of the document.
- UC2: Any white spaces group is removed if it's after a non inline (see NONINLINE_ELEMENTS) element or at the end of the document.
- UC3: Inside inline content any white spaces group become a single space.
- UC4: Non visible elements (comments, CDATA and NONVISIBLE_ELEMENTS) are invisible and do not cut a
white space group.
text(sp)(sp)textbecomestext(sp)text - UC5: Visible empty element like img count as text when grouping white spaces
- UC6: Semantic comment count as text when grouping white spaces
- Since:
- 4.0M1
- Version:
- $Id: 0a43fce9f6d05413910b06619b5016fcf89262e4 $
-
-
Field Summary
-
Fields inherited from class org.xwiki.rendering.wikimodel.xhtml.filter.DefaultXMLFilter
SAX_LEXICAL_HANDLER_PROPERTY
-
-
Constructor Summary
Constructors Constructor Description XHTMLWhitespaceXMLFilter()XHTMLWhitespaceXMLFilter(XMLReader reader)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcharacters(char[] ch, int start, int length)protected voidcleanContentExtraWhiteSpaces()UC3: Replace group of white spaces by a single space.voidcomment(char[] ch, int start, int length)voidendCDATA()voidendDocument()voidendElement(String uri, String localName, String qName)protected voidendEmptyVisibleElement()protected voidendNonVisibleElement()Flush previous content and print current one.protected voidflushContent()Flush previous content and print current one.protected voidflushContent(boolean trimTrailing)Flush previous content and print current one.protected StringBuffergetContent()protected booleanisSemanticComment(String comment)We remove spaces around non semantic comments.protected voidsendCharacters(char[] ch)protected voidsendCharacters(char[] ch, int start, int length)protected voidsendPreviousContent(boolean trimTrailing)protected booleanshouldRemoveWhiteSpaces()voidstartCDATA()voidstartElement(String uri, String localName, String qName, Attributes atts)protected voidstartEmptyVisibleElement()protected voidstartNonVisibleElement()Append an non visible element.protected voidtrimLeadingWhiteSpaces()protected StringtrimLeadingWhiteSpaces(CharSequence content)protected voidtrimTrailingWhiteSpaces()protected StringtrimTrailingWhiteSpaces(CharSequence content)-
Methods inherited from class org.xwiki.rendering.wikimodel.xhtml.filter.DefaultXMLFilter
endDTD, endEntity, getProperty, parse, setProperty, startDTD, startEntity
-
Methods inherited from class org.xml.sax.helpers.XMLFilterImpl
endPrefixMapping, error, fatalError, getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getParent, ignorableWhitespace, notationDecl, parse, processingInstruction, resolveEntity, setContentHandler, setDocumentLocator, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setParent, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
-
-
-
-
Constructor Detail
-
XHTMLWhitespaceXMLFilter
public XHTMLWhitespaceXMLFilter()
-
XHTMLWhitespaceXMLFilter
public XHTMLWhitespaceXMLFilter(XMLReader reader)
-
-
Method Detail
-
characters
public void characters(char[] ch, int start, int length) throws SAXException- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classXMLFilterImpl- Throws:
SAXException
-
startElement
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException
- Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classXMLFilterImpl- Throws:
SAXException
-
endElement
public void endElement(String uri, String localName, String qName) throws SAXException
- Specified by:
endElementin interfaceContentHandler- Overrides:
endElementin classXMLFilterImpl- Throws:
SAXException
-
startCDATA
public void startCDATA() throws SAXException- Specified by:
startCDATAin interfaceLexicalHandler- Overrides:
startCDATAin classDefaultXMLFilter- Throws:
SAXException
-
endCDATA
public void endCDATA() throws SAXException- Specified by:
endCDATAin interfaceLexicalHandler- Overrides:
endCDATAin classDefaultXMLFilter- Throws:
SAXException
-
comment
public void comment(char[] ch, int start, int length) throws SAXException- Specified by:
commentin interfaceLexicalHandler- Overrides:
commentin classDefaultXMLFilter- Throws:
SAXException
-
endDocument
public void endDocument() throws SAXException- Specified by:
endDocumentin interfaceContentHandler- Overrides:
endDocumentin classXMLFilterImpl- Throws:
SAXException
-
shouldRemoveWhiteSpaces
protected boolean shouldRemoveWhiteSpaces()
-
sendPreviousContent
protected void sendPreviousContent(boolean trimTrailing) throws SAXException- Throws:
SAXException
-
flushContent
protected void flushContent() throws SAXExceptionFlush previous content and print current one.- Throws:
SAXException
-
flushContent
protected void flushContent(boolean trimTrailing) throws SAXExceptionFlush previous content and print current one.- Throws:
SAXException
-
startEmptyVisibleElement
protected void startEmptyVisibleElement() throws SAXException- Throws:
SAXException
-
endEmptyVisibleElement
protected void endEmptyVisibleElement() throws SAXException- Throws:
SAXException
-
startNonVisibleElement
protected void startNonVisibleElement() throws SAXExceptionAppend an non visible element.- Throws:
SAXException
-
endNonVisibleElement
protected void endNonVisibleElement() throws SAXExceptionFlush previous content and print current one.- Throws:
SAXException
-
sendCharacters
protected void sendCharacters(char[] ch) throws SAXException- Throws:
SAXException
-
sendCharacters
protected void sendCharacters(char[] ch, int start, int length) throws SAXException- Throws:
SAXException
-
cleanContentExtraWhiteSpaces
protected void cleanContentExtraWhiteSpaces()
UC3: Replace group of white spaces by a single space.
-
trimLeadingWhiteSpaces
protected void trimLeadingWhiteSpaces()
-
trimLeadingWhiteSpaces
protected String trimLeadingWhiteSpaces(CharSequence content)
-
trimTrailingWhiteSpaces
protected void trimTrailingWhiteSpaces()
-
trimTrailingWhiteSpaces
protected String trimTrailingWhiteSpaces(CharSequence content)
-
getContent
protected StringBuffer getContent()
-
isSemanticComment
protected boolean isSemanticComment(String comment)
We remove spaces around non semantic comments.- Parameters:
comment- the comment to evaluate- Returns:
- true if the comment is a semantic one
-
-