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

aQute.libg.xslt.Transform Maven / Gradle / Ivy

Go to download

The bndlib project is a general library to be used with OSGi bundles. It contains lots of cool functionality that calculates dependencies, etc.

There is a newer version: 2.4.0
Show newest version
package aQute.libg.xslt;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;

import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public class Transform {
	static TransformerFactory	transformerFactory	= TransformerFactory.newInstance();

	static Map	cache				= new ConcurrentHashMap();

	public static void transform(TransformerFactory transformerFactory, URL xslt, InputStream in, OutputStream out)
			throws Exception {
		if (xslt == null)
			throw new IllegalArgumentException("No source template specified");

		Templates templates = cache.get(xslt.toURI());
		if (templates == null) {
			InputStream xsltIn = xslt.openStream();
			try {
				templates = transformerFactory.newTemplates(new StreamSource(xsltIn));

				cache.put(xslt.toURI(), templates);
			}
			finally {
				in.close();
			}
		}
		Result xmlResult = new StreamResult(out);
		Source xmlSource = new StreamSource(in);
		Transformer t = templates.newTransformer();
		t.transform(xmlSource, xmlResult);
		out.flush();
	}

	public static void transform(URL xslt, InputStream in, OutputStream out) throws Exception {
		transform(transformerFactory, xslt, in, out);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy