TraxUtils { public static Document getDocument(DOMSource source) { Node node = source.getNode(); if (node instanceof Document) { return (Document) node; } else if (node != null) { return node.getOwnerDocument(); } else { return null; } }  static Document getDocument(DOMSource source); static void doWithSource(Source source, SourceCallback callback); static void doWithResult(Result result, ResultCallback callback);  }
@Test public void testGetDocument() throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactoryUtils.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Assert.assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(document))); Element element = document.createElement("element"); document.appendChild(element); Assert.assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(element))); }