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

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

package com.github.gkutiel.flip.web.res;

import java.text.MessageFormat;
import java.util.Map;

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 String xsl;
	private static final XStream X_STREAM = new XStream();

	@Override
	public byte[] getBytes(final Object obj) {
		try {
			final Document doc = Utils.Xml.document(X_STREAM.toXML(obj));
			if (xsl != null) {
				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() {
		return "text/xml";
	}

	@Override
	public void setProps(final Map props) {
		xsl = props.get("xsl");
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy