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.odf.content; 017 018import java.io.IOException; 019import java.util.List; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.ProcessingException; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Request; 026import org.apache.cocoon.generation.ServiceableGenerator; 027import org.apache.cocoon.xml.AttributesImpl; 028import org.apache.cocoon.xml.XMLUtils; 029import org.apache.commons.lang3.StringUtils; 030import org.xml.sax.SAXException; 031 032import org.ametys.cms.repository.Content; 033import org.ametys.odf.ODFHelper; 034import org.ametys.odf.ProgramItem; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036 037/** 038 * Generates additional information about a ODF content 039 * 040 */ 041public class AdditionalOdfContentPropertiesGenerator extends ServiceableGenerator 042{ 043 /** The AmetysObject resolver. */ 044 protected AmetysObjectResolver _resolver; 045 /** The ODF helper */ 046 protected ODFHelper _odfHelper; 047 048 @Override 049 public void service(ServiceManager serviceManager) throws ServiceException 050 { 051 super.service(serviceManager); 052 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 053 _odfHelper = (ODFHelper) serviceManager.lookup(ODFHelper.ROLE); 054 } 055 056 @Override 057 public void generate() throws IOException, SAXException, ProcessingException 058 { 059 Request request = ObjectModelHelper.getRequest(objectModel); 060 061 String contentId = parameters.getParameter("contentId", request.getParameter("contentId")); 062 063 Content content = null; 064 if (StringUtils.isNotEmpty(contentId)) 065 { 066 content = _resolver.resolveById(contentId); 067 } 068 else 069 { 070 content = (Content) request.getAttribute(Content.class.getName()); 071 } 072 073 contentHandler.startDocument(); 074 XMLUtils.startElement(contentHandler, "odf"); 075 076 if (content instanceof ProgramItem) 077 { 078 XMLUtils.startElement(contentHandler, "programItem"); 079 saxProgramItemProperties((ProgramItem) content); 080 XMLUtils.endElement(contentHandler, "programItem"); 081 } 082 083 XMLUtils.endElement(contentHandler, "odf"); 084 contentHandler.startDocument(); 085 contentHandler.endDocument(); 086 } 087 088 /** 089 * SAX specifics properties for a program item 090 * @param programItem the program item 091 * @throws SAXException if an error occurs while saxing 092 */ 093 protected void saxProgramItemProperties(ProgramItem programItem) throws SAXException 094 { 095 _saxProgramItemAncestorPaths(programItem); 096 _saxChildProgramItems(programItem); 097 } 098 099 /** 100 * SAX all paths of program item 101 * @param programItem The program item 102 * @throws SAXException if an error occurs while saxing 103 */ 104 protected void _saxProgramItemAncestorPaths(ProgramItem programItem) throws SAXException 105 { 106 if (_odfHelper.hasParentProgramItems(programItem)) 107 { 108 List<List<ProgramItem>> ancestorPaths = _odfHelper.getPathOfAncestors(programItem); 109 110 if (!ancestorPaths.isEmpty()) 111 { 112 XMLUtils.startElement(contentHandler, "ancestors"); 113 for (List<ProgramItem> ancestorPath : ancestorPaths) 114 { 115 XMLUtils.startElement(contentHandler, "path"); 116 for (ProgramItem ancestor : ancestorPath) 117 { 118 _saxProgramItem(ancestor); 119 } 120 XMLUtils.endElement(contentHandler, "path"); 121 } 122 123 XMLUtils.endElement(contentHandler, "ancestors"); 124 } 125 } 126 } 127 128 /** 129 * SAX the child program items 130 * @param programItem the program item 131 * @throws SAXException if an error occurs while saxing 132 */ 133 protected void _saxChildProgramItems(ProgramItem programItem) throws SAXException 134 { 135 List<ProgramItem> children = _odfHelper.getChildProgramItems(programItem); 136 137 if (!children.isEmpty()) 138 { 139 XMLUtils.startElement(contentHandler, "children"); 140 for (ProgramItem child : children) 141 { 142 _saxProgramItem(child); 143 } 144 XMLUtils.endElement(contentHandler, "children"); 145 } 146 } 147 148 /** 149 * Sax a program item 150 * @param item the program item 151 * @throws SAXException if an error occurs while saxing 152 */ 153 protected void _saxProgramItem(ProgramItem item) throws SAXException 154 { 155 Content content = (Content) item; 156 AttributesImpl attrs = new AttributesImpl(); 157 attrs.addCDATAAttribute("id", content.getId()); 158 attrs.addCDATAAttribute("code", item.getCode()); 159 XMLUtils.createElement(contentHandler, "item", attrs, content.getTitle()); 160 } 161}