class XmlStringPrettyFormatter {

  private static final String FORMAT_ERROR = "Unable to format XML string";

  public static String xmlPrettyFormat(String xmlStringToFormat) {
    checkArgument(xmlStringToFormat != null, "Expecting XML String not to be null");
    // convert String to an XML Document and then back to String but prettily formatted.
    return prettyFormat(toXmlDocument(xmlStringToFormat), xmlStringToFormat.startsWith("<?xml"));
  }

  private static String prettyFormat(Document document, boolean keepXmlDeclaration) {<FILL_FUNCTION_BODY>}

  private static Document toXmlDocument(String xmlString) {
    try {
      InputSource xmlInputSource = new InputSource(new StringReader(xmlString));
      DocumentBuilder xmlDocumentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      return xmlDocumentBuilder.parse(xmlInputSource);
    } catch (Exception e) {
      throw new RuntimeException(FORMAT_ERROR, e);
    }
  }

  private XmlStringPrettyFormatter() {
    // utility class
  }
}


    try {
      DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
      DOMImplementationLS domImplementation = (DOMImplementationLS) registry.getDOMImplementation("LS");
      Writer stringWriter = new StringWriter();
      LSOutput formattedOutput = domImplementation.createLSOutput();
      formattedOutput.setCharacterStream(stringWriter);
      LSSerializer domSerializer = domImplementation.createLSSerializer();
      domSerializer.getDomConfig().setParameter("format-pretty-print", true);
      // Set this to true if the declaration is needed to be in the output.
      domSerializer.getDomConfig().setParameter("xml-declaration", keepXmlDeclaration);
      domSerializer.write(document, formattedOutput);
      return stringWriter.toString();
    } catch (Exception e) {
      throw new RuntimeException(FORMAT_ERROR, e);
    }
  
<no_super_class>