001/* 002 * Copyright 2010 Anyware Services 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 */ 016package org.ametys.web.source; 017 018import java.io.IOException; 019import java.net.MalformedURLException; 020import java.util.Map; 021import java.util.regex.Matcher; 022 023import org.apache.avalon.framework.context.Context; 024import org.apache.avalon.framework.context.ContextException; 025import org.apache.avalon.framework.context.Contextualizable; 026import org.apache.cocoon.components.ContextHelper; 027import org.apache.cocoon.environment.Request; 028import org.apache.commons.lang.StringUtils; 029import org.apache.excalibur.source.Source; 030import org.apache.excalibur.source.SourceNotFoundException; 031 032import org.ametys.web.skin.Skin; 033import org.ametys.web.skin.SkinTemplate; 034 035/** 036 * Resolve the template:// protocol seeking file in skins/{siteConf:skin}/templates/{currentTemplate}/ 037 * or template:forcedvalue#defaultvalue:// 038 * @see SkinSourceFactory 039 */ 040public class TemplateSourceFactory extends SkinSourceFactory implements Contextualizable 041{ 042 /** Avalon context */ 043 protected Context _context; 044 045 public void contextualize(Context context) throws ContextException 046 { 047 _context = context; 048 } 049 050 @Override 051 public Source getSource(String location, Map parameters) throws IOException, SourceNotFoundException 052 { 053 // lazy initialization to prevent chicken/egg scenario on startup 054 initializeComponents(); 055 056 Matcher m = __SOURCE_PATTERN.matcher(location); 057 if (!m.matches()) 058 { 059 throw new MalformedURLException("URI must be like protocol://path/to/resource. Location was '" + location + "'"); 060 } 061 062 String templateName = getTemplate(m.group(2), m.group(4)).getId(); 063 064 String templateLocation = m.group(5); 065 066 return super.getSource("skin://templates/" + templateName + "/" + templateLocation, parameters); 067 } 068 069 /** 070 * Return the current template 071 * @param name the skin name. Can be null or empty 072 * @param defaultValue switch to this value if skin cannot be determined, or if the determined skin does not exist. Can be null 073 * @return the template. Cannot be null. 074 * @throws SourceNotFoundException If the template cannot be determiner or the template do not exist 075 */ 076 protected SkinTemplate getTemplate(String name, String defaultValue) throws SourceNotFoundException 077 { 078 Skin skin = getSkin(null, null); 079 080 String templateName = name; 081 082 if (StringUtils.isEmpty(templateName)) 083 { 084 Request request = ContextHelper.getRequest(_context); 085 templateName = (String) request.getAttribute("template"); 086 } 087 088 if (StringUtils.isEmpty(templateName)) 089 { 090 templateName = defaultValue; 091 } 092 093 if (StringUtils.isEmpty(templateName)) 094 { 095 String message = "PageMatcher has to be used in current request to use this protocol."; 096 getLogger().info(message); 097 throw new SourceNotFoundException(message); 098 } 099 100 SkinTemplate template = skin.getTemplate(templateName); 101 if (template == null) 102 { 103 template = skin.getTemplate(defaultValue); 104 } 105 if (template == null) 106 { 107 String message = "The template '" + templateName + "' in the skin '" + skin.getId() + "' site does not exist."; 108 getLogger().info(message); 109 throw new SourceNotFoundException(message); 110 } 111 112 return template; 113 } 114}