public static String format( String xml ) {
    XMLStreamReader rd = null;
    XMLStreamWriter wr = null;

    StringWriter result = new StringWriter();
    try {
      rd = INPUT_FACTORY.createXMLStreamReader( new StringReader( xml ) );

      synchronized ( OUTPUT_FACTORY ) {
        // BACKLOG-18743: This object was not thread safe in some scenarios
        // causing the `result` variable to have data from other concurrent executions
        // and making the final output invalid.
        wr = OUTPUT_FACTORY.createXMLStreamWriter( result );
      }

      StartElementBuffer startElementBuffer = null;
      StringBuilder str = new StringBuilder();
      StringBuilder prefix = new StringBuilder();
      StringBuilder cdata = new StringBuilder();
      boolean wasStart = false;
      boolean wasSomething = false;
      while ( rd.hasNext() ) {
        int event = rd.next();
        if ( event != XMLStreamConstants.CDATA && cdata.length() > 0 ) {
          // was CDATA
          wr.writeCData( cdata.toString() );
          cdata.setLength( 0 );
        }

        if ( startElementBuffer != null ) {
          if ( event == XMLStreamConstants.END_ELEMENT ) {
            startElementBuffer.writeTo( wr, true );
            startElementBuffer = null;
            prefix.setLength( prefix.length() - STEP_PREFIX.length() );
            wasStart = false;
            continue;
          } else {
            startElementBuffer.writeTo( wr, false );
            startElementBuffer = null;
          }
        }

        switch ( event ) {
          case XMLStreamConstants.START_ELEMENT:
            if ( !whitespacesOnly( str ) ) {
              wr.writeCharacters( str.toString() );
            } else if ( wasSomething ) {
              wr.writeCharacters( "\n" + prefix );
            }
            str.setLength( 0 );
            prefix.append( STEP_PREFIX );
            startElementBuffer = new StartElementBuffer( rd );
            wasStart = true;
            wasSomething = true;
            break;
          case XMLStreamConstants.END_ELEMENT:
            prefix.setLength( prefix.length() - STEP_PREFIX.length() );
            if ( wasStart ) {
              wr.writeCharacters( str.toString() );
            } else {
              if ( !whitespacesOnly( str ) ) {
                wr.writeCharacters( str.toString() );
              } else {
                wr.writeCharacters( "\n" + prefix );
              }
            }
            str.setLength( 0 );
            wr.writeEndElement();
            wasStart = false;
            break;
          case XMLStreamConstants.SPACE:
          case XMLStreamConstants.CHARACTERS:
            str.append( rd.getText() );
            break;
          case XMLStreamConstants.CDATA:
            if ( !whitespacesOnly( str ) ) {
              wr.writeCharacters( str.toString() );
            }
            str.setLength( 0 );
            cdata.append( rd.getText() );
            wasSomething = true;
            break;
          case XMLStreamConstants.COMMENT:
            if ( !whitespacesOnly( str ) ) {
              wr.writeCharacters( str.toString() );
            } else if ( wasSomething ) {
              wr.writeCharacters( "\n" + prefix );
            }
            str.setLength( 0 );
            wr.writeComment( rd.getText() );
            wasSomething = true;
            break;
          case XMLStreamConstants.END_DOCUMENT:
            wr.writeCharacters( "\n" );
            wr.writeEndDocument();
            break;
          default:
            throw new RuntimeException( "Unknown XML event: " + event );
        }
      }

      wr.flush();

      return result.toString();
    } catch ( XMLStreamException ex ) {
      throw new RuntimeException( ex );
    } finally {
      try {
        if ( wr != null ) {
          wr.close();
        }
      } catch ( Exception ex ) {
      }
      try {
        if ( rd != null ) {
          rd.close();
        }
      } catch ( Exception ex ) {
      }
    }
  }
@Test
  public void test1() throws Exception {
    String inXml, expectedXml;
    try ( InputStream in = XMLFormatterTest.class.getResourceAsStream( "XMLFormatterIn1.xml" ) ) {
      inXml = IOUtils.toString( in );
    }
    try ( InputStream in = XMLFormatterTest.class.getResourceAsStream( "XMLFormatterExpected1.xml" ) ) {
      expectedXml = IOUtils.toString( in );
    }

    String result = XMLFormatter.format( inXml );
    assertXMLEqual( expectedXml, result );
  }