001/* 002 * Copyright 2013 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.explorer; 017 018import java.io.IOException; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.environment.ObjectModelHelper; 024import org.apache.cocoon.environment.Request; 025import org.apache.cocoon.xml.AttributesImpl; 026import org.apache.commons.lang.StringUtils; 027import org.xml.sax.SAXException; 028 029import org.ametys.core.util.DateUtils; 030import org.ametys.core.util.I18nUtils; 031import org.ametys.plugins.explorer.ExplorerNode; 032import org.ametys.plugins.explorer.dublincore.DublinCoreMetadataProvider; 033import org.ametys.plugins.explorer.resources.Resource; 034import org.ametys.plugins.repository.jcr.DublinCoreHelper; 035import org.ametys.runtime.i18n.I18nizableText; 036import org.ametys.web.WebConstants; 037import org.ametys.web.repository.page.Page; 038 039/** 040 * Generates a subtree of {@link ExplorerNode} with the DublinCore metadata. 041 */ 042public class ResourcesExplorerGenerator extends org.ametys.plugins.explorer.resources.generators.ResourcesExplorerGenerator 043{ 044 /** The DublinCore metadata provider */ 045 protected DublinCoreMetadataProvider _dcProvider; 046 /** The I18n utils */ 047 protected I18nUtils _i18nUtils; 048 049 @Override 050 public void service(ServiceManager sManager) throws ServiceException 051 { 052 super.service(sManager); 053 _dcProvider = (DublinCoreMetadataProvider) sManager.lookup(DublinCoreMetadataProvider.ROLE); 054 _i18nUtils = (I18nUtils) sManager.lookup(I18nUtils.ROLE); 055 } 056 057 @Override 058 public void generate() throws IOException, SAXException, ProcessingException 059 { 060 Request request = ObjectModelHelper.getRequest(objectModel); 061 062 String id = parameters.getParameter("node", request.getParameter("node")); 063 int depth = parameters.getParameterAsInteger("depth", 0); 064 if (depth == -1) 065 { 066 // -1 means no limit 067 depth = Integer.MAX_VALUE; 068 } 069 070 String[] allowedExtensions = request.getParameterValues("allowedExtensions"); 071 072 ExplorerNode node = _resolver.resolveById(id); 073 074 contentHandler.startDocument(); 075 076 long t0 = System.currentTimeMillis(); 077 078 saxExplorerNode(node, depth + 1, allowedExtensions); 079 080 if (getLogger().isDebugEnabled()) 081 { 082 getLogger().debug("SAXed collection " + node.getExplorerPath() + " in " + (System.currentTimeMillis() - t0) + " ms"); 083 } 084 085 contentHandler.endDocument(); 086 } 087 088 @Override 089 protected void getAdditionalAttributes(AttributesImpl attrs, Resource resource) 090 { 091 Request request = ObjectModelHelper.getRequest(objectModel); 092 Page page = (Page) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE); 093 String lang = null; 094 if (page != null) 095 { 096 lang = page.getSitemapName(); 097 } 098 099 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_TITLE, resource.getDCTitle(), lang); 100 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_CONTRIBUTOR, resource.getDCContributor(), lang); 101 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_CREATOR, resource.getDCCreator(), lang); 102 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_DESCRIPTION, resource.getDCDescription(), lang); 103 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_SUBJECT, StringUtils.join(resource.getDCSubject(), ", "), lang); 104 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_PUBLISHER, resource.getDCPublisher(), lang); 105 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_LANGUAGE, resource.getDCLanguage(), lang); 106 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_COVERAGE, resource.getDCCoverage(), lang); 107 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_RELATION, resource.getDCRelation(), lang); 108 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_RIGHTS, resource.getDCRights(), lang); 109 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_SOURCE, resource.getDCSource(), lang); 110 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_FORMAT, resource.getDCFormat(), lang); 111 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_DATE, DateUtils.dateToString(resource.getDCDate()), lang); 112 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_IDENTIFIER, resource.getDCIdentifier(), lang); 113 _saxNonEmptyAttributes (attrs, DublinCoreHelper.METADATA_DC_TYPE, resource.getDCType(), lang); 114 } 115 116 /** 117 * SAX attribute if value is not empty 118 * @param attrs The attributes to sax into 119 * @param metadataName The name of attribute 120 * @param value The value. 121 * @param language The sitemap language 122 */ 123 protected void _saxNonEmptyAttributes (AttributesImpl attrs, String metadataName, String value, String language) 124 { 125 if (StringUtils.isNotEmpty(value)) 126 { 127 if (_dcProvider.isEnumerated(metadataName)) 128 { 129 I18nizableText entry = _dcProvider.getEntry(metadataName, value); 130 attrs.addCDATAAttribute(metadataName, entry != null ? _i18nUtils.translate(entry, language) : value); 131 } 132 else 133 { 134 attrs.addCDATAAttribute(metadataName, value); 135 } 136 } 137 } 138}