001/* 002 * Copyright 2017 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.plugins.calendar.events; 017 018import java.io.IOException; 019import java.time.LocalDate; 020import java.time.format.DateTimeFormatter; 021import java.util.ArrayList; 022import java.util.Collection; 023import java.util.List; 024 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.components.source.impl.SitemapSource; 028import org.apache.cocoon.generation.ServiceableGenerator; 029import org.apache.cocoon.xml.AttributesImpl; 030import org.apache.cocoon.xml.XMLUtils; 031import org.apache.excalibur.source.SourceResolver; 032import org.xml.sax.ContentHandler; 033import org.xml.sax.SAXException; 034 035import org.ametys.cms.repository.Content; 036import org.ametys.core.util.IgnoreRootHandler; 037import org.ametys.runtime.i18n.I18nizableText; 038import org.ametys.web.filter.ContentFilterHelper; 039import org.ametys.web.filter.WebContentFilter; 040import org.ametys.web.repository.content.WebContent; 041import org.ametys.web.repository.page.Page; 042 043/** 044 * Abstract generator provinding methods to sax an event content 045 */ 046public abstract class AbstractEventGenerator extends ServiceableGenerator 047{ 048 /** The source resolver. */ 049 protected SourceResolver _resolver; 050 051 /** The content filter helper. */ 052 protected ContentFilterHelper _filterHelper; 053 054 @Override 055 public void service(ServiceManager serviceManager) throws ServiceException 056 { 057 super.service(serviceManager); 058 _resolver = (SourceResolver) serviceManager.lookup(SourceResolver.ROLE); 059 _filterHelper = (ContentFilterHelper) serviceManager.lookup(ContentFilterHelper.ROLE); 060 } 061 062 /** 063 * SAX a content 064 * 065 * @param handler The content handler to SAX into 066 * @param content The content. 067 * @param saxContentItSelf true to sax the content, false will only sax some meta 068 * @param filter The filter. Can be null if saxContentItSelf is false 069 * @param checkUserAccess True to check user access when saxing the content itself 070 * @throws SAXException If an error occurs while SAXing 071 * @throws IOException If an error occurs while retrieving content. 072 */ 073 public void saxContent(ContentHandler handler, Content content, boolean saxContentItSelf, WebContentFilter filter, boolean checkUserAccess) throws SAXException, IOException 074 { 075 List<String> params = new ArrayList<>(); 076 params.add(content.getTitle()); 077 078 AttributesImpl attrs = new AttributesImpl(); 079 080 LocalDate start = content.getValue(EventsFilterHelper.START_DATE_META); 081 String startStr = start != null ? start.atStartOfDay().format(DateTimeFormatter.ISO_LOCAL_DATE) : null; 082 if (startStr != null) 083 { 084 params.add(startStr); 085 attrs.addCDATAAttribute("start", startStr); 086 } 087 088 LocalDate end = content.getValue(EventsFilterHelper.END_DATE_META); 089 String endStr = end != null ? end.atStartOfDay().format(DateTimeFormatter.ISO_LOCAL_DATE) : null; 090 if (endStr != null) 091 { 092 params.add(endStr); 093 attrs.addCDATAAttribute("end", endStr); 094 } 095 096 XMLUtils.startElement(handler, "event", attrs); 097 098 String key = end == null ? "CALENDAR_SERVICE_AGENDA_EVENT_TITLE_SINGLE_DAY" : "CALENDAR_SERVICE_AGENDA_FROM_TO"; 099 I18nizableText description = new I18nizableText(null, key, params); 100 101 description.toSAX(handler, "description"); 102 103 if (saxContentItSelf) 104 { 105 // Link view. 106 XMLUtils.startElement(handler, "view"); 107 _filterHelper.saxContent(handler, content, filter.getView(), checkUserAccess); 108 XMLUtils.endElement(handler, "view"); 109 } 110 111 // XML full view. 112 saxXMLContent(handler, content, "main"); 113 114 if (content instanceof WebContent) 115 { 116 WebContent webContent = (WebContent) content; 117 118 XMLUtils.startElement(handler, "pages"); 119 Collection<Page> pages = webContent.getReferencingPages(); 120 for (Page page : pages) 121 { 122 AttributesImpl atts = new AttributesImpl(); 123 atts.addCDATAAttribute("id", page.getId()); 124 atts.addCDATAAttribute("name", page.getName()); 125 atts.addCDATAAttribute("lang", page.getSitemapName()); 126 atts.addCDATAAttribute("site", page.getSiteName()); 127 atts.addCDATAAttribute("path", page.getPathInSitemap()); 128 atts.addCDATAAttribute("title", page.getTitle()); 129 XMLUtils.createElement(handler, "page", atts); 130 } 131 XMLUtils.endElement(handler, "pages"); 132 } 133 XMLUtils.endElement(handler, "event"); 134 } 135 136 /** 137 * SAX a content in XML mode. 138 * 139 * @param handler The content handler to SAX into 140 * @param content The content to SAX 141 * @param metadataSetName The metadata set to use 142 * @throws SAXException If an error occurs while SAXing 143 * @throws IOException If an error occurs while retrieving content. 144 */ 145 public void saxXMLContent(ContentHandler handler, Content content, String metadataSetName) throws SAXException, IOException 146 { 147 String uri = "cocoon://_content.xml?contentId=" + content.getId() + "&metadataSetName=" + metadataSetName; 148 SitemapSource src = null; 149 150 try 151 { 152 src = (SitemapSource) _resolver.resolveURI(uri); 153 src.toSAX(new IgnoreRootHandler(handler)); 154 } 155 finally 156 { 157 _resolver.release(src); 158 } 159 } 160}