Refactored XSLTRenderingEngine and TemplatingRenderingEngine (now called FreemarkerRenderingEngine) to extract common elements.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@19168 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Brian Remmington
2010-03-09 21:19:25 +00:00
parent 47837f9286
commit 2c9854b53b
7 changed files with 193 additions and 535 deletions

View File

@@ -24,7 +24,6 @@ import java.io.File;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -41,9 +40,9 @@ import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
import org.alfresco.repo.content.transform.magick.ImageTransformationOptions;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.rendition.executer.AbstractRenderingEngine;
import org.alfresco.repo.rendition.executer.FreemarkerRenderingEngine;
import org.alfresco.repo.rendition.executer.ImageRenderingEngine;
import org.alfresco.repo.rendition.executer.ReformatRenderingEngine;
import org.alfresco.repo.rendition.executer.TemplatingRenderingEngine;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.service.cmr.action.Action;
@@ -59,7 +58,6 @@ import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.thumbnail.ThumbnailService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
@@ -203,7 +201,7 @@ public class RenditionServiceIntegrationTest extends BaseAlfrescoSpringTest
this.setComplete();
this.endTransaction();
final QName renditionName = QName.createQName(NamespaceService.RENDITION_MODEL_1_0_URI,
TemplatingRenderingEngine.NAME);
FreemarkerRenderingEngine.NAME);
this.renditionNode = transactionHelper
.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>()
@@ -212,8 +210,8 @@ public class RenditionServiceIntegrationTest extends BaseAlfrescoSpringTest
{
// create test model
RenditionDefinition definition = renditionService.createRenditionDefinition(renditionName,
TemplatingRenderingEngine.NAME);
definition.setParameterValue(TemplatingRenderingEngine.PARAM_TEMPLATE_NODE,
FreemarkerRenderingEngine.NAME);
definition.setParameterValue(FreemarkerRenderingEngine.PARAM_TEMPLATE_NODE,
nodeWithFreeMarkerContent);
ChildAssociationRef renditionAssoc = renditionService
.render(nodeWithDocContent, definition);
@@ -240,7 +238,7 @@ public class RenditionServiceIntegrationTest extends BaseAlfrescoSpringTest
this.setComplete();
this.endTransaction();
final QName renditionName = QName.createQName(NamespaceService.RENDITION_MODEL_1_0_URI,
TemplatingRenderingEngine.NAME);
FreemarkerRenderingEngine.NAME);
this.renditionNode = transactionHelper
.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<NodeRef>()
@@ -249,8 +247,8 @@ public class RenditionServiceIntegrationTest extends BaseAlfrescoSpringTest
{
// create test model
RenditionDefinition definition = renditionService.createRenditionDefinition(renditionName,
TemplatingRenderingEngine.NAME);
definition.setParameterValue(TemplatingRenderingEngine.PARAM_TEMPLATE_NODE,
FreemarkerRenderingEngine.NAME);
definition.setParameterValue(FreemarkerRenderingEngine.PARAM_TEMPLATE_NODE,
nodeWithFreeMarkerContent);
ChildAssociationRef renditionAssoc = renditionService
.render(nodeWithDocContent, definition);
@@ -270,9 +268,9 @@ public class RenditionServiceIntegrationTest extends BaseAlfrescoSpringTest
this.setComplete();
this.endTransaction();
final QName renditionName1 = QName.createQName(NamespaceService.RENDITION_MODEL_1_0_URI,
TemplatingRenderingEngine.NAME + "_UpdateOnAnyPropChange");
FreemarkerRenderingEngine.NAME + "_UpdateOnAnyPropChange");
final QName renditionName2 = QName.createQName(NamespaceService.RENDITION_MODEL_1_0_URI,
TemplatingRenderingEngine.NAME + "_UpdateOnContentPropChange");
FreemarkerRenderingEngine.NAME + "_UpdateOnContentPropChange");
final Pair<NodeRef, NodeRef> renditions = transactionHelper
.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Pair<NodeRef, NodeRef>>()
@@ -285,15 +283,15 @@ public class RenditionServiceIntegrationTest extends BaseAlfrescoSpringTest
// create test model 1 - rendition to update on any property change
RenditionDefinition definition1 = renditionService.createRenditionDefinition(renditionName1,
TemplatingRenderingEngine.NAME);
definition1.setParameterValue(TemplatingRenderingEngine.PARAM_TEMPLATE_PATH,
FreemarkerRenderingEngine.NAME);
definition1.setParameterValue(FreemarkerRenderingEngine.PARAM_TEMPLATE_PATH,
templatePath);
definition1.setParameterValue(AbstractRenderingEngine.PARAM_UPDATE_RENDITIONS_ON_ANY_PROPERTY_CHANGE, Boolean.TRUE);
// create test model 2 - rendition to update on content property change
RenditionDefinition definition2 = renditionService.createRenditionDefinition(renditionName2,
TemplatingRenderingEngine.NAME);
definition2.setParameterValue(TemplatingRenderingEngine.PARAM_TEMPLATE_PATH,
FreemarkerRenderingEngine.NAME);
definition2.setParameterValue(FreemarkerRenderingEngine.PARAM_TEMPLATE_PATH,
templatePath);
definition2.setParameterValue(AbstractRenderingEngine.PARAM_UPDATE_RENDITIONS_ON_ANY_PROPERTY_CHANGE, Boolean.FALSE);

View File

@@ -0,0 +1,124 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.rendition.executer;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.template.TemplateNode;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.TemplateImageResolver;
/**
* @author Nick Smith
* @since 3.3
*/
public class FreemarkerRenderingEngine
extends BaseTemplateRenderingEngine
{
public static final String NAME = "freemarkerRenderingEngine";
private static final String PARAM_IMAGE_RESOLVER = "image_resolver";
/**
* The name of the source node as it appears in the model supplied to the freemarker template
*/
public static final String KEY_NODE = "node";
private Repository repository;
private ServiceRegistry serviceRegistry;
/*
* @seeorg.alfresco.repo.rendition.executer.AbstractRenderingEngine#
* getParameterDefinitions()
*/
@Override
protected Collection<ParameterDefinition> getParameterDefinitions()
{
Collection<ParameterDefinition> paramList = super.getParameterDefinitions();
paramList.add(new ParameterDefinitionImpl(
PARAM_IMAGE_RESOLVER,
DataTypeDefinition.ANY,
false,
getParamDisplayLabel(PARAM_IMAGE_RESOLVER)));
return paramList;
}
@SuppressWarnings("unchecked")
@Override
protected Object buildModel(RenderingContext context)
{
// The templateNode can be null.
NodeRef companyHome = repository.getCompanyHome();
NodeRef templateNode = getTemplateNode(context);
Map<String, Serializable> paramMap = context.getCheckedParam(PARAM_MODEL, Map.class);
TemplateImageResolver imgResolver = context.getCheckedParam(PARAM_IMAGE_RESOLVER,
TemplateImageResolver.class);
// The fully authenticated user below is the username of the person who logged in and
// who requested the execution of the current rendition. This will not be the
// same person as the current user as renditions are executed by the system user.
String fullyAuthenticatedUser = AuthenticationUtil.getFullyAuthenticatedUser();
NodeRef person = serviceRegistry.getPersonService().getPerson(fullyAuthenticatedUser);
NodeRef userHome = repository.getUserHome(person);
Map<String, Object> model = getTemplateService().buildDefaultModel(person, companyHome,
userHome, templateNode, imgResolver);
TemplateNode sourceTemplateNode = new TemplateNode(context.getSourceNode(), serviceRegistry, imgResolver);
// TODO Add xml dom here.
// model.put("xml", NodeModel.wrap(null));
model.put(KEY_NODE, sourceTemplateNode);
if (paramMap != null)
model.putAll(paramMap);
return model;
}
@Override
protected String getTemplateType()
{
return "freemarker";
}
/**
* @param repository the repository to set
*/
public void setRepositoryHelper(Repository repository)
{
this.repository = repository;
}
/**
* @param serviceRegistry the serviceRegistry to set
*/
public void setServiceRegistry(ServiceRegistry serviceRegistry)
{
this.serviceRegistry = serviceRegistry;
}
}

View File

@@ -1,113 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.rendition.executer;
import java.io.Serializable;
import java.util.Map;
import org.alfresco.repo.model.Repository;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.template.TemplateNode;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.TemplateImageResolver;
import org.alfresco.service.cmr.repository.TemplateService;
/**
* @author Nick Smith
*/
public class TemplateModelHelper
{
public static final String KEY_NODE = "node";
private final Repository repository;
private final ServiceRegistry serviceRegistry;
private final TemplateService templateService;
/**
* @param templateService
* @param repository
* @param serviceRegistry
*/
public TemplateModelHelper(TemplateService templateService, Repository repository, ServiceRegistry serviceRegistry)
{
super();
this.templateService = templateService;
this.repository = repository;
this.serviceRegistry = serviceRegistry;
}
/**
* Builds up the model map used by the {@link TemplateService} to process
* FreeMarker templates.
*
* @param sourceNode the node containing the content to be processed by the
* template.
* @param templateNode the node containing the template. Can be
* <code>null</code>.
* @param imgResolver the image resolver used to process images. Can be
* <code>null</code>.
* @param paramMap a map of parameters to add to the model. Can be
* <code>null</code>.
* @return the populated model {@link Map}.
*/
public Map<String, Object> buildModelMap(NodeRef sourceNode,//
NodeRef templateNode,//
TemplateImageResolver imgResolver,//
Map<String, Serializable> paramMap)
{
Map<String, Object> model = buildDefaultModel(templateNode, imgResolver);
TemplateNode sourceTemplateNode = new TemplateNode(sourceNode, serviceRegistry, null);
// TODO Add xml dom here.
// model.put("xml", NodeModel.wrap(null));
model.put(KEY_NODE, sourceTemplateNode);
if (paramMap != null)
model.putAll(paramMap);
return model;
}
/**
* Builds the default model populated with the current user, company home
* and user home.
*
* @param templateNode the node containing the template. Can be
* <code>null</code>.
* @param imgResolver the image resolver used to process images. Can be
* <code>null</code>.
* @return the default model {@link Map}.
*/
public Map<String, Object> buildDefaultModel(NodeRef templateNode, TemplateImageResolver imgResolver)
{
// The templateNode can be null.
NodeRef companyHome = repository.getCompanyHome();
// The fully authenticated user below is the username of the person who logged in and
// who requested the execution of the current rendition. This will not be the
// same person as the current user as renditions are executed by the system user.
String fullyAuthenticatedUser = AuthenticationUtil.getFullyAuthenticatedUser();
NodeRef person = serviceRegistry.getPersonService().getPerson(fullyAuthenticatedUser);
NodeRef userHome = repository.getUserHome(person);
Map<String, Object> model = templateService.buildDefaultModel(person, companyHome, userHome, templateNode,
imgResolver);
return model;
}
}

View File

@@ -1,225 +0,0 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.rendition.executer;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.Writer;
import java.util.Collection;
import java.util.Map;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.model.Repository;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.rendition.RenditionServiceException;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.TemplateImageResolver;
import org.alfresco.service.cmr.repository.TemplateService;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
/**
* @author Nick Smith
*/
public class TemplatingRenderingEngine//
extends AbstractRenderingEngine//
implements InitializingBean
{
private final static Log log = LogFactory.getLog(TemplatingRenderingEngine.class);
public static final String NAME = "templatingRenderingEngine";
public static final String PARAM_MODEL = "model";
public static final String PARAM_TEMPLATE = "template_string";
public static final String PARAM_TEMPLATE_NODE = "template_node";
public static final String PARAM_TEMPLATE_PATH = "template_path";
private static final String PARAM_IMAGE_RESOLVER = "image_resolver";
private TemplateService templateService;
private Repository repository;
private ServiceRegistry serviceRegistry;
private TemplateModelHelper modelHelper;
/*
* @see
* org.alfresco.repo.rendition.executer.AbstractRenderingEngine#render(org
* .alfresco.service.cmr.repository.NodeRef,
* org.alfresco.service.cmr.rendition.RenditionDefinition,
* org.alfresco.service.cmr.repository.ContentReader,
* org.alfresco.service.cmr.repository.ChildAssociationRef)
*/
@SuppressWarnings("unchecked")
@Override
protected void render(RenderingContext context)
{
NodeRef sourceNode = context.getSourceNode();
NodeRef templateNode = getTemplateNode(context);
Map<String, Serializable> paramMap = context.getCheckedParam(PARAM_MODEL, Map.class);
TemplateImageResolver imgResolver = context.getCheckedParam(PARAM_IMAGE_RESOLVER, TemplateImageResolver.class);
Map<String, Object> model = modelHelper.buildModelMap(sourceNode, templateNode, imgResolver, paramMap);
processTemplate(context, templateNode, model);
}
private void processTemplate(RenderingContext context, NodeRef templateNode, Map<String, Object> model)
{
String template = context.getCheckedParam(PARAM_TEMPLATE, String.class);
if ((template == null) && (templateNode == null))
{
throwTemplateParamsNotFoundException();
}
ContentWriter contentWriter = context.makeContentWriter();
Writer writer = new OutputStreamWriter(contentWriter.getContentOutputStream());
try
{
if (template != null)
{
templateService.processTemplateString("freemarker", template, model, writer);
}
else if (templateNode != null)
{
templateService.processTemplate("freemarker", templateNode.toString(), model, writer);
}
}
finally
{
try
{
writer.close();
} catch (IOException ex)
{
// Nothing that can be done. Log it and move on.
log.warn("Failed to close content writer: ", ex);
}
}
}
private void throwTemplateParamsNotFoundException()
{
StringBuilder msg = new StringBuilder("This action requires that either the ");
msg.append(PARAM_TEMPLATE);
msg.append(" parameter or the ");
msg.append(PARAM_TEMPLATE_NODE);
msg.append(" parameter be specified. ");
throw new RenditionServiceException(msg.toString());
}
private NodeRef getTemplateNode(RenderingContext context)
{
NodeRef node = context.getCheckedParam(PARAM_TEMPLATE_NODE, NodeRef.class);
if (node == null)
{
String path = context.getCheckedParam(PARAM_TEMPLATE_PATH, String.class);
if (path != null && path.length() > 0)
{
SearchService searchService = serviceRegistry.getSearchService();
StoreRef storeRef = context.getDestinationNode().getStoreRef();
ResultSet result = searchService.query(storeRef, SearchService.LANGUAGE_XPATH, path);
if (result.length() != 1)
{
throw new RenditionServiceException("Could not find template node for path: " + path);
}
node = result.getNodeRef(0);
}
}
return node;
}
/*
* @seeorg.alfresco.repo.rendition.executer.AbstractRenderingEngine#
* getParameterDefinitions()
*/
@Override
protected Collection<ParameterDefinition> getParameterDefinitions()
{
Collection<ParameterDefinition> paramList = super.getParameterDefinitions();
ParameterDefinitionImpl modelParamDef = new ParameterDefinitionImpl(//
PARAM_MODEL,//
DataTypeDefinition.ANY,//
false,//
getParamDisplayLabel(PARAM_MODEL));
ParameterDefinitionImpl templateParamDef = new ParameterDefinitionImpl(//
PARAM_TEMPLATE,//
DataTypeDefinition.TEXT,//
false,//
getParamDisplayLabel(PARAM_TEMPLATE));
ParameterDefinitionImpl templateNodeParamDef = new ParameterDefinitionImpl(//
PARAM_TEMPLATE_NODE,//
DataTypeDefinition.NODE_REF,//
false,//
getParamDisplayLabel(PARAM_TEMPLATE_NODE));
ParameterDefinitionImpl templatePathParamDef = new ParameterDefinitionImpl(//
PARAM_TEMPLATE_PATH,//
DataTypeDefinition.TEXT,//
false,//
getParamDisplayLabel(PARAM_TEMPLATE_PATH));
ParameterDefinitionImpl imgResolverParamDef = new ParameterDefinitionImpl(//
PARAM_IMAGE_RESOLVER,//
DataTypeDefinition.ANY,//
false,//
getParamDisplayLabel(PARAM_IMAGE_RESOLVER));
paramList.add(modelParamDef);
paramList.add(templateParamDef);
paramList.add(templateNodeParamDef);
paramList.add(templatePathParamDef);
paramList.add(imgResolverParamDef);
return paramList;
}
/**
* @param templateService the templateService to set
*/
public void setTemplateService(TemplateService templateService)
{
this.templateService = templateService;
}
/**
* @param repository the repository to set
*/
public void setRepositoryHelper(Repository repository)
{
this.repository = repository;
}
/**
* @param serviceRegistry the serviceRegistry to set
*/
public void setServiceRegistry(ServiceRegistry serviceRegistry)
{
this.serviceRegistry = serviceRegistry;
}
/**
* Sets up {@link TemplateModelHelper}.
*/
public void afterPropertiesSet()
{
this.modelHelper = new TemplateModelHelper(templateService, repository, serviceRegistry);
}
}

View File

@@ -1,57 +1,40 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.rendition.executer;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.template.TemplateProcessorMethod;
import org.alfresco.repo.template.XSLTProcessor;
import org.alfresco.repo.template.XSLTemplateModel;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.model.FileNotFoundException;
import org.alfresco.service.cmr.rendition.RenditionServiceException;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.TemplateService;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.NamespacePrefixResolver;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
@@ -67,99 +50,18 @@ import org.xml.sax.SAXException;
* @author Brian Remmington
* @since 3.3
*/
public class XSLTRenderingEngine extends AbstractRenderingEngine
public class XSLTRenderingEngine extends BaseTemplateRenderingEngine
{
private static final Log log = LogFactory.getLog(XSLTRenderingEngine.class);
public static final String NAME = "xsltRenderingEngine";
public static final String PARAM_MODEL = "model";
public static final String PARAM_TEMPLATE = "template_string";
public static final String PARAM_TEMPLATE_NODE = "template_node";
public static final String PARAM_TEMPLATE_PATH = "template_path";
private TemplateService templateService;
private XSLTFunctions xsltFunctions;
private NamespacePrefixResolver namespacePrefixResolver;
private FileFolderService fileFolderService;
private SearchService searchService;
/*
* @see org.alfresco.repo.rendition.executer.AbstractRenderingEngine#render(org
* .alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.rendition.RenditionDefinition,
* org.alfresco.service.cmr.repository.ContentReader, org.alfresco.service.cmr.repository.ChildAssociationRef)
*/
@SuppressWarnings("unchecked")
@Override
protected void render(RenderingContext context)
{
NodeRef templateNode = getTemplateNode(context);
Map<String, Serializable> paramMap = context.getCheckedParam(PARAM_MODEL, Map.class);
try
{
XSLTemplateModel model = buildModel(context, paramMap);
ContentWriter contentWriter = context.makeContentWriter();
Writer writer = new OutputStreamWriter(contentWriter.getContentOutputStream());
processTemplate(context, templateNode, model, writer);
writer.flush();
writer.close();
}
catch (Exception ex)
{
log.warn("Unexpected error while rendering through XSLT rendering engine.", ex);
}
}
private void processTemplate(RenderingContext context, NodeRef templateNode, XSLTemplateModel model, Writer out)
{
String template = context.getCheckedParam(PARAM_TEMPLATE, String.class);
if (template != null)
{
templateService.processTemplateString("xslt", (String) template, model, out);
}
else if (templateNode != null)
{
templateService.processTemplate("xslt", templateNode.toString(), model, out);
}
else
{
throwTemplateParamsNotFoundException();
}
}
private void throwTemplateParamsNotFoundException()
{
StringBuilder msg = new StringBuilder("This action requires that either the ");
msg.append(PARAM_TEMPLATE);
msg.append(" parameter or the ");
msg.append(PARAM_TEMPLATE_NODE);
msg.append(" parameter be specified. ");
throw new RenditionServiceException(msg.toString());
}
private NodeRef getTemplateNode(RenderingContext context)
{
NodeRef node = context.getCheckedParam(PARAM_TEMPLATE_NODE, NodeRef.class);
if (node == null)
{
String path = context.getCheckedParam(PARAM_TEMPLATE_PATH, String.class);
if (path != null && path.length() > 0)
{
StoreRef storeRef = context.getDestinationNode().getStoreRef();
ResultSet result = searchService.query(storeRef, SearchService.LANGUAGE_XPATH, path);
if (result.length() != 1)
{
throw new RenditionServiceException("Could not find template node for path: " + path);
}
node = result.getNodeRef(0);
}
}
return node;
}
@SuppressWarnings("serial")
protected XSLTemplateModel buildModel(RenderingContext context, Map<String, Serializable> suppliedParams)
throws IOException, SAXException
@SuppressWarnings({ "serial", "unchecked" })
protected Object buildModel(RenderingContext context)
{
Map<String, Serializable> suppliedParams = context.getCheckedParam(PARAM_MODEL, Map.class);
final NodeRef sourceNode = context.getSourceNode();
final NodeRef parentNode = nodeService.getPrimaryParent(context.getSourceNode()).getParentRef();
final String sourcePath = getPath(sourceNode);
@@ -316,10 +218,27 @@ public class XSLTRenderingEngine extends AbstractRenderingEngine
}
// add the xml document
model.put(XSLTProcessor.ROOT_NAMESPACE, XMLUtil.parse(sourceNode, contentService));
try
{
model.put(XSLTProcessor.ROOT_NAMESPACE, XMLUtil.parse(sourceNode, contentService));
}
catch (RuntimeException ex)
{
throw ex;
}
catch (Exception ex)
{
throw new RenditionServiceException("Failed to parse XML from source node.", ex);
}
return model;
}
@Override
protected String getTemplateType()
{
return "xslt";
}
/**
* @param nodeRef
* @return
@@ -345,37 +264,6 @@ public class XSLTRenderingEngine extends AbstractRenderingEngine
return path;
}
/*
* @seeorg.alfresco.repo.rendition.executer.AbstractRenderingEngine# getParameterDefinitions()
*/
@Override
protected Collection<ParameterDefinition> getParameterDefinitions()
{
Collection<ParameterDefinition> paramList = super.getParameterDefinitions();
ParameterDefinitionImpl modelParamDef = new ParameterDefinitionImpl(PARAM_MODEL, DataTypeDefinition.ANY, false,
getParamDisplayLabel(PARAM_MODEL));
ParameterDefinitionImpl templateParamDef = new ParameterDefinitionImpl(//
PARAM_TEMPLATE, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_TEMPLATE));
ParameterDefinitionImpl templateNodeParamDef = new ParameterDefinitionImpl(PARAM_TEMPLATE_NODE,
DataTypeDefinition.NODE_REF, false, getParamDisplayLabel(PARAM_TEMPLATE_NODE));
ParameterDefinitionImpl templatePathParamDef = new ParameterDefinitionImpl(PARAM_TEMPLATE_PATH,
DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_TEMPLATE_PATH));
paramList.add(modelParamDef);
paramList.add(templateParamDef);
paramList.add(templateNodeParamDef);
paramList.add(templatePathParamDef);
return paramList;
}
/**
* @param templateService
* the templateService to set
*/
public void setTemplateService(TemplateService templateService)
{
this.templateService = templateService;
}
/**
* @param xsltFunctions
* the xsltFunctions to set
@@ -402,13 +290,4 @@ public class XSLTRenderingEngine extends AbstractRenderingEngine
{
this.fileFolderService = fileFolderService;
}
/**
* @param searchService
* the searchService to set
*/
public void setSearchService(SearchService searchService)
{
this.searchService = searchService;
}
}

View File

@@ -1,26 +1,20 @@
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have received a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.rendition.executer;