use of javax.xml.transform.Templates in project bioformats by openmicroscopy.
the class Schema_Transform_Test method applyTransforms.
/**
* Applies the transforms to the specified XML file.
*
* @param inputXML
* The file to transforms.
* @param transforms
* The collection of transforms.
* @return See above.
* @throws Exception
* Thrown if an error occurred during the transformations.
*/
private File applyTransforms(File inputXML, List<InputStream> transforms) throws Exception {
TransformerFactory factory;
Transformer transformer;
InputStream stream;
Iterator<InputStream> i = transforms.iterator();
File output;
InputStream in = null;
OutputStream out = null;
Resolver resolver = null;
while (i.hasNext()) {
stream = i.next();
try {
factory = TransformerFactory.newInstance();
resolver = new Resolver();
factory.setURIResolver(resolver);
output = File.createTempFile("tempFileName", "." + OME_XML);
output.deleteOnExit();
Source src = new StreamSource(stream);
Templates template = factory.newTemplates(src);
transformer = template.newTransformer();
transformer.setParameter(OutputKeys.ENCODING, Constants.ENCODING);
out = new FileOutputStream(output);
in = new FileInputStream(inputXML);
transformer.transform(new StreamSource(in), new StreamResult(out));
inputXML = output;
} catch (Exception e) {
throw new Exception("Cannot apply transform", e);
} finally {
if (stream != null)
stream.close();
if (out != null)
out.close();
if (in != null)
in.close();
if (resolver != null)
resolver.close();
}
}
return inputXML;
}
use of javax.xml.transform.Templates in project lionengine by b3dgs.
the class XmlTest method testTransformerError.
/**
* Test transformer error.
*
* @throws IllegalArgumentException If error.
* @throws IllegalAccessException If error.
* @throws NoSuchFieldException If error.
*/
@Test
void testTransformerError() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
final Field field = DocumentFactory.class.getDeclaredField("transformerFactory");
UtilReflection.setAccessible(field, true);
final javax.xml.transform.TransformerFactory old = (TransformerFactory) field.get(DocumentFactory.class);
try {
field.set(DocumentFactory.class, new javax.xml.transform.TransformerFactory() {
@Override
public Transformer newTransformer() throws TransformerConfigurationException {
throw new TransformerConfigurationException();
}
@Override
public Transformer newTransformer(Source source) throws TransformerConfigurationException {
return null;
}
@Override
public Templates newTemplates(Source source) throws TransformerConfigurationException {
return null;
}
@Override
public Source getAssociatedStylesheet(Source source, String media, String title, String charset) throws TransformerConfigurationException {
return null;
}
@Override
public void setURIResolver(URIResolver resolver) {
// Mock
}
@Override
public URIResolver getURIResolver() {
return null;
}
@Override
public void setFeature(String name, boolean value) throws TransformerConfigurationException {
// Mock
}
@Override
public boolean getFeature(String name) {
return false;
}
@Override
public void setAttribute(String name, Object value) {
// Mock
}
@Override
public Object getAttribute(String name) {
return null;
}
@Override
public void setErrorListener(ErrorListener listener) {
// Mock
}
@Override
public ErrorListener getErrorListener() {
return null;
}
});
final Media output = Medias.create("out.xml");
assertThrows(() -> new Xml(Medias.create("normalize.xml")).save(output), "[out.xml] " + Xml.ERROR_WRITING);
} finally {
field.set(DocumentFactory.class, old);
}
}
use of javax.xml.transform.Templates in project uPortal by Jasig.
the class TemplatesBuilder method loadResource.
/* (non-Javadoc)
* @see org.apereo.portal.utils.cache.resource.ResourceBuilder#buildResource(org.springframework.core.io.Resource, java.io.InputStream)
*/
@Override
public LoadedResource<Templates> loadResource(Resource resource) throws IOException {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
if (this.transformerAttributes != null) {
for (final Map.Entry<String, Object> attributeEntry : this.transformerAttributes.entrySet()) {
transformerFactory.setAttribute(attributeEntry.getKey(), attributeEntry.getValue());
}
}
final ResourceTrackingURIResolver uriResolver = new ResourceTrackingURIResolver(this.resourceLoader);
transformerFactory.setURIResolver(uriResolver);
final URI uri = resource.getURI();
final String systemId = uri.toString();
final InputStream stream = resource.getInputStream();
final Templates templates;
try {
final StreamSource source = new StreamSource(stream, systemId);
templates = transformerFactory.newTemplates(source);
} catch (TransformerConfigurationException e) {
throw new IOException("Failed to parse stream into Templates", e);
} finally {
IOUtils.closeQuietly(stream);
}
final Map<Resource, Long> resolvedResources = uriResolver.getResolvedResources();
return new LoadedResourceImpl<Templates>(templates, resolvedResources);
}
use of javax.xml.transform.Templates in project uPortal by Jasig.
the class JaxbPortalDataHandlerServiceTest method setup.
@Before
public void setup() throws Exception {
xmlUtilities = new XmlUtilitiesImpl() {
@Override
public Templates getTemplates(Resource stylesheet) throws TransformerConfigurationException, IOException {
final TransformerFactory transformerFactory = TransformerFactory.newInstance();
return transformerFactory.newTemplates(new StreamSource(stylesheet.getInputStream()));
}
};
dataImportExportService.setXmlUtilities(xmlUtilities);
final ThreadPoolExecutorFactoryBean threadPoolExecutorFactoryBean = new ThreadPoolExecutorFactoryBean();
threadPoolExecutorFactoryBean.setCorePoolSize(0);
threadPoolExecutorFactoryBean.setMaxPoolSize(20);
threadPoolExecutorFactoryBean.setQueueCapacity(20);
threadPoolExecutorFactoryBean.setThreadGroupName("uPortal-ImportExportThreadGroup");
threadPoolExecutorFactoryBean.setThreadNamePrefix("uPortal-ImportExport-");
threadPoolExecutorFactoryBean.setThreadPriority(5);
threadPoolExecutorFactoryBean.setKeepAliveSeconds(30);
threadPoolExecutorFactoryBean.setDaemon(true);
threadPoolExecutorFactoryBean.setAllowCoreThreadTimeOut(true);
threadPoolExecutorFactoryBean.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
threadPoolExecutorFactoryBean.afterPropertiesSet();
threadPoolExecutor = threadPoolExecutorFactoryBean.getObject();
this.dataImportExportService.setImportExportThreadPool(threadPoolExecutor);
dataImportExportService.setDataFileIncludes(ImmutableSet.of("**/*.xml", "**/*.entity-type", "**/*.template-user", "**/*.user", "**/*.group", "**/*.group_membership", "**/*.membership", "**/*.portlet-type", "**/*.channel-type", "**/*.portlet", "**/*.channel", "**/*.permission", "**/*.permission_set", "**/*.permission_owner", "**/*.profile", "**/*.fragment-layout", "**/*.layout", "**/*.fragment-definition"));
dataImportExportService.setDataTypeImportOrder(getPortalDataTypes());
}
use of javax.xml.transform.Templates in project tomee by apache.
the class XSLTJaxbProvider method getAnnotationTemplates.
protected Templates getAnnotationTemplates(Annotation[] anns) {
Templates t = null;
XSLTTransform ann = AnnotationUtils.getAnnotation(anns, XSLTTransform.class);
if (ann != null) {
t = annotationTemplates.get(ann.value());
}
return t;
}
Aggregations