Search in sources :

Example 56 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project nhin-d by DirectProject.

the class XslConversion method run.

/**
     * Perform the XSL conversion using the provided map file and message.
     * 
     * @param mapFile
     *            The map file.
     * @param message
     *            The message.
     * @return an XSL conversion.
     * @throws Exception
     */
public String run(String mapFile, String message) throws Exception {
    long start = System.currentTimeMillis();
    String retXml = "";
    Transformer transformer = null;
    try {
        if (conversions.containsKey(mapFile)) {
            Templates temp = conversions.get(mapFile);
            transformer = temp.newTransformer();
            LOGGER.info("From xsl cache");
        } else {
            synchronized (conversions) {
                if (!conversions.containsKey(mapFile)) {
                    /*
                         * Use the static TransformerFactory.newInstance()
                         * method to instantiate a TransformerFactory. The
                         * javax.xml.transform.TransformerFactory system
                         * property setting determines the actual class to
                         * instantiate --
                         * org.apache.xalan.transformer.TransformerImpl.
                         */
                    TransformerFactory tFactory = TransformerFactory.newInstance();
                    /*
                         * Use the TransformerFactory to instantiate a Template
                         * that is thread safe for use in generating Transfomers
                         */
                    InputStream is = this.getClass().getClassLoader().getResourceAsStream(mapFile);
                    if (is == null) {
                        LOGGER.info("Mapfile did not read " + mapFile);
                    }
                    Templates temp = tFactory.newTemplates(new StreamSource(is));
                    transformer = temp.newTransformer();
                    conversions.put(mapFile, temp);
                }
            }
        }
        CharArrayWriter to = new CharArrayWriter();
        transformer.transform(new StreamSource(new CharArrayReader(message.toCharArray())), new StreamResult(to));
        retXml = to.toString();
    } catch (TransformerConfigurationException e) {
        LOGGER.error("Exception occured during XSL conversion", e);
        throw e;
    } catch (TransformerException e) {
        LOGGER.error("Exception occured during XSL conversion", e);
        throw e;
    }
    if (LOGGER.isInfoEnabled()) {
        long elapse = System.currentTimeMillis() - start;
        LOGGER.info("Started at " + new Timestamp(start).toString());
        LOGGER.info("Elapsed conversion time was " + elapse + "ms");
    }
    return retXml;
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) Timestamp(java.sql.Timestamp) CharArrayWriter(java.io.CharArrayWriter) CharArrayReader(java.io.CharArrayReader) TransformerException(javax.xml.transform.TransformerException)

Example 57 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.

the class FragmentDefinitionImporter method unmarshal.

/* (non-Javadoc)
     * @see org.springframework.oxm.Unmarshaller#unmarshal(javax.xml.transform.Source)
     */
@Override
public Object unmarshal(Source source) throws IOException, XmlMappingException {
    if (source instanceof StAXSource) {
        source = fixStAXSource((StAXSource) source);
    }
    final Transformer identityTransformer;
    try {
        identityTransformer = this.xmlUtilities.getIdentityTransformer();
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException("Failed to create identity Transformer", e);
    }
    final DOMResult domResult = new DOMResult();
    try {
        identityTransformer.transform(source, domResult);
    } catch (TransformerException e) {
        throw new RuntimeException("Failed to transform " + source + " into Document", e);
    }
    final Document resultDoc = (Document) domResult.getNode();
    return new Tuple<String, Document>(source.getSystemId(), resultDoc);
}
Also used : Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) Tuple(org.apereo.portal.utils.Tuple)

Example 58 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException 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 59 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project Asqatasun by Asqatasun.

the class AsqatasunCrawlJob method initializeCrawlContext.

/**
     * 
     * @param url
     * @param crawlParameterSet
     * @param heritrixFileName
     * @return
     */
private File initializeCrawlContext(Collection<String> urlList, Set<Parameter> crawlParameterSet, String heritrixFileName) {
    buildOutputDirectory();
    BufferedReader in = null;
    FileWriter fw = null;
    try {
        LOGGER.debug("crawlConfigFilePath: " + crawlConfigFilePath + " for copy");
        String filepath = crawlConfigFilePath + "/" + heritrixFileName;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("filepath : " + filepath);
            for (Parameter param : crawlParameterSet) {
                LOGGER.debug(param.getParameterElement().getParameterElementCode() + " " + param.getValue());
            }
        }
        doc = setOptionToDocument(urlList, crawlParameterSet, doc);
        //write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        String resultFileName = currentJobOutputDir.getPath() + "/" + heritrixFileName;
        StreamResult result = new StreamResult(new File(resultFileName));
        transformer.transform(source, result);
    } catch (IOException | ParserConfigurationException | SAXException ex) {
        LOGGER.error(ex);
        throw new CrawlerException(ex);
    } catch (TransformerConfigurationException ex) {
        LOGGER.error(ex);
        throw new CrawlerException(ex);
    } catch (TransformerException ex) {
        LOGGER.error(ex);
        throw new CrawlerException(ex);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                LOGGER.error(ex);
                throw new CrawlerException(ex);
            }
        }
        if (fw != null) {
            try {
                fw.close();
            } catch (IOException ex) {
                LOGGER.error(ex);
                throw new CrawlerException(ex);
            }
        }
    }
    return new File(currentJobOutputDir.getPath() + "/" + heritrixFileName);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) Document(org.w3c.dom.Document) CrawlerException(org.asqatasun.crawler.exception.CrawlerException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) BufferedReader(java.io.BufferedReader) Parameter(org.asqatasun.entity.parameterization.Parameter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 60 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project camel by apache.

the class XsltBuilder method setTransformerSource.

/**
     * Sets the XSLT transformer from a Source
     *
     * @param source  the source
     * @throws TransformerConfigurationException is thrown if creating a XSLT transformer failed.
     */
public void setTransformerSource(Source source) throws TransformerConfigurationException {
    TransformerFactory factory = converter.getTransformerFactory();
    if (errorListener != null) {
        factory.setErrorListener(errorListener);
    } else {
        // use a logger error listener so users can see from the logs what the error may be
        factory.setErrorListener(new XsltErrorListener());
    }
    if (getUriResolver() != null) {
        factory.setURIResolver(getUriResolver());
    }
    // Check that the call to newTemplates() returns a valid template instance.
    // In case of an xslt parse error, it will return null and we should stop the
    // deployment and raise an exception as the route will not be setup properly.
    Templates templates = factory.newTemplates(source);
    if (templates != null) {
        setTemplate(templates);
    } else {
        throw new TransformerConfigurationException("Error creating XSLT template. " + "This is most likely be caused by a XML parse error. " + "Please verify your XSLT file configured.");
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Templates(javax.xml.transform.Templates)

Aggregations

TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)93 TransformerException (javax.xml.transform.TransformerException)62 Transformer (javax.xml.transform.Transformer)52 StreamResult (javax.xml.transform.stream.StreamResult)49 DOMSource (javax.xml.transform.dom.DOMSource)42 IOException (java.io.IOException)35 TransformerFactory (javax.xml.transform.TransformerFactory)33 StreamSource (javax.xml.transform.stream.StreamSource)23 SAXException (org.xml.sax.SAXException)21 StringWriter (java.io.StringWriter)17 Source (javax.xml.transform.Source)16 TransformerHandler (javax.xml.transform.sax.TransformerHandler)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Document (org.w3c.dom.Document)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 InputStream (java.io.InputStream)10 Node (org.w3c.dom.Node)10 File (java.io.File)9 Result (javax.xml.transform.Result)9