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 java.io.ByteArrayOutputStream; 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.List; 025 026import javax.xml.transform.ErrorListener; 027import javax.xml.transform.Transformer; 028import javax.xml.transform.TransformerConfigurationException; 029import javax.xml.transform.TransformerException; 030import javax.xml.transform.TransformerFactory; 031import javax.xml.transform.stream.StreamResult; 032import javax.xml.transform.stream.StreamSource; 033 034import org.nuxeo.common.utils.FileUtils; 035import org.nuxeo.ecm.core.api.Blob; 036import org.nuxeo.ecm.core.api.NuxeoException; 037import org.nuxeo.ecm.core.api.blobholder.BlobHolder; 038import org.nuxeo.ecm.core.api.impl.blob.ByteArrayBlob; 039import org.nuxeo.template.api.TemplateInput; 040import org.nuxeo.template.api.TemplateProcessor; 041import org.nuxeo.template.api.adapters.TemplateBasedDocument; 042import org.nuxeo.template.processors.AbstractTemplateProcessor; 043 044public class XSLTProcessor extends AbstractTemplateProcessor implements TemplateProcessor { 045 046 @Override 047 public Blob renderTemplate(TemplateBasedDocument templateBasedDocument, String templateName) { 048 049 BlobHolder bh = templateBasedDocument.getAdaptedDoc().getAdapter(BlobHolder.class); 050 if (bh == null) { 051 return null; 052 } 053 054 Blob xmlContent = bh.getBlob(); 055 if (xmlContent == null) { 056 return null; 057 } 058 059 Blob sourceTemplateBlob = getSourceTemplateBlob(templateBasedDocument, templateName); 060 061 TransformerFactory tFactory = TransformerFactory.newInstance(); 062 Transformer transformer; 063 try { 064 transformer = tFactory.newTransformer(new StreamSource(sourceTemplateBlob.getStream())); 065 } catch (TransformerConfigurationException | IOException e) { 066 throw new NuxeoException(e); 067 } 068 transformer.setErrorListener(new ErrorListener() { 069 070 @Override 071 public void warning(TransformerException exception) throws TransformerException { 072 log.warn("Problem during transformation", exception); 073 } 074 075 @Override 076 public void fatalError(TransformerException exception) throws TransformerException { 077 log.error("Fatal error during transformation", exception); 078 } 079 080 @Override 081 public void error(TransformerException exception) throws TransformerException { 082 log.error("Error during transformation", exception); 083 } 084 }); 085 transformer.setURIResolver(null); 086 087 ByteArrayOutputStream out = new ByteArrayOutputStream(); 088 089 try { 090 transformer.transform(new StreamSource(xmlContent.getStream()), new StreamResult(out)); 091 } catch (TransformerException | IOException e) { 092 throw new NuxeoException(e); 093 } 094 095 Blob result = new ByteArrayBlob(out.toByteArray(), "text/xml"); 096 String targetFileName = FileUtils.getFileNameNoExt(templateBasedDocument.getAdaptedDoc().getTitle()); 097 // result.setFilename(targetFileName + ".xml"); 098 result.setFilename(targetFileName + ".html"); 099 100 return result; 101 102 } 103 104 @Override 105 public List<TemplateInput> getInitialParametersDefinition(Blob blob) { 106 return new ArrayList<TemplateInput>(); 107 } 108 109}