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