-
Notifications
You must be signed in to change notification settings - Fork 17
StringEscapeHTMLToXMLUtil
StringEscapeHTMLToXMLUtil provides a mechanism to ensure HTML is XML valid. It does this by unescaping HTML entities but not those which are valid XML entities: <
, >
, "
, '
and &
. It would, for example, convert •
back to •
(U+2022
) whereas it will not convert <p>
to <p>
.
All components which output unescaped HTML/String content (such as WText, WTextArea and WLabel) will use this utility to ensure the text content is XML safe.
The utility is based on org.apache.commons.lang3.StringEscapeUtils
. It is almost completely pointless because no-one in their right mind would write a web application which output XML would they?
Any WComponent which requires HTML to XML safety will use this utility as part of the render process. It may be used as part of a custom component. The utility class exposes a single static method StringEscapeHTMLToXMLUtil.unescapeToXML(String)
. The following snippet shows an example render for a component which is able to output unescaped string content.
import com.github.bordertech.wcomponents.util.StringEscapeHTMLToXMLUtil;
//...
// inside doRender...
XmlStringBuilder xml = renderContext.getWriter();
// Let us assume the component being implemented has
// method to get its content as a String
String outputHtml = myComponent.getDataAsString();
// then we need to make sure it is valid XML.
if (outputHtml != null) {
xml.print(StringEscapeHTMLToXMLUtil.unescapeToXML(outputHtml));
}