Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/java/beans/XMLDecoder.java
41152 views
1
/*
2
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package java.beans;
26
27
import com.sun.beans.decoder.DocumentHandler;
28
29
import java.io.Closeable;
30
import java.io.InputStream;
31
import java.io.IOException;
32
import java.security.AccessControlContext;
33
import java.security.AccessController;
34
import java.security.PrivilegedAction;
35
36
import org.xml.sax.InputSource;
37
import org.xml.sax.helpers.DefaultHandler;
38
39
/**
40
* The {@code XMLDecoder} class is used to read XML documents
41
* created using the {@code XMLEncoder} and is used just like
42
* the {@code ObjectInputStream}. For example, one can use
43
* the following fragment to read the first object defined
44
* in an XML document written by the {@code XMLEncoder}
45
* class:
46
* <pre>
47
* XMLDecoder d = new XMLDecoder(
48
* new BufferedInputStream(
49
* new FileInputStream("Test.xml")));
50
* Object result = d.readObject();
51
* d.close();
52
* </pre>
53
*
54
*<p>
55
* For more information you might also want to check out
56
* <a href="http://www.oracle.com/technetwork/java/persistence3-139471.html">
57
* Long Term Persistence of JavaBeans Components: XML Schema</a>,
58
* an article in <em>The Swing Connection.</em>
59
* @see XMLEncoder
60
* @see java.io.ObjectInputStream
61
*
62
* @since 1.4
63
*
64
* @author Philip Milne
65
*/
66
public class XMLDecoder implements AutoCloseable {
67
@SuppressWarnings("removal")
68
private final AccessControlContext acc = AccessController.getContext();
69
private final DocumentHandler handler = new DocumentHandler();
70
private final InputSource input;
71
private Object owner;
72
private Object[] array;
73
private int index;
74
75
/**
76
* Creates a new input stream for reading archives
77
* created by the {@code XMLEncoder} class.
78
*
79
* @param in The underlying stream.
80
*
81
* @see XMLEncoder#XMLEncoder(java.io.OutputStream)
82
*/
83
public XMLDecoder(InputStream in) {
84
this(in, null);
85
}
86
87
/**
88
* Creates a new input stream for reading archives
89
* created by the {@code XMLEncoder} class.
90
*
91
* @param in The underlying stream.
92
* @param owner The owner of this stream.
93
*
94
*/
95
public XMLDecoder(InputStream in, Object owner) {
96
this(in, owner, null);
97
}
98
99
/**
100
* Creates a new input stream for reading archives
101
* created by the {@code XMLEncoder} class.
102
*
103
* @param in the underlying stream.
104
* @param owner the owner of this stream.
105
* @param exceptionListener the exception handler for the stream;
106
* if {@code null} the default exception listener will be used.
107
*/
108
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener) {
109
this(in, owner, exceptionListener, null);
110
}
111
112
/**
113
* Creates a new input stream for reading archives
114
* created by the {@code XMLEncoder} class.
115
*
116
* @param in the underlying stream. {@code null} may be passed without
117
* error, though the resulting XMLDecoder will be useless
118
* @param owner the owner of this stream. {@code null} is a legal
119
* value
120
* @param exceptionListener the exception handler for the stream, or
121
* {@code null} to use the default
122
* @param cl the class loader used for instantiating objects.
123
* {@code null} indicates that the default class loader should
124
* be used
125
* @since 1.5
126
*/
127
public XMLDecoder(InputStream in, Object owner,
128
ExceptionListener exceptionListener, ClassLoader cl) {
129
this(new InputSource(in), owner, exceptionListener, cl);
130
}
131
132
133
/**
134
* Creates a new decoder to parse XML archives
135
* created by the {@code XMLEncoder} class.
136
* If the input source {@code is} is {@code null},
137
* no exception is thrown and no parsing is performed.
138
* This behavior is similar to behavior of other constructors
139
* that use {@code InputStream} as a parameter.
140
*
141
* @param is the input source to parse
142
*
143
* @since 1.7
144
*/
145
public XMLDecoder(InputSource is) {
146
this(is, null, null, null);
147
}
148
149
/**
150
* Creates a new decoder to parse XML archives
151
* created by the {@code XMLEncoder} class.
152
*
153
* @param is the input source to parse
154
* @param owner the owner of this decoder
155
* @param el the exception handler for the parser,
156
* or {@code null} to use the default exception handler
157
* @param cl the class loader used for instantiating objects,
158
* or {@code null} to use the default class loader
159
*
160
* @since 1.7
161
*/
162
private XMLDecoder(InputSource is, Object owner, ExceptionListener el, ClassLoader cl) {
163
this.input = is;
164
this.owner = owner;
165
setExceptionListener(el);
166
this.handler.setClassLoader(cl);
167
this.handler.setOwner(this);
168
}
169
170
/**
171
* This method closes the input stream associated
172
* with this stream.
173
*/
174
public void close() {
175
if (parsingComplete()) {
176
close(this.input.getCharacterStream());
177
close(this.input.getByteStream());
178
}
179
}
180
181
private void close(Closeable in) {
182
if (in != null) {
183
try {
184
in.close();
185
}
186
catch (IOException e) {
187
getExceptionListener().exceptionThrown(e);
188
}
189
}
190
}
191
192
@SuppressWarnings("removal")
193
private boolean parsingComplete() {
194
if (this.input == null) {
195
return false;
196
}
197
if (this.array == null) {
198
if ((this.acc == null) && (null != System.getSecurityManager())) {
199
throw new SecurityException("AccessControlContext is not set");
200
}
201
AccessController.doPrivileged(new PrivilegedAction<Void>() {
202
public Void run() {
203
XMLDecoder.this.handler.parse(XMLDecoder.this.input);
204
return null;
205
}
206
}, this.acc);
207
this.array = this.handler.getObjects();
208
}
209
return true;
210
}
211
212
/**
213
* Sets the exception handler for this stream to {@code exceptionListener}.
214
* The exception handler is notified when this stream catches recoverable
215
* exceptions.
216
*
217
* @param exceptionListener The exception handler for this stream;
218
* if {@code null} the default exception listener will be used.
219
*
220
* @see #getExceptionListener
221
*/
222
public void setExceptionListener(ExceptionListener exceptionListener) {
223
if (exceptionListener == null) {
224
exceptionListener = Statement.defaultExceptionListener;
225
}
226
this.handler.setExceptionListener(exceptionListener);
227
}
228
229
/**
230
* Gets the exception handler for this stream.
231
*
232
* @return The exception handler for this stream.
233
* Will return the default exception listener if this has not explicitly been set.
234
*
235
* @see #setExceptionListener
236
*/
237
public ExceptionListener getExceptionListener() {
238
return this.handler.getExceptionListener();
239
}
240
241
/**
242
* Reads the next object from the underlying input stream.
243
*
244
* @return the next object read
245
*
246
* @throws ArrayIndexOutOfBoundsException if the stream contains no objects
247
* (or no more objects)
248
*
249
* @see XMLEncoder#writeObject
250
*/
251
public Object readObject() {
252
return (parsingComplete())
253
? this.array[this.index++]
254
: null;
255
}
256
257
/**
258
* Sets the owner of this decoder to {@code owner}.
259
*
260
* @param owner The owner of this decoder.
261
*
262
* @see #getOwner
263
*/
264
public void setOwner(Object owner) {
265
this.owner = owner;
266
}
267
268
/**
269
* Gets the owner of this decoder.
270
*
271
* @return The owner of this decoder.
272
*
273
* @see #setOwner
274
*/
275
public Object getOwner() {
276
return owner;
277
}
278
279
/**
280
* Creates a new handler for SAX parser
281
* that can be used to parse embedded XML archives
282
* created by the {@code XMLEncoder} class.
283
*
284
* The {@code owner} should be used if parsed XML document contains
285
* the method call within context of the &lt;java&gt; element.
286
* The {@code null} value may cause illegal parsing in such case.
287
* The same problem may occur, if the {@code owner} class
288
* does not contain expected method to call. See details <a
289
* href="http://www.oracle.com/technetwork/java/persistence3-139471.html">
290
* here</a>.
291
*
292
* @param owner the owner of the default handler
293
* that can be used as a value of &lt;java&gt; element
294
* @param el the exception handler for the parser,
295
* or {@code null} to use the default exception handler
296
* @param cl the class loader used for instantiating objects,
297
* or {@code null} to use the default class loader
298
* @return an instance of {@code DefaultHandler} for SAX parser
299
*
300
* @since 1.7
301
*/
302
public static DefaultHandler createHandler(Object owner, ExceptionListener el, ClassLoader cl) {
303
DocumentHandler handler = new DocumentHandler();
304
handler.setOwner(owner);
305
handler.setExceptionListener(el);
306
handler.setClassLoader(cl);
307
return handler;
308
}
309
}
310
311