Search in sources :

Example 91 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project winery by eclipse.

the class Util method getXMLAsString.

public static String getXMLAsString(Element el) {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t;
    try {
        t = tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new IllegalStateException("Could not instantiate Transformer", e);
    }
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    Source source = new DOMSource(el);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Result target = new StreamResult(os);
    try {
        t.transform(source, target);
    } catch (TransformerException e) {
        Util.LOGGER.debug(e.getMessage(), e);
        throw new IllegalStateException("Could not transform dom node to string", e);
    }
    return os.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 92 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project winery by eclipse.

the class CsarExporter method writeCsar.

/**
 * Writes a complete CSAR containing all necessary things reachable from the given service template
 *
 * @param entryId the id of the service template to export
 * @param out     the output stream to write to
 */
public void writeCsar(IRepository repository, DefinitionsChildId entryId, OutputStream out) throws ArchiveException, IOException, JAXBException, RepositoryCorruptException {
    CsarExporter.LOGGER.trace("Starting CSAR export with {}", entryId.toString());
    Map<RepositoryFileReference, String> refMap = new HashMap<>();
    Collection<String> definitionNames = new ArrayList<>();
    try (final ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", out)) {
        ToscaExportUtil exporter = new ToscaExportUtil();
        Map<String, Object> conf = new HashMap<>();
        ExportedState exportedState = new ExportedState();
        DefinitionsChildId currentId = entryId;
        do {
            String defName = CsarExporter.getDefinitionsPathInsideCSAR(repository, currentId);
            definitionNames.add(defName);
            zos.putArchiveEntry(new ZipArchiveEntry(defName));
            Collection<DefinitionsChildId> referencedIds;
            referencedIds = exporter.exportTOSCA(repository, currentId, zos, refMap, conf);
            zos.closeArchiveEntry();
            exportedState.flagAsExported(currentId);
            exportedState.flagAsExportRequired(referencedIds);
            currentId = exportedState.pop();
        } while (currentId != null);
        // if we export a ServiceTemplate, data for the self-service portal might exist
        if (entryId instanceof ServiceTemplateId) {
            ServiceTemplateId serviceTemplateId = (ServiceTemplateId) entryId;
            this.addSelfServiceMetaData(repository, serviceTemplateId, refMap);
            this.addSelfServiceFiles(repository, serviceTemplateId, refMap, zos);
        }
        // now, refMap contains all files to be added to the CSAR
        // write manifest directly after the definitions to have it more at the beginning of the ZIP rather than having it at the very end
        this.addManifest(repository, entryId, definitionNames, refMap, zos);
        // used for generated XSD schemas
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = tFactory.newTransformer();
        } catch (TransformerConfigurationException e1) {
            CsarExporter.LOGGER.debug(e1.getMessage(), e1);
            throw new IllegalStateException("Could not instantiate transformer", e1);
        }
        // write all referenced files
        for (RepositoryFileReference ref : refMap.keySet()) {
            String archivePath = refMap.get(ref);
            CsarExporter.LOGGER.trace("Creating {}", archivePath);
            if (ref instanceof DummyRepositoryFileReferenceForGeneratedXSD) {
                addDummyRepositoryFileReferenceForGeneratedXSD(zos, transformer, (DummyRepositoryFileReferenceForGeneratedXSD) ref, archivePath);
            } else {
                if (ref.getParent() instanceof DirectoryId) {
                    // special handling for artifact template directories "source" and "files"
                    addArtifactTemplateToZipFile(zos, repository, ref, archivePath);
                } else {
                    addFileToZipArchive(zos, repository, ref, archivePath);
                    zos.closeArchiveEntry();
                }
            }
        }
        this.addNamespacePrefixes(zos, repository);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DefinitionsChildId(org.eclipse.winery.common.ids.definitions.DefinitionsChildId) ServiceTemplateId(org.eclipse.winery.common.ids.definitions.ServiceTemplateId) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ServiceTemplateSelfServiceFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ServiceTemplateSelfServiceFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) RepositoryFileReference(org.eclipse.winery.common.RepositoryFileReference) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream)

Example 93 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project wcomponents by BorderTech.

the class TransformXMLInterceptor method initTemplates.

/**
 * Statically initialize the XSLT templates that are cached for all future transforms.
 *
 * @return the XSLT Templates.
 */
private static Templates initTemplates() {
    try {
        URL xsltURL = ThemeUtil.class.getResource(RESOURCE_NAME);
        if (xsltURL != null) {
            Source xsltSource = new StreamSource(xsltURL.openStream(), xsltURL.toExternalForm());
            TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();
            Templates templates = factory.newTemplates(xsltSource);
            LOG.debug("Generated XSLT templates for: " + RESOURCE_NAME);
            return templates;
        } else {
            // Server-side XSLT enabled but theme resource not on classpath.
            throw new IllegalStateException(RESOURCE_NAME + " not on classpath");
        }
    } catch (IOException | TransformerConfigurationException ex) {
        throw new SystemException("Could not create transformer for " + RESOURCE_NAME, ex);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SystemException(com.github.bordertech.wcomponents.util.SystemException) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

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