1 package org.alfresco.repo.content.transform; 2 3 import java.io.File ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Map ; 7 8 import org.alfresco.error.AlfrescoRuntimeException; 9 import org.alfresco.repo.content.filestore.FileContentWriter; 10 import org.alfresco.service.cmr.repository.ContentReader; 11 import org.alfresco.service.cmr.repository.ContentWriter; 12 import org.alfresco.util.TempFileProvider; 13 import org.springframework.beans.factory.InitializingBean; 14 15 21 public class ComplexContentTransformer extends AbstractContentTransformer implements InitializingBean 22 { 23 private List <ContentTransformer> transformers; 24 private List <String > intermediateMimetypes; 25 26 public ComplexContentTransformer() 27 { 28 } 29 30 37 public void setTransformers(List <ContentTransformer> transformers) 38 { 39 this.transformers = transformers; 40 } 41 42 53 public void setIntermediateMimetypes(List <String > intermediateMimetypes) 54 { 55 this.intermediateMimetypes = intermediateMimetypes; 56 } 57 58 61 public void afterPropertiesSet() throws Exception 62 { 63 if (transformers == null || transformers.size() == 0) 64 { 65 throw new AlfrescoRuntimeException("At least one inner transformer must be supplied: " + this); 66 } 67 if (intermediateMimetypes == null || intermediateMimetypes.size() != transformers.size() - 1) 68 { 69 throw new AlfrescoRuntimeException( 70 "There must be n-1 intermediate mimetypes, where n is the number of transformers"); 71 } 72 if (getMimetypeService() == null) 73 { 74 throw new AlfrescoRuntimeException("'mimetypeService' is a required property"); 75 } 76 } 77 78 81 public double getReliability(String sourceMimetype, String targetMimetype) 82 { 83 double reliability = 1.0; 84 String currentSourceMimetype = sourceMimetype; 85 86 Iterator <ContentTransformer> transformerIterator = transformers.iterator(); 87 Iterator <String > intermediateMimetypeIterator = intermediateMimetypes.iterator(); 88 while (transformerIterator.hasNext()) 89 { 90 ContentTransformer transformer = transformerIterator.next(); 91 String currentTargetMimetype = null; 93 if (!transformerIterator.hasNext()) 94 { 95 currentTargetMimetype = targetMimetype; 96 } 97 else 98 { 99 currentTargetMimetype = intermediateMimetypeIterator.next(); 101 } 102 reliability *= transformer.getReliability(currentSourceMimetype, currentTargetMimetype); 104 currentSourceMimetype = currentTargetMimetype; 106 } 107 return reliability; 109 } 110 111 @Override 112 public void transformInternal( 113 ContentReader reader, 114 ContentWriter writer, 115 Map <String , Object > options) throws Exception 116 { 117 ContentReader currentReader = reader; 118 119 Iterator <ContentTransformer> transformerIterator = transformers.iterator(); 120 Iterator <String > intermediateMimetypeIterator = intermediateMimetypes.iterator(); 121 while (transformerIterator.hasNext()) 122 { 123 ContentTransformer transformer = transformerIterator.next(); 124 ContentWriter currentWriter = null; 126 if (!transformerIterator.hasNext()) 127 { 128 currentWriter = writer; 129 } 130 else 131 { 132 String nextMimetype = intermediateMimetypeIterator.next(); 133 String sourceExt = getMimetypeService().getExtension(currentReader.getMimetype()); 135 String targetExt = getMimetypeService().getExtension(nextMimetype); 136 File tempFile = TempFileProvider.createTempFile( 137 "ComplextTransformer_intermediate_" + sourceExt + "_", 138 "." + targetExt); 139 currentWriter = new FileContentWriter(tempFile); 140 currentWriter.setMimetype(nextMimetype); 141 } 142 transformer.transform(currentReader, currentWriter, options); 144 currentReader = currentWriter.getReader(); 146 } 147 } 149 } 150 | Popular Tags |