code: public static XMLStreamReader getXMLStreamReader(XMLInputFactory factory, Source source) throws XMLStreamException
      {
          XMLStreamReader xmlStreamReader;
  
          if (source instanceof StAXSource) {
              // StAXSource is not supported by standard XMLInputFactory
              StAXSource staxSource = (StAXSource) source;
              if (staxSource.getXMLStreamReader() != null) {
                  xmlStreamReader = staxSource.getXMLStreamReader();
              } else {
                  // TODO: add support for XMLStreamReader -> XMLEventReader
                  throw new XMLStreamException("XMLEventReader is not supported as source");
              }
          } else {
              xmlStreamReader = factory.createXMLStreamReader(source);
          }
  
          return xmlStreamReader;
      }
docs: Extract or create an instance of {@link XMLStreamReader} from the provided {@link Source}.
  
  @param factory the {@link XMLStreamReader} to use (if needed)
  @param source the source
  @return the {@link XMLStreamReader}
  @throws XMLStreamException when failing to extract xml stream reader
  @since 9.5
  @since 9.6RC1