}
private Document parseFragment(String source) throws SAXException, IOException {
InputSource input = new InputSource(new StringReader(source));
if (attemptFullDocParseFirst(source)) {
DOMParser parser = new DOMParser();
// Force parser not to use HTMLDocumentImpl as document implementation otherwise
// it forces all element names to uppercase.
parser.setProperty("http://apache.org/xml/properties/dom/document-class-name",
"org.apache.xerces.dom.DocumentImpl");
// Dont convert element names to upper/lowercase
parser.setProperty("http://cyberneko.org/html/properties/names/elems", "default");
// Preserve case of attributes
parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "no-change");
// Record entity references
parser.setFeature("http://apache.org/xml/features/scanner/notify-char-refs", true);
parser.setFeature("http://cyberneko.org/html/features/scanner/notify-builtin-refs", true);
// No need to defer as full DOM is walked later
parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
parser.parse(input);
return parser.getDocument();
} else {
Document htmlDoc = documentProvider.createDocument(null, null, null);
// Workaround for error check failure adding text node to entity ref as a child
htmlDoc.setStrictErrorChecking(false);
DOMFragmentParser parser = new DOMFragmentParser();
parser.setProperty("http://cyberneko.org/html/properties/names/elems", "default");
parser.setFeature("http://cyberneko.org/html/features/document-fragment", true);
parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "no-change");
parser.setFeature("http://apache.org/xml/features/scanner/notify-char-refs", true);
parser.setFeature("http://cyberneko.org/html/features/scanner/notify-builtin-refs", true);
DocumentFragment fragment = htmlDoc.createDocumentFragment();
parser.parse(input, fragment);
normalizeFragment(htmlDoc, fragment);
return htmlDoc;
}
}