Lines 24-29
Link Here
|
24 |
import java.util.zip.ZipEntry; |
24 |
import java.util.zip.ZipEntry; |
25 |
import java.util.zip.ZipOutputStream; |
25 |
import java.util.zip.ZipOutputStream; |
26 |
|
26 |
|
|
|
27 |
import javax.xml.transform.TransformerException; |
28 |
|
27 |
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; |
29 |
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; |
28 |
import org.apache.poi.openxml4j.opc.PackageNamespaces; |
30 |
import org.apache.poi.openxml4j.opc.PackageNamespaces; |
29 |
import org.apache.poi.openxml4j.opc.PackagePart; |
31 |
import org.apache.poi.openxml4j.opc.PackagePart; |
Lines 48-54
Link Here
|
48 |
* @author Julien Chable |
50 |
* @author Julien Chable |
49 |
*/ |
51 |
*/ |
50 |
public final class ZipPartMarshaller implements PartMarshaller { |
52 |
public final class ZipPartMarshaller implements PartMarshaller { |
51 |
private static POILogger logger = POILogFactory.getLogger(ZipPartMarshaller.class); |
53 |
private static final POILogger logger = POILogFactory.getLogger(ZipPartMarshaller.class); |
52 |
|
54 |
|
53 |
/** |
55 |
/** |
54 |
* Save the specified part. |
56 |
* Save the specified part. |
Lines 56-63
Link Here
|
56 |
* @throws OpenXML4JException |
58 |
* @throws OpenXML4JException |
57 |
* Throws if an internal exception is thrown. |
59 |
* Throws if an internal exception is thrown. |
58 |
*/ |
60 |
*/ |
59 |
public boolean marshall(PackagePart part, OutputStream os) |
61 |
public void marshall(PackagePart part, OutputStream os) |
60 |
throws OpenXML4JException { |
62 |
throws IOException, OpenXML4JException { |
61 |
if (!(os instanceof ZipOutputStream)) { |
63 |
if (!(os instanceof ZipOutputStream)) { |
62 |
logger.log(POILogger.ERROR,"Unexpected class " + os.getClass().getName()); |
64 |
logger.log(POILogger.ERROR,"Unexpected class " + os.getClass().getName()); |
63 |
throw new OpenXML4JException("ZipOutputStream expected !"); |
65 |
throw new OpenXML4JException("ZipOutputStream expected !"); |
Lines 68-74
Link Here
|
68 |
// check if there is anything to save for some parts. We don't do this for all parts as some code |
70 |
// check if there is anything to save for some parts. We don't do this for all parts as some code |
69 |
// might depend on empty parts being saved, e.g. some unit tests verify this currently. |
71 |
// might depend on empty parts being saved, e.g. some unit tests verify this currently. |
70 |
if(part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) { |
72 |
if(part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) { |
71 |
return true; |
73 |
return; |
72 |
} |
74 |
} |
73 |
|
75 |
|
74 |
ZipOutputStream zos = (ZipOutputStream) os; |
76 |
ZipOutputStream zos = (ZipOutputStream) os; |
Lines 92-100
Link Here
|
92 |
} |
94 |
} |
93 |
zos.closeEntry(); |
95 |
zos.closeEntry(); |
94 |
} catch (IOException ioe) { |
96 |
} catch (IOException ioe) { |
95 |
logger.log(POILogger.ERROR,"Cannot write: " + part.getPartName() + ": in ZIP", |
97 |
final String msg = "Cannot write: " + part.getPartName() + ": in ZIP"; |
96 |
ioe); |
98 |
logger.log(POILogger.ERROR, msg, ioe); |
97 |
return false; |
99 |
throw new IOException(msg, ioe); |
98 |
} |
100 |
} |
99 |
|
101 |
|
100 |
// Saving relationship part |
102 |
// Saving relationship part |
Lines 106-112
Link Here
|
106 |
relationshipPartName, zos); |
108 |
relationshipPartName, zos); |
107 |
|
109 |
|
108 |
} |
110 |
} |
109 |
return true; |
|
|
110 |
} |
111 |
} |
111 |
|
112 |
|
112 |
/** |
113 |
/** |
Lines 119-128
Link Here
|
119 |
* @param zos |
120 |
* @param zos |
120 |
* Zip output stream in which to save the XML content of the |
121 |
* Zip output stream in which to save the XML content of the |
121 |
* relationships serialization. |
122 |
* relationships serialization. |
|
|
123 |
* @throws IOException if an error occurred while saving the relationship part |
124 |
* to the output stream. This is often because the output stream is closed. |
125 |
* @throws OpenXML4JException if an error occurred while transforming the XML source. |
122 |
*/ |
126 |
*/ |
123 |
public static boolean marshallRelationshipPart( |
127 |
public static void marshallRelationshipPart( |
124 |
PackageRelationshipCollection rels, PackagePartName relPartName, |
128 |
PackageRelationshipCollection rels, PackagePartName relPartName, |
125 |
ZipOutputStream zos) { |
129 |
ZipOutputStream zos) throws IOException, OpenXML4JException { |
126 |
// Building xml |
130 |
// Building xml |
127 |
Document xmlOutDoc = DocumentHelper.createDocument(); |
131 |
Document xmlOutDoc = DocumentHelper.createDocument(); |
128 |
// make something like <Relationships |
132 |
// make something like <Relationships |
Lines 178-191
Link Here
|
178 |
relPartName.getURI().toASCIIString()).getPath()); |
182 |
relPartName.getURI().toASCIIString()).getPath()); |
179 |
try { |
183 |
try { |
180 |
zos.putNextEntry(ctEntry); |
184 |
zos.putNextEntry(ctEntry); |
181 |
if (!StreamHelper.saveXmlInStream(xmlOutDoc, zos)) { |
185 |
StreamHelper.saveXmlInStream(xmlOutDoc, zos); |
182 |
return false; |
|
|
183 |
} |
184 |
zos.closeEntry(); |
186 |
zos.closeEntry(); |
185 |
} catch (IOException e) { |
187 |
} catch (IOException e) { |
186 |
logger.log(POILogger.ERROR,"Cannot create zip entry " + relPartName, e); |
188 |
logger.log(POILogger.ERROR, |
187 |
return false; |
189 |
"Cannot create zip entry " + relPartName + ". Verify output stream has not been closed.", e); |
|
|
190 |
throw e; |
191 |
} catch (final TransformerException e) { |
192 |
final String msg = "An error occurred while transforming the XML source for " + relPartName + "."; |
193 |
logger.log(POILogger.ERROR, msg, e); |
194 |
throw new OpenXML4JException(msg, e); |
188 |
} |
195 |
} |
189 |
return true; // success |
|
|
190 |
} |
196 |
} |
191 |
} |
197 |
} |