use of javax.xml.transform.Templates in project mycore by MyCoRe-Org.
the class MCRVFSContentTest method testGetSource.
/**
* Test method for {@link org.mycore.common.content.MCRContent#getSource()}.
* @throws IOException
* @throws TransformerException
*/
@Test
public final void testGetSource() throws IOException, TransformerException {
CommonVFSResolver resolver = new CommonVFSResolver(fileObject);
assertFalse("File is open", resolver.isContentOpen());
// identity transformation
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
StreamResult result = new StreamResult(System.out);
transformer.transform(resolver.resolve("test://test", null), result);
assertFalse("File is open after identity transformation", resolver.isContentOpen());
// simple transformation
URL xslURL = MCRVFSContentTest.class.getResource(TEST_BASE + "test.xsl");
URL xmlURL = MCRVFSContentTest.class.getResource(TEST_BASE + "test.xml");
Source xsl = new StreamSource(xslURL.toString());
Source xml = new StreamSource(xmlURL.toString());
transformer = TransformerFactory.newInstance().newTransformer(xsl);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setURIResolver(resolver);
transformer.transform(xml, result);
assertFalse("File is open after simple transformation", resolver.isContentOpen());
// cacheable transformation
Templates templates = TransformerFactory.newInstance().newTemplates(xsl);
transformer = templates.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setURIResolver(resolver);
transformer.transform(xml, result);
assertFalse("File is open after cacheable transformation", resolver.isContentOpen());
}
use of javax.xml.transform.Templates in project mycore by MyCoRe-Org.
the class MCRXSLTransformer method getOutputProperties.
public Properties getOutputProperties() throws TransformerConfigurationException, SAXException, ParserConfigurationException {
checkTemplateUptodate();
Templates lastTemplate = templates[templates.length - 1];
Properties outputProperties = lastTemplate.getOutputProperties();
return outputProperties;
}
use of javax.xml.transform.Templates in project mycore by MyCoRe-Org.
the class MCRXSLTransformerFactory method compileTemplates.
/**
* Compiles the given XSL source, and caches the result
*/
private static Templates compileTemplates(MCRTemplatesSource source) {
Templates templates = MCRTemplatesCompiler.compileTemplates(source);
cache.put(source.getKey(), templates);
return templates;
}
use of javax.xml.transform.Templates in project enclojure by EricThorsen.
the class Processor method process.
public int process() throws TransformerException, IOException, SAXException {
ZipInputStream zis = new ZipInputStream(input);
final ZipOutputStream zos = new ZipOutputStream(output);
final OutputStreamWriter osw = new OutputStreamWriter(zos);
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
TransformerFactory tf = TransformerFactory.newInstance();
if (!tf.getFeature(SAXSource.FEATURE) || !tf.getFeature(SAXResult.FEATURE)) {
return 0;
}
SAXTransformerFactory saxtf = (SAXTransformerFactory) tf;
Templates templates = null;
if (xslt != null) {
templates = saxtf.newTemplates(xslt);
}
// configuring outHandlerFactory
// ///////////////////////////////////////////////////////
EntryElement entryElement = getEntryElement(zos);
ContentHandler outDocHandler = null;
switch(outRepresentation) {
case BYTECODE:
outDocHandler = new OutputSlicingHandler(new ASMContentHandlerFactory(zos, computeMax), entryElement, false);
break;
case MULTI_XML:
outDocHandler = new OutputSlicingHandler(new SAXWriterFactory(osw, true), entryElement, true);
break;
case SINGLE_XML:
ZipEntry outputEntry = new ZipEntry(SINGLE_XML_NAME);
zos.putNextEntry(outputEntry);
outDocHandler = new SAXWriter(osw, false);
break;
}
// configuring inputDocHandlerFactory
// /////////////////////////////////////////////////
ContentHandler inDocHandler;
if (templates == null) {
inDocHandler = outDocHandler;
} else {
inDocHandler = new InputSlicingHandler("class", outDocHandler, new TransformerHandlerFactory(saxtf, templates, outDocHandler));
}
ContentHandlerFactory inDocHandlerFactory = new SubdocumentHandlerFactory(inDocHandler);
if (inDocHandler != null && inRepresentation != SINGLE_XML) {
inDocHandler.startDocument();
inDocHandler.startElement("", "classes", "classes", new AttributesImpl());
}
int i = 0;
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
update(ze.getName(), n++);
if (isClassEntry(ze)) {
processEntry(zis, ze, inDocHandlerFactory);
} else {
OutputStream os = entryElement.openEntry(getName(ze));
copyEntry(zis, os);
entryElement.closeEntry();
}
i++;
}
if (inDocHandler != null && inRepresentation != SINGLE_XML) {
inDocHandler.endElement("", "classes", "classes");
inDocHandler.endDocument();
}
if (outRepresentation == SINGLE_XML) {
zos.closeEntry();
}
zos.flush();
zos.close();
return i;
}
use of javax.xml.transform.Templates in project wcomponents by BorderTech.
the class TransformXMLTestHelper method reloadTransformer.
/**
* Use reflection the reinitialize the TransformXMLInterceptor class.
*/
public static void reloadTransformer() {
try {
Field field = TransformXMLInterceptor.class.getDeclaredField("TEMPLATES");
// Make the field accessible.
field.setAccessible(true);
// Make the field non-final.
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
// Get the value from the static method
Method initTemplates = TransformXMLInterceptor.class.getDeclaredMethod("initTemplates");
initTemplates.setAccessible(true);
Templates value = (Templates) initTemplates.invoke(null);
field.set(null, value);
} catch (SecurityException | InvocationTargetException | NoSuchFieldException | NoSuchMethodException | IllegalArgumentException | IllegalAccessException ex) {
throw new SystemException(ex);
}
}
Aggregations