View | Details | Raw Unified | Return to bug 60102
Collapse All | Expand All

(-)src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java (-1 / +2 lines)
Lines 687-694 Link Here
687
	 *            Output stream to save this part.
687
	 *            Output stream to save this part.
688
	 * @throws OpenXML4JException
688
	 * @throws OpenXML4JException
689
	 *             If any exception occur.
689
	 *             If any exception occur.
690
	 * @throws IOException 
690
	 */
691
	 */
691
	public abstract boolean save(OutputStream zos) throws OpenXML4JException;
692
	public abstract void save(OutputStream zos) throws OpenXML4JException, IOException;
692
693
693
	/**
694
	/**
694
	 * Load the content of this part.
695
	 * Load the content of this part.
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/StreamHelper.java (-45 / +41 lines)
Lines 31-50 Link Here
31
import javax.xml.transform.dom.DOMSource;
31
import javax.xml.transform.dom.DOMSource;
32
import javax.xml.transform.stream.StreamResult;
32
import javax.xml.transform.stream.StreamResult;
33
33
34
import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller;
35
import org.apache.poi.util.POILogFactory;
36
import org.apache.poi.util.POILogger;
34
import org.w3c.dom.Document;
37
import org.w3c.dom.Document;
35
38
36
public final class StreamHelper {
39
public final class StreamHelper {
37
40
41
    private static final POILogger logger = POILogFactory.getLogger(StreamHelper.class);
42
38
	private StreamHelper() {
43
	private StreamHelper() {
39
		// Do nothing
44
		// Do nothing
40
	}
45
	}
41
  
42
  private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
43
  
44
  private static synchronized Transformer getIdentityTransformer() throws TransformerException {
45
    return transformerFactory.newTransformer();
46
  }
47
46
47
    private static final TransformerFactory transformerFactory = TransformerFactory.newInstance();
48
49
    private static synchronized Transformer getIdentityTransformer() throws TransformerException {
50
        return transformerFactory.newTransformer();
51
    }
52
48
	/**
53
	/**
49
	 * Save the document object in the specified output stream.
54
	 * Save the document object in the specified output stream.
50
	 *
55
	 *
Lines 52-87 Link Here
52
	 *            The XML document.
57
	 *            The XML document.
53
	 * @param outStream
58
	 * @param outStream
54
	 *            The OutputStream in which the XML document will be written.
59
	 *            The OutputStream in which the XML document will be written.
55
	 * @return <b>true</b> if the xml is successfully written in the stream,
60
	 * @throws TransformerException if an error occurred while transforming XML Source.
56
	 *         else <b>false</b>.
61
	 * @throws IOException if the output stream is closed.
57
	 */
62
	 */
58
    public static boolean saveXmlInStream(Document xmlContent,
63
    public static void saveXmlInStream(Document xmlContent,
59
            OutputStream outStream) {
64
            OutputStream outStream) throws TransformerException, IOException {
60
        try {
65
        Transformer trans = getIdentityTransformer();
61
            Transformer trans = getIdentityTransformer();
66
        Source xmlSource = new DOMSource(xmlContent);
62
            Source xmlSource = new DOMSource(xmlContent);
67
        // prevent close of stream by transformer:
63
            // prevent close of stream by transformer:
68
        Result outputTarget = new StreamResult(new FilterOutputStream(
64
            Result outputTarget = new StreamResult(new FilterOutputStream(
69
                outStream) {
65
                    outStream) {
70
            @Override
66
                @Override
71
            public void write(byte b[], int off, int len)
67
                public void write(byte b[], int off, int len)
72
                    throws IOException {
68
                        throws IOException {
73
                out.write(b, off, len);
69
                    out.write(b, off, len);
74
            }
70
                }
71
75
72
                @Override
76
            @Override
73
                public void close() throws IOException {
77
            public void close() throws IOException {
74
                    out.flush(); // only flush, don't close!
78
                out.flush(); // only flush, don't close!
75
                }
79
            }
76
            });
80
        });
77
            trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
81
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
78
            trans.setOutputProperty(OutputKeys.INDENT, "yes");
82
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
79
            trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
83
        trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
80
            trans.transform(xmlSource, outputTarget);
84
        trans.transform(xmlSource, outputTarget);
81
        } catch (TransformerException e) {
82
            return false;
83
        }
84
        return true;
85
    }
85
    }
86
86
87
	/**
87
	/**
Lines 91-108 Link Here
91
	 *            The source stream.
91
	 *            The source stream.
92
	 * @param outStream
92
	 * @param outStream
93
	 *            The destination stream.
93
	 *            The destination stream.
94
	 * @return <b>true</b> if the operation succeed, else return <b>false</b>.
94
	 * throws IOException if the input stream cannot be read or the output stream cannot be written to.
95
	 * Verify that both streams are open.
95
	 */
96
	 */
96
	public static boolean copyStream(InputStream inStream, OutputStream outStream) {
97
	public static void copyStream(InputStream inStream, OutputStream outStream) throws IOException {
97
		try {
98
		byte[] buffer = new byte[1024];
98
			byte[] buffer = new byte[1024];
99
		int bytesRead;
99
			int bytesRead;
100
		while ((bytesRead = inStream.read(buffer)) >= 0) {
100
			while ((bytesRead = inStream.read(buffer)) >= 0) {
101
			outStream.write(buffer, 0, bytesRead);
101
				outStream.write(buffer, 0, bytesRead);
102
			}
103
		} catch (Exception e) {
104
			return false;
105
		}
102
		}
106
		return true;
107
	}
103
	}
108
}
104
}
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java (-16 / +28 lines)
Lines 473-484 Link Here
473
		throwExceptionIfReadOnly();
473
		throwExceptionIfReadOnly();
474
474
475
		final ZipOutputStream zos;
475
		final ZipOutputStream zos;
476
		final boolean newZOSWasCreated;
476
		try {
477
		try {
477
			if (!(outputStream instanceof ZipOutputStream))
478
			if (!(outputStream instanceof ZipOutputStream)) {
478
				zos = new ZipOutputStream(outputStream);
479
				zos = new ZipOutputStream(outputStream);
479
			else
480
				newZOSWasCreated = true;
481
			} else {
480
				zos = (ZipOutputStream) outputStream;
482
				zos = (ZipOutputStream) outputStream;
481
483
				newZOSWasCreated = false;
484
			}
485
				
482
			// If the core properties part does not exist in the part list,
486
			// If the core properties part does not exist in the part list,
483
			// we save it as well
487
			// we save it as well
484
			if (this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).size() == 0 &&
488
			if (this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).size() == 0 &&
Lines 525-547 Link Here
525
				PartMarshaller marshaller = partMarshallers
529
				PartMarshaller marshaller = partMarshallers
526
						.get(part._contentType);
530
						.get(part._contentType);
527
				if (marshaller != null) {
531
				if (marshaller != null) {
528
					if (!marshaller.marshall(part, zos)) {
532
					try {
529
						throw new OpenXML4JException(
533
						marshaller.marshall(part, zos);
530
								"The part "
534
					} catch (final OpenXML4JException e) {
531
										+ part.getPartName().getURI()
535
						final String msg = "The part "
532
										+ " fail to be saved in the stream with marshaller "
536
								+ part.getPartName().getURI()
533
										+ marshaller);
537
								+ " fail to be saved in the stream with marshaller "
538
								+ marshaller;
539
						throw new OpenXML4JException(msg, e);
534
					}
540
					}
535
				} else {
541
				} else {
536
					if (!defaultPartMarshaller.marshall(part, zos))
542
					try {
537
						throw new OpenXML4JException(
543
						defaultPartMarshaller.marshall(part, zos);
538
								"The part "
544
					} catch (final OpenXML4JException e) {
539
										+ part.getPartName().getURI()
545
						final String msg = "The part "
540
										+ " fail to be saved in the stream with marshaller "
546
								+ part.getPartName().getURI()
541
										+ defaultPartMarshaller);
547
								+ " fail to be saved in the stream with marshaller "
548
								+ defaultPartMarshaller;
549
						throw new OpenXML4JException(msg, e);
550
					}
542
				}
551
				}
543
			}
552
			}
544
			zos.close();
553
			
554
			if (newZOSWasCreated) {
555
				zos.close();
556
			}
545
		} catch (OpenXML4JRuntimeException e) {
557
		} catch (OpenXML4JRuntimeException e) {
546
			// no need to wrap this type of Exception
558
			// no need to wrap this type of Exception
547
			throw e;
559
			throw e;
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackagePart.java (-2 / +2 lines)
Lines 120-127 Link Here
120
	}
120
	}
121
121
122
	@Override
122
	@Override
123
	public boolean save(OutputStream os) throws OpenXML4JException {
123
	public void save(OutputStream os) throws OpenXML4JException, IOException {
124
		return new ZipPartMarshaller().marshall(this, os);
124
		new ZipPartMarshaller().marshall(this, os);
125
	}
125
	}
126
126
127
	@Override
127
	@Override
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ContentTypeManager.java (-3 / +4 lines)
Lines 409-416 Link Here
409
	 *            The output stream use to save the XML content of the content
409
	 *            The output stream use to save the XML content of the content
410
	 *            types part.
410
	 *            types part.
411
	 * @return <b>true</b> if the operation success, else <b>false</b>.
411
	 * @return <b>true</b> if the operation success, else <b>false</b>.
412
	 * @throws IOException 
412
	 */
413
	 */
413
	public boolean save(OutputStream outStream) {
414
	public void save(OutputStream outStream) throws IOException {
414
		Document xmlOutDoc = DocumentHelper.createDocument();
415
		Document xmlOutDoc = DocumentHelper.createDocument();
415
416
416
		// Building namespace
417
		// Building namespace
Lines 432-438 Link Here
432
		xmlOutDoc.normalize();
433
		xmlOutDoc.normalize();
433
434
434
		// Save content in the specified output stream
435
		// Save content in the specified output stream
435
		return this.saveImpl(xmlOutDoc, outStream);
436
		this.saveImpl(xmlOutDoc, outStream);
436
	}
437
	}
437
438
438
	/**
439
	/**
Lines 475-479 Link Here
475
	 * @param out
476
	 * @param out
476
	 *            The output stream use to write the content type XML.
477
	 *            The output stream use to write the content type XML.
477
	 */
478
	 */
478
	public abstract boolean saveImpl(Document content, OutputStream out);
479
	public abstract void saveImpl(Document content, OutputStream out) throws IOException;
479
}
480
}
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/MemoryPackagePart.java (-2 / +3 lines)
Lines 108-115 Link Here
108
	}
108
	}
109
109
110
	@Override
110
	@Override
111
	public boolean save(OutputStream os) throws OpenXML4JException {
111
	public void save(OutputStream os) throws OpenXML4JException, IOException {
112
		return new ZipPartMarshaller().marshall(this, os);
112
		ZipPartMarshaller marshaller = new ZipPartMarshaller();
113
		marshaller.marshall(this, os);
113
	}
114
	}
114
115
115
	@Override
116
	@Override
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PackagePropertiesPart.java (-1 / +1 lines)
Lines 653-659 Link Here
653
	}
653
	}
654
654
655
	@Override
655
	@Override
656
	public boolean save(OutputStream zos) {
656
	public void save(OutputStream zos) {
657
		throw new InvalidOperationException("Operation not authorized. This part may only be manipulated using the getters and setters on PackagePropertiesPart");
657
		throw new InvalidOperationException("Operation not authorized. This part may only be manipulated using the getters and setters on PackagePropertiesPart");
658
	}
658
	}
659
659
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/PartMarshaller.java (-2 / +4 lines)
Lines 17-22 Link Here
17
17
18
package org.apache.poi.openxml4j.opc.internal;
18
package org.apache.poi.openxml4j.opc.internal;
19
19
20
import java.io.IOException;
20
import java.io.OutputStream;
21
import java.io.OutputStream;
21
22
22
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
23
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
Lines 43-49 Link Here
43
	 * @throws OpenXML4JException
44
	 * @throws OpenXML4JException
44
	 *             Throws only if any other exceptions are thrown by inner
45
	 *             Throws only if any other exceptions are thrown by inner
45
	 *             methods.
46
	 *             methods.
47
	 * @throws IOException if an error occurred while writing to output stream. Verify that {@code out} is open for writing. 
46
	 */
48
	 */
47
	public boolean marshall(PackagePart part, OutputStream out)
49
	public void marshall(PackagePart part, OutputStream out)
48
			throws OpenXML4JException;
50
			throws OpenXML4JException, IOException;
49
}
51
}
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipContentTypeManager.java (-10 / +13 lines)
Lines 23-28 Link Here
23
import java.util.zip.ZipEntry;
23
import java.util.zip.ZipEntry;
24
import java.util.zip.ZipOutputStream;
24
import java.util.zip.ZipOutputStream;
25
25
26
import javax.xml.transform.TransformerException;
27
26
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
28
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
27
import org.apache.poi.openxml4j.opc.OPCPackage;
29
import org.apache.poi.openxml4j.opc.OPCPackage;
28
import org.apache.poi.openxml4j.opc.StreamHelper;
30
import org.apache.poi.openxml4j.opc.StreamHelper;
Lines 38-44 Link Here
38
 * @see ContentTypeManager
40
 * @see ContentTypeManager
39
 */
41
 */
40
public class ZipContentTypeManager extends ContentTypeManager {
42
public class ZipContentTypeManager extends ContentTypeManager {
41
    private static POILogger logger = POILogFactory.getLogger(ZipContentTypeManager.class);
43
	private static final POILogger logger = POILogFactory.getLogger(ZipContentTypeManager.class);
42
44
43
	/**
45
	/**
44
	 * Delegate constructor to the super constructor.
46
	 * Delegate constructor to the super constructor.
Lines 56-62 Link Here
56
58
57
	@SuppressWarnings("resource")
59
	@SuppressWarnings("resource")
58
    @Override
60
    @Override
59
	public boolean saveImpl(Document content, OutputStream out) {
61
	public void saveImpl(Document content, OutputStream out) throws IOException {
60
		ZipOutputStream zos = null;
62
		ZipOutputStream zos = null;
61
		if (out instanceof ZipOutputStream)
63
		if (out instanceof ZipOutputStream)
62
			zos = (ZipOutputStream) out;
64
			zos = (ZipOutputStream) out;
Lines 68-82 Link Here
68
			// Referenced in ZIP
70
			// Referenced in ZIP
69
			zos.putNextEntry(partEntry);
71
			zos.putNextEntry(partEntry);
70
			// Saving data in the ZIP file
72
			// Saving data in the ZIP file
71
			if (!StreamHelper.saveXmlInStream(content, zos)) {
73
			StreamHelper.saveXmlInStream(content, zos);
72
			    return false;
73
			}
74
			zos.closeEntry();
74
			zos.closeEntry();
75
		} catch (IOException ioe) {
75
		} catch (IOException e) {
76
			logger.log(POILogger.ERROR, "Cannot write: " + CONTENT_TYPES_PART_NAME
76
			final String msg = "Cannot write: " + CONTENT_TYPES_PART_NAME + " in Zip !";
77
					+ " in Zip !", ioe);
77
			logger.log(POILogger.ERROR, msg, e);
78
			return false;
78
			throw new IOException(msg, e);
79
		} catch (TransformerException e) {
80
			final String msg = "Cannot write: " + CONTENT_TYPES_PART_NAME + " in Zip !";
81
			logger.log(POILogger.ERROR, msg, e);
82
			throw new IOException(msg, e);
79
		}
83
		}
80
		return true;
81
	}
84
	}
82
}
85
}
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/marshallers/DefaultMarshaller.java (-3 / +5 lines)
Lines 17-22 Link Here
17
17
18
package org.apache.poi.openxml4j.opc.internal.marshallers;
18
package org.apache.poi.openxml4j.opc.internal.marshallers;
19
19
20
import java.io.IOException;
20
import java.io.OutputStream;
21
import java.io.OutputStream;
21
22
22
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
23
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
Lines 37-45 Link Here
37
	 *
38
	 *
38
	 * @throws OpenXML4JException
39
	 * @throws OpenXML4JException
39
	 *             If any error occur.
40
	 *             If any error occur.
41
	 * @throws IOException 
40
	 */
42
	 */
41
	public boolean marshall(PackagePart part, OutputStream out)
43
	public void marshall(PackagePart part, OutputStream out)
42
			throws OpenXML4JException {
44
			throws OpenXML4JException, IOException {
43
		return part.save(out);
45
		part.save(out);
44
	}
46
	}
45
}
47
}
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/marshallers/PackagePropertiesMarshaller.java (-2 / +1 lines)
Lines 87-93 Link Here
87
	 * <code>true</code>.
87
	 * <code>true</code>.
88
	 */
88
	 */
89
	@Override
89
	@Override
90
	public boolean marshall(PackagePart part, OutputStream out)
90
	public void marshall(PackagePart part, OutputStream out)
91
			throws OpenXML4JException {
91
			throws OpenXML4JException {
92
		if (!(part instanceof PackagePropertiesPart))
92
		if (!(part instanceof PackagePropertiesPart))
93
			throw new IllegalArgumentException(
93
			throw new IllegalArgumentException(
Lines 120-126 Link Here
120
		addSubject();
120
		addSubject();
121
		addTitle();
121
		addTitle();
122
		addVersion();
122
		addVersion();
123
		return true;
124
	}
123
	}
125
124
126
    /**
125
    /**
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPackagePropertiesMarshaller.java (-5 / +6 lines)
Lines 22-27 Link Here
22
import java.util.zip.ZipEntry;
22
import java.util.zip.ZipEntry;
23
import java.util.zip.ZipOutputStream;
23
import java.util.zip.ZipOutputStream;
24
24
25
import javax.xml.transform.TransformerException;
26
25
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
27
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
26
import org.apache.poi.openxml4j.opc.PackagePart;
28
import org.apache.poi.openxml4j.opc.PackagePart;
27
import org.apache.poi.openxml4j.opc.StreamHelper;
29
import org.apache.poi.openxml4j.opc.StreamHelper;
Lines 35-41 Link Here
35
public final class ZipPackagePropertiesMarshaller extends PackagePropertiesMarshaller {
37
public final class ZipPackagePropertiesMarshaller extends PackagePropertiesMarshaller {
36
38
37
	@Override
39
	@Override
38
	public boolean marshall(PackagePart part, OutputStream out)
40
	public void marshall(PackagePart part, OutputStream out)
39
			throws OpenXML4JException {
41
			throws OpenXML4JException {
40
		if (!(out instanceof ZipOutputStream)) {
42
		if (!(out instanceof ZipOutputStream)) {
41
			throw new IllegalArgumentException("ZipOutputStream expected!");
43
			throw new IllegalArgumentException("ZipOutputStream expected!");
Lines 51-63 Link Here
51
			zos.putNextEntry(ctEntry); // Add entry in ZIP
53
			zos.putNextEntry(ctEntry); // Add entry in ZIP
52
			super.marshall(part, out); // Marshall the properties inside a XML
54
			super.marshall(part, out); // Marshall the properties inside a XML
53
			// Document
55
			// Document
54
			if (!StreamHelper.saveXmlInStream(xmlDoc, out)) {
56
			StreamHelper.saveXmlInStream(xmlDoc, out);
55
				return false;
56
			}
57
			zos.closeEntry();
57
			zos.closeEntry();
58
		} catch (IOException e) {
58
		} catch (IOException e) {
59
			throw new OpenXML4JException(e.getLocalizedMessage(), e);
59
			throw new OpenXML4JException(e.getLocalizedMessage(), e);
60
		} catch (TransformerException e) {
61
			throw new OpenXML4JException(e.getLocalizedMessage(), e);
60
		}
62
		}
61
		return true;
62
	}
63
	}
63
}
64
}
(-)src/ooxml/java/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.java (-16 / +22 lines)
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
}
(-)src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java (-1 / +1 lines)
Lines 132-138 Link Here
132
            // FIXME: A better exception class (IOException?) and message should be raised
132
            // FIXME: A better exception class (IOException?) and message should be raised
133
            // indicating that the document could not be written because the output stream is closed.
133
            // indicating that the document could not be written because the output stream is closed.
134
            // see {@link org.apache.poi.openxml4j.opc.ZipPackage#saveImpl(java.io.OutputStream)}
134
            // see {@link org.apache.poi.openxml4j.opc.ZipPackage#saveImpl(java.io.OutputStream)}
135
            if (e.getMessage().matches("Fail to save: an error occurs while saving the package : The part .+ fail to be saved in the stream with marshaller .+")) {
135
            if (e.getMessage().contains("Stream Closed")) {
136
                // expected
136
                // expected
137
            } else {
137
            } else {
138
                throw e;
138
                throw e;
(-)src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java (-2 / +4 lines)
Lines 44-49 Link Here
44
import java.util.zip.ZipFile;
44
import java.util.zip.ZipFile;
45
import java.util.zip.ZipOutputStream;
45
import java.util.zip.ZipOutputStream;
46
46
47
import javax.xml.transform.TransformerException;
48
47
import org.apache.poi.EncryptedDocumentException;
49
import org.apache.poi.EncryptedDocumentException;
48
import org.apache.poi.POIDataSamples;
50
import org.apache.poi.POIDataSamples;
49
import org.apache.poi.POITestCase;
51
import org.apache.poi.POITestCase;
Lines 148-154 Link Here
148
	 * Test package creation.
150
	 * Test package creation.
149
	 */
151
	 */
150
    @Test
152
    @Test
151
	public void createPackageAddPart() throws IOException, InvalidFormatException {
153
	public void createPackageAddPart() throws IOException, InvalidFormatException, TransformerException {
152
		File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx");
154
		File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestCreatePackageTMP.docx");
153
155
154
		File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestCreatePackageOUTPUT.docx");
156
		File expectedFile = OpenXML4JTestDataSamples.getSampleFile("TestCreatePackageOUTPUT.docx");
Lines 298-304 Link Here
298
	 * Test package opening.
300
	 * Test package opening.
299
	 */
301
	 */
300
    @Test
302
    @Test
301
	public void openPackage() throws IOException, InvalidFormatException {
303
	public void openPackage() throws IOException, InvalidFormatException, TransformerException {
302
		File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestOpenPackageTMP.docx");
304
		File targetFile = OpenXML4JTestDataSamples.getOutputFile("TestOpenPackageTMP.docx");
303
305
304
		File inputFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageINPUT.docx");
306
		File inputFile = OpenXML4JTestDataSamples.getSampleFile("TestOpenPackageINPUT.docx");
(-)src/ooxml/testcases/org/apache/poi/openxml4j/opc/internal/TestZipPackagePropertiesMarshaller.java (-1 / +2 lines)
Lines 18-23 Link Here
18
package org.apache.poi.openxml4j.opc.internal;
18
package org.apache.poi.openxml4j.opc.internal;
19
19
20
import java.io.ByteArrayOutputStream;
20
import java.io.ByteArrayOutputStream;
21
import java.io.IOException;
21
import java.io.OutputStream;
22
import java.io.OutputStream;
22
23
23
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
24
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
Lines 27-33 Link Here
27
public class TestZipPackagePropertiesMarshaller {
28
public class TestZipPackagePropertiesMarshaller {
28
29
29
    @Test(expected=IllegalArgumentException.class)
30
    @Test(expected=IllegalArgumentException.class)
30
    public void nonZipOutputStream() throws OpenXML4JException {
31
    public void nonZipOutputStream() throws OpenXML4JException, IOException {
31
        PartMarshaller marshaller = new ZipPackagePropertiesMarshaller();
32
        PartMarshaller marshaller = new ZipPackagePropertiesMarshaller();
32
        OutputStream notAZipOutputStream = new ByteArrayOutputStream(0);
33
        OutputStream notAZipOutputStream = new ByteArrayOutputStream(0);
33
        marshaller.marshall(null, notAZipOutputStream);
34
        marshaller.marshall(null, notAZipOutputStream);

Return to bug 60102