-
Notifications
You must be signed in to change notification settings - Fork 16
/
EfficientStreamingTransformer.java
441 lines (388 loc) · 14.9 KB
/
EfficientStreamingTransformer.java
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/*
* EfficientStreamingTransformer.java
*
* Created on July 29, 2002, 3:49 PM
*/
package com.sun.xml.messaging.saaj.util.transform;
import java.io.*;
import java.net.URISyntaxException;
import javax.xml.XMLConstants;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import com.sun.xml.messaging.saaj.util.LogDomainConstants;
import com.sun.xml.messaging.saaj.util.SAAJUtil;
import org.w3c.dom.Document;
import com.sun.xml.messaging.saaj.util.XMLDeclarationParser;
import com.sun.xml.messaging.saaj.util.FastInfosetReflection;
import java.net.URI;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
/**
* This class is a proxy for a Transformer object with optimizations
* for certain cases. If source and result are of type stream, then
* bytes are simply copied whenever possible (note that this assumes
* that the input is well formed). In addition, it provides support for
* FI using native DOM parsers and serializers.
*
* @author Panos Kougiouris panos@acm.org
* @author Santiago.PericasGeertsen@sun.com
*
*/
public class EfficientStreamingTransformer extends Transformer {
private static final Logger LOGGER = Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, "com.sun.xml.messaging.saaj.soap.LocalStrings");
private static final AtomicBoolean LOG = new AtomicBoolean(true);
//static final String version;
//static final String vendor;
// removing static : security issue : CR 6813167Z
private final TransformerFactory transformerFactory;
/**
* Underlying XSLT transformer.
*/
private Transformer m_realTransformer = null;
/**
* Undelying FI DOM parser.
*/
private Object m_fiDOMDocumentParser = null;
/**
* Underlying FI DOM serializer.
*/
private Object m_fiDOMDocumentSerializer = null;
private EfficientStreamingTransformer() {
boolean log = LOG.compareAndSet(true, false);
boolean useDefaultTransformerFactory = SAAJUtil.getSystemBoolean("saaj.use.default.transformer.factory");
TransformerFactory tf = useDefaultTransformerFactory ? TransformerFactory.newDefaultInstance() : TransformerFactory.newInstance();
try {
tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (TransformerConfigurationException e) {
if (log) {
LOGGER.log(Level.WARNING, "Factory [{0}] doesn't support secure xml processing!", new Object[]{tf.getClass().getName()});
}
}
//ie xalan, as of 2.7.2, does not support these
try {
tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
} catch (IllegalArgumentException e) {
if (log) {
LOGGER.log(Level.FINE, "Factory [{0}] doesn't support accessExternalDTD property", new Object[]{tf.getClass().getName()});
}
}
try {
tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
} catch (IllegalArgumentException e) {
if (log) {
LOGGER.log(Level.FINE, "Factory [{0}] doesn't support accessExternalStylesheet property", new Object[]{tf.getClass().getName()});
}
}
transformerFactory = tf;
}
private void materialize() throws TransformerException {
if (m_realTransformer == null) {
m_realTransformer = transformerFactory.newTransformer();
}
}
@Override
public void clearParameters() {
if (m_realTransformer != null)
m_realTransformer.clearParameters();
}
@Override
public javax.xml.transform.ErrorListener getErrorListener() {
try {
materialize();
return m_realTransformer.getErrorListener();
} catch (TransformerException e) {
// will be caught later
}
return null;
}
@Override
public java.util.Properties getOutputProperties() {
try {
materialize();
return m_realTransformer.getOutputProperties();
} catch (TransformerException e) {
// will be caught later
}
return null;
}
@Override
public String getOutputProperty(String str)
throws java.lang.IllegalArgumentException {
try {
materialize();
return m_realTransformer.getOutputProperty(str);
} catch (TransformerException e) {
// will be caught later
}
return null;
}
@Override
public Object getParameter(String str) {
try {
materialize();
return m_realTransformer.getParameter(str);
} catch (TransformerException e) {
// will be caught later
}
return null;
}
@Override
public javax.xml.transform.URIResolver getURIResolver() {
try {
materialize();
return m_realTransformer.getURIResolver();
} catch (TransformerException e) {
// will be caught later
}
return null;
}
@Override
public void setErrorListener(
javax.xml.transform.ErrorListener errorListener)
throws java.lang.IllegalArgumentException {
try {
materialize();
m_realTransformer.setErrorListener(errorListener);
} catch (TransformerException e) {
// will be caught later
}
}
@Override
public void setOutputProperties(java.util.Properties properties)
throws java.lang.IllegalArgumentException {
try {
materialize();
m_realTransformer.setOutputProperties(properties);
} catch (TransformerException e) {
// will be caught later
}
}
@Override
public void setOutputProperty(String str, String str1)
throws java.lang.IllegalArgumentException {
try {
materialize();
m_realTransformer.setOutputProperty(str, str1);
} catch (TransformerException e) {
// will be caught later
}
}
@Override
public void setParameter(String str, Object obj) {
try {
materialize();
m_realTransformer.setParameter(str, obj);
} catch (TransformerException e) {
// will be caught later
}
}
@Override
public void setURIResolver(javax.xml.transform.URIResolver uRIResolver) {
try {
materialize();
m_realTransformer.setURIResolver(uRIResolver);
} catch (TransformerException e) {
// will be caught later
}
}
private InputStream getInputStreamFromSource(StreamSource s)
throws TransformerException {
InputStream stream = s.getInputStream();
if (stream != null)
return stream;
if (s.getReader() != null)
return null;
String systemId = s.getSystemId();
if (systemId != null) {
try {
String fileURL = systemId;
if (systemId.startsWith("file:///"))
{
/*
systemId is:
file:///<drive>:/some/path/file.xml
or
file:///some/path/file.xml
*/
String absolutePath = systemId.substring(7);
/*
/<drive>:/some/path/file.xml
or
/some/path/file.xml
*/
boolean hasDriveDesignator = absolutePath.indexOf(':') > 0;
if (hasDriveDesignator) {
String driveDesignatedPath = absolutePath.substring(1);
/*
<drive>:/some/path/file.xml */
fileURL = driveDesignatedPath;
}
else {
/*
/some/path/file.xml */
fileURL = absolutePath;
}
}
//return new FileInputStream(fileURL);
try {
return new FileInputStream(new File(new URI(fileURL)));
} catch (URISyntaxException ex) {
throw new TransformerException(ex);
}
} catch (IOException e) {
throw new TransformerException(e.toString());
}
}
throw new TransformerException("Unexpected StreamSource object");
}
//------------------------------------------------------------------------
@Override
public void transform(
javax.xml.transform.Source source,
javax.xml.transform.Result result)
throws javax.xml.transform.TransformerException
{
// StreamSource -> StreamResult
if ((source instanceof StreamSource)
&& (result instanceof StreamResult)) {
try {
StreamSource streamSource = (StreamSource) source;
InputStream is = getInputStreamFromSource(streamSource);
OutputStream os = ((StreamResult) result).getOutputStream();
if (os == null)
// TODO: We might want to fix this if it were to be used beyond
// XmlDataContentHandler that we know uses only OutputStream
throw new TransformerException("Unexpected StreamResult object contains null OutputStream");
if (is != null) {
if (is.markSupported())
is.mark(Integer.MAX_VALUE);
int num;
byte[] b = new byte[8192];
while ((num = is.read(b)) != -1) {
os.write(b, 0, num);
}
if (is.markSupported())
is.reset();
return;
}
Reader reader = streamSource.getReader();
if (reader != null) {
if (reader.markSupported())
reader.mark(Integer.MAX_VALUE);
PushbackReader pushbackReader = new PushbackReader(reader, 4096);
//some size to unread <?xml ....?>
XMLDeclarationParser ev =
new XMLDeclarationParser(pushbackReader);
try {
ev.parse();
} catch (Exception ex) {
throw new TransformerException(
"Unable to run the JAXP transformer on a stream "
+ ex.getMessage());
}
Writer writer =
new OutputStreamWriter(os /*, ev.getEncoding()*/);
ev.writeTo(writer); // doesn't write any, if no header
int num;
char[] ac = new char[8192];
while ((num = pushbackReader.read(ac)) != -1) {
writer.write(ac, 0, num);
}
writer.flush();
if (reader.markSupported())
reader.reset();
return;
}
} catch (IOException e) {
e.printStackTrace();
throw new TransformerException(e.toString());
}
throw new TransformerException("Unexpected StreamSource object");
}
// FastInfosetSource -> DOMResult
else if (FastInfosetReflection.isFastInfosetSource(source)
&& (result instanceof DOMResult))
{
try {
// Use reflection to avoid a static dep with FI
if (m_fiDOMDocumentParser == null) {
m_fiDOMDocumentParser = FastInfosetReflection.DOMDocumentParser_new();
}
// m_fiDOMDocumentParser.parse(document, source.getInputStream())
FastInfosetReflection.DOMDocumentParser_parse(
m_fiDOMDocumentParser,
(Document) ((DOMResult) result).getNode(),
FastInfosetReflection.FastInfosetSource_getInputStream(source));
// We're done!
return;
}
catch (Exception e) {
throw new TransformerException(e);
}
}
// DOMSource -> FastInfosetResult
else if ((source instanceof DOMSource)
&& FastInfosetReflection.isFastInfosetResult(result))
{
try {
// Use reflection to avoid a static dep with FI
if (m_fiDOMDocumentSerializer == null) {
m_fiDOMDocumentSerializer = FastInfosetReflection.DOMDocumentSerializer_new();
}
// m_fiDOMDocumentSerializer.setOutputStream(result.getOutputStream())
FastInfosetReflection.DOMDocumentSerializer_setOutputStream(
m_fiDOMDocumentSerializer,
FastInfosetReflection.FastInfosetResult_getOutputStream(result));
// m_fiDOMDocumentSerializer.serialize(node)
FastInfosetReflection.DOMDocumentSerializer_serialize(
m_fiDOMDocumentSerializer,
((DOMSource) source).getNode());
// We're done!
return;
}
catch (Exception e) {
throw new TransformerException(e);
}
}
// All other cases -- use transformer object
materialize();
m_realTransformer.transform(source, result);
}
/*
* Threadlocal to hold a Transformer instance for this thread.
* CR : 6813167
*/
//private static ThreadLocal effTransformer = new ThreadLocal();
/**
* Return Transformer instance for this thread, allocating a new one if
* necessary. Note that this method does not clear global parameters,
* properties or any other data set on a previously used transformer.
*
* @return Transformer instance
*/
public static Transformer newTransformer() {
//CR : 6813167
/*Transformer tt = (Transformer) effTransformer.get();
if (tt == null) {
effTransformer.set(tt = new EfficientStreamingTransformer());
}
return tt;*/
return new EfficientStreamingTransformer();
}
}