KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > transform > ComplexContentTransformer


1 package org.alfresco.repo.content.transform;
2
3 import java.io.File JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
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 /**
16  * Transformer that passes a document through several nested transformations
17  * in order to accomplish its goal.
18  *
19  * @author Derek Hulley
20  */

21 public class ComplexContentTransformer extends AbstractContentTransformer implements InitializingBean
22 {
23     private List JavaDoc<ContentTransformer> transformers;
24     private List JavaDoc<String JavaDoc> intermediateMimetypes;
25     
26     public ComplexContentTransformer()
27     {
28     }
29
30     /**
31      * The list of transformers to use.
32      * <p>
33      * If a single transformer is supplied, then it will still be used.
34      *
35      * @param transformers list of <b>at least one</b> transformer
36      */

37     public void setTransformers(List JavaDoc<ContentTransformer> transformers)
38     {
39         this.transformers = transformers;
40     }
41
42     /**
43      * Set the intermediate mimetypes that the transformer must take the content
44      * through. If the transformation <b>A..B..C</b> is performed in order to
45      * simulate <b>A..C</b>, then <b>B</b> is the intermediate mimetype. There
46      * must always be <b>n-1</b> intermediate mimetypes, where <b>n</b> is the
47      * number of {@link #setTransformers(List) transformers} taking part in the
48      * transformation.
49      *
50      * @param intermediateMimetypes intermediate mimetypes to transition the content
51      * through.
52      */

53     public void setIntermediateMimetypes(List JavaDoc<String JavaDoc> intermediateMimetypes)
54     {
55         this.intermediateMimetypes = intermediateMimetypes;
56     }
57
58     /**
59      * Ensures that required properties have been set
60      */

61     public void afterPropertiesSet() throws Exception JavaDoc
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     /**
79      * @return Returns the multiple of the reliabilities of the chain of transformers
80      */

81     public double getReliability(String JavaDoc sourceMimetype, String JavaDoc targetMimetype)
82     {
83         double reliability = 1.0;
84         String JavaDoc currentSourceMimetype = sourceMimetype;
85         
86         Iterator JavaDoc<ContentTransformer> transformerIterator = transformers.iterator();
87         Iterator JavaDoc<String JavaDoc> intermediateMimetypeIterator = intermediateMimetypes.iterator();
88         while (transformerIterator.hasNext())
89         {
90             ContentTransformer transformer = transformerIterator.next();
91             // determine the target mimetype. This is the final target if we are on the last transformation
92
String JavaDoc currentTargetMimetype = null;
93             if (!transformerIterator.hasNext())
94             {
95                 currentTargetMimetype = targetMimetype;
96             }
97             else
98             {
99                 // use an intermediate transformation mimetype
100
currentTargetMimetype = intermediateMimetypeIterator.next();
101             }
102             // the reliability is a multiple
103
reliability *= transformer.getReliability(currentSourceMimetype, currentTargetMimetype);
104             // move the source on
105
currentSourceMimetype = currentTargetMimetype;
106         }
107         // done
108
return reliability;
109     }
110
111     @Override JavaDoc
112     public void transformInternal(
113             ContentReader reader,
114             ContentWriter writer,
115             Map JavaDoc<String JavaDoc, Object JavaDoc> options) throws Exception JavaDoc
116     {
117         ContentReader currentReader = reader;
118         
119         Iterator JavaDoc<ContentTransformer> transformerIterator = transformers.iterator();
120         Iterator JavaDoc<String JavaDoc> intermediateMimetypeIterator = intermediateMimetypes.iterator();
121         while (transformerIterator.hasNext())
122         {
123             ContentTransformer transformer = transformerIterator.next();
124             // determine the target mimetype. This is the final target if we are on the last transformation
125
ContentWriter currentWriter = null;
126             if (!transformerIterator.hasNext())
127             {
128                 currentWriter = writer;
129             }
130             else
131             {
132                 String JavaDoc nextMimetype = intermediateMimetypeIterator.next();
133                 // make a temp file writer with the correct extension
134
String JavaDoc sourceExt = getMimetypeService().getExtension(currentReader.getMimetype());
135                 String JavaDoc targetExt = getMimetypeService().getExtension(nextMimetype);
136                 File JavaDoc tempFile = TempFileProvider.createTempFile(
137                         "ComplextTransformer_intermediate_" + sourceExt + "_",
138                         "." + targetExt);
139                 currentWriter = new FileContentWriter(tempFile);
140                 currentWriter.setMimetype(nextMimetype);
141             }
142             // transform
143
transformer.transform(currentReader, currentWriter, options);
144             // move the source on
145
currentReader = currentWriter.getReader();
146         }
147         // done
148
}
149 }
150
Popular Tags