1/* 2 * FileRenderSpecification.java 3 * Copyright (C) 2005 Amin Ahmad. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 * 19 * Amin Ahmad can be contacted at amin.ahmad@gmail.com or on the web at 20 * www.ahmadsoft.org. 21 */ 22package org.ahmadsoft.foprocessor.core; 23 24import java.io.ByteArrayInputStream; 25import java.io.ByteArrayOutputStream; 26import java.io.Externalizable; 27import java.io.IOException; 28import java.io.ObjectInput; 29import java.io.ObjectInputStream; 30import java.io.ObjectOutput; 31import java.io.ObjectOutputStream; 32import java.io.Serializable; 33import java.util.HashMap; 34import java.util.Map; 35 36import org.eclipse.core.resources.IFile; 37import org.eclipse.core.runtime.IPath; 38import org.eclipse.core.runtime.Path; 39 40/** 41 * Encapsulates a specification for rendering a file. 42 */ 43public class FileRenderSpecification implements Externalizable { 44 private IFile inputFile; 45 private IPath outputPath; 46 private String mimeType; 47 private Map<String, Serializable> properties 48 = new HashMap<String, Serializable>(); 49 50 public IFile getInputFile() { 51 return inputFile; 52 } 53 public void setInputFile(IFile inputFile) { 54 this.inputFile = inputFile; 55 } 56 public String getMimeType() { 57 return mimeType; 58 } 59 public void setMimeType(String mimeType) { 60 this.mimeType = mimeType; 61 } 62 public IPath getOutputPath() { 63 return outputPath; 64 } 65 public void setOutputFile(IPath outputPath) { 66 this.outputPath = outputPath; 67 } 68 69 public static IPath computeOutputFile(IFile inputFile, String extension) { 70 IPath fullPath = inputFile.getLocation(); 71 IPath path = fullPath.removeFileExtension().addFileExtension(extension); 72 return path; 73 } 74 public void writeExternal(ObjectOutput out) throws IOException { 75 76 // do not externalize the input file. Rather, the render spec is 77 // expected to be stored with the input file. 78 79 out.writeUTF(outputPath.toPortableString()); 80 out.writeUTF(mimeType); 81 out.writeObject(properties); 82 } 83 @SuppressWarnings("unchecked") 84 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 85 outputPath = Path.fromPortableString(in.readUTF()); 86 mimeType = in.readUTF(); 87 properties = (Map<String, Serializable>) in.readObject(); 88 } 89 90 /** 91 * Serializes a file render spec into a string. Returns 92 * <code>null</code> on unexpected failure. 93 * 94 * @param spec 95 * @return 96 */ 97 public static String toString(FileRenderSpecification spec) { 98 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 99 try { 100 ObjectOutputStream oos = new ObjectOutputStream(bos); 101 102 oos.writeObject(spec); 103 oos.close(); 104 return Base64.byteArrayToBase64(bos.toByteArray()); 105 } catch (IOException e) { 106 return null; 107 } 108 } 109 public static FileRenderSpecification fromString(IFile inputFile, String spec) { 110 if (spec == null) { 111 return null; 112 } 113 114 byte[] specBytes = Base64.base64ToByteArray(spec); 115 ByteArrayInputStream bis = new ByteArrayInputStream(specBytes); 116 try { 117 ObjectInputStream ois = new ObjectInputStream(bis); 118 119 FileRenderSpecification result = (FileRenderSpecification) ois.readObject(); 120 result.setInputFile(inputFile); 121 return result; 122 } catch (Exception e) { // IO, Class not found, etc.. 123 return null; 124 } 125 } 126} 127