001/* 002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 */ 020package org.nuxeo.ecm.core.convert.service; 021 022import java.util.ArrayList; 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029 030import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 031 032/** 033 * Helper class to manage chains of converters. 034 * 035 * @author tiry 036 */ 037public class MimeTypeTranslationHelper { 038 039 protected final Log log = LogFactory.getLog(MimeTypeTranslationHelper.class); 040 041 protected final Map<String, List<ConvertOption>> srcMappings = new HashMap<>(); 042 043 protected final Map<String, List<ConvertOption>> dstMappings = new HashMap<>(); 044 045 public void addConverter(ConverterDescriptor desc) { 046 List<String> sMts = desc.getSourceMimeTypes(); 047 String dMt = desc.getDestinationMimeType(); 048 049 List<ConvertOption> dco = dstMappings.get(dMt); 050 if (dco == null) { 051 dco = new ArrayList<>(); 052 } 053 054 for (String sMT : sMts) { 055 List<ConvertOption> sco = srcMappings.get(sMT); 056 057 if (sco == null) { 058 sco = new ArrayList<>(); 059 } 060 061 sco.add(new ConvertOption(desc.getConverterName(), dMt)); 062 srcMappings.put(sMT, sco); 063 064 dco.add(new ConvertOption(desc.getConverterName(), sMT)); 065 } 066 067 dstMappings.put(dMt, dco); 068 log.debug("Added converter " + desc.getSourceMimeTypes() + " to " + desc.getDestinationMimeType()); 069 } 070 071 public String getConverterName(String sourceMimeType, String destMimeType) { 072 073 List<ConvertOption> sco = srcMappings.get(sourceMimeType); 074 if (sco == null) { 075 // use wildcard 076 sco = srcMappings.get("*"); 077 if (sco == null) { 078 return null; 079 } 080 } 081 for (ConvertOption co : sco) { 082 if (co.mimeType.equals(destMimeType)) { 083 return co.getConverterName(); 084 } 085 } 086 return null; 087 } 088 089 public List<String> getConverterNames(String sourceMimeType, String destMimeType) { 090 List<ConvertOption> sco = srcMappings.get(sourceMimeType); 091 List<String> converterNames = new ArrayList<>(); 092 if (sco == null) { 093 // use wildcard 094 sco = srcMappings.get("*"); 095 if (sco == null) { 096 return converterNames; 097 } 098 } 099 for (ConvertOption co : sco) { 100 if (co.mimeType.equals(destMimeType)) { 101 converterNames.add(co.getConverterName()); 102 } 103 } 104 return converterNames; 105 } 106 107 public List<String> getDestinationMimeTypes(String sourceMimeType) { 108 List<String> dst = new ArrayList<>(); 109 110 List<ConvertOption> sco = srcMappings.get(sourceMimeType); 111 112 if (sco != null) { 113 for (ConvertOption co : sco) { 114 dst.add(co.getMimeType()); 115 } 116 } 117 return dst; 118 } 119 120 public List<String> getSourceMimeTypes(String destinationMimeType) { 121 List<String> src = new ArrayList<>(); 122 123 List<ConvertOption> dco = dstMappings.get(destinationMimeType); 124 125 if (dco != null) { 126 for (ConvertOption co : dco) { 127 src.add(co.getMimeType()); 128 } 129 } 130 return src; 131 } 132 133 public void clear() { 134 dstMappings.clear(); 135 srcMappings.clear(); 136 log.debug("clear"); 137 } 138 139}