1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
public class XsltTransformerForTest {
protected Source xmlSource;
protected CharArrayWriter charArrayWriter;
public XsltTransformerForTest(Source xmlSource) {
this.xmlSource = xmlSource;
}
public XsltTransformerForTest(File xmlFile) {
this(new StreamSource(xmlFile));
}
public Source getXmlSource() {
return this.xmlSource;
}
public void setXmlSource(Source xmlSource) {
this.xmlSource = xmlSource;
}
public char[] getCharArray() {
return this.charArrayWriter.toCharArray();
}
public void transform(File xslFile, Map<String, Object> args) throws SocomecException {
LinkedList<File> xslFiles = new LinkedList<File>();
xslFiles.add(xslFile);
transform(xslFiles, args);
}
public void transform(LinkedList<File> xslFiles, Map<String, Object> args) {
Source source = this.xmlSource;
for (File xslFile : xslFiles) {
this.charArrayWriter = new CharArrayWriter();
Result result = new StreamResult(this.charArrayWriter);
Source xslSource = new StreamSource(xslFile);
try {
Transformer transformer = TransformerFactoryImpl.newInstance().newTransformer(xslSource);
for (Entry<String, Object> entry : args.entrySet())
transformer.setParameter(entry.getKey(), args.get(entry.getKey()));
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
Reader reader = new CharArrayReader(this.charArrayWriter.toCharArray());
source = new StreamSource(reader);
}
}
} |
Partager