Skip to content

Commit ab782ee

Browse files
author
s_zhangziang
committed
Added an interface for saving xmlobject objects to zip
1 parent 1abd8e1 commit ab782ee

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/main/java/org/apache/xmlbeans/FilterXmlObject.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Calendar;
2929
import java.util.Date;
3030
import java.util.List;
31+
import java.util.zip.ZipOutputStream;
3132

3233
/**
3334
* A FilterXmlObject delegates to some other XmlObject, which it can use as
@@ -199,6 +200,10 @@ public void save(OutputStream os, XmlOptions options) throws IOException {
199200
underlyingXmlObject().save(os, options);
200201
}
201202

203+
public void save(ZipOutputStream zos, XmlOptions options) throws IOException {
204+
underlyingXmlObject().save(zos, options);
205+
}
206+
202207
public void save(Writer w, XmlOptions options) throws IOException {
203208
underlyingXmlObject().save(w, options);
204209
}

src/main/java/org/apache/xmlbeans/XmlTokenSource.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import javax.xml.stream.XMLStreamReader;
2424
import java.io.*;
25+
import java.util.zip.ZipOutputStream;
2526

2627
/**
2728
* Represents a holder of XML that can return an {@link XmlCursor}
@@ -170,6 +171,14 @@ public interface XmlTokenSource {
170171
*/
171172
void save(OutputStream os) throws IOException;
172173

174+
/**
175+
* Writes the XML represented by this source to the given zip output stream.
176+
* This method will save the XML declaration, including encoding information,
177+
* with the XML.
178+
*/
179+
void save(ZipOutputStream os) throws IOException;
180+
181+
173182
/**
174183
* Writes the XML represented by this source to the given output.
175184
* Note that this method does not save the XML declaration, including the encoding information.
@@ -336,6 +345,13 @@ public interface XmlTokenSource {
336345
*/
337346
void save(OutputStream os, XmlOptions options) throws IOException;
338347

348+
/**
349+
* Writes the XML represented by this source to the given zip output stream.
350+
* This method will save the XML declaration, including encoding information,
351+
* with the XML.
352+
*/
353+
void save(ZipOutputStream os, XmlOptions options) throws IOException;
354+
339355
/**
340356
* Writes the XML represented by this source to the given output.
341357
* Note that this method does not save the XML declaration, including the encoding information.

src/main/java/org/apache/xmlbeans/impl/store/Cursor.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.Collection;
3838
import java.util.Map;
3939
import java.util.function.Supplier;
40+
import java.util.zip.ZipOutputStream;
4041

4142
public final class Cursor implements XmlCursor, ChangeListener {
4243
static final int ROOT = Cur.ROOT;
@@ -510,6 +511,10 @@ public void _save(OutputStream os) throws IOException {
510511
_save(os, null);
511512
}
512513

514+
public void _save(ZipOutputStream zos) throws IOException {
515+
_save(zos, null);
516+
}
517+
513518
public void _save(Writer w) throws IOException {
514519
_save(w, null);
515520
}
@@ -579,6 +584,26 @@ public void _save(OutputStream os, XmlOptions options) throws IOException {
579584
}
580585
}
581586

587+
public void _save(ZipOutputStream zos, XmlOptions options) throws IOException {
588+
if (zos == null) {
589+
throw new IllegalArgumentException("Null ZipOutputStream specified");
590+
}
591+
592+
try (InputStream is = _newInputStream(options)) {
593+
byte[] bytes = new byte[8192];
594+
595+
for (; ; ) {
596+
int n = is.read(bytes);
597+
598+
if (n < 0) {
599+
break;
600+
}
601+
602+
zos.write(bytes, 0, n);
603+
}
604+
}
605+
}
606+
582607
public void _save(Writer w, XmlOptions options) throws IOException {
583608
if (w == null) {
584609
throw new IllegalArgumentException("Null Writer specified");
@@ -1975,6 +2000,10 @@ public void save(OutputStream os) throws IOException {
19752000
syncWrapIOEx(() -> _save(os));
19762001
}
19772002

2003+
public void save(ZipOutputStream zos) throws IOException {
2004+
syncWrapIOEx(() -> _save(zos));
2005+
}
2006+
19782007
public void save(Writer w) throws IOException {
19792008
syncWrapIOEx(() -> _save(w));
19802009
}
@@ -2007,6 +2036,10 @@ public void save(OutputStream os, XmlOptions options) throws IOException {
20072036
syncWrapIOEx(() -> _save(os, options));
20082037
}
20092038

2039+
public void save(ZipOutputStream zos, XmlOptions options) throws IOException {
2040+
syncWrapIOEx(() -> _save(zos, options));
2041+
}
2042+
20102043
public void save(Writer w, XmlOptions options) throws IOException {
20112044
syncWrapIOEx(() -> _save(w, options));
20122045
}

src/main/java/org/apache/xmlbeans/impl/values/XmlObjectBase.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.*;
3636
import java.util.function.Function;
3737
import java.util.function.IntFunction;
38+
import java.util.zip.ZipOutputStream;
3839

3940
public abstract class XmlObjectBase implements TypeStoreUser, Serializable, XmlObject, SimpleValue {
4041
public static final short MAJOR_VERSION_NUMBER = (short) 1; // for serialization
@@ -191,6 +192,12 @@ public void save(OutputStream os, XmlOptions options) throws IOException {
191192
}
192193
}
193194

195+
public void save(ZipOutputStream zos, XmlOptions options) throws IOException {
196+
try (XmlCursor cur = newCursorForce()) {
197+
cur.save(zos, makeInnerOptions(options));
198+
}
199+
}
200+
194201
public void save(Writer w, XmlOptions options) throws IOException {
195202
try (XmlCursor cur = newCursorForce()) {
196203
cur.save(w, makeInnerOptions(options));
@@ -209,6 +216,10 @@ public void save(OutputStream os) throws IOException {
209216
save(os, null);
210217
}
211218

219+
public void save(ZipOutputStream zos) throws IOException {
220+
save(zos, null);
221+
}
222+
212223
public void save(Writer w) throws IOException {
213224
save(w, null);
214225
}

0 commit comments

Comments
 (0)