/
MXParser.java
4143 lines (3810 loc) · 146 KB
/
MXParser.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
/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
/*
* Copyright (c) 2003 Extreme! Lab, Indiana University. All rights reserved.
*
* This software is open source. See the bottom of this file for the licence.
*
* $Id$
*/
package org.codehaus.plexus.util.xml.pull;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import org.codehaus.plexus.util.ReaderFactory;
//import java.util.Hashtable;
//TODO best handling of interning issues
// have isAllNewStringInterned ???
//TODO handling surrogate pairs: http://www.unicode.org/unicode/faq/utf_bom.html#6
//TODO review code for use of bufAbsoluteStart when keeping pos between next()/fillBuf()
/**
* Absolutely minimal implementation of XMLPULL V1 API. Encoding handling done with XmlReader
*
* @see org.codehaus.plexus.util.xml.XmlReader
* @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
*/
public class MXParser
implements XmlPullParser
{
// NOTE: no interning of those strings --> by Java leng spec they MUST be already interned
private final static String XML_URI = "http://www.w3.org/XML/1998/namespace";
private final static String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
private final static String FEATURE_XML_ROUNDTRIP =
// "http://xmlpull.org/v1/doc/features.html#xml-roundtrip";
"http://xmlpull.org/v1/doc/features.html#xml-roundtrip";
private final static String FEATURE_NAMES_INTERNED = "http://xmlpull.org/v1/doc/features.html#names-interned";
private final static String PROPERTY_XMLDECL_VERSION =
"http://xmlpull.org/v1/doc/properties.html#xmldecl-version";
private final static String PROPERTY_XMLDECL_STANDALONE =
"http://xmlpull.org/v1/doc/properties.html#xmldecl-standalone";
private final static String PROPERTY_XMLDECL_CONTENT =
"http://xmlpull.org/v1/doc/properties.html#xmldecl-content";
private final static String PROPERTY_LOCATION = "http://xmlpull.org/v1/doc/properties.html#location";
/**
* Implementation notice: the is instance variable that controls if newString() is interning.
* <p>
* <b>NOTE:</b> newStringIntern <b>always</b> returns interned strings and newString MAY return interned String
* depending on this variable.
* <p>
* <b>NOTE:</b> by default in this minimal implementation it is false!
*/
private boolean allStringsInterned;
private void resetStringCache()
{
// System.out.println("resetStringCache() minimum called");
}
private String newString( char[] cbuf, int off, int len )
{
return new String( cbuf, off, len );
}
private String newStringIntern( char[] cbuf, int off, int len )
{
return ( new String( cbuf, off, len ) ).intern();
}
private static final boolean TRACE_SIZING = false;
// NOTE: features are not resetable and typically defaults to false ...
private boolean processNamespaces;
private boolean roundtripSupported;
// global parser state
private String location;
private int lineNumber;
private int columnNumber;
private boolean seenRoot;
private boolean reachedEnd;
private int eventType;
private boolean emptyElementTag;
// element stack
private int depth;
private char[] elRawName[];
private int elRawNameEnd[];
private int elRawNameLine[];
private String elName[];
private String elPrefix[];
private String elUri[];
// private String elValue[];
private int elNamespaceCount[];
private String fileEncoding = "UTF8";
/**
* Make sure that we have enough space to keep element stack if passed size. It will always create one additional
* slot then current depth
*/
private void ensureElementsCapacity()
{
final int elStackSize = elName != null ? elName.length : 0;
if ( ( depth + 1 ) >= elStackSize )
{
// we add at least one extra slot ...
final int newSize = ( depth >= 7 ? 2 * depth : 8 ) + 2; // = lucky 7 + 1 //25
if ( TRACE_SIZING )
{
System.err.println( "TRACE_SIZING elStackSize " + elStackSize + " ==> " + newSize );
}
final boolean needsCopying = elStackSize > 0;
String[] arr = null;
// resue arr local variable slot
arr = new String[newSize];
if ( needsCopying )
System.arraycopy( elName, 0, arr, 0, elStackSize );
elName = arr;
arr = new String[newSize];
if ( needsCopying )
System.arraycopy( elPrefix, 0, arr, 0, elStackSize );
elPrefix = arr;
arr = new String[newSize];
if ( needsCopying )
System.arraycopy( elUri, 0, arr, 0, elStackSize );
elUri = arr;
int[] iarr = new int[newSize];
if ( needsCopying )
{
System.arraycopy( elNamespaceCount, 0, iarr, 0, elStackSize );
}
else
{
// special initialization
iarr[0] = 0;
}
elNamespaceCount = iarr;
// TODO: avoid using element raw name ...
iarr = new int[newSize];
if ( needsCopying )
{
System.arraycopy( elRawNameEnd, 0, iarr, 0, elStackSize );
}
elRawNameEnd = iarr;
iarr = new int[newSize];
if ( needsCopying )
{
System.arraycopy( elRawNameLine, 0, iarr, 0, elStackSize );
}
elRawNameLine = iarr;
final char[][] carr = new char[newSize][];
if ( needsCopying )
{
System.arraycopy( elRawName, 0, carr, 0, elStackSize );
}
elRawName = carr;
// arr = new String[newSize];
// if(needsCopying) System.arraycopy(elLocalName, 0, arr, 0, elStackSize);
// elLocalName = arr;
// arr = new String[newSize];
// if(needsCopying) System.arraycopy(elDefaultNs, 0, arr, 0, elStackSize);
// elDefaultNs = arr;
// int[] iarr = new int[newSize];
// if(needsCopying) System.arraycopy(elNsStackPos, 0, iarr, 0, elStackSize);
// for (int i = elStackSize; i < iarr.length; i++)
// {
// iarr[i] = (i > 0) ? -1 : 0;
// }
// elNsStackPos = iarr;
// assert depth < elName.length;
}
}
// attribute stack
private int attributeCount;
private String attributeName[];
private int attributeNameHash[];
// private int attributeNameStart[];
// private int attributeNameEnd[];
private String attributePrefix[];
private String attributeUri[];
private String attributeValue[];
// private int attributeValueStart[];
// private int attributeValueEnd[];
// Make sure that in attributes temporary array is enough space.
private void ensureAttributesCapacity( int size )
{
final int attrPosSize = attributeName != null ? attributeName.length : 0;
if ( size >= attrPosSize )
{
final int newSize = size > 7 ? 2 * size : 8; // = lucky 7 + 1 //25
if ( TRACE_SIZING )
{
System.err.println( "TRACE_SIZING attrPosSize " + attrPosSize + " ==> " + newSize );
}
final boolean needsCopying = attrPosSize > 0;
String[] arr = null;
arr = new String[newSize];
if ( needsCopying )
System.arraycopy( attributeName, 0, arr, 0, attrPosSize );
attributeName = arr;
arr = new String[newSize];
if ( needsCopying )
System.arraycopy( attributePrefix, 0, arr, 0, attrPosSize );
attributePrefix = arr;
arr = new String[newSize];
if ( needsCopying )
System.arraycopy( attributeUri, 0, arr, 0, attrPosSize );
attributeUri = arr;
arr = new String[newSize];
if ( needsCopying )
System.arraycopy( attributeValue, 0, arr, 0, attrPosSize );
attributeValue = arr;
if ( !allStringsInterned )
{
final int[] iarr = new int[newSize];
if ( needsCopying )
System.arraycopy( attributeNameHash, 0, iarr, 0, attrPosSize );
attributeNameHash = iarr;
}
arr = null;
// //assert attrUri.length > size
}
}
// namespace stack
private int namespaceEnd;
private String namespacePrefix[];
private int namespacePrefixHash[];
private String namespaceUri[];
private void ensureNamespacesCapacity( int size )
{
final int namespaceSize = namespacePrefix != null ? namespacePrefix.length : 0;
if ( size >= namespaceSize )
{
final int newSize = size > 7 ? 2 * size : 8; // = lucky 7 + 1 //25
if ( TRACE_SIZING )
{
System.err.println( "TRACE_SIZING namespaceSize " + namespaceSize + " ==> " + newSize );
}
final String[] newNamespacePrefix = new String[newSize];
final String[] newNamespaceUri = new String[newSize];
if ( namespacePrefix != null )
{
System.arraycopy( namespacePrefix, 0, newNamespacePrefix, 0, namespaceEnd );
System.arraycopy( namespaceUri, 0, newNamespaceUri, 0, namespaceEnd );
}
namespacePrefix = newNamespacePrefix;
namespaceUri = newNamespaceUri;
if ( !allStringsInterned )
{
final int[] newNamespacePrefixHash = new int[newSize];
if ( namespacePrefixHash != null )
{
System.arraycopy( namespacePrefixHash, 0, newNamespacePrefixHash, 0, namespaceEnd );
}
namespacePrefixHash = newNamespacePrefixHash;
}
// prefixesSize = newSize;
// //assert nsPrefixes.length > size && nsPrefixes.length == newSize
}
}
// simplistic implementation of hash function that has <b>constant</b> time to compute - so it also means
// diminishing hash quality for long strings but for XML parsing it should be good enough ...
private static final int fastHash( char ch[], int off, int len )
{
if ( len == 0 )
return 0;
// assert len >0
int hash = ch[off]; // hash at beginning
// try {
hash = ( hash << 7 ) + ch[off + len - 1]; // hash at the end
// } catch(ArrayIndexOutOfBoundsException aie) {
// aie.printStackTrace(); //should never happen ...
// throw new RuntimeException("this is violation of pre-condition");
// }
if ( len > 16 )
hash = ( hash << 7 ) + ch[off + ( len / 4 )]; // 1/4 from beginning
if ( len > 8 )
hash = ( hash << 7 ) + ch[off + ( len / 2 )]; // 1/2 of string size ...
// notice that hash is at most done 3 times <<7 so shifted by 21 bits 8 bit value
// so max result == 29 bits so it is quite just below 31 bits for long (2^32) ...
// assert hash >= 0;
return hash;
}
// entity replacement stack
private int entityEnd;
private String entityName[];
private char[] entityNameBuf[];
private String entityReplacement[];
private char[] entityReplacementBuf[];
private int entityNameHash[];
private final EntityReplacementMap replacementMapTemplate;
private void ensureEntityCapacity()
{
final int entitySize = entityReplacementBuf != null ? entityReplacementBuf.length : 0;
if ( entityEnd >= entitySize )
{
final int newSize = entityEnd > 7 ? 2 * entityEnd : 8; // = lucky 7 + 1 //25
if ( TRACE_SIZING )
{
System.err.println( "TRACE_SIZING entitySize " + entitySize + " ==> " + newSize );
}
final String[] newEntityName = new String[newSize];
final char[] newEntityNameBuf[] = new char[newSize][];
final String[] newEntityReplacement = new String[newSize];
final char[] newEntityReplacementBuf[] = new char[newSize][];
if ( entityName != null )
{
System.arraycopy( entityName, 0, newEntityName, 0, entityEnd );
System.arraycopy( entityNameBuf, 0, newEntityNameBuf, 0, entityEnd );
System.arraycopy( entityReplacement, 0, newEntityReplacement, 0, entityEnd );
System.arraycopy( entityReplacementBuf, 0, newEntityReplacementBuf, 0, entityEnd );
}
entityName = newEntityName;
entityNameBuf = newEntityNameBuf;
entityReplacement = newEntityReplacement;
entityReplacementBuf = newEntityReplacementBuf;
if ( !allStringsInterned )
{
final int[] newEntityNameHash = new int[newSize];
if ( entityNameHash != null )
{
System.arraycopy( entityNameHash, 0, newEntityNameHash, 0, entityEnd );
}
entityNameHash = newEntityNameHash;
}
}
}
// input buffer management
private static final int READ_CHUNK_SIZE = 8 * 1024; // max data chars in one read() call
private Reader reader;
private String inputEncoding;
private int bufLoadFactor = 95; // 99%
// private int bufHardLimit; // only matters when expanding
private float bufferLoadFactor = bufLoadFactor / 100f;
private char buf[] = new char[Runtime.getRuntime().freeMemory() > 1000000L ? READ_CHUNK_SIZE : 256];
private int bufSoftLimit = (int) ( bufferLoadFactor * buf.length ); // desirable size of buffer
private boolean preventBufferCompaction;
private int bufAbsoluteStart; // this is buf
private int bufStart;
private int bufEnd;
private int pos;
private int posStart;
private int posEnd;
private char pc[] = new char[Runtime.getRuntime().freeMemory() > 1000000L ? READ_CHUNK_SIZE : 64];
private int pcStart;
private int pcEnd;
// parsing state
// private boolean needsMore;
// private boolean seenMarkup;
private boolean usePC;
private boolean seenStartTag;
private boolean seenEndTag;
private boolean pastEndTag;
private boolean seenAmpersand;
private boolean seenMarkup;
private boolean seenDocdecl;
// transient variable set during each call to next/Token()
private boolean tokenize;
private String text;
private String entityRefName;
private String xmlDeclVersion;
private Boolean xmlDeclStandalone;
private String xmlDeclContent;
private void reset()
{
// System.out.println("reset() called");
location = null;
lineNumber = 1;
columnNumber = 0;
seenRoot = false;
reachedEnd = false;
eventType = START_DOCUMENT;
emptyElementTag = false;
depth = 0;
attributeCount = 0;
namespaceEnd = 0;
entityEnd = 0;
setupFromTemplate();
reader = null;
inputEncoding = null;
preventBufferCompaction = false;
bufAbsoluteStart = 0;
bufEnd = bufStart = 0;
pos = posStart = posEnd = 0;
pcEnd = pcStart = 0;
usePC = false;
seenStartTag = false;
seenEndTag = false;
pastEndTag = false;
seenAmpersand = false;
seenMarkup = false;
seenDocdecl = false;
xmlDeclVersion = null;
xmlDeclStandalone = null;
xmlDeclContent = null;
resetStringCache();
}
public MXParser()
{
replacementMapTemplate = null;
}
public MXParser( EntityReplacementMap entityReplacementMap )
{
this.replacementMapTemplate = entityReplacementMap;
}
public void setupFromTemplate()
{
if ( replacementMapTemplate != null )
{
int length = replacementMapTemplate.entityEnd;
// This is a bit cheeky, since the EntityReplacementMap contains exact-sized arrays,
// and elements are always added to the array, we can use the array from the template.
// Kids; dont do this at home.
entityName = replacementMapTemplate.entityName;
entityNameBuf = replacementMapTemplate.entityNameBuf;
entityReplacement = replacementMapTemplate.entityReplacement;
entityReplacementBuf = replacementMapTemplate.entityReplacementBuf;
entityNameHash = replacementMapTemplate.entityNameHash;
entityEnd = length;
}
}
/**
* Method setFeature
*
* @param name a String
* @param state a boolean
* @throws XmlPullParserException issue
*/
@Override
public void setFeature( String name, boolean state )
throws XmlPullParserException
{
if ( name == null )
throw new IllegalArgumentException( "feature name should not be null" );
if ( FEATURE_PROCESS_NAMESPACES.equals( name ) )
{
if ( eventType != START_DOCUMENT )
throw new XmlPullParserException( "namespace processing feature can only be changed before parsing",
this, null );
processNamespaces = state;
// } else if(FEATURE_REPORT_NAMESPACE_ATTRIBUTES.equals(name)) {
// if(type != START_DOCUMENT) throw new XmlPullParserException(
// "namespace reporting feature can only be changed before parsing", this, null);
// reportNsAttribs = state;
}
else if ( FEATURE_NAMES_INTERNED.equals( name ) )
{
if ( state != false )
{
throw new XmlPullParserException( "interning names in this implementation is not supported" );
}
}
else if ( FEATURE_PROCESS_DOCDECL.equals( name ) )
{
if ( state != false )
{
throw new XmlPullParserException( "processing DOCDECL is not supported" );
}
// } else if(REPORT_DOCDECL.equals(name)) {
// paramNotifyDoctype = state;
}
else if ( FEATURE_XML_ROUNDTRIP.equals( name ) )
{
// if(state == false) {
// throw new XmlPullParserException(
// "roundtrip feature can not be switched off");
// }
roundtripSupported = state;
}
else
{
throw new XmlPullParserException( "unsupported feature " + name );
}
}
/**
* Unknown properties are <strong>always</strong> returned as false
*/
@Override
public boolean getFeature( String name )
{
if ( name == null )
throw new IllegalArgumentException( "feature name should not be null" );
if ( FEATURE_PROCESS_NAMESPACES.equals( name ) )
{
return processNamespaces;
// } else if(FEATURE_REPORT_NAMESPACE_ATTRIBUTES.equals(name)) {
// return reportNsAttribs;
}
else if ( FEATURE_NAMES_INTERNED.equals( name ) )
{
return false;
}
else if ( FEATURE_PROCESS_DOCDECL.equals( name ) )
{
return false;
// } else if(REPORT_DOCDECL.equals(name)) {
// return paramNotifyDoctype;
}
else if ( FEATURE_XML_ROUNDTRIP.equals( name ) )
{
// return true;
return roundtripSupported;
}
return false;
}
@Override
public void setProperty( String name, Object value )
throws XmlPullParserException
{
if ( PROPERTY_LOCATION.equals( name ) )
{
location = (String) value;
}
else
{
throw new XmlPullParserException( "unsupported property: '" + name + "'" );
}
}
@Override
public Object getProperty( String name )
{
if ( name == null )
throw new IllegalArgumentException( "property name should not be null" );
if ( PROPERTY_XMLDECL_VERSION.equals( name ) )
{
return xmlDeclVersion;
}
else if ( PROPERTY_XMLDECL_STANDALONE.equals( name ) )
{
return xmlDeclStandalone;
}
else if ( PROPERTY_XMLDECL_CONTENT.equals( name ) )
{
return xmlDeclContent;
}
else if ( PROPERTY_LOCATION.equals( name ) )
{
return location;
}
return null;
}
@Override
public void setInput( Reader in )
throws XmlPullParserException
{
reset();
reader = in;
if ( reader instanceof InputStreamReader )
{
InputStreamReader isr = (InputStreamReader) reader;
if ( isr.getEncoding() != null )
{
fileEncoding = isr.getEncoding().toUpperCase();
}
}
}
@Override
public void setInput( java.io.InputStream inputStream, String inputEncoding )
throws XmlPullParserException
{
if ( inputStream == null )
{
throw new IllegalArgumentException( "input stream can not be null" );
}
Reader reader;
try
{
if ( inputEncoding != null )
{
reader = ReaderFactory.newReader( inputStream, inputEncoding );
}
else
{
reader = ReaderFactory.newXmlReader( inputStream );
}
}
catch ( UnsupportedEncodingException une )
{
throw new XmlPullParserException( "could not create reader for encoding " + inputEncoding + " : " + une,
this, une );
}
catch ( IOException e )
{
throw new XmlPullParserException( "could not create reader : " + e, this, e );
}
setInput( reader );
// must be here as reset() was called in setInput() and has set this.inputEncoding to null ...
this.inputEncoding = inputEncoding;
}
@Override
public String getInputEncoding()
{
return inputEncoding;
}
@Override
public void defineEntityReplacementText( String entityName, String replacementText )
throws XmlPullParserException
{
// throw new XmlPullParserException("not allowed");
if ( !replacementText.startsWith( "&#" ) && this.entityName != null && replacementText.length() > 1 )
{
String tmp = replacementText.substring( 1, replacementText.length() - 1 );
for ( int i = 0; i < this.entityName.length; i++ )
{
if ( this.entityName[i] != null && this.entityName[i].equals( tmp ) )
{
replacementText = this.entityReplacement[i];
}
}
}
// private char[] entityReplacement[];
ensureEntityCapacity();
// this is to make sure that if interning works we will take advantage of it ...
char[] entityNameCharData = entityName.toCharArray();
this.entityName[entityEnd] = newString( entityNameCharData, 0, entityName.length() );
entityNameBuf[entityEnd] = entityNameCharData;
entityReplacement[entityEnd] = replacementText;
entityReplacementBuf[entityEnd] = replacementText.toCharArray();
if ( !allStringsInterned )
{
entityNameHash[entityEnd] = fastHash( entityNameBuf[entityEnd], 0, entityNameBuf[entityEnd].length );
}
++entityEnd;
// TODO disallow < or & in entity replacement text (or ]]>???)
// TOOD keepEntityNormalizedForAttributeValue cached as well ...
}
@Override
public int getNamespaceCount( int depth )
throws XmlPullParserException
{
if ( !processNamespaces || depth == 0 )
{
return 0;
}
// int maxDepth = eventType == END_TAG ? this.depth + 1 : this.depth;
// if(depth < 0 || depth > maxDepth) throw new IllegalArgumentException(
if ( depth < 0 || depth > this.depth )
throw new IllegalArgumentException( "namespace count may be for depth 0.." + this.depth + " not " + depth );
return elNamespaceCount[depth];
}
@Override
public String getNamespacePrefix( int pos )
throws XmlPullParserException
{
// int end = eventType == END_TAG ? elNamespaceCount[ depth + 1 ] : namespaceEnd;
// if(pos < end) {
if ( pos < namespaceEnd )
{
return namespacePrefix[pos];
}
else
{
throw new XmlPullParserException( "position " + pos + " exceeded number of available namespaces "
+ namespaceEnd );
}
}
@Override
public String getNamespaceUri( int pos )
throws XmlPullParserException
{
// int end = eventType == END_TAG ? elNamespaceCount[ depth + 1 ] : namespaceEnd;
// if(pos < end) {
if ( pos < namespaceEnd )
{
return namespaceUri[pos];
}
else
{
throw new XmlPullParserException( "position " + pos + " exceeded number of available namespaces "
+ namespaceEnd );
}
}
@Override
public String getNamespace( String prefix )
// throws XmlPullParserException
{
// int count = namespaceCount[ depth ];
if ( prefix != null )
{
for ( int i = namespaceEnd - 1; i >= 0; i-- )
{
if ( prefix.equals( namespacePrefix[i] ) )
{
return namespaceUri[i];
}
}
if ( "xml".equals( prefix ) )
{
return XML_URI;
}
else if ( "xmlns".equals( prefix ) )
{
return XMLNS_URI;
}
}
else
{
for ( int i = namespaceEnd - 1; i >= 0; i-- )
{
if ( namespacePrefix[i] == null )
{ // "") { //null ) { //TODO check FIXME Alek
return namespaceUri[i];
}
}
}
return null;
}
@Override
public int getDepth()
{
return depth;
}
private static int findFragment( int bufMinPos, char[] b, int start, int end )
{
// System.err.println("bufStart="+bufStart+" b="+printable(new String(b, start, end - start))+" start="+start+"
// end="+end);
if ( start < bufMinPos )
{
start = bufMinPos;
if ( start > end )
start = end;
return start;
}
if ( end - start > 65 )
{
start = end - 10; // try to find good location
}
int i = start + 1;
while ( --i > bufMinPos )
{
if ( ( end - i ) > 65 )
break;
final char c = b[i];
if ( c == '<' && ( start - i ) > 10 )
break;
}
return i;
}
/**
* Return string describing current position of parsers as text 'STATE [seen %s...] @line:column'.
*/
@Override
public String getPositionDescription()
{
String fragment = null;
if ( posStart <= pos )
{
final int start = findFragment( 0, buf, posStart, pos );
// System.err.println("start="+start);
if ( start < pos )
{
fragment = new String( buf, start, pos - start );
}
if ( bufAbsoluteStart > 0 || start > 0 )
fragment = "..." + fragment;
}
// return " at line "+tokenizerPosRow
// +" and column "+(tokenizerPosCol-1)
// +(fragment != null ? " seen "+printable(fragment)+"..." : "");
return " " + TYPES[eventType] + ( fragment != null ? " seen " + printable( fragment ) + "..." : "" ) + " "
+ ( location != null ? location : "" ) + "@" + getLineNumber() + ":" + getColumnNumber();
}
@Override
public int getLineNumber()
{
return lineNumber;
}
@Override
public int getColumnNumber()
{
return columnNumber;
}
@Override
public boolean isWhitespace()
throws XmlPullParserException
{
if ( eventType == TEXT || eventType == CDSECT )
{
if ( usePC )
{
for ( int i = pcStart; i < pcEnd; i++ )
{
if ( !isS( pc[i] ) )
return false;
}
return true;
}
else
{
for ( int i = posStart; i < posEnd; i++ )
{
if ( !isS( buf[i] ) )
return false;
}
return true;
}
}
else if ( eventType == IGNORABLE_WHITESPACE )
{
return true;
}
throw new XmlPullParserException( "no content available to check for whitespaces" );
}
@Override
public String getText()
{
if ( eventType == START_DOCUMENT || eventType == END_DOCUMENT )
{
// throw new XmlPullParserException("no content available to read");
// if(roundtripSupported) {
// text = new String(buf, posStart, posEnd - posStart);
// } else {
return null;
// }
}
else if ( eventType == ENTITY_REF )
{
return text;
}
if ( text == null )
{
if ( !usePC || eventType == START_TAG || eventType == END_TAG )
{
text = new String( buf, posStart, posEnd - posStart );
}
else
{
text = new String( pc, pcStart, pcEnd - pcStart );
}
}
return text;
}
@Override
public char[] getTextCharacters( int[] holderForStartAndLength )
{
if ( eventType == TEXT )
{
if ( usePC )
{
holderForStartAndLength[0] = pcStart;
holderForStartAndLength[1] = pcEnd - pcStart;
return pc;
}
else
{
holderForStartAndLength[0] = posStart;
holderForStartAndLength[1] = posEnd - posStart;
return buf;
}
}
else if ( eventType == START_TAG || eventType == END_TAG || eventType == CDSECT || eventType == COMMENT
|| eventType == ENTITY_REF || eventType == PROCESSING_INSTRUCTION || eventType == IGNORABLE_WHITESPACE
|| eventType == DOCDECL )
{
holderForStartAndLength[0] = posStart;
holderForStartAndLength[1] = posEnd - posStart;
return buf;
}
else if ( eventType == START_DOCUMENT || eventType == END_DOCUMENT )
{