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;
}
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);
}
}
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;
}
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;
}
Aggregations