001/* 002 * Copyright (c) 2006-2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 * 012 */ 013package org.nuxeo.ecm.core.convert.service; 014 015import java.util.ArrayList; 016import java.util.HashMap; 017import java.util.List; 018import java.util.Map; 019 020import org.apache.commons.logging.Log; 021import org.apache.commons.logging.LogFactory; 022 023import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor; 024 025/** 026 * Helper class to manage chains of converters. 027 * 028 * @author tiry 029 */ 030public class MimeTypeTranslationHelper { 031 032 protected final Log log = LogFactory.getLog(MimeTypeTranslationHelper.class); 033 034 protected final Map<String, List<ConvertOption>> srcMappings = new HashMap<>(); 035 036 protected final Map<String, List<ConvertOption>> dstMappings = new HashMap<>(); 037 038 public void addConverter(ConverterDescriptor desc) { 039 List<String> sMts = desc.getSourceMimeTypes(); 040 String dMt = desc.getDestinationMimeType(); 041 042 List<ConvertOption> dco = dstMappings.get(dMt); 043 if (dco == null) { 044 dco = new ArrayList<>(); 045 } 046 047 for (String sMT : sMts) { 048 List<ConvertOption> sco = srcMappings.get(sMT); 049 050 if (sco == null) { 051 sco = new ArrayList<>(); 052 } 053 054 sco.add(new ConvertOption(desc.getConverterName(), dMt)); 055 srcMappings.put(sMT, sco); 056 057 dco.add(new ConvertOption(desc.getConverterName(), sMT)); 058 } 059 060 dstMappings.put(dMt, dco); 061 log.debug("Added converter " + desc.getSourceMimeTypes() + " to " + desc.getDestinationMimeType()); 062 } 063 064 public String getConverterName(String sourceMimeType, String destMimeType) { 065 066 List<ConvertOption> sco = srcMappings.get(sourceMimeType); 067 if (sco == null) { 068 // use wildcard 069 sco = srcMappings.get("*"); 070 if (sco == null) { 071 return null; 072 } 073 } 074 for (ConvertOption co : sco) { 075 if (co.mimeType.equals(destMimeType)) { 076 return co.getConverterName(); 077 } 078 } 079 return null; 080 } 081 082 public List<String> getConverterNames(String sourceMimeType, String destMimeType) { 083 List<ConvertOption> sco = srcMappings.get(sourceMimeType); 084 List<String> converterNames = new ArrayList<>(); 085 if (sco == null) { 086 // use wildcard 087 sco = srcMappings.get("*"); 088 if (sco == null) { 089 return converterNames; 090 } 091 } 092 for (ConvertOption co : sco) { 093 if (co.mimeType.equals(destMimeType)) { 094 converterNames.add(co.getConverterName()); 095 } 096 } 097 return converterNames; 098 } 099 100 public List<String> getDestinationMimeTypes(String sourceMimeType) { 101 List<String> dst = new ArrayList<>(); 102 103 List<ConvertOption> sco = srcMappings.get(sourceMimeType); 104 105 if (sco != null) { 106 for (ConvertOption co : sco) { 107 dst.add(co.getMimeType()); 108 } 109 } 110 return dst; 111 } 112 113 public List<String> getSourceMimeTypes(String destinationMimeType) { 114 List<String> src = new ArrayList<>(); 115 116 List<ConvertOption> dco = dstMappings.get(destinationMimeType); 117 118 if (dco != null) { 119 for (ConvertOption co : dco) { 120 src.add(co.getMimeType()); 121 } 122 } 123 return src; 124 } 125 126 public void clear() { 127 dstMappings.clear(); 128 srcMappings.clear(); 129 log.debug("clear"); 130 } 131 132}