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)text
becomestext(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 void
characters(char[] ch, int start, int length)
protected void
cleanContentExtraWhiteSpaces()
UC3: Replace group of white spaces by a single space.void
comment(char[] ch, int start, int length)
void
endCDATA()
void
endDocument()
void
endElement(String uri, String localName, String qName)
protected void
endEmptyVisibleElement()
protected void
endNonVisibleElement()
Flush previous content and print current one.protected void
flushContent()
Flush previous content and print current one.protected void
flushContent(boolean trimTrailing)
Flush previous content and print current one.protected StringBuffer
getContent()
protected boolean
isSemanticComment(String comment)
We remove spaces around non semantic comments.protected void
sendCharacters(char[] ch)
protected void
sendCharacters(char[] ch, int start, int length)
protected void
sendPreviousContent(boolean trimTrailing)
protected boolean
shouldRemoveWhiteSpaces()
void
startCDATA()
void
startElement(String uri, String localName, String qName, Attributes atts)
protected void
startEmptyVisibleElement()
protected void
startNonVisibleElement()
Append an non visible element.protected void
trimLeadingWhiteSpaces()
protected String
trimLeadingWhiteSpaces(CharSequence content)
protected void
trimTrailingWhiteSpaces()
protected String
trimTrailingWhiteSpaces(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:
characters
in interfaceContentHandler
- Overrides:
characters
in classXMLFilterImpl
- Throws:
SAXException
-
startElement
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException
- Specified by:
startElement
in interfaceContentHandler
- Overrides:
startElement
in classXMLFilterImpl
- Throws:
SAXException
-
endElement
public void endElement(String uri, String localName, String qName) throws SAXException
- Specified by:
endElement
in interfaceContentHandler
- Overrides:
endElement
in classXMLFilterImpl
- Throws:
SAXException
-
startCDATA
public void startCDATA() throws SAXException
- Specified by:
startCDATA
in interfaceLexicalHandler
- Overrides:
startCDATA
in classDefaultXMLFilter
- Throws:
SAXException
-
endCDATA
public void endCDATA() throws SAXException
- Specified by:
endCDATA
in interfaceLexicalHandler
- Overrides:
endCDATA
in classDefaultXMLFilter
- Throws:
SAXException
-
comment
public void comment(char[] ch, int start, int length) throws SAXException
- Specified by:
comment
in interfaceLexicalHandler
- Overrides:
comment
in classDefaultXMLFilter
- Throws:
SAXException
-
endDocument
public void endDocument() throws SAXException
- Specified by:
endDocument
in interfaceContentHandler
- Overrides:
endDocument
in classXMLFilterImpl
- Throws:
SAXException
-
shouldRemoveWhiteSpaces
protected boolean shouldRemoveWhiteSpaces()
-
sendPreviousContent
protected void sendPreviousContent(boolean trimTrailing) throws SAXException
- Throws:
SAXException
-
flushContent
protected void flushContent() throws SAXException
Flush previous content and print current one.- Throws:
SAXException
-
flushContent
protected void flushContent(boolean trimTrailing) throws SAXException
Flush 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 SAXException
Append an non visible element.- Throws:
SAXException
-
endNonVisibleElement
protected void endNonVisibleElement() throws SAXException
Flush 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
-
-