1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package org.openoffice.xmerge;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.openoffice.xmerge.util.registry.ConverterInfo;
/**
* The {@code Convert} class manages a conversion from one mime-type to another.
*
* <p>The {@code ConvertFactory} is responsible for returning the appropriate
* {@code Convert} class for a specified conversion. This class is responsible
* for all interactions with the {@code PluginFactory} implementation.</p>
*
* @see ConverterFactory
* @see PluginFactory
* @see org.openoffice.xmerge.util.registry.ConverterInfo
*/
public class Convert implements Cloneable {
/** ConvertInfo that corresponds to the from-mime/to-mime conversion. */
private final ConverterInfo ci;
/**
* {@code true} if converting to the Office format, {@code false} if
* converting to the device format.
*/
private final boolean toOffice;
/** Holds the convert input data. */
private final ConvertData inputCD = new ConvertData();
/**
* Construct a Convert class with specified {@code ConvertInfo} registry
* information.
*
* @param ci A {@code ConvertInfo} object containing registry
* information corresponding to a specific plug-in.
* @param toOffice {@code true} if converting to the Office format,
* {@code false} if converting to the device format.
*/
public Convert(ConverterInfo ci, boolean toOffice) {
this.ci = ci;
this.toOffice = toOffice;
}
/**
* Adds an {@code InputStream} to be used as input by the {@code Convert}
* class.
*
* <p>It is possible that many files need to be converted into a single
* output {@code Document}, so this function may be called more than one
* time. It is the plug-in's responsibility to know how to handle the input.
* </p>
*
* @param name The name corresponding to the {@code InputStream}.
* @param is {@code InputStream} to be used as input.
*
* @throws IOException If any I/O error occurs.
*/
public void addInputStream(String name, InputStream is)
throws IOException {
Document inputDoc;
if (toOffice) {
inputDoc = ci.getPluginFactory().createDeviceDocument(name, is);
} else {
inputDoc = ci.getPluginFactory().createOfficeDocument(name, is);
}
inputCD.addDocument(inputDoc);
}
/**
* Adds an {@code InputStream} to be used as input by the {@code Convert}
* class.
*
* <p>It is possible that many files need to be converted into a single
* output {@code Document}, so this function may be called more than one
* time. It is the plug-in's responsibility to know how to handle the input.
* </p>
*
* @param name The name corresponding to the {@code InputStream}.
* @param is {@code InputStream} to be used as input.
* @param isZip {@code boolean} to identify that incoming stream is * zipped.
*
* @throws IOException If any I/O error occurs.
*/
public void addInputStream(String name, InputStream is,boolean isZip)
throws IOException {
Document inputDoc;
if (toOffice) {
inputDoc = ci.getPluginFactory().createDeviceDocument(name, is);
} else {
inputDoc = ci.getPluginFactory().createOfficeDocument(name, is, isZip);
}
inputCD.addDocument(inputDoc);
}
/**
* Returns a {@code DocumentMerger} for the given {@code Document}.
*
* @param origDoc The {@code Document} were later changes will be merged to.
*
* @return The {@code DocumentMerger} object for the given document.
*
* @throws IOException If any I/O error occurs.
*/
public DocumentMerger getDocumentMerger(Document origDoc) throws IOException {
DocumentMergerFactory myDocMergerFactory = ci.getDocMergerFactory();
DocumentMerger merger = myDocMergerFactory.createDocumentMerger(origDoc);
return merger;
}
/**
* Resets the input queue, so that the user can use this class to perform
* another conversion.
*
* <p>This causes the {@code addInputStream} method to accept input for the
* next conversion.</p>
*/
private void reset() {
inputCD.reset();
}
/**
* Clones a {@code Convert} object so another Convert object can do the same
* conversion.
*
* <p>{@code InputStream} objects passed in via calls to the
* {@code addInputStream} method are not copied.</p>
*
* @return The cloned {@code Convert} object.
*/
@Override
public Object clone() {
Convert aClone = null;
try {
aClone = (Convert) super.clone();
aClone.reset();
}
catch (CloneNotSupportedException e) {
System.out.println("Convert clone could not be created");
}
return aClone;
}
/**
* Convert the input specified in calls to the {@code addInputStream}
* method to the output format specified by this {@code Convert} class.
*
* @return The output data.
*
* @throws ConvertException If any conversion error occurs.
* @throws IOException If any I/O error occurs.
*/
public ConvertData convert() throws ConvertException, IOException {
ConvertData dataOut = new ConvertData();
if (toOffice) {
// From device format to Office format
DocumentDeserializerFactory myDocDeserializerFactory =
ci.getDocDeserializerFactory();
DocumentDeserializer deser =
myDocDeserializerFactory.createDocumentDeserializer(inputCD);
Document deviceDoc = deser.deserialize();
dataOut.addDocument(deviceDoc);
return dataOut;
} else {
// From Office format to device format
DocumentSerializerFactory myDocSerializerFactory =
ci.getDocSerializerFactory();
Iterator<Object> e = inputCD.getDocumentEnumeration();
Document doc = (Document) e.next();
DocumentSerializer ser = myDocSerializerFactory.createDocumentSerializer(doc);
dataOut = ser.serialize();
return dataOut;
}
}
/**
* Returns the appropriate "Office" {@code Document} object for
* this plug-in.
*
* @param name The name of the {@code Document} to create.
* @param is The {@code InputStream} corresponding to the
* {@code Document} to create.
*
* @return The appropriate "Office" {@code Document} object for
* this plug-in.
*
* @throws IOException If any I/O error occurs.
*/
public Document getOfficeDocument(String name, InputStream is)
throws IOException {
return ci.getPluginFactory().createOfficeDocument(name, is);
}
}
|