All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.assertj.util.xml.XmlStringPrettyFormatter Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2016 the original author or authors.
 */
package com.fitbur.assertj.util.xml;

import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;

/**
 * Format an XML String with indent = 2 space.
 * 

* Very much inspired by http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java and * http://pastebin.com/XL7932aC *

*/ public class XmlStringPrettyFormatter { private static final String FORMAT_ERROR = "Unable to format XML string"; public static String xmlPrettyFormat(String xmlStringToFormat) { if (xmlStringToFormat == null) throw new IllegalArgumentException("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("




© 2015 - 2024 Weber Informatics LLC | Privacy Policy