1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package org.openoffice.xmerge; 25 26 import org.openoffice.xmerge.util.registry.ConverterInfo; 27 import org.openoffice.xmerge.util.registry.ConverterInfoMgr; 28 29 /** 30 * Factory that provides access to <code>Convert</code> objects, which 31 * are used to do a conversion. The <code>ConvertFactory</code> does 32 * this via the <code>ConvertInfoMgr</code> which maintains a list of 33 * which <code>Convert</code> objects are available and their 34 * capabilities. 35 * 36 * @see Convert 37 * @see org.openoffice.xmerge.util.registry.ConverterInfoMgr 38 * 39 * @author Martin Maher 40 */ 41 public class ConverterFactory { 42 43 /** 44 * Confirms whether or not a particular conversion can be done 45 * based on the Mime types of the files to be converted to and 46 * from. 47 * 48 * @param mimeTypeIn The mime input type. 49 * @param mimeTypeOut The mime output type. 50 * 51 * @return true if the conversion is possible, false otherwise. 52 */ canConvert(String mimeTypeIn, String mimeTypeOut)53 public boolean canConvert(String mimeTypeIn, String mimeTypeOut) { 54 55 ConverterInfo foundInfo = null; 56 57 // findConverterInfo expects the second paramenter to be the 58 // destination MimeType 59 if (foundInfo.isValidOfficeType(mimeTypeOut)) 60 foundInfo = ConverterInfoMgr.findConverterInfo(mimeTypeIn, mimeTypeOut); 61 else 62 foundInfo = ConverterInfoMgr.findConverterInfo(mimeTypeOut, mimeTypeIn); 63 64 if (foundInfo != null) 65 return true; 66 else 67 return false; 68 } 69 70 71 /** 72 * Returns the <code>Convert</code> object that converts 73 * the specified device/office mime type conversion. If there 74 * are multiple <code>Converter</code> objects registered 75 * that support this conversion, only the first is returned. 76 * 77 * @param mimeTypeIn The mime input type. 78 * @param mimeTypeOut The mime output type. 79 * 80 * @return The first <code>Convert</code> object that supports 81 * the specified conversion. 82 */ getConverter(String mimeTypeIn, String mimeTypeOut)83 public Convert getConverter(String mimeTypeIn, String mimeTypeOut) { 84 85 ConverterInfo foundInfo = null; 86 boolean toOffice; 87 88 toOffice = foundInfo.isValidOfficeType(mimeTypeOut); 89 90 // findConverterInfo expects the second paramenter to be the 91 // destination MimeType 92 if (toOffice) 93 foundInfo = ConverterInfoMgr.findConverterInfo(mimeTypeIn, mimeTypeOut); 94 else 95 foundInfo = ConverterInfoMgr.findConverterInfo(mimeTypeOut, mimeTypeIn); 96 97 if (foundInfo != null) 98 return getConverter(foundInfo, toOffice); 99 else 100 return null; 101 } 102 103 104 /** 105 * Returns the <code>Convert</code> object that is described 106 * by the <code>ConverterInfo</code> parameter. 107 * 108 * @param ci The <code>ConverterInfo</code> describing the converter. 109 * 110 * @param toOffice true to convert to office, false to convert to device. 111 * 112 * @return The <code>Convert</code> object 113 */ getConverter(ConverterInfo ci, boolean toOffice)114 public Convert getConverter(ConverterInfo ci, boolean toOffice) { 115 116 Convert myConvert = new Convert(ci, toOffice); 117 return myConvert; 118 } 119 } 120 121