Search in sources :

Example 46 with Templates

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;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) URIResolver(javax.xml.transform.URIResolver) StreamResult(javax.xml.transform.stream.StreamResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 47 with Templates

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);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Templates(javax.xml.transform.Templates) URIResolver(javax.xml.transform.URIResolver) Source(javax.xml.transform.Source) ErrorListener(javax.xml.transform.ErrorListener) Field(java.lang.reflect.Field) TransformerFactory(javax.xml.transform.TransformerFactory) Test(org.junit.jupiter.api.Test)

Example 48 with Templates

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);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Resource(org.springframework.core.io.Resource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 49 with Templates

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());
}
Also used : XmlUtilitiesImpl(org.apereo.portal.xml.XmlUtilitiesImpl) TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ThreadPoolExecutorFactoryBean(org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean) StreamSource(javax.xml.transform.stream.StreamSource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Before(org.junit.Before)

Example 50 with Templates

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;
}
Also used : XSLTTransform(org.apache.cxf.jaxrs.ext.xml.XSLTTransform) Templates(javax.xml.transform.Templates)

Aggregations

Templates (javax.xml.transform.Templates)61 TransformerFactory (javax.xml.transform.TransformerFactory)26 StreamSource (javax.xml.transform.stream.StreamSource)25 Transformer (javax.xml.transform.Transformer)21 Source (javax.xml.transform.Source)20 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)18 StreamResult (javax.xml.transform.stream.StreamResult)16 TransformerException (javax.xml.transform.TransformerException)10 InputStream (java.io.InputStream)8 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 DOMResult (javax.xml.transform.dom.DOMResult)7 DOMSource (javax.xml.transform.dom.DOMSource)7 File (java.io.File)5 Result (javax.xml.transform.Result)5 SAXSource (javax.xml.transform.sax.SAXSource)5 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3