Search in sources :

Example 81 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project ant-ivy by apache.

the class IvyArtifactReport method createTransformerHandler.

private TransformerHandler createTransformerHandler(FileOutputStream fileOutputStream) throws TransformerFactoryConfigurationError, TransformerConfigurationException {
    SAXTransformerFactory transformerFact = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler saxHandler = transformerFact.newTransformerHandler();
    saxHandler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    saxHandler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
    saxHandler.setResult(new StreamResult(fileOutputStream));
    return saxHandler;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory)

Example 82 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project ant-ivy by apache.

the class IvyArtifactReport method generateXml.

private void generateXml(IvyNode[] dependencies, Map<ModuleRevisionId, Set<ArtifactDownloadReport>> moduleRevToArtifactsMap, Map<ArtifactDownloadReport, Set<String>> artifactsToCopy) {
    try {
        try (FileOutputStream fileOutputStream = new FileOutputStream(tofile)) {
            TransformerHandler saxHandler = createTransformerHandler(fileOutputStream);
            saxHandler.startDocument();
            saxHandler.startElement(null, "modules", "modules", new AttributesImpl());
            for (IvyNode dependency : dependencies) {
                if (dependency.getModuleRevision() == null || dependency.isCompletelyEvicted()) {
                    continue;
                }
                startModule(saxHandler, dependency);
                Set<ArtifactDownloadReport> artifactsOfModuleRev = moduleRevToArtifactsMap.get(dependency.getModuleRevision().getId());
                if (artifactsOfModuleRev != null) {
                    for (ArtifactDownloadReport artifact : artifactsOfModuleRev) {
                        RepositoryCacheManager cache = dependency.getModuleRevision().getArtifactResolver().getRepositoryCacheManager();
                        startArtifact(saxHandler, artifact.getArtifact());
                        writeOriginLocationIfPresent(cache, saxHandler, artifact);
                        writeCacheLocationIfPresent(cache, saxHandler, artifact);
                        for (String artifactDestPath : artifactsToCopy.get(artifact)) {
                            writeRetrieveLocation(saxHandler, artifactDestPath);
                        }
                        saxHandler.endElement(null, "artifact", "artifact");
                    }
                }
                saxHandler.endElement(null, "module", "module");
            }
            saxHandler.endElement(null, "modules", "modules");
            saxHandler.endDocument();
        }
    } catch (SAXException | IOException | TransformerConfigurationException e) {
        throw new BuildException("impossible to generate report", e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) RepositoryCacheManager(org.apache.ivy.core.cache.RepositoryCacheManager) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) FileOutputStream(java.io.FileOutputStream) BuildException(org.apache.tools.ant.BuildException) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Example 83 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project alfresco-repository by Alfresco.

the class TikaPoweredContentTransformer method getContentHandler.

/**
 * Returns an appropriate Tika ContentHandler for the
 *  requested content type. Normally you'll let this
 *  work as default, but if you need fine-grained
 *  control of how the Tika events become text then
 *  override and supply your own.
 */
protected ContentHandler getContentHandler(String targetMimeType, Writer output) throws TransformerConfigurationException {
    if (MimetypeMap.MIMETYPE_TEXT_PLAIN.equals(targetMimeType)) {
        return new BodyContentHandler(output);
    }
    SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler handler = factory.newTransformerHandler();
    handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
    handler.setResult(new StreamResult(output));
    if (MimetypeMap.MIMETYPE_HTML.equals(targetMimeType)) {
        handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
        return new ExpandedTitleContentHandler(handler);
    } else if (MimetypeMap.MIMETYPE_XHTML.equals(targetMimeType) || MimetypeMap.MIMETYPE_XML.equals(targetMimeType)) {
        handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");
    } else {
        throw new TransformerInfoException(WRONG_FORMAT_MESSAGE_ID, new IllegalArgumentException("Requested target type " + targetMimeType + " not supported"));
    }
    return handler;
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) ExpandedTitleContentHandler(org.apache.tika.sax.ExpandedTitleContentHandler)

Example 84 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project alfresco-repository by Alfresco.

the class HTMLRenderingEngine method buildContentHandler.

/**
 * Builds a Tika-compatible SAX content handler, which will
 *  be used to generate+capture the XHTML
 */
private ContentHandler buildContentHandler(Writer output, RenderingContext context) {
    // Create the main transformer
    SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler handler;
    try {
        handler = factory.newTransformerHandler();
    } catch (TransformerConfigurationException e) {
        throw new RenditionServiceException("SAX Processing isn't available - " + e);
    }
    handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
    handler.setResult(new StreamResult(output));
    handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");
    // Change the image links as they go past
    String dirName = null, imgPrefix = null;
    if (context.getParamWithDefault(PARAM_IMAGES_SAME_FOLDER, false)) {
        imgPrefix = getImagesPrefixName(context);
    } else {
        dirName = getImagesDirectoryName(context);
    }
    ContentHandler contentHandler = new TikaImageRewritingContentHandler(handler, dirName, imgPrefix);
    // If required, wrap it to only return the body
    boolean bodyOnly = context.getParamWithDefault(PARAM_BODY_CONTENTS_ONLY, false);
    if (bodyOnly) {
        contentHandler = new BodyContentHandler(contentHandler);
    }
    // All done
    return contentHandler;
}
Also used : BodyContentHandler(org.apache.tika.sax.BodyContentHandler) TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) RenditionServiceException(org.alfresco.service.cmr.rendition.RenditionServiceException) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler)

Aggregations

TransformerHandler (javax.xml.transform.sax.TransformerHandler)84 StreamResult (javax.xml.transform.stream.StreamResult)57 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)51 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)33 Transformer (javax.xml.transform.Transformer)29 IOException (java.io.IOException)23 SAXException (org.xml.sax.SAXException)22 AttributesImpl (org.xml.sax.helpers.AttributesImpl)17 StringWriter (java.io.StringWriter)13 SAXResult (javax.xml.transform.sax.SAXResult)13 File (java.io.File)11 XMLReader (org.xml.sax.XMLReader)11 FileOutputStream (java.io.FileOutputStream)10 Test (org.junit.Test)10 InputStream (java.io.InputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 OutputStream (java.io.OutputStream)8 ContentHandler (org.xml.sax.ContentHandler)8 InputSource (org.xml.sax.InputSource)8 Metadata (org.apache.tika.metadata.Metadata)7