use of javax.xml.transform.Source in project intellij-community by JetBrains.
the class ExportTestResultsAction method getOutputText.
@Nullable
private String getOutputText(ExportTestResultsConfiguration config) throws IOException, TransformerException, SAXException {
ExportTestResultsConfiguration.ExportFormat exportFormat = config.getExportFormat();
SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler;
if (exportFormat == ExportTestResultsConfiguration.ExportFormat.Xml) {
handler = transformerFactory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
} else {
Source xslSource;
if (config.getExportFormat() == ExportTestResultsConfiguration.ExportFormat.BundledTemplate) {
URL bundledXsltUrl = getClass().getResource("intellij-export.xsl");
xslSource = new StreamSource(URLUtil.openStream(bundledXsltUrl));
} else {
File xslFile = new File(config.getUserTemplatePath());
if (!xslFile.isFile()) {
showBalloon(myRunConfiguration.getProject(), MessageType.ERROR, ExecutionBundle.message("export.test.results.custom.template.not.found", xslFile.getPath()), null);
return null;
}
xslSource = new StreamSource(xslFile);
}
handler = transformerFactory.newTransformerHandler(xslSource);
handler.getTransformer().setParameter("TITLE", ExecutionBundle.message("export.test.results.filename", myRunConfiguration.getName(), myRunConfiguration.getType().getDisplayName()));
}
StringWriter w = new StringWriter();
handler.setResult(new StreamResult(w));
try {
TestResultsXmlFormatter.execute(myModel.getRoot(), myRunConfiguration, myModel.getProperties(), handler);
} catch (ProcessCanceledException e) {
return null;
}
return w.toString();
}
use of javax.xml.transform.Source in project uPortal by Jasig.
the class XSLTComponent method getEventReader.
/* (non-Javadoc)
* @see org.apereo.portal.rendering.StAXPipelineComponent#getXmlStreamReader(java.lang.Object, java.lang.Object)
*/
@Override
public PipelineEventReader<XMLEventReader, XMLEvent> getEventReader(HttpServletRequest request, HttpServletResponse response) {
final PipelineEventReader<XMLEventReader, XMLEvent> pipelineEventReader = this.wrappedComponent.getEventReader(request, response);
final Transformer transformer = this.transformerSource.getTransformer(request, response);
//Setup a URIResolver based on the current resource loader
transformer.setURIResolver(this.uriResolver);
//Configure the Transformer via injected class
if (this.xsltParameterSource != null) {
final Map<String, Object> transformerParameters = this.xsltParameterSource.getParameters(request, response);
if (transformerParameters != null) {
this.logger.debug("{} - Setting Transformer Parameters: ", this.beanName, transformerParameters);
for (final Map.Entry<String, Object> transformerParametersEntry : transformerParameters.entrySet()) {
final String name = transformerParametersEntry.getKey();
final Object value = transformerParametersEntry.getValue();
if (value != null) {
transformer.setParameter(name, value);
}
}
}
final Properties outputProperties = this.xsltParameterSource.getOutputProperties(request, response);
if (outputProperties != null) {
this.logger.debug("{} - Setting Transformer Output Properties: ", this.beanName, outputProperties);
transformer.setOutputProperties(outputProperties);
}
}
//The event reader from the previous component in the pipeline
final XMLEventReader eventReader = pipelineEventReader.getEventReader();
//Wrap the event reader in a stream reader to avoid a JDK bug
final XMLStreamReader streamReader;
try {
streamReader = new FixedXMLEventStreamReader(eventReader);
} catch (XMLStreamException e) {
throw new RuntimeException("Failed to create XMLStreamReader from XMLEventReader", e);
}
final Source xmlReaderSource = new StAXSource(streamReader);
//Setup logging for the transform
transformer.setErrorListener(this.errorListener);
//Transform to a SAX ContentHandler to avoid JDK bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6775588
final XMLEventBufferWriter eventWriterBuffer = new XMLEventBufferWriter();
final ContentHandler contentHandler = StaxUtils.createLexicalContentHandler(eventWriterBuffer);
contentHandler.setDocumentLocator(new LocatorImpl());
final SAXResult outputTarget = new SAXResult(contentHandler);
try {
this.logger.debug("{} - Begining XML Transformation", this.beanName);
transformer.transform(xmlReaderSource, outputTarget);
this.logger.debug("{} - XML Transformation complete", this.beanName);
} catch (TransformerException e) {
throw new RuntimeException("Failed to transform document", e);
}
final String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
final List<XMLEvent> eventBuffer = eventWriterBuffer.getEventBuffer();
final XMLEventReader outputEventReader = new XMLEventBufferReader(eventBuffer.listIterator());
final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
final PipelineEventReaderImpl<XMLEventReader, XMLEvent> pipelineEventReaderImpl = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(outputEventReader, outputProperties);
pipelineEventReaderImpl.setOutputProperty(OutputKeys.MEDIA_TYPE, mediaType);
return pipelineEventReaderImpl;
}
use of javax.xml.transform.Source in project uPortal by Jasig.
the class BaseXsltDataUpgraderTest method loadSchema.
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
Assert.notEmpty(resources, "No resources given");
Assert.hasLength(schemaLanguage, "No schema language provided");
Source[] schemaSources = new Source[resources.length];
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
for (int i = 0; i < resources.length; i++) {
Assert.notNull(resources[i], "Resource is null");
Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
schemaSources[i] = new SAXSource(xmlReader, inputSource);
}
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
return schemaFactory.newSchema(schemaSources);
}
use of javax.xml.transform.Source in project uPortal by Jasig.
the class SitemapTest method testStylesheetCompilation.
@Test
public void testStylesheetCompilation() throws IOException {
final MockHttpServletRequest request = new MockHttpServletRequest();
Resource resource = new ClassPathResource(STYLESHEET_LOCATION);
Source source = new StreamSource(resource.getInputStream(), resource.getURI().toASCIIString());
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer(source);
transformer.setParameter(SitemapPortletController.USE_TAB_GROUPS, useTabGroups);
transformer.setParameter(SitemapPortletController.USER_LANG, "en_US");
transformer.setParameter(XsltPortalUrlProvider.CURRENT_REQUEST, request);
transformer.setParameter(XsltPortalUrlProvider.XSLT_PORTAL_URL_PROVIDER, this.xsltPortalUrlProvider);
Source xmlSource = new StreamSource(new ClassPathResource(XML_LOCATION).getFile());
CharArrayWriter buffer = new CharArrayWriter();
TransformerUtils.enableIndenting(transformer);
transformer.transform(xmlSource, new StreamResult(buffer));
if (logger.isTraceEnabled()) {
logger.trace("XML: " + new String(buffer.toCharArray()));
}
} catch (TransformerConfigurationException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
} catch (TransformerException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
use of javax.xml.transform.Source in project uPortal by Jasig.
the class TemplateDataTenantOperationsListener method importQueue.
/** Imports the specified entities in the proper order. */
private void importQueue(final ITenant tenant, final Map<PortalDataKey, Set<BucketTuple>> queue, final StringBuilder importReport) throws Exception {
final StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.setRootObject(new RootObjectImpl(tenant));
IDataTemplatingStrategy templating = new SpELDataTemplatingStrategy(portalSpELService, ctx);
Document doc = null;
try {
for (PortalDataKey pdk : dataKeyImportOrder) {
Set<BucketTuple> bucket = queue.get(pdk);
if (bucket != null) {
log.debug("Importing the specified PortalDataKey tenant '{}': {}", tenant.getName(), pdk.getName());
for (BucketTuple tuple : bucket) {
doc = tuple.getDocument();
Source data = templating.processTemplates(doc, tuple.getResource().getURL().toString());
dataHandlerService.importData(data, pdk);
importReport.append(createImportReportLineItem(pdk, tuple));
}
}
}
} catch (Exception e) {
log.error("Failed to process the specified template document:\n{}", (doc != null ? doc.asXML() : "null"), e);
throw e;
}
}
Aggregations