code: public static Document toDocument(Source s,
                                        DocumentBuilderFactory factory) {
          Document d = tryExtractDocFromDOMSource(s);
          if (d == null) {
              InputSource is = toInputSource(s);
              DocumentBuilder b = null;
  
              // yes, there is a race condition but it is so unlikely to
              // happen that I currently don't care enough
              boolean oldNsAware = factory.isNamespaceAware();
              try {
                  if (!oldNsAware) {
                      factory.setNamespaceAware(true);
                  }
                  b = factory.newDocumentBuilder();
              } catch (javax.xml.parsers.ParserConfigurationException e) {
                  throw new ConfigurationException(e);
              } finally {
                  if (!oldNsAware) {
                      factory.setNamespaceAware(false);
                  }
              }
  
              try {
                  d = b.parse(is);
              } catch (org.xml.sax.SAXException e) {
                  throw new XMLUnitException(e);
              } catch (java.io.IOException e) {
                  throw new XMLUnitException(e);
              }
          }
          return d;
      }
docs: Creates a DOM Document from a TraX Source.
  
  <p>If the source is a {@link DOMSource} holding a Document
  Node, this one will be returned.  Otherwise {@link
  #toInputSource} and a namespace aware DocumentBuilder (created
  by given DocumentBuilderFactory) will be used to read the
  source.  This may involve an XSLT identity transform in
  toInputSource.</p>