-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathWstxSAXParser.java
1497 lines (1272 loc) · 44.2 KB
/
WstxSAXParser.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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2004- Tatu Saloranta, [email protected]
*
* Licensed under the License specified in file LICENSE, included with
* the source code.
* You may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ctc.wstx.sax;
import java.io.*;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.parsers.SAXParser;
import javax.xml.stream.Location;
import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.ext.Attributes2;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.ext.Locator2;
//import org.codehaus.stax2.DTDInfo;
import com.ctc.wstx.api.ReaderConfig;
import com.ctc.wstx.dtd.DTDEventListener;
import com.ctc.wstx.exc.WstxIOException;
import com.ctc.wstx.io.DefaultInputResolver;
import com.ctc.wstx.io.InputBootstrapper;
import com.ctc.wstx.io.ReaderBootstrapper;
import com.ctc.wstx.io.StreamBootstrapper;
import com.ctc.wstx.io.SystemId;
import com.ctc.wstx.sr.*;
import com.ctc.wstx.stax.WstxInputFactory;
import com.ctc.wstx.util.ExceptionUtil;
import com.ctc.wstx.util.URLUtil;
/**
* This class implements parser part of JAXP and SAX interfaces; and
* effectively offers an alternative to using Stax input factory /
* stream reader combination.
*/
@SuppressWarnings("deprecation")
public class WstxSAXParser
extends SAXParser
implements Parser // SAX1
,XMLReader // SAX2
,Attributes2 // SAX2
,Locator2 // SAX2
,DTDEventListener // Woodstox-internal
{
final static boolean FEAT_DEFAULT_NS_PREFIXES = false;
/**
* We will need the factory reference mostly for constructing
* underlying stream reader we use.
*/
protected final WstxInputFactory mStaxFactory;
protected final ReaderConfig mConfig;
protected boolean mFeatNsPrefixes;
/**
* Since the stream reader would mostly be just a wrapper around
* the underlying scanner (its main job is to implement Stax
* interface), we can and should just use the scanner. In effect,
* this class is then a replacement of BasicStreamReader, when
* using SAX interfaces.
*/
protected BasicStreamReader mScanner;
protected AttributeCollector mAttrCollector;
protected InputElementStack mElemStack;
// // // Info from xml declaration
protected String mEncoding;
protected String mXmlVersion;
protected boolean mStandalone;
// // // Listeners attached:
protected ContentHandler mContentHandler;
protected DTDHandler mDTDHandler;
protected EntityResolver mEntityResolver;
protected ErrorHandler mErrorHandler;
protected LexicalHandler mLexicalHandler;
protected DeclHandler mDeclHandler;
// // // State:
/**
* Number of attributes accessible via {@link Attributes} and
* {@link Attributes2} interfaces, for the current start element.
*<p>
* Note: does not include namespace declarations, even they are to
* be reported as attributes.
*/
protected int mAttrCount;
/**
* Need to keep track of number of namespaces, if namespace declarations
* are to be reported along with attributes (see
* {@link #mFeatNsPrefixes}).
*/
protected int mNsCount = 0;
/*
/////////////////////////////////////////////////
// Life-cycle
/////////////////////////////////////////////////
*/
/**
*<p>
* NOTE: this was a protected constructor for versions 4.0
* and 3.2; changed to public in 4.1
*/
public WstxSAXParser(WstxInputFactory sf, boolean nsPrefixes)
{
mStaxFactory = sf;
mFeatNsPrefixes = nsPrefixes;
mConfig = sf.createPrivateConfig();
mConfig.doSupportDTDs(true);
/* Lazy parsing is a tricky thing: although most of the time
* it's useless with SAX, it is actually necessary to be able
* to properly model internal DTD subsets, for example. So,
* we can not really easily determine defaults.
*/
ResolverProxy r = new ResolverProxy();
/* SAX doesn't distinguish between DTD (ext. subset, PEs) and
* entity (external general entities) resolvers, so let's
* assign them both:
*/
mConfig.setDtdResolver(r);
mConfig.setEntityResolver(r);
mConfig.setDTDEventListener(this);
/* These settings do NOT make sense as generic defaults, but
* are helpful when using some test frameworks. Specifically,
* - DTD caching may remove calls to resolvers, changing
* observed behavior
* - Using min. segment length of 1 will force flushing of
* all content before entity expansion, which will
* completely serialize entity resolution calls wrt.
* CHARACTERS events.
*/
// !!! ONLY for testing; never remove for prod use
//mConfig.setShortestReportedTextSegment(1);
//mConfig.doCacheDTDs(false);
}
/*
* This constructor is provided for two main use cases: testing,
* and introspection via SAX classes (as opposed to JAXP-based
* introspection).
*/
public WstxSAXParser()
{
this(new WstxInputFactory(), FEAT_DEFAULT_NS_PREFIXES);
}
@Override
public final Parser getParser() {
return this;
}
@Override
public final XMLReader getXMLReader() {
return this;
}
/**
* Accessor used to allow configuring all standard Stax configuration
* settings that the underlying reader uses.
*
* @since 4.0.8
*/
public final ReaderConfig getStaxConfig() {
return mConfig;
}
/*
/////////////////////////////////////////////////
// Configuration, SAXParser
/////////////////////////////////////////////////
*/
@Override
public boolean isNamespaceAware() {
return mConfig.willSupportNamespaces();
}
@Override
public boolean isValidating() {
return mConfig.willValidateWithDTD();
}
@Override
public Object getProperty(String name)
throws SAXNotRecognizedException, SAXNotSupportedException
{
SAXProperty prop = SAXProperty.findByUri(name);
if (prop == SAXProperty.DECLARATION_HANDLER) {
return mDeclHandler;
} else if (prop == SAXProperty.DOCUMENT_XML_VERSION) {
return mXmlVersion;
} else if (prop == SAXProperty.DOM_NODE) {
return null;
} else if (prop == SAXProperty.LEXICAL_HANDLER) {
return mLexicalHandler;
} else if (prop == SAXProperty.XML_STRING) {
return null;
}
throw new SAXNotRecognizedException("Property '"+name+"' not recognized");
}
@Override
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
SAXProperty prop = SAXProperty.findByUri(name);
if (prop == SAXProperty.DECLARATION_HANDLER) {
mDeclHandler = (DeclHandler) value;
return;
} else if (prop == SAXProperty.DOCUMENT_XML_VERSION) {
; // read-only
} else if (prop == SAXProperty.DOM_NODE) {
; // read-only
} else if (prop == SAXProperty.LEXICAL_HANDLER) {
mLexicalHandler = (LexicalHandler) value;
return;
} else if (prop == SAXProperty.XML_STRING) {
; // read-only
} else {
throw new SAXNotRecognizedException("Property '"+name+"' not recognized");
}
// Trying to modify read-only properties?
throw new SAXNotSupportedException("Property '"+name+"' is read-only, can not be modified");
}
/*
/////////////////////////////////////////////////
// Overrides, SAXParser
/////////////////////////////////////////////////
*/
/* Have to override some methods from SAXParser; JDK
* implementation is sucky, as it tries to override
* many things it really should not...
*/
@Override
public void parse(InputSource is, HandlerBase hb)
throws SAXException, IOException
{
if (hb != null) {
/* Ok: let's ONLY set if there are no explicit sets... not
* extremely clear, but JDK tries to set them always so
* let's at least do damage control.
*/
if (mContentHandler == null) {
setDocumentHandler(hb);
}
if (mEntityResolver == null) {
setEntityResolver(hb);
}
if (mErrorHandler == null) {
setErrorHandler(hb);
}
if (mDTDHandler == null) {
setDTDHandler(hb);
}
}
parse(is);
}
@Override
public void parse(InputSource is, DefaultHandler dh)
throws SAXException, IOException
{
if (dh != null) {
/* Ok: let's ONLY set if there are no explicit sets... not
* extremely clear, but JDK tries to set them always so
* let's at least do damage control.
*/
if (mContentHandler == null) {
setContentHandler(dh);
}
if (mEntityResolver == null) {
setEntityResolver(dh);
}
if (mErrorHandler == null) {
setErrorHandler(dh);
}
if (mDTDHandler == null) {
setDTDHandler(dh);
}
}
parse(is);
}
/*
/////////////////////////////////////////////////////
// XLMReader (SAX2) implementation: cfg access
/////////////////////////////////////////////////////
*/
@Override
public ContentHandler getContentHandler() {
return mContentHandler;
}
@Override
public DTDHandler getDTDHandler() {
return mDTDHandler;
}
@Override
public EntityResolver getEntityResolver() {
return mEntityResolver;
}
@Override
public ErrorHandler getErrorHandler() {
return mErrorHandler;
}
@Override
public boolean getFeature(String name)
throws SAXNotRecognizedException
{
SAXFeature stdFeat = SAXFeature.findByUri(name);
if (stdFeat == SAXFeature.EXTERNAL_GENERAL_ENTITIES) {
return mConfig.willSupportExternalEntities();
} else if (stdFeat == SAXFeature.EXTERNAL_PARAMETER_ENTITIES) {
return mConfig.willSupportExternalEntities();
} else if (stdFeat == SAXFeature.IS_STANDALONE) {
return mStandalone;
} else if (stdFeat == SAXFeature.LEXICAL_HANDLER_PARAMETER_ENTITIES) {
// !!! TODO:
return false;
} else if (stdFeat == SAXFeature.NAMESPACES) {
return mConfig.willSupportNamespaces();
} else if (stdFeat == SAXFeature.NAMESPACE_PREFIXES) {
return !mConfig.willSupportNamespaces();
} else if (stdFeat == SAXFeature.RESOLVE_DTD_URIS) {
// !!! TODO:
return false;
} else if (stdFeat == SAXFeature.STRING_INTERNING) {
return true;
} else if (stdFeat == SAXFeature.UNICODE_NORMALIZATION_CHECKING) {
return false;
} else if (stdFeat == SAXFeature.USE_ATTRIBUTES2) {
return true;
} else if (stdFeat == SAXFeature.USE_LOCATOR2) {
return true;
} else if (stdFeat == SAXFeature.USE_ENTITY_RESOLVER2) {
return true;
} else if (stdFeat == SAXFeature.VALIDATION) {
return mConfig.willValidateWithDTD();
} else if (stdFeat == SAXFeature.XMLNS_URIS) {
/* !!! TODO: default value should be false... but not sure
* if implementing that mode makes sense
*/
return true;
} else if (stdFeat == SAXFeature.XML_1_1) {
return true;
}
throw new SAXNotRecognizedException("Feature '"+name+"' not recognized");
}
// Already implemented for SAXParser
//public Object getProperty(String name)
/*
/////////////////////////////////////////////////////
// XLMReader (SAX2) implementation: cfg changing
/////////////////////////////////////////////////////
*/
@Override
public void setContentHandler(ContentHandler handler) {
mContentHandler = handler;
}
@Override
public void setDTDHandler(DTDHandler handler) {
mDTDHandler = handler;
}
@Override
public void setEntityResolver(EntityResolver resolver) {
mEntityResolver = resolver;
}
@Override
public void setErrorHandler(ErrorHandler handler) {
mErrorHandler = handler;
}
@Override
public void setFeature(String name, boolean value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
boolean invalidValue = false;
boolean readOnly = false;
SAXFeature stdFeat = SAXFeature.findByUri(name);
if (stdFeat == SAXFeature.EXTERNAL_GENERAL_ENTITIES) {
mConfig.doSupportExternalEntities(value);
} else if (stdFeat == SAXFeature.EXTERNAL_PARAMETER_ENTITIES) {
// !!! TODO
} else if (stdFeat == SAXFeature.IS_STANDALONE) {
readOnly = true;
} else if (stdFeat == SAXFeature.LEXICAL_HANDLER_PARAMETER_ENTITIES) {
// !!! TODO
} else if (stdFeat == SAXFeature.NAMESPACES) {
mConfig.doSupportNamespaces(value);
} else if (stdFeat == SAXFeature.NAMESPACE_PREFIXES) {
mFeatNsPrefixes = value;
} else if (stdFeat == SAXFeature.RESOLVE_DTD_URIS) {
// !!! TODO
} else if (stdFeat == SAXFeature.STRING_INTERNING) {
invalidValue = !value;
} else if (stdFeat == SAXFeature.UNICODE_NORMALIZATION_CHECKING) {
invalidValue = value;
} else if (stdFeat == SAXFeature.USE_ATTRIBUTES2) {
readOnly = true;
} else if (stdFeat == SAXFeature.USE_LOCATOR2) {
readOnly = true;
} else if (stdFeat == SAXFeature.USE_ENTITY_RESOLVER2) {
readOnly = true;
} else if (stdFeat == SAXFeature.VALIDATION) {
mConfig.doValidateWithDTD(value);
} else if (stdFeat == SAXFeature.XMLNS_URIS) {
invalidValue = !value;
} else if (stdFeat == SAXFeature.XML_1_1) {
readOnly = true;
} else {
throw new SAXNotRecognizedException("Feature '"+name+"' not recognized");
}
// Trying to modify read-only properties?
if (readOnly) {
throw new SAXNotSupportedException("Feature '"+name+"' is read-only, can not be modified");
}
if (invalidValue) {
throw new SAXNotSupportedException("Trying to set invalid value for feature '"+name+"', '"+value+"'");
}
}
// Already implemented for SAXParser
//public void setProperty(String name, Object value)
/*
/////////////////////////////////////////////////////
// XLMReader (SAX2) implementation: parsing
/////////////////////////////////////////////////////
*/
@SuppressWarnings("resource")
@Override
public void parse(InputSource input) throws SAXException
{
mScanner = null;
String sysIdStr = input.getSystemId();
ReaderConfig cfg = mConfig;
URL srcUrl = null;
// Let's figure out input, first, before sending start-doc event
InputStream is = null;
Reader r = input.getCharacterStream();
if (r == null) {
is = input.getByteStream();
if (is == null) {
if (sysIdStr == null) {
throw new SAXException("Invalid InputSource passed: neither character or byte stream passed, nor system id specified");
}
try {
srcUrl = URLUtil.urlFromSystemId(sysIdStr);
is = URLUtil.inputStreamFromURL(srcUrl);
} catch (IOException ioe) {
SAXException saxe = new SAXException(ioe);
ExceptionUtil.setInitCause(saxe, ioe);
throw saxe;
}
}
}
if (mContentHandler != null) {
mContentHandler.setDocumentLocator(this);
mContentHandler.startDocument();
}
/* Note: since we are reusing the same config instance, need to
* make sure state is not carried forward. Thus:
*/
cfg.resetState();
try {
String inputEnc = input.getEncoding();
String publicId = input.getPublicId();
// Got an InputStream and encoding? Can create a Reader:
if (r == null && (inputEnc != null && inputEnc.length() > 0)) {
r = DefaultInputResolver.constructOptimizedReader(cfg, is, false, inputEnc);
}
InputBootstrapper bs;
SystemId systemId = SystemId.construct(sysIdStr, srcUrl);
if (r != null) {
bs = ReaderBootstrapper.getInstance(publicId, systemId, r, inputEnc);
// false -> not for event reader; false -> no auto-closing
mScanner = (BasicStreamReader) mStaxFactory.createSR(cfg, systemId, bs, false, false);
} else {
bs = StreamBootstrapper.getInstance(publicId, systemId, is);
mScanner = (BasicStreamReader) mStaxFactory.createSR(cfg, systemId, bs, false, false);
}
// Need to get xml declaration stuff out now:
{
String enc2 = mScanner.getEncoding();
if (enc2 == null) {
enc2 = mScanner.getCharacterEncodingScheme();
}
mEncoding = enc2;
}
mXmlVersion = mScanner.getVersion();
mStandalone = mScanner.standaloneSet();
mAttrCollector = mScanner.getAttributeCollector();
mElemStack = mScanner.getInputElementStack();
fireEvents();
} catch (IOException io) {
throwSaxException(io);
} catch (XMLStreamException strex) {
throwSaxException(strex);
} finally {
if (mContentHandler != null) {
mContentHandler.endDocument();
}
// Could try holding onto the buffers, too... but
// maybe it's better to allow them to be reclaimed, if
// needed by GC
if (mScanner != null) {
BasicStreamReader sr = mScanner;
mScanner = null;
try {
sr.close();
} catch (XMLStreamException sex) { }
}
if (r != null) {
try {
r.close();
} catch (IOException ioe) { }
}
if (is != null) {
try {
is.close();
} catch (IOException ioe) { }
}
}
}
@Override
public void parse(String systemId) throws SAXException
{
InputSource src = new InputSource(systemId);
parse(src);
}
/*
/////////////////////////////////////////////////
// Parsing loop, helper methods
/////////////////////////////////////////////////
*/
/**
* This is the actual "tight event loop" that will send all events
* between start and end document events. Although we could
* use the stream reader here, there's not much as it mostly
* just forwards requests to the scanner: and so we can as well
* just copy the little code stream reader's next() method has.
*/
private final void fireEvents()
throws IOException, SAXException, XMLStreamException
{
// First we are in prolog:
int type;
/* Need to enable lazy parsing, to get DTD start events before
* its content events. Plus, can skip more efficiently too.
*/
mConfig.doParseLazily(false);
while ((type = mScanner.next()) != XMLStreamConstants.START_ELEMENT) {
fireAuxEvent(type, false);
}
// Now just starting the tree, need to process the START_ELEMENT
fireStartTag();
int depth = 1;
while (true) {
type = mScanner.next();
if (type == XMLStreamConstants.START_ELEMENT) {
fireStartTag();
++depth;
} else if (type == XMLStreamConstants.END_ELEMENT) {
mScanner.fireSaxEndElement(mContentHandler);
if (--depth < 1) {
break;
}
} else if (type == XMLStreamConstants.CHARACTERS) {
mScanner.fireSaxCharacterEvents(mContentHandler);
} else {
fireAuxEvent(type, true);
}
}
// And then epilog:
while (true) {
type = mScanner.next();
if (type == XMLStreamConstants.END_DOCUMENT) {
break;
}
if (type == XMLStreamConstants.SPACE) {
// Not to be reported via SAX interface (which may or may not
// be different from Stax)
continue;
}
fireAuxEvent(type, false);
}
}
private final void fireAuxEvent(int type, boolean inTree)
throws IOException, SAXException, XMLStreamException
{
switch (type) {
case XMLStreamConstants.COMMENT:
mScanner.fireSaxCommentEvent(mLexicalHandler);
break;
case XMLStreamConstants.CDATA:
if (mLexicalHandler != null) {
mLexicalHandler.startCDATA();
mScanner.fireSaxCharacterEvents(mContentHandler);
mLexicalHandler.endCDATA();
} else {
mScanner.fireSaxCharacterEvents(mContentHandler);
}
break;
case XMLStreamConstants.DTD:
if (mLexicalHandler != null) {
/* Note: this is bit tricky, since calling getDTDInfo() will
* trigger full reading of the subsets... but we need to
* get some info first, to be able to send dtd-start event,
* and only then get the rest. Thus, need to call separate
* accessors first:
*/
String rootName = mScanner.getDTDRootName();
String sysId = mScanner.getDTDSystemId();
String pubId = mScanner.getDTDPublicId();
mLexicalHandler.startDTD(rootName, pubId, sysId);
// Ok, let's get rest (if any) read:
try {
/*DTDInfo dtdInfo =*/ mScanner.getDTDInfo();
} catch (WrappedSaxException wse) {
throw wse.getSaxException();
}
mLexicalHandler.endDTD();
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
mScanner.fireSaxPIEvent(mContentHandler);
break;
case XMLStreamConstants.SPACE:
// With SAX, only to be sent as an event if inside the
// tree, not from within prolog/epilog
if (inTree) {
mScanner.fireSaxSpaceEvents(mContentHandler);
}
break;
case XMLStreamConstants.ENTITY_REFERENCE:
/* Only occurs in non-entity-expanding mode; so effectively
* we are skipping the entity?
*/
if (mContentHandler != null) {
mContentHandler.skippedEntity(mScanner.getLocalName());
}
break;
default:
if (type == XMLStreamConstants.END_DOCUMENT) {
throwSaxException("Unexpected end-of-input in "+(inTree ? "tree" : "prolog"));
}
throw new RuntimeException("Internal error: unexpected type, "+type);
}
}
private final void fireStartTag()
throws SAXException
{
mAttrCount = mAttrCollector.getCount();
if (mFeatNsPrefixes) {
/* 15-Dec-2006, TSa: Note: apparently namespace bindings that
* are added via defaulting are only visible via element
* stack. Thus, we MUST access things via element stack,
* not attribute collector; even though latter seems like
* the more direct route. See
* {@link NsInputElementStack#addNsBinding} for the method
* that injects such special namespace bindings (yes, it's
* a hack, afterthought)
*/
//mNsCount = mAttrCollector.getNsCount();
mNsCount = mElemStack.getCurrentNsCount();
}
mScanner.fireSaxStartElement(mContentHandler, this);
}
/*
/////////////////////////////////////////////////
// Parser (SAX1) implementation
/////////////////////////////////////////////////
*/
// Already implemented for XMLReader:
//public void parse(InputSource source)
//public void parse(String systemId)
//public void setEntityResolver(EntityResolver resolver)
//public void setErrorHandler(ErrorHandler handler)
@Override
public void setDocumentHandler(DocumentHandler handler) {
setContentHandler(new DocHandlerWrapper(handler));
}
@Override
public void setLocale(java.util.Locale locale) {
// Not supported, let's just ignore
}
/*
/////////////////////////////////////////////////////
// Attributes (SAX2) implementation
/////////////////////////////////////////////////////
*/
@Override
public int getIndex(String qName)
{
if (mElemStack == null) {
return -1;
}
int ix = mElemStack.findAttributeIndex(null, qName);
// !!! In ns-as-attrs mode, should also match ns decls?
return ix;
}
@Override
public int getIndex(String uri, String localName)
{
if (mElemStack == null) {
return -1;
}
int ix = mElemStack.findAttributeIndex(uri, localName);
// !!! In ns-as-attrs mode, should also match ns decls?
return ix;
}
@Override
public int getLength()
{
return mAttrCount + mNsCount;
}
@Override
public String getLocalName(int index)
{
if (index < mAttrCount) {
return (index < 0) ? null : mAttrCollector.getLocalName(index);
}
index -= mAttrCount;
if (index < mNsCount) {
/* As discussed in <code>fireStartTag</code>, we must use
* element stack, not attribute collector:
*/
//String prefix = mAttrCollector.getNsPrefix(index);
String prefix = mElemStack.getLocalNsPrefix(index);
return (prefix == null || prefix.length() == 0) ?
"xmlns" : prefix;
}
return null;
}
@Override
public String getQName(int index)
{
if (index < mAttrCount) {
if (index < 0) {
return null;
}
String prefix = mAttrCollector.getPrefix(index);
String ln = mAttrCollector.getLocalName(index);
return (prefix == null || prefix.length() == 0) ?
ln : (prefix + ":" + ln);
}
index -= mAttrCount;
if (index < mNsCount) {
/* As discussed in <code>fireStartTag</code>, we must use
* element stack, not attribute collector:
*/
//String prefix = mAttrCollector.getNsPrefix(index);
String prefix = mElemStack.getLocalNsPrefix(index);
if (prefix == null || prefix.length() == 0) {
return "xmlns";
}
return "xmlns:"+prefix;
}
return null;
}
@Override
public String getType(int index)
{
if (index < mAttrCount) {
if (index < 0) {
return null;
}
/* Note: Woodstox will have separate type for enumerated values;
* SAX considers these NMTOKENs, so may need to convert (but
* note: some SAX impls also use "ENUMERATED")
*/
String type = mElemStack.getAttributeType(index);
// Let's count on it being interned:
if (type == "ENUMERATED") {
type = "NMTOKEN";
}
return type;
}
// But how about namespace declarations... let's just call them CDATA?
index -= mAttrCount;
if (index < mNsCount) {
return "CDATA";
}
return null;
}
@Override
public String getType(String qName) {
return getType(getIndex(qName));
}
@Override
public String getType(String uri, String localName) {
return getType(getIndex(uri, localName));
}
@Override
public String getURI(int index)
{
if (index < mAttrCount) {
if (index < 0) {
return null;
}
String uri = mAttrCollector.getURI(index);
return (uri == null) ? "" : uri;
}
if ((index - mAttrCount) < mNsCount) {
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
}
return null;
}
@Override
public String getValue(int index)
{
if (index < mAttrCount) {
return (index < 0) ? null : mAttrCollector.getValue(index);
}
index -= mAttrCount;
if (index < mNsCount) {
/* As discussed in <code>fireStartTag</code>, we must use
* element stack, not attribute collector:
*/
//String uri = mAttrCollector.getNsURI(index);
String uri = mElemStack.getLocalNsURI(index);
return (uri == null) ? "" : uri;
}
return null;
}
@Override
public String getValue(String qName) {
return getValue(getIndex(qName));
}
@Override
public String getValue(String uri, String localName) {
return getValue(getIndex(uri, localName));
}
/*
/////////////////////////////////////////////////////
// Attributes2 (SAX2) implementation
/////////////////////////////////////////////////////
*/
@Override
public boolean isDeclared(int index)
{
if (index < mAttrCount) {