-
-
Save jpukg/913c43fb888a2d05014ca490e08d20a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.StringWriter; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.bind.util.JAXBSource; | |
import javax.xml.transform.Source; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerConfigurationException; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.TransformerFactoryConfigurationError; | |
import javax.xml.transform.stream.StreamResult; | |
public class XslTransformer<T> { | |
private Source stylesheet = null; | |
private T source; | |
private Map<String, Object> parameters; | |
private XslTransformer(Source stylesheet, T source, Map<String, Object> parameters) { | |
this.stylesheet = stylesheet; | |
this.source = source; | |
this.parameters = parameters; | |
} | |
public String transform() { | |
if (stylesheet == null) { | |
throw new IllegalArgumentException("Stylesheet not found"); | |
} | |
try { | |
StringWriter writer = new StringWriter(); | |
Transformer transformer = XslTransformer.createTransformer(stylesheet, parameters); | |
JAXBSource jaxbSource = XslTransformer.retrieveJAXBSourceFromObject(source); | |
transformer.transform(jaxbSource, new StreamResult(writer)); | |
return writer.toString(); | |
} | |
catch (Exception e) { | |
throw new RuntimeException("An error occured during the rendering of the document", e); | |
} | |
} | |
public static class Builder<T> { | |
private Source stylesheet = null; | |
private T source = null; | |
private Map<String, Object> parameters = new LinkedHashMap<String, Object>(); | |
public Builder<T> stylesheet(Source stylesheet) { | |
this.stylesheet = stylesheet; | |
return this; | |
} | |
public Builder<T> source(T source) { | |
this.source = source; | |
return this; | |
} | |
public Builder<T> parameters(Map<String, Object> parameters) { | |
this.parameters.putAll(parameters); | |
return this; | |
} | |
public Builder<T> parameter(String key, Object value) { | |
this.parameters.put(key, value); | |
return this; | |
} | |
public XslTransformer<T> build() { | |
return new XslTransformer<T>(stylesheet, source, parameters); | |
} | |
} | |
public static <T> JAXBSource retrieveJAXBSourceFromObject(T document) { | |
JAXBSource source = null; | |
try { | |
JAXBContext context = JAXBContext.newInstance(document.getClass()); | |
Marshaller marshaller = context.createMarshaller(); | |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | |
source = new JAXBSource(marshaller, document); | |
} | |
catch (JAXBException e) { | |
throw new IllegalArgumentException(e); | |
} | |
return source; | |
} | |
public static Transformer createTransformer(Source stylesheet, Map<String, Object> parameters) { | |
Transformer transformer = null; | |
try { | |
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); | |
TransformerFactory factory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null); | |
transformer = factory.newTransformer(stylesheet); | |
for (Entry<String, Object> parameter : parameters.entrySet()) { | |
transformer.setParameter(parameter.getKey(), parameter.getValue()); | |
} | |
} | |
catch (TransformerConfigurationException | TransformerFactoryConfigurationError e) { | |
throw new IllegalArgumentException(e); | |
} | |
return transformer; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertNotNull; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import javax.xml.transform.stream.StreamSource; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.context.support.AnnotationConfigContextLoader; | |
import com.company.jaxb.MyJaxbDocument; | |
import com.company.jaxb.ObjectFactory; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(loader = AnnotationConfigContextLoader.class) | |
public class XslTransformerTest { | |
@Configuration | |
static class ContextConfiguration { | |
@Bean | |
public ObjectFactory objectFactory() { | |
return new ObjectFactory(); | |
} | |
} | |
@Autowired | |
private ObjectFactory objectFactory; | |
private MyJaxbDocument source; | |
private InputStream stylesheet; | |
@Before | |
public void onSetUp() throws IOException { | |
this.source = this.objectFactory.createBarcodeDocument(); | |
this.source.setId("123"); | |
this.source.setTitle("Un titre"); | |
this.source.setWebsite("http://www.nouvellemarque.com"); | |
this.stylesheet = getClass().getResourceAsStream("stylesheet.xsl"); | |
} | |
@Test | |
public void testGenerationSuccess() { | |
// @formatter:off | |
XslTransformer<BarcodeDocument> renderer = new XslTransformer.Builder<BarcodeDocument>() | |
.stylesheet(new StreamSource(stylesheet)) | |
.source(source) | |
.parameter("paramtext", "un texte") | |
.build(); | |
// @formatter:on | |
String html = renderer.transform(); | |
assertNotNull(html); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment