Moving to root below branch label

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2005-12-08 07:13:07 +00:00
commit e1e6508fec
1095 changed files with 230566 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.TemplateNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A special Map that executes an XPath against the parent Node as part of the get()
* Map interface implementation.
*
* @author Kevin Roast
*/
public abstract class BasePathResultsMap extends HashMap implements Cloneable
{
protected static Log logger = LogFactory.getLog(BasePathResultsMap.class);
protected TemplateNode parent;
protected ServiceRegistry services = null;
/**
* Constructor
*
* @param parent The parent TemplateNode to execute searches from
* @param services The ServiceRegistry to use
*/
public BasePathResultsMap(TemplateNode parent, ServiceRegistry services)
{
super(1, 1.0f);
this.services = services;
this.parent = parent;
}
/**
* @see java.util.Map#get(java.lang.Object)
*/
public abstract Object get(Object key);
protected List<TemplateNode> getChildrenByXPath(String xpath, boolean firstOnly)
{
List<TemplateNode> result = null;
if (xpath.length() != 0)
{
if (logger.isDebugEnabled())
logger.debug("Executing xpath: " + xpath);
List<NodeRef> nodes = this.services.getSearchService().selectNodes(
this.parent.getNodeRef(),
xpath,
null,
this.services.getNamespaceService(),
false);
// see if we only want the first result
if (firstOnly == true)
{
if (nodes.size() != 0)
{
result = new ArrayList<TemplateNode>(1);
result.add(new TemplateNode(nodes.get(0), this.services, this.parent.getImageResolver()));
}
}
// or all the results
else
{
result = new ArrayList<TemplateNode>(nodes.size());
for (NodeRef ref : nodes)
{
result.add(new TemplateNode(ref, this.services, this.parent.getImageResolver()));
}
}
}
return result != null ? result : new ArrayList<TemplateNode>(0);
}
}

View File

@@ -0,0 +1,247 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import org.alfresco.model.ContentModel;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.util.ApplicationContextHelper;
import freemarker.cache.TemplateLoader;
/**
* Custom FreeMarker template loader to locate templates stored either from the ClassPath
* or in a Alfresco Repository.
* <p>
* The template name should be supplied either as a NodeRef String or a ClassPath path String.
*
* @author Kevin Roast
*/
public class ClassPathRepoTemplateLoader implements TemplateLoader
{
private NodeService nodeService;
private ContentService contentService;
public ClassPathRepoTemplateLoader(NodeService nodeService, ContentService contentService)
{
if (nodeService == null)
{
throw new IllegalArgumentException("NodeService is mandatory.");
}
if (contentService == null)
{
throw new IllegalArgumentException("ContentService is mandatory.");
}
this.nodeService = nodeService;
this.contentService = contentService;
}
/**
* Return an object wrapping a source for a template
*/
public Object findTemplateSource(String name)
throws IOException
{
if (name.indexOf(StoreRef.URI_FILLER) != -1)
{
NodeRef ref = new NodeRef(name);
if (this.nodeService.exists(ref) == true)
{
return new RepoTemplateSource(ref);
}
else
{
return null;
}
}
else
{
URL url = this.getClass().getClassLoader().getResource(name);
return url == null ? null : new ClassPathTemplateSource(url);
}
}
public long getLastModified(Object templateSource)
{
return ((BaseTemplateSource)templateSource).lastModified();
}
public Reader getReader(Object templateSource, String encoding) throws IOException
{
return ((BaseTemplateSource)templateSource).getReader();
}
public void closeTemplateSource(Object templateSource) throws IOException
{
((BaseTemplateSource)templateSource).close();
}
/**
* Class used as a base for custom Template Source objects
*/
abstract class BaseTemplateSource
{
public abstract Reader getReader() throws IOException;
public abstract void close() throws IOException;
public abstract long lastModified();
}
/**
* Class providing a ClassPath based Template Source
*/
class ClassPathTemplateSource extends BaseTemplateSource
{
private final URL url;
private URLConnection conn;
private InputStream inputStream;
ClassPathTemplateSource(URL url) throws IOException
{
this.url = url;
this.conn = url.openConnection();
}
public boolean equals(Object o)
{
if (o instanceof ClassPathTemplateSource)
{
return url.equals(((ClassPathTemplateSource)o).url);
}
else
{
return false;
}
}
public int hashCode()
{
return url.hashCode();
}
public String toString()
{
return url.toString();
}
public long lastModified()
{
return conn.getLastModified();
}
public Reader getReader() throws IOException
{
inputStream = conn.getInputStream();
return new InputStreamReader(inputStream);
}
public void close() throws IOException
{
try
{
if (inputStream != null)
{
inputStream.close();
}
}
finally
{
inputStream = null;
conn = null;
}
}
}
/**
* Class providing a Repository based Template Source
*/
class RepoTemplateSource extends BaseTemplateSource
{
private final NodeRef nodeRef;
private InputStream inputStream;
private ContentReader conn;
RepoTemplateSource(NodeRef ref) throws IOException
{
this.nodeRef = ref;
this.conn = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
}
public boolean equals(Object o)
{
if (o instanceof RepoTemplateSource)
{
return nodeRef.equals(((RepoTemplateSource)o).nodeRef);
}
else
{
return false;
}
}
public int hashCode()
{
return nodeRef.hashCode();
}
public String toString()
{
return nodeRef.toString();
}
public long lastModified()
{
return conn.getLastModified();
}
public Reader getReader() throws IOException
{
inputStream = conn.getContentInputStream();
return new InputStreamReader(inputStream);
}
public void close() throws IOException
{
try
{
if (inputStream != null)
{
inputStream.close();
}
}
finally
{
inputStream = null;
conn = null;
}
}
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.net.URL;
import freemarker.cache.URLTemplateLoader;
/**
* Custom FreeMarker template loader to locate templates stored on the ClassPath.
*
* @author Kevin Roast
*/
public class ClassPathTemplateLoader extends URLTemplateLoader
{
/**
* @see freemarker.cache.URLTemplateLoader#getURL(java.lang.String)
*/
protected URL getURL(String name)
{
return this.getClass().getClassLoader().getResource(name);
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.util.Date;
import java.util.List;
import freemarker.ext.beans.BeanModel;
import freemarker.template.TemplateDateModel;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNumberModel;
/**
* @author Kevin Roast
*
* Custom FreeMarker Template language method.
* <p>
* Compare two dates to see if they differ by the specified number of miliseconds
* <p>
* Usage:
* dateCompare(dateA, dateB) - 1 if dateA if greater than dateB
* dateCompare(dateA, dateB, millis) - 1 if dateA is greater than dateB by at least millis, else 0
*/
public final class DateCompareMethod implements TemplateMethodModelEx
{
/**
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
*/
public Object exec(List args) throws TemplateModelException
{
int result = 0;
if (args.size() >= 2)
{
Object arg0 = args.get(0);
Object arg1 = args.get(1);
long diff = 0;
if (args.size() == 3)
{
Object arg2 = args.get(2);
if (arg2 instanceof TemplateNumberModel)
{
Number number = ((TemplateNumberModel)arg2).getAsNumber();
diff = number.longValue();
}
}
if (arg0 instanceof TemplateDateModel && arg1 instanceof TemplateDateModel)
{
Date dateA = (Date)((TemplateDateModel)arg0).getAsDate();
Date dateB = (Date)((TemplateDateModel)arg1).getAsDate();
if (dateA.getTime() > (dateB.getTime() - diff))
{
result = 1;
}
}
}
return result;
}
}

View File

@@ -0,0 +1,147 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.io.IOException;
import java.io.Writer;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.TemplateException;
import org.alfresco.service.cmr.repository.TemplateProcessor;
import org.apache.log4j.Logger;
import freemarker.cache.MruCacheStorage;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
/**
* FreeMarker implementation the template processor interface
*
* @author Kevin Roast
*/
public class FreeMarkerProcessor implements TemplateProcessor
{
private final static String MSG_ERROR_NO_TEMPLATE = "error_no_template";
private final static String MSG_ERROR_TEMPLATE_FAIL = "error_template_fail";
private final static String MSG_ERROR_TEMPLATE_IO = "error_template_io";
private static Logger logger = Logger.getLogger(FreeMarkerProcessor.class);
/** FreeMarker processor configuration */
private Configuration config = null;
/** The permission-safe node service */
private NodeService nodeService;
/** The Content Service to use */
private ContentService contentService;
/**
* Set the node service
*
* @param nodeService The permission-safe node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* Set the content service
*
* @param contentService The ContentService to use
*/
public void setContentService(ContentService contentService)
{
this.contentService = contentService;
}
/**
* @return The FreeMarker config instance for this processor
*/
private Configuration getConfig()
{
if (this.config == null)
{
Configuration config = new Configuration();
// setup template cache
config.setCacheStorage(new MruCacheStorage(20, 0));
// use our custom loader to find templates on the ClassPath
config.setTemplateLoader(new ClassPathRepoTemplateLoader(nodeService, contentService));
// use our custom object wrapper that can deal with QNameMap objects directly
config.setObjectWrapper(new QNameAwareObjectWrapper());
// rethrow any exception so we can deal with them
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
this.config = config;
}
return this.config;
}
/**
* @see org.alfresco.service.cmr.repository.TemplateProcessor#process(java.lang.String, java.lang.Object, java.io.Writer)
*/
public void process(String template, Object model, Writer out)
{
if (template == null || template.length() == 0)
{
throw new IllegalArgumentException("Template name is mandatory.");
}
if (model == null)
{
throw new IllegalArgumentException("Model is mandatory.");
}
if (out == null)
{
throw new IllegalArgumentException("Output Writer is mandatory.");
}
try
{
if (logger.isDebugEnabled())
logger.debug("Executing template: " + template + " on model: " + model);
Template t = getConfig().getTemplate(template);
if (t != null)
{
try
{
// perform the template processing against supplied data model
t.process(model, out);
}
catch (Throwable err)
{
throw new TemplateException(MSG_ERROR_TEMPLATE_FAIL, new Object[] {err.getMessage()}, err);
}
}
else
{
throw new TemplateException(MSG_ERROR_NO_TEMPLATE, new Object[] {template});
}
}
catch (IOException ioerr)
{
throw new TemplateException(MSG_ERROR_TEMPLATE_IO, new Object[] {template}, ioerr);
}
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.service.cmr.repository.TemplateNode;
import org.alfresco.service.namespace.QName;
import freemarker.ext.beans.BeanModel;
import freemarker.ext.beans.StringModel;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;
/**
* @author Kevin Roast
*
* Custom FreeMarker Template language method.
* <p>
* Method returns whether a TemplateNode has a particular aspect applied to it. The aspect
* name can be either the fully qualified QName or the short prefixed name string.
* <p>
* Usage: hasAspect(TemplateNode node, String aspect)
*/
public final class HasAspectMethod implements TemplateMethodModelEx
{
/**
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
*/
public Object exec(List args) throws TemplateModelException
{
int result = 0;
if (args.size() == 2)
{
// arg 0 must be a wrapped TemplateNode object
BeanModel arg0 = (BeanModel)args.get(0);
// arg 1 can be either wrapped QName object or a String
String arg1String = null;
Object arg1 = args.get(1);
if (arg1 instanceof BeanModel)
{
arg1String = ((BeanModel)arg1).getWrappedObject().toString();
}
else if (arg1 instanceof TemplateScalarModel)
{
arg1String = ((TemplateScalarModel)arg1).getAsString();
}
if (arg0.getWrappedObject() instanceof TemplateNode)
{
// test to see if this node has the aspect
if ( ((TemplateNode)arg0.getWrappedObject()).hasAspect(arg1String) )
{
result = 1;
}
}
}
return Integer.valueOf(result);
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.util.List;
import org.alfresco.i18n.I18NUtil;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;
/**
* @author Kevin Roast
*
* Custom FreeMarker Template language method.
* <p>
* Method an I18N message resolved for the current locale and specified message ID.
* <p>
* Usage: message(String id)
*/
public final class I18NMessageMethod implements TemplateMethodModelEx
{
/**
* @see freemarker.template.TemplateMethodModel#exec(java.util.List)
*/
public Object exec(List args) throws TemplateModelException
{
String result = "";
if (args.size() == 1)
{
Object arg0 = args.get(0);
if (arg0 instanceof TemplateScalarModel)
{
String id = ((TemplateScalarModel)arg0).getAsString();
result = I18NUtil.getMessage(id);
}
}
return result;
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.util.List;
import java.util.StringTokenizer;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.TemplateNode;
/**
* A special Map that executes an XPath against the parent Node as part of the get()
* Map interface implementation.
*
* @author Kevin Roast
*/
public final class NamePathResultsMap extends BasePathResultsMap implements Cloneable
{
/**
* Constructor
*
* @param parent The parent TemplateNode to execute searches from
* @param services The ServiceRegistry to use
*/
public NamePathResultsMap(TemplateNode parent, ServiceRegistry services)
{
super(parent, services);
}
/**
* @see java.util.Map#get(java.lang.Object)
*/
public Object get(Object key)
{
StringBuilder xpath = new StringBuilder(128);
for (StringTokenizer t = new StringTokenizer(key.toString(), "/"); t.hasMoreTokens(); /**/)
{
if (xpath.length() != 0)
{
xpath.append('/');
}
xpath.append("*[@cm:name='")
.append(t.nextToken()) // TODO: escape quotes?
.append("']");
}
List<TemplateNode> nodes = getChildrenByXPath(xpath.toString(), true);
return (nodes.size() != 0) ? nodes.get(0) : null;
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.util.Map;
import org.alfresco.service.namespace.QNameMap;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleHash;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
/**
* @author Kevin Roast
*/
public class QNameAwareObjectWrapper extends DefaultObjectWrapper
{
/**
* Override to support wrapping of a QNameNodeMap by our custom wrapper object
*/
public TemplateModel wrap(Object obj) throws TemplateModelException
{
if (obj instanceof QNameMap)
{
return new QNameHash((QNameMap)obj, this);
}
else
{
return super.wrap(obj);
}
}
/**
* Inner class to support clone of QNameNodeMap
*/
class QNameHash extends SimpleHash
{
/**
* Constructor
*
* @param map
* @param wrapper
*/
public QNameHash(QNameMap map, ObjectWrapper wrapper)
{
super(map, wrapper);
}
/**
* Override to support clone of a QNameNodeMap object
*/
protected Map copyMap(Map map)
{
if (map instanceof QNameMap)
{
return (Map)((QNameMap)map).clone();
}
else
{
return super.copyMap(map);
}
}
}
}

View File

@@ -0,0 +1,204 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.cmr.repository.TemplateException;
import org.alfresco.service.cmr.repository.TemplateProcessor;
import org.alfresco.service.cmr.repository.TemplateService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author Kevin Roast
*/
public class TemplateServiceImpl implements TemplateService, ApplicationContextAware
{
private static Log logger = LogFactory.getLog(TemplateService.class);
/** Spring ApplicationContext for bean lookup by ID */
private ApplicationContext applicationContext;
/** Default Template processor engine to use */
private String defaultTemplateEngine;
/** Available template engine names to impl class names */
private Map<String, String> templateEngines;
/** Threadlocal instance for template processor cache */
private static ThreadLocal<Map<String, TemplateProcessor>> processors = new ThreadLocal();
/**
* Set the application context
*
* @param applicationContext the application context
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
this.applicationContext = applicationContext;
}
/**
* @param defaultTemplateEngine The default Template Engine name to set.
*/
public void setDefaultTemplateEngine(String defaultTemplateEngine)
{
this.defaultTemplateEngine = defaultTemplateEngine;
}
/**
* @param templateEngines The Map of template engine name to impl class name to set.
*/
public void setTemplateEngines(Map<String, String> templateEngines)
{
this.templateEngines = templateEngines;
}
/**
* @see org.alfresco.service.cmr.repository.TemplateService#getTemplateProcessor(java.lang.String)
*/
public TemplateProcessor getTemplateProcessor(String engine)
{
try
{
return getTemplateProcessorImpl(engine);
}
catch (Throwable err)
{
if (logger.isDebugEnabled())
logger.debug("Unable to load template processor.", err);
return null;
}
}
/**
* @see org.alfresco.service.cmr.repository.TemplateService#processTemplate(java.lang.String, java.lang.String, java.lang.Object, java.io.Writer)
*/
public void processTemplate(String engine, String template, Object model, Writer out)
throws TemplateException
{
try
{
// execute template processor
TemplateProcessor processor = getTemplateProcessorImpl(engine);
processor.process(template, model, out);
}
catch (TemplateException terr)
{
throw terr;
}
catch (Throwable err)
{
throw new TemplateException(err.getMessage(), err);
}
}
/**
* @see org.alfresco.service.cmr.repository.TemplateService#processTemplate(java.lang.String, java.lang.String, java.lang.Object)
*/
public String processTemplate(String engine, String template, Object model)
throws TemplateException
{
Writer out = new StringWriter(1024);
processTemplate(engine, template, model, out);
return out.toString();
}
/**
* Return the TemplateProcessor implementation for the named template engine
*
* @param name Template Engine name
*
* @return TemplateProcessor
*/
private TemplateProcessor getTemplateProcessorImpl(String name)
{
// use the ThreadLocal map to find the processors instance
// create the cache map for this thread if required
Map<String, TemplateProcessor> procMap = processors.get();
if (procMap == null)
{
procMap = new HashMap<String, TemplateProcessor>(7, 1.0f);
processors.set(procMap);
}
if (name == null)
{
name = defaultTemplateEngine;
}
// find the impl for the named processor
TemplateProcessor processor = procMap.get(name);
if (processor == null)
{
String className = templateEngines.get(name);
if (className == null)
{
throw new AlfrescoRuntimeException("Unable to find configured ClassName for template engine: " + name);
}
try
{
Object obj;
try
{
obj = this.applicationContext.getBean(className);
}
catch (BeansException err)
{
// instantiate the processor class directory if not a Spring bean
obj = Class.forName(className).newInstance();
}
if (obj instanceof TemplateProcessor)
{
processor = (TemplateProcessor)obj;
}
else
{
throw new AlfrescoRuntimeException("Supplied template processors does not implement TemplateProcessor: " + className);
}
}
catch (ClassNotFoundException err1)
{
// if the bean is not a classname, then it may be a spring bean Id
throw new AlfrescoRuntimeException("Unable to load class for supplied template processors: " + className, err1);
}
catch (IllegalAccessException err2)
{
throw new AlfrescoRuntimeException("Unable to load class for supplied template processors: " + className, err2);
}
catch (InstantiationException err3)
{
throw new AlfrescoRuntimeException("Unable to instantiate class for supplied template processors: " + className, err3);
}
// cache for later
procMap.put(name, processor);
}
return processor;
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.alfresco.repo.dictionary.DictionaryComponent;
import org.alfresco.repo.dictionary.DictionaryDAO;
import org.alfresco.repo.dictionary.M2Model;
import org.alfresco.repo.node.BaseNodeServiceTest;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.transaction.TransactionUtil;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.TemplateNode;
import org.alfresco.service.cmr.repository.TemplateService;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
/**
* @author Kevin Roast
*/
public class TemplateServiceImplTest extends TestCase
{
private static final ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private ContentService contentService;
private TemplateService templateService;
private NodeService nodeService;
private TransactionService transactionService;
private ServiceRegistry serviceRegistry;
private AuthenticationComponent authenticationComponent;
/*
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception
{
super.setUp();
transactionService = (TransactionService)this.ctx.getBean("transactionComponent");
contentService = (ContentService)this.ctx.getBean("contentService");
nodeService = (NodeService)this.ctx.getBean("nodeService");
templateService = (TemplateService)this.ctx.getBean("templateService");
serviceRegistry = (ServiceRegistry)this.ctx.getBean("ServiceRegistry");
this.authenticationComponent = (AuthenticationComponent)ctx.getBean("authenticationComponent");
this.authenticationComponent.setSystemUserAsCurrentUser();
DictionaryDAO dictionaryDao = (DictionaryDAO)ctx.getBean("dictionaryDAO");
// load the system model
ClassLoader cl = BaseNodeServiceTest.class.getClassLoader();
InputStream modelStream = cl.getResourceAsStream("alfresco/model/contentModel.xml");
assertNotNull(modelStream);
M2Model model = M2Model.createModel(modelStream);
dictionaryDao.putModel(model);
// load the test model
modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
assertNotNull(modelStream);
model = M2Model.createModel(modelStream);
dictionaryDao.putModel(model);
DictionaryComponent dictionary = new DictionaryComponent();
dictionary.setDictionaryDAO(dictionaryDao);
BaseNodeServiceTest.loadModel(ctx);
}
@Override
protected void tearDown() throws Exception
{
authenticationComponent.clearCurrentSecurityContext();
super.tearDown();
}
public void testTemplates()
{
TransactionUtil.executeInUserTransaction(
transactionService,
new TransactionUtil.TransactionWork<Object>()
{
public Object doWork() throws Exception
{
StoreRef store = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "template_" + System.currentTimeMillis());
NodeRef root = nodeService.getRootNode(store);
BaseNodeServiceTest.buildNodeGraph(nodeService, root);
// check the default template engine exists
assertNotNull(templateService.getTemplateProcessor("freemarker"));
// create test model
Map model = new HashMap(7, 1.0f);
model.put("root", new TemplateNode(root, serviceRegistry, null));
// execute on test template
String output = templateService.processTemplate("freemarker", TEMPLATE_1, model);
// check template contains the expected output
assertTrue( (output.indexOf(root.getId()) != -1) );
System.out.print(output);
return null;
}
});
}
private static final String TEMPLATE_1 = "org/alfresco/repo/template/test_template1.ftl";
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.template;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.TemplateNode;
/**
* A special Map that executes an XPath against the parent Node as part of the get()
* Map interface implementation.
*
* @author Kevin Roast
*/
public final class XPathResultsMap extends BasePathResultsMap implements Cloneable
{
/**
* Constructor
*
* @param parent The parent TemplateNode to execute searches from
* @param services The ServiceRegistry to use
*/
public XPathResultsMap(TemplateNode parent, ServiceRegistry services)
{
super(parent, services);
}
/**
* @see java.util.Map#get(java.lang.Object)
*/
public Object get(Object key)
{
return getChildrenByXPath(key.toString(), false);
}
}

View File

@@ -0,0 +1,54 @@
<h6>Test Template 1</h6>
<#-- Test basic properties -->
${root.id}<br>
${root.name}<br>
${root.properties?size}<br>
${root.children?size}<br>
<#if root.assocs["cm:translations"]?exists>
root.assocs<br>
</#if>
${root.aspects?size}<br>
<#if root.isContainer>root.isContainer</#if><br>
<#if root.isDocument>root.isDocumentr</#if><br>
<#--${root.content}<br>-->
${root.url}<br>
${root.displayPath}<br>
${root.icon16}<br>
${root.icon32}<br>
<#if root.mimetype?exists>root.mimetype</#if><br>
<#if root.size?exists>root.size</#if><br>
<#if root.isLocked>root.isLocked</#if><br>
<#-- Test child walking and property resolving -->
<table>
<#list root.children as child>
<#-- show properties of each child -->
<#assign props = child.properties?keys>
<#list props as t>
<#-- If the property exists -->
<#if child.properties[t]?exists>
<#-- If it is a date, format it accordingly-->
<#if child.properties[t]?is_date>
<tr><td>${t} = ${child.properties[t]?date}</td></tr>
<#-- If it is a boolean, format it accordingly-->
<#elseif child.properties[t]?is_boolean>
<tr><td>${t} = ${child.properties[t]?string("yes", "no")}</td></tr>
<#-- Otherwise treat it as a string -->
<#else>
<tr><td>${t} = ${child.properties[t]}</td></tr>
</#if>
</#if>
</#list>
</#list>
</table>
<#-- Test XPath -->
<#list root.childrenByXPath["//*[@sys:store-protocol='workspace']"] as child>
${child.name}
</#list>
<h6>End Test Template 1</h6>