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