forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLDecoder.java
More file actions
307 lines (287 loc) · 10.3 KB
/
Copy pathXMLDecoder.java
File metadata and controls
307 lines (287 loc) · 10.3 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.beans;
import com.sun.beans.decoder.DocumentHandler;
import java.io.Closeable;
import java.io.InputStream;
import java.io.IOException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
/**
* The {@code XMLDecoder} class is used to read XML documents
* created using the {@code XMLEncoder} and is used just like
* the {@code ObjectInputStream}. For example, one can use
* the following fragment to read the first object defined
* in an XML document written by the {@code XMLEncoder}
* class:
* <pre>
* XMLDecoder d = new XMLDecoder(
* new BufferedInputStream(
* new FileInputStream("Test.xml")));
* Object result = d.readObject();
* d.close();
* </pre>
*
*<p>
* For more information you might also want to check out
* <a href="http://www.oracle.com/technetwork/java/persistence3-139471.html">
* Long Term Persistence of JavaBeans Components: XML Schema</a>,
* an article in <em>The Swing Connection.</em>
* @see XMLEncoder
* @see java.io.ObjectInputStream
*
* @since 1.4
*
* @author Philip Milne
*/
public class XMLDecoder implements AutoCloseable {
private final AccessControlContext acc = AccessController.getContext();
private final DocumentHandler handler = new DocumentHandler();
private final InputSource input;
private Object owner;
private Object[] array;
private int index;
/**
* Creates a new input stream for reading archives
* created by the {@code XMLEncoder} class.
*
* @param in The underlying stream.
*
* @see XMLEncoder#XMLEncoder(java.io.OutputStream)
*/
public XMLDecoder(InputStream in) {
this(in, null);
}
/**
* Creates a new input stream for reading archives
* created by the {@code XMLEncoder} class.
*
* @param in The underlying stream.
* @param owner The owner of this stream.
*
*/
public XMLDecoder(InputStream in, Object owner) {
this(in, owner, null);
}
/**
* Creates a new input stream for reading archives
* created by the {@code XMLEncoder} class.
*
* @param in the underlying stream.
* @param owner the owner of this stream.
* @param exceptionListener the exception handler for the stream;
* if {@code null} the default exception listener will be used.
*/
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener) {
this(in, owner, exceptionListener, null);
}
/**
* Creates a new input stream for reading archives
* created by the {@code XMLEncoder} class.
*
* @param in the underlying stream. {@code null} may be passed without
* error, though the resulting XMLDecoder will be useless
* @param owner the owner of this stream. {@code null} is a legal
* value
* @param exceptionListener the exception handler for the stream, or
* {@code null} to use the default
* @param cl the class loader used for instantiating objects.
* {@code null} indicates that the default class loader should
* be used
* @since 1.5
*/
public XMLDecoder(InputStream in, Object owner,
ExceptionListener exceptionListener, ClassLoader cl) {
this(new InputSource(in), owner, exceptionListener, cl);
}
/**
* Creates a new decoder to parse XML archives
* created by the {@code XMLEncoder} class.
* If the input source {@code is} is {@code null},
* no exception is thrown and no parsing is performed.
* This behavior is similar to behavior of other constructors
* that use {@code InputStream} as a parameter.
*
* @param is the input source to parse
*
* @since 1.7
*/
public XMLDecoder(InputSource is) {
this(is, null, null, null);
}
/**
* Creates a new decoder to parse XML archives
* created by the {@code XMLEncoder} class.
*
* @param is the input source to parse
* @param owner the owner of this decoder
* @param el the exception handler for the parser,
* or {@code null} to use the default exception handler
* @param cl the class loader used for instantiating objects,
* or {@code null} to use the default class loader
*
* @since 1.7
*/
private XMLDecoder(InputSource is, Object owner, ExceptionListener el, ClassLoader cl) {
this.input = is;
this.owner = owner;
setExceptionListener(el);
this.handler.setClassLoader(cl);
this.handler.setOwner(this);
}
/**
* This method closes the input stream associated
* with this stream.
*/
public void close() {
if (parsingComplete()) {
close(this.input.getCharacterStream());
close(this.input.getByteStream());
}
}
private void close(Closeable in) {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
getExceptionListener().exceptionThrown(e);
}
}
}
private boolean parsingComplete() {
if (this.input == null) {
return false;
}
if (this.array == null) {
if ((this.acc == null) && (null != System.getSecurityManager())) {
throw new SecurityException("AccessControlContext is not set");
}
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
XMLDecoder.this.handler.parse(XMLDecoder.this.input);
return null;
}
}, this.acc);
this.array = this.handler.getObjects();
}
return true;
}
/**
* Sets the exception handler for this stream to {@code exceptionListener}.
* The exception handler is notified when this stream catches recoverable
* exceptions.
*
* @param exceptionListener The exception handler for this stream;
* if {@code null} the default exception listener will be used.
*
* @see #getExceptionListener
*/
public void setExceptionListener(ExceptionListener exceptionListener) {
if (exceptionListener == null) {
exceptionListener = Statement.defaultExceptionListener;
}
this.handler.setExceptionListener(exceptionListener);
}
/**
* Gets the exception handler for this stream.
*
* @return The exception handler for this stream.
* Will return the default exception listener if this has not explicitly been set.
*
* @see #setExceptionListener
*/
public ExceptionListener getExceptionListener() {
return this.handler.getExceptionListener();
}
/**
* Reads the next object from the underlying input stream.
*
* @return the next object read
*
* @throws ArrayIndexOutOfBoundsException if the stream contains no objects
* (or no more objects)
*
* @see XMLEncoder#writeObject
*/
public Object readObject() {
return (parsingComplete())
? this.array[this.index++]
: null;
}
/**
* Sets the owner of this decoder to {@code owner}.
*
* @param owner The owner of this decoder.
*
* @see #getOwner
*/
public void setOwner(Object owner) {
this.owner = owner;
}
/**
* Gets the owner of this decoder.
*
* @return The owner of this decoder.
*
* @see #setOwner
*/
public Object getOwner() {
return owner;
}
/**
* Creates a new handler for SAX parser
* that can be used to parse embedded XML archives
* created by the {@code XMLEncoder} class.
*
* The {@code owner} should be used if parsed XML document contains
* the method call within context of the <java> element.
* The {@code null} value may cause illegal parsing in such case.
* The same problem may occur, if the {@code owner} class
* does not contain expected method to call. See details <a
* href="http://www.oracle.com/technetwork/java/persistence3-139471.html">
* here</a>.
*
* @param owner the owner of the default handler
* that can be used as a value of <java> element
* @param el the exception handler for the parser,
* or {@code null} to use the default exception handler
* @param cl the class loader used for instantiating objects,
* or {@code null} to use the default class loader
* @return an instance of {@code DefaultHandler} for SAX parser
*
* @since 1.7
*/
public static DefaultHandler createHandler(Object owner, ExceptionListener el, ClassLoader cl) {
DocumentHandler handler = new DocumentHandler();
handler.setOwner(owner);
handler.setExceptionListener(el);
handler.setClassLoader(cl);
return handler;
}
}