001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 */ 019package org.nuxeo.template.processors.xslt; 020 021import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING; 022 023import java.io.ByteArrayOutputStream; 024import java.io.IOException; 025import java.util.ArrayList; 026import java.util.List; 027 028import javax.xml.transform.ErrorListener; 029import javax.xml.transform.Transformer; 030import javax.xml.transform.TransformerConfigurationException; 031import javax.xml.transform.TransformerException; 032import javax.xml.transform.TransformerFactory; 033import javax.xml.transform.stream.StreamResult; 034import javax.xml.transform.stream.StreamSource; 035 036import org.nuxeo.common.utils.FileUtils; 037import org.nuxeo.ecm.core.api.Blob; 038import org.nuxeo.ecm.core.api.NuxeoException; 039import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 040import org.nuxeo.ecm.core.api.impl.blob.ByteArrayBlob; 041import org.nuxeo.template.api.TemplateInput; 042import org.nuxeo.template.api.TemplateProcessor; 043import org.nuxeo.template.api.adapters.TemplateBasedDocument; 044import org.nuxeo.template.processors.AbstractTemplateProcessor; 045 046public class XSLTProcessor extends AbstractTemplateProcessor implements TemplateProcessor { 047 048 @Override 049 public Blob renderTemplate(TemplateBasedDocument templateBasedDocument, String templateName) { 050 051 BlobHolder bh = templateBasedDocument.getAdaptedDoc().getAdapter(BlobHolder.class); 052 if (bh == null) { 053 return null; 054 } 055 056 Blob xmlContent = bh.getBlob(); 057 if (xmlContent == null) { 058 return null; 059 } 060 061 Blob sourceTemplateBlob = getSourceTemplateBlob(templateBasedDocument, templateName); 062 063 TransformerFactory tFactory = TransformerFactory.newInstance(); 064 Transformer transformer; 065 try { 066 tFactory.setFeature(FEATURE_SECURE_PROCESSING, true); 067 transformer = tFactory.newTransformer(new StreamSource(sourceTemplateBlob.getStream())); 068 } catch (TransformerConfigurationException | IOException e) { 069 throw new NuxeoException(e); 070 } 071 transformer.setErrorListener(new ErrorListener() { 072 073 @Override 074 public void warning(TransformerException exception) throws TransformerException { 075 log.warn("Problem during transformation", exception); 076 } 077 078 @Override 079 public void fatalError(TransformerException exception) throws TransformerException { 080 log.error("Fatal error during transformation", exception); 081 } 082 083 @Override 084 public void error(TransformerException exception) throws TransformerException { 085 log.error("Error during transformation", exception); 086 } 087 }); 088 transformer.setURIResolver(null); 089 090 ByteArrayOutputStream out = new ByteArrayOutputStream(); 091 092 try { 093 transformer.transform(new StreamSource(xmlContent.getStream()), new StreamResult(out)); 094 } catch (TransformerException | IOException e) { 095 throw new NuxeoException(e); 096 } 097 098 Blob result = new ByteArrayBlob(out.toByteArray(), "text/xml"); 099 String targetFileName = FileUtils.getFileNameNoExt(templateBasedDocument.getAdaptedDoc().getTitle()); 100 // result.setFilename(targetFileName + ".xml"); 101 result.setFilename(targetFileName + ".html"); 102 103 return result; 104 105 } 106 107 @Override 108 public List<TemplateInput> getInitialParametersDefinition(Blob blob) { 109 return new ArrayList<>(); 110 } 111 112}