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.apache.commons.lang3.StringUtils; 033import org.xml.sax.ContentHandler; 034import org.xml.sax.SAXException; 035 036import org.ametys.cms.repository.Content; 037import org.ametys.cms.tag.Tag; 038import org.ametys.cms.tag.TagProviderExtensionPoint; 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 private TagProviderExtensionPoint _tagProviderEP; 050 051 private Context _context; 052 053 @Override 054 public void service(ServiceManager manager) throws ServiceException 055 { 056 super.service(manager); 057 _tagProviderEP = (TagProviderExtensionPoint) manager.lookup(TagProviderExtensionPoint.ROLE); 058 } 059 060 public void contextualize(Context context) throws ContextException 061 { 062 _context = context; 063 } 064 065 @Override 066 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 067 { 068 super.saxBody(content, contentHandler, locale, view, tagName, saxWorkflowStep, saxWorkflowInfo, saxLanguageInfo, attributesTagName, isEdition, renderDisableValues); 069 070 if (content instanceof WebContent) 071 { 072 saxSiteName((WebContent) content, contentHandler); 073 } 074 075 saxTags(content, contentHandler); 076 } 077 078 /** 079 * Generates SAX events for the site name. 080 * @param content the {@link WebContent}. 081 * @param contentHandler the ContentHandler receving SAX events. 082 * @throws SAXException if an error occurs during the SAX events generation. 083 */ 084 protected void saxSiteName(WebContent content, ContentHandler contentHandler) throws SAXException 085 { 086 AttributesImpl attrs = new AttributesImpl(); 087 attrs.addCDATAAttribute("name", content.getSiteName()); 088 XMLUtils.createElement(contentHandler, "site", attrs); 089 } 090 091 /** 092 * Generates SAX events for tags. 093 * @param content the content 094 * @param contentHandler the ContentHandler receving SAX events. 095 * @throws SAXException if an error occurs during the SAX events generation. 096 */ 097 protected void saxTags(Content content, ContentHandler contentHandler) throws SAXException 098 { 099 XMLUtils.startElement(contentHandler, "tags"); 100 101 Request request = ContextHelper.getRequest(_context); 102 String siteName = WebHelper.getSiteName(request, content); 103 104 Set<String> tags = content.getTags(); 105 for (String tagName : tags) 106 { 107 Map<String, Object> contextParameters = new HashMap<>(); 108 if (StringUtils.isNotEmpty(siteName)) 109 { 110 contextParameters.put("siteName", siteName); 111 } 112 113 Tag tag = _tagProviderEP.getTag(tagName, contextParameters); 114 if (tag != null) 115 { 116 AttributesImpl attrs = new AttributesImpl(); 117 if (tag.getParentName() != null) 118 { 119 attrs.addCDATAAttribute("parent", tag.getParentName()); 120 } 121 122 XMLUtils.startElement(contentHandler, tagName, attrs); 123 tag.getTitle().toSAX(contentHandler); 124 XMLUtils.endElement(contentHandler, tagName); 125 } 126 } 127 128 XMLUtils.endElement(contentHandler, "tags"); 129 } 130 131 @Override 132 protected Map<String, Object> getContextualParameters(Content content) 133 { 134 Map<String, Object> ctxParams = new HashMap<>(super.getContextualParameters(content)); 135 136 Request request = ContextHelper.getRequest(_context); 137 138 ctxParams.put("sitemapLanguage", request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME)); 139 ctxParams.put("siteName", WebHelper.getSiteName(request)); 140 141 return ctxParams; 142 } 143}