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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
/* SAXNullTransformerFactory.java --
Copyright (C) 2001 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath 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 for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.xml.util;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Hashtable;
import java.util.Properties;
import gnu.xml.dom.Consumer;
import gnu.xml.dom.DomDocument;
import gnu.xml.pipeline.DomConsumer;
import gnu.xml.pipeline.EventFilter;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.LocatorImpl;
/**
* Implements null transforms. XSLT stylesheets are not supported.
* This class provides a way to translate three representations of
* XML data (SAX event stream, DOM tree, and XML text) into each other.
* In essence it's a thinnish wrapper around basic SAX event
* <a href="../pipeline/package-summary.html">pipeline</a> facilities, which
* exposes only limited functionality. The <em>javax.xml.transform</em>
* functionality is implemented as follows: <ul>
*
* <li>The {@link javax.xml.transform.sax.SAXSource SAXSource} class
* just wraps an {@link XMLReader} and {@link InputSource}, while the
* {@link javax.xml.transform.sax.SAXResult SAXResult} class is less
* functional than a {@link gnu.xml.pipeline.EventConsumer EventConsumer}.
* (Notably, it drops all but one declaration from any DTD.)</li>
*
* <li>The {@link javax.xml.transform.dom.DOMSource DOMSource} class
* corresponds to special SAX parsers like {@link DomParser}, and the
* {@link javax.xml.transform.dom.DOMResult DOMResult} class corresponds
* to a {@link gnu.xml.pipeline.DomConsumer DomConsumer}.</li>
*
* <li>The {@link javax.xml.transform.stream.StreamSource StreamSource}
* class corresponds to a SAX {@link InputSource}, and the
* {@link javax.xml.transform.stream.StreamResult StreamResult} class
* corresponds to a {@link gnu.xml.pipeline.TextConsumer TextConsumer}.</li>
*
* </ul>
*
* <p><em>This implementation is preliminary.</em>
*
* @see gnu.xml.pipeline.XsltFilter
*
* @author David Brownell
*/
public class SAXNullTransformerFactory extends SAXTransformerFactory
{
private ErrorListener errListener;
private URIResolver uriResolver;
/** Default constructor */
public SAXNullTransformerFactory () { }
//
// only has stuff that makes sense with null transforms
//
/**
* Returns true if the requested feature is supported.
* All three kinds of input and output are accepted:
* XML text, SAX events, and DOM nodes.
*/
public boolean getFeature (String feature)
{
return SAXTransformerFactory.FEATURE.equals (feature)
|| SAXResult.FEATURE.equals (feature)
|| SAXSource.FEATURE.equals (feature)
|| DOMResult.FEATURE.equals (feature)
|| DOMSource.FEATURE.equals (feature)
|| StreamResult.FEATURE.equals (feature)
|| StreamSource.FEATURE.equals (feature)
;
}
public void setFeature(String name, boolean value)
throws TransformerConfigurationException
{
throw new TransformerConfigurationException(name);
}
/** Throws an exception (no implementation attributes are supported) */
public void setAttribute (String key, Object value)
{
throw new IllegalArgumentException ();
}
/** Throws an exception (no implementation attributes are supported) */
public Object getAttribute (String key)
{
throw new IllegalArgumentException ();
}
/** (not yet implemented) */
public Source getAssociatedStylesheet (Source source,
String media,
String title,
String charset)
throws TransformerConfigurationException
{
// parse, and find the appropriate xsl-stylesheet PI contents
throw new IllegalArgumentException ();
}
public Transformer newTransformer ()
throws TransformerConfigurationException
{
return new NullTransformer ();
}
/**
* Returns a TransformerHandler that knows how to generate output
* in all three standard formats. Output text is generated using
* {@link XMLWriter}, and the GNU implementation of
* {@link DomDocument DOM} is used.
*
* @see SAXResult
* @see StreamResult
* @see DOMResult
*/
public TransformerHandler newTransformerHandler ()
throws TransformerConfigurationException
{
NullTransformer transformer = new NullTransformer ();
return transformer.handler;
}
//
// Stuff that depends on XSLT support, which we don't provide
//
private static final String noXSLT = "No XSLT support";
/** Throws an exception (XSLT is not supported). */
public Transformer newTransformer (Source stylesheet)
throws TransformerConfigurationException
{
throw new TransformerConfigurationException (noXSLT);
}
/** Throws an exception (XSLT is not supported). */
public Templates newTemplates (Source stylesheet)
throws TransformerConfigurationException
{
throw new TransformerConfigurationException (noXSLT);
}
/** Throws an exception (XSLT is not supported). */
public TemplatesHandler newTemplatesHandler ()
throws TransformerConfigurationException
{
throw new TransformerConfigurationException (noXSLT);
}
/** Throws an exception (XSLT is not supported). */
public TransformerHandler newTransformerHandler (Source stylesheet)
throws TransformerConfigurationException
{
throw new TransformerConfigurationException (noXSLT);
}
/** Throws an exception (XSLT is not supported). */
public TransformerHandler newTransformerHandler (Templates stylesheet)
throws TransformerConfigurationException
{
throw new TransformerConfigurationException (noXSLT);
}
/** Throws an exception (XSLT is not supported). */
public XMLFilter newXMLFilter (Source stylesheet)
throws TransformerConfigurationException
{
throw new TransformerConfigurationException (noXSLT);
}
/** Throws an exception (XSLT is not supported). */
public XMLFilter newXMLFilter (Templates stylesheet)
throws TransformerConfigurationException
{
throw new TransformerConfigurationException (noXSLT);
}
/** Returns the value assigned by {@link #setErrorListener}. */
public ErrorListener getErrorListener ()
{
return errListener;
}
/** Assigns a value that would be used when parsing stylesheets */
public void setErrorListener (ErrorListener e)
{
errListener = e;
}
/** Returns the value assigned by {@link #setURIResolver}. */
public URIResolver getURIResolver ()
{
return uriResolver;
}
/** Assigns a value that would be used when parsing stylesheets */
public void setURIResolver (URIResolver u)
{
uriResolver = u;
}
//
// Helper classes. These might in theory be subclassed
// by an XSLT implementation, if they were exported.
//
static class DomTerminus
extends DomConsumer
{
DomTerminus (DOMResult result)
throws SAXException
{
// won't really throw SAXException
super (DomDocument.class);
setHandler (new DomHandler (this, result));
}
}
static class DomHandler
extends Consumer.Backdoor
{
private DOMResult result;
DomHandler (DomConsumer c, DOMResult r)
throws SAXException
{
// won't really throw SAXException
super (c);
result = r;
}
public void endDocument ()
throws SAXException
{
super.endDocument ();
result.setNode (getDocument ());
}
}
private static OutputStream getOutputStream (String uri)
throws IOException
{
// JDK stupidity: file "protocol does not support output" ...
if (uri.startsWith ("file:"))
return new FileOutputStream (uri.substring (5));
// Otherwise ...
URL url = new URL (uri);
URLConnection conn = url.openConnection ();
conn.setDoOutput (true);
return conn.getOutputStream ();
}
static class NullHandler
extends EventFilter
implements TransformerHandler
{
private String systemId;
private Transformer transformer;
NullHandler (Transformer t)
{
transformer = t;
}
public Transformer getTransformer ()
{
return transformer;
}
public String getSystemId ()
{
return systemId;
}
public void setSystemId (String id)
{
systemId = id;
}
public void setResult (Result result)
{
if (result.getSystemId () != null)
systemId = result.getSystemId ();
try
{
// output to partial SAX event stream?
if (result instanceof SAXResult)
{
SAXResult r = (SAXResult) result;
setContentHandler (r.getHandler ());
setProperty (LEXICAL_HANDLER, r.getLexicalHandler ());
// DTD info is filtered out by javax.transform
// output to DOM tree?
}
else if (result instanceof DOMResult)
{
DomTerminus out = new DomTerminus ((DOMResult) result);
setContentHandler (out.getContentHandler ());
setProperty (LEXICAL_HANDLER,
out.getProperty (LEXICAL_HANDLER));
// save DTD-derived info, if any.
setDTDHandler (out.getDTDHandler ());
setProperty (DECL_HANDLER,
out.getProperty (DECL_HANDLER));
// node is saved into result on endDocument()
// output to (XML) text?
}
else if (result instanceof StreamResult)
{
StreamResult r = (StreamResult) result;
XMLWriter out;
// FIXME: when do output properties take effect?
// encoding, standalone decl, xml/xhtml/... ...
// FIXME: maybe put nsfix filter up front
try
{
if (r.getWriter () != null)
out = new XMLWriter (r.getWriter ());
else if (r.getOutputStream () != null)
out = new XMLWriter (r.getOutputStream ());
else if (r.getSystemId () != null)
out = new XMLWriter (
getOutputStream (r.getSystemId ()));
else
throw new IllegalArgumentException (
"bad StreamResult");
}
catch (IOException e)
{
e.printStackTrace ();
// on jdk 1.4, pass the root cause ...
throw new IllegalArgumentException (e.getMessage ());
}
// out.setExpandingEntities (true);
// out.setPrettyPrinting (true);
// out.setXhtml (true);
setContentHandler (out);
setProperty (LEXICAL_HANDLER, out);
// save DTD info, if any; why not?
setDTDHandler (out);
setProperty (DECL_HANDLER, out);
}
}
catch (SAXException e)
{
// SAXNotSupportedException or SAXNotRecognizedException:
// "can't happen" ... but SAXException for DOM build probs
// could happen, so ...
// on jdk 1.4, pass the root cause ...
throw new IllegalArgumentException (e.getMessage ());
}
}
}
// an interface that adds no value
static class LocatorAdapter
extends LocatorImpl
implements SourceLocator
{
LocatorAdapter (SAXParseException e)
{
setSystemId (e.getSystemId ());
setPublicId (e.getPublicId ());
setLineNumber (e.getLineNumber ());
setColumnNumber (e.getColumnNumber ());
}
}
// another interface that adds no value
static class ListenerAdapter
implements ErrorHandler
{
NullTransformer transformer;
ListenerAdapter (NullTransformer t)
{
transformer = t;
}
private TransformerException map (SAXParseException e)
{
return new TransformerException (
e.getMessage (),
new LocatorAdapter (e),
e);
}
public void error (SAXParseException e)
throws SAXParseException
{
try
{
if (transformer.errListener != null)
transformer.errListener.error (map (e));
}
catch (TransformerException ex)
{
transformer.ex = ex;
throw e;
}
}
public void fatalError (SAXParseException e)
throws SAXParseException
{
try
{
if (transformer.errListener != null)
transformer.errListener.fatalError (map (e));
else
throw map (e);
} catch (TransformerException ex) {
transformer.ex = ex;
throw e;
}
}
public void warning (SAXParseException e)
throws SAXParseException
{
try
{
if (transformer.errListener != null)
transformer.errListener.warning (map (e));
}
catch (TransformerException ex)
{
transformer.ex = ex;
throw e;
}
}
}
static class NullTransformer
extends Transformer
{
private URIResolver uriResolver;
private Properties props = new Properties ();
private Hashtable params = new Hashtable (7);
ErrorListener errListener = null;
TransformerException ex = null;
NullHandler handler;
NullTransformer ()
{
super ();
handler = new NullHandler (this);
}
public ErrorListener getErrorListener ()
{
return errListener;
}
public void setErrorListener (ErrorListener e)
{
errListener = e;
}
public URIResolver getURIResolver ()
{
return uriResolver;
}
public void setURIResolver (URIResolver u)
{
uriResolver = u;
}
public void setOutputProperties (Properties p)
{
props = (Properties) p.clone ();
}
public Properties getOutputProperties ()
{
return (Properties) props.clone ();
}
public void setOutputProperty (String name, String value)
{
props.setProperty (name, value);
}
public String getOutputProperty (String name)
{
return props.getProperty (name);
}
public void clearParameters ()
{
params.clear ();
}
public void setParameter (String name, Object value)
{
props.put (name, value);
}
public Object getParameter (String name)
{
return props.get (name);
}
public void transform (Source in, Result out)
throws TransformerException
{
try
{
XMLReader producer;
InputSource input;
// Input from DOM?
if (in instanceof DOMSource)
{
DOMSource source = (DOMSource) in;
if (source.getNode () == null)
throw new IllegalArgumentException ("no DOM node");
producer = new DomParser (source.getNode ());
input = null;
// Input from SAX?
}
else if (in instanceof SAXSource)
{
SAXSource source = (SAXSource) in;
producer = source.getXMLReader ();
if (producer == null)
producer = XMLReaderFactory.createXMLReader ();
input = source.getInputSource ();
if (input == null)
{
if (source.getSystemId () != null)
input = new InputSource (source.getSystemId ());
else
throw new IllegalArgumentException (
"missing SAX input");
}
// Input from a stream or something?
}
else
{
producer = XMLReaderFactory.createXMLReader ();
input = SAXSource.sourceToInputSource (in);
if (input == null)
throw new IllegalArgumentException ("missing input");
}
// preserve original namespace prefixes
try
{
producer.setFeature(EventFilter.FEATURE_URI +
"namespace-prefixes",
true);
}
catch (Exception e)
{
/* ignore */
// FIXME if we couldn't, "NsFix" stage before the output ..
}
// arrange the output
handler.setResult (out);
EventFilter.bind (producer, handler);
// then parse ... single element pipeline
producer.parse (input);
}
catch (IOException e)
{
throw new TransformerException ("transform failed", e);
}
catch (SAXException e)
{
if (ex == null && ex.getCause () == e)
throw ex;
else
throw new TransformerException ("transform failed", e);
}
finally
{
ex = null;
}
}
}
}
|