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 java.io.InputStream;
27 import java.io.IOException;
28 import java.util.Enumeration;
29 
30 import org.openoffice.xmerge.util.registry.ConverterInfo;
31 
32 /**
33  *  The <code>Convert</code> class manages a conversion from one
34  *  mime-type to another.  The <code>ConvertFactory</code> is
35  *  responsible for returning the appropriate <code>Convert</code>
36  *  class for a specified conversion.  This class is responsible
37  *  for all interactions with the <code>PluginFactory</code>
38  *  implementation.
39  *
40  *  @see  ConverterFactory
41  *  @see  PluginFactory
42  *  @see  org.openoffice.xmerge.util.registry.ConverterInfo
43  *
44  *  @author  Martin Maher
45  */
46 public class Convert implements Cloneable {
47 
48     /**
49      *  ConvertInfo that corresponds to the from-mime/to-mime
50      *  conversion.
51      */
52     private ConverterInfo ci;
53 
54     /**
55      *  true if converting to the Office format, false if converting
56      *  to the device format.
57      */
58     private boolean toOffice;
59 
60     /**
61      *  Holds the convert input data.
62      */
63     private ConvertData inputCD = new ConvertData();
64 
65 
66     /**
67      *  Construct a Convert class with specified <code>ConvertInfo</code>
68      *  registry information.
69      *
70      *  @param  ci        A <code>ConvertInfo</code> object containing
71      *                    registry information corresponding to a
72      *                    specific plug-in.
73      *  @param  toOffice  true if converting to the Office format,
74      *                    false if converting to the device format.
75      */
Convert(ConverterInfo ci, boolean toOffice)76     public Convert(ConverterInfo ci, boolean toOffice) {
77         this.ci = ci;
78         this.toOffice = toOffice;
79     }
80 
81 
82     /**
83      *  Adds an <code>InputStream</code> to be used as input by the
84      *  <code>Convert</code> class.  It is possible that many files
85      *  need to be converted into a single output <code>Documetn</code>,
86      *  so this function may be called more than one time.  It is the
87      *  plug-in's responsibility to know how to handle the input.
88      *
89      *  @param  name  The name corresponding to the <code>InputStream</code>.
90      *  @param  is    <code>InputStream</code> to be used as input.
91      *
92      *  @throws  IOException  If any I/O error occurs.
93      */
addInputStream(String name, InputStream is)94     public void addInputStream(String name, InputStream is)
95         throws IOException {
96 
97         Document inputDoc;
98 
99         if (toOffice == true) {
100             inputDoc = ci.getPluginFactory().createDeviceDocument(name, is);
101         } else {
102             inputDoc = ci.getPluginFactory().createOfficeDocument(name, is);
103         }
104         inputCD.addDocument(inputDoc);
105     }
106 
107      /**
108      *  Adds an <code>InputStream</code> to be used as input by the
109      *  <code>Convert</code> class.  It is possible that many files
110      *  need to be converted into a single output <code>Documetn</code>,
111      *  so this function may be called more than one time.  It is the
112      *  plug-in's responsibility to know how to handle the input.
113      *
114      *  @param  name  The name corresponding to the <code>InputStream</code>.
115      *  @param  is    <code>InputStream</code> to be used as input.
116      *  @param  isZip <code>boolean</code> to identify that incoming stream is      *                zipped
117      *
118      *  @throws  IOException  If any I/O error occurs.
119      */
addInputStream(String name, InputStream is,boolean isZip)120     public void addInputStream(String name, InputStream is,boolean isZip)
121         throws IOException {
122 
123         Document inputDoc;
124 
125         if (toOffice == true) {
126             inputDoc = ci.getPluginFactory().createDeviceDocument(name, is);
127         } else {
128             inputDoc = ci.getPluginFactory().createOfficeDocument(name, is, isZip);
129         }
130         inputCD.addDocument(inputDoc);
131     }
132 
133 
134     /**
135      *  Returns a <code>DocumentMerger</code> for the given <code>Document</code>.
136      *
137      *  @param  origDoc The <code>Document</code> were later changes will be merged to
138      *
139      *  @return  The <code>DocumentMerger</code> object for the given document.
140      *
141      *  @throws  IOException  If any I/O error occurs.
142      */
getDocumentMerger(Document origDoc)143     public DocumentMerger getDocumentMerger(Document origDoc)
144         throws IOException {
145 
146      DocumentMergerFactory myDocMergerFactory = ci.getDocMergerFactory();
147      DocumentMerger merger = myDocMergerFactory.createDocumentMerger(origDoc);
148 	 return merger;
149     }
150 
151     /**
152      *  Resets the input queue, so that the user can use this class to
153      *  perform another conversion.  This causes the
154      *  <code>addInputStream</code> method to accept input for the next
155      *  conversion.
156      */
reset()157     public void reset() {
158         inputCD.reset();
159     }
160 
161 
162     /**
163      *  Clones a Convert object so another Convert object can
164      *  do the same conversion.  <code>InputStream</code> objects passed
165      *  in via calls to the <code>addInputStream</code> method are not
166      *  copied.
167      *
168      *  @return  The cloned <code>Convert</code> object.
169      */
clone()170     public Object clone() {
171 
172 		Convert aClone = null;
173 
174 		try {
175         	aClone = (Convert) super.clone();
176 			aClone.reset();
177 		}
178 		catch (CloneNotSupportedException e) {
179 			System.out.println("Convert clone could not be created");
180 		}
181 		return aClone;
182     }
183 
184 
185     /**
186      *  Convert the input specified in calls to the <code>addInputStream</code>
187      *  method to the output format specified by this <code>Convert</code>
188      *  class.
189      *
190      *  @return  The output data.
191      *
192      *  @throws  ConvertException  If any conversion error occurs.
193      *  @throws  IOException       If any I/O error occurs.
194      */
convert()195     public ConvertData convert() throws ConvertException, IOException {
196 
197         ConvertData dataOut = new ConvertData();
198 
199         if (toOffice) {
200 
201             //  From device format to Office format
202             //
203             DocumentDeserializerFactory myDocDeserializerFactory =
204                 ci.getDocDeserializerFactory();
205             DocumentDeserializer deser =
206                 myDocDeserializerFactory.createDocumentDeserializer(inputCD);
207             Document deviceDoc = deser.deserialize();
208 
209 
210             dataOut.addDocument(deviceDoc);
211             return dataOut;
212 
213         } else {
214 
215             //  From Office format to device format
216             //
217             DocumentSerializerFactory myDocSerializerFactory =
218                 ci.getDocSerializerFactory();
219 
220             Enumeration e = inputCD.getDocumentEnumeration();
221 
222             Document doc = (Document) e.nextElement();
223             DocumentSerializer ser = myDocSerializerFactory.createDocumentSerializer(doc);
224             dataOut = ser.serialize();
225 
226             return dataOut;
227         }
228     }
229 
230     /**
231      *  NEW (HJ):
232      *  Convert the input specified in calls to the <code>addInputStream</code>
233      *  method to the output format specified by this <code>Convert</code>
234      *  class.
235      *  The (de)serializer may use the URLs to resolve links and choose name(s)
236      *  for destination document(s).
237      *
238      *  @return  The output data.
239      *
240      *  @param   sFromURL          URL of the source document (may be null if unknown)
241      *  @param   sToURL            URL of the destination document (may be null if unknown)
242      *
243      *  @throws  ConvertException  If any conversion error occurs.
244      *  @throws  IOException       If any I/O error occurs.
245      */
convert(String sFromURL, String sToURL)246     public ConvertData convert(String sFromURL, String sToURL) throws
247         ConvertException, IOException {
248 
249         ConvertData dataOut = new ConvertData();
250 
251         if (toOffice) {
252 
253             //  From device format to Office format
254             //
255             DocumentDeserializerFactory myDocDeserializerFactory =
256                 ci.getDocDeserializerFactory();
257             DocumentDeserializer deser =
258                 myDocDeserializerFactory.createDocumentDeserializer(inputCD);
259             Document officeDoc = deser instanceof DocumentSerializer2 ?
260                 ((DocumentDeserializer2) deser).deserialize(sFromURL,sToURL) :
261                 deser.deserialize();
262 
263 
264             dataOut.addDocument(officeDoc);
265             return dataOut;
266 
267         } else {
268 
269             //  From Office format to device format
270             //
271             DocumentSerializerFactory myDocSerializerFactory =
272                 ci.getDocSerializerFactory();
273 
274             Enumeration e = inputCD.getDocumentEnumeration();
275 
276             Document doc = (Document) e.nextElement();
277             DocumentSerializer ser = myDocSerializerFactory.createDocumentSerializer(doc);
278             dataOut = ser instanceof DocumentSerializer2 ?
279                 ((DocumentSerializer2) ser).serialize(sFromURL,sToURL) :
280                 ser.serialize();
281 
282             return dataOut;
283         }
284     }
285 
286     /**
287      *  Returns the appropriate &quot;Office&quot; <code>Document</code>
288      *  object for this plug-in.
289      *
290      *  @param  name  The name of the <code>Document</code> to create.
291      *  @param  is    The <code>InputStream</code> corresponding to the
292      *                <code>Document</code> to create.
293      *
294      *  @return  The appropriate &quot;Office&quot; <code>Document</code>
295      *           object for this plug-in.
296      *
297      *  @throws  IOException  If any I/O error occurs.
298      */
getOfficeDocument(String name, InputStream is)299     public Document getOfficeDocument(String name, InputStream is)
300         throws IOException {
301         return(ci.getPluginFactory().createOfficeDocument(name, is));
302     }
303 
304 
305     /**
306      *  Returns the appropriate &quot;Device&quot; <code>Document</code>
307      *  object for this plug-in.
308      *
309      *  @param  name  The name of the <code>Document</code> to create.
310      *  @param  is    The <code>InputStream</code> corresponding to the
311      *                <code>Document</code> to create.
312      *
313      *  @return  The appropriate &quot;Device&quot; <code>Document</code>
314      *           object for this plug-in.
315      *
316      *  @throws  IOException  If any I/O error occurs.
317      */
getDeviceDocument(String name, InputStream is)318     public Document getDeviceDocument(String name, InputStream is)
319         throws IOException {
320         return(ci.getPluginFactory().createDeviceDocument(name, is));
321     }
322 }
323 
324