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

com.github.gkutiel.flip.res.Xml Maven / Gradle / Ivy

The newest version!
package com.github.gkutiel.flip.res;

import java.text.MessageFormat;

import org.w3c.dom.Document;

import com.github.gkutiel.flip.Response;
import com.github.gkutiel.flip.utils.Utils;
import com.thoughtworks.xstream.XStream;

public class Xml implements Response {

	private static final XStream X_STREAM = new XStream();

	@Override
	public byte[] getBytes(final Object obj, final String... args) {
		try {
			final Document doc = Utils.Xml.document(X_STREAM.toXML(obj));
			final String xsl = args.length > 0 ? args[0] : "";
			if (xsl.length() > 0) {
				final String stylesheet = MessageFormat.format("type=''text/xsl'' href=''{0}''", xsl);
				doc.insertBefore(doc.createProcessingInstruction("xml-stylesheet", stylesheet), doc.getDocumentElement());
			}
			return Utils.Xml.toString(doc).getBytes();
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}
	}
	@Override
	public String getMimeType(final String mime) {
		return "text/xml";
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy