001/* 002 * Copyright 2019 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.content; 017 018import java.util.HashMap; 019import java.util.Locale; 020import java.util.Map; 021import java.util.Set; 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.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.cocoon.components.ContextHelper; 029import org.apache.cocoon.environment.Request; 030import org.apache.cocoon.xml.AttributesImpl; 031import org.apache.cocoon.xml.XMLUtils; 032import org.xml.sax.ContentHandler; 033import org.xml.sax.SAXException; 034 035import org.ametys.cms.repository.Content; 036import org.ametys.cms.tag.Tag; 037import org.ametys.cms.tag.TagProviderExtensionPoint; 038import org.ametys.plugins.repository.tag.TagAwareAmetysObject; 039import org.ametys.runtime.model.View; 040import org.ametys.web.WebConstants; 041import org.ametys.web.WebHelper; 042import org.ametys.web.repository.content.WebContent; 043 044/** 045 * Generates SAX events for Content, including tags for {@link WebContent}. 046 */ 047public class ContentSaxer extends org.ametys.cms.content.ContentSaxer implements Contextualizable 048{ 049 /** Avalon role. */ 050 public static final String WEB_CONTENT_SAXER_ROLE = ContentSaxer.class.getName(); 051 052 private TagProviderExtensionPoint _tagProviderEP; 053 054 private Context _context; 055 056 @Override 057 public void service(ServiceManager manager) throws ServiceException 058 { 059 super.service(manager); 060 _tagProviderEP = (TagProviderExtensionPoint) manager.lookup(TagProviderExtensionPoint.ROLE); 061 } 062 063 public void contextualize(Context context) throws ContextException 064 { 065 _context = context; 066 } 067 068 @Override 069 protected void saxBody(Content content, ContentHandler contentHandler, Locale locale, View view, String tagName, boolean saxWorkflowStep, boolean saxWorkflowInfo, boolean saxLanguageInfo, String attributesTagName, boolean isEdition, boolean renderDisableValues) throws SAXException 070 { 071 super.saxBody(content, contentHandler, locale, view, tagName, saxWorkflowStep, saxWorkflowInfo, saxLanguageInfo, attributesTagName, isEdition, renderDisableValues); 072 073 if (content instanceof WebContent) 074 { 075 saxSiteName((WebContent) content, contentHandler); 076 saxTags((WebContent) content, contentHandler); 077 } 078 } 079 080 /** 081 * Generates SAX events for the site name. 082 * @param content the {@link WebContent}. 083 * @param contentHandler the ContentHandler receving SAX events. 084 * @throws SAXException if an error occurs during the SAX events generation. 085 */ 086 protected void saxSiteName(WebContent content, ContentHandler contentHandler) throws SAXException 087 { 088 AttributesImpl attrs = new AttributesImpl(); 089 attrs.addCDATAAttribute("name", content.getSiteName()); 090 XMLUtils.createElement(contentHandler, "site", attrs); 091 } 092 093 /** 094 * Generates SAX events for tags. 095 * @param content the {@link WebContent}. 096 * @param contentHandler the ContentHandler receving SAX events. 097 * @throws SAXException if an error occurs during the SAX events generation. 098 */ 099 protected void saxTags(WebContent content, ContentHandler contentHandler) throws SAXException 100 { 101 XMLUtils.startElement(contentHandler, "tags"); 102 103 String siteName = content.getSiteName(); 104 105 Set<String> tags = ((TagAwareAmetysObject) content).getTags(); 106 for (String tagName : tags) 107 { 108 Map<String, Object> contextParameters = new HashMap<>(); 109 contextParameters.put("siteName", siteName); 110 Tag tag = _tagProviderEP.getTag(tagName, contextParameters); 111 112 if (tag != null) 113 { 114 AttributesImpl attrs = new AttributesImpl(); 115 if (tag.getParentName() != null) 116 { 117 attrs.addCDATAAttribute("parent", tag.getParentName()); 118 } 119 120 XMLUtils.startElement(contentHandler, tagName, attrs); 121 tag.getTitle().toSAX(contentHandler); 122 XMLUtils.endElement(contentHandler, tagName); 123 } 124 } 125 126 XMLUtils.endElement(contentHandler, "tags"); 127 } 128 129 @SuppressWarnings("cast") 130 @Override 131 protected Map<String, Object> getContextualParameters(Content content) 132 { 133 Map<String, Object> ctxParams = new HashMap<>(super.getContextualParameters(content)); 134 135 Request request = ContextHelper.getRequest(_context); 136 137 ctxParams.put("sitemapLanguage", (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME)); 138 ctxParams.put("siteName", WebHelper.getSiteName(request)); 139 140 return ctxParams; 141 } 142}