}
private Document parseDomInternal(String source) throws SAXException, IOException, GadgetException {
if (attemptFullDocParseFirst(source)) {
InputSource input = new InputSource(new StringReader(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 {
DocumentFragment fragment = parseFragmentImpl(source);
normalizeFragment(fragment.getOwnerDocument(), fragment);
return fragment.getOwnerDocument();
}