@@ -854,110 +854,114 @@ public void flush() throws IOException {
854
854
// --- utility methods
855
855
856
856
protected void writeAttributeValue (String value , Writer out ) throws IOException {
857
- // .[apostrophe and <, & escaped],
858
- final char quot = attributeUseApostrophe ? '\'' : '"' ;
859
- final String quotEntity = attributeUseApostrophe ? "'" : """ ;
860
-
861
- int pos = 0 ;
862
- for (int i = 0 ; i < value .length (); i ++) {
863
- char ch = value .charAt (i );
864
- if (ch == '&' ) {
865
- if (i > pos ) out .write (value .substring (pos , i ));
866
- out .write ("&" );
867
- pos = i + 1 ;
868
- }
869
- if (ch == '<' ) {
870
- if (i > pos ) out .write (value .substring (pos , i ));
871
- out .write ("<" );
872
- pos = i + 1 ;
873
- } else if (ch == quot ) {
874
- if (i > pos ) out .write (value .substring (pos , i ));
875
- out .write (quotEntity );
876
- pos = i + 1 ;
877
- } else if (ch < 32 ) {
878
- // in XML 1.0 only legal character are #x9 | #xA | #xD
879
- // and they must be escaped otherwise in attribute value they are normalized to spaces
880
- if (ch == 13 || ch == 10 || ch == 9 ) {
881
- if (i > pos ) out .write (value .substring (pos , i ));
882
- out .write ("&#" );
883
- out .write (Integer .toString (ch ));
884
- out .write (';' );
885
- pos = i + 1 ;
886
- } else {
887
- throw new IllegalStateException (
888
- "character " + Integer .toString (ch ) + " is not allowed in output" + getLocation ());
889
- // in XML 1.1 legal are [#x1-#xD7FF]
890
- // if(ch > 0) {
891
- // if(i > pos) out.write(text.substring(pos, i));
892
- // out.write("&#");
893
- // out.write(Integer.toString(ch));
894
- // out.write(';');
895
- // pos = i + 1;
896
- // } else {
897
- // throw new IllegalStateException(
898
- // "character zero is not allowed in XML 1.1 output"+getLocation());
899
- // }
900
- }
901
- }
902
- }
903
- if (pos > 0 ) {
904
- out .write (value .substring (pos ));
905
- } else {
906
- out .write (value ); // this is shortcut to the most common case
907
- }
908
- }
909
-
910
- protected void writeElementContent (String text , Writer out ) throws IOException {
911
- // escape '<', '&', ']]>', <32 if necessary
912
- int pos = 0 ;
913
- for (int i = 0 ; i < text .length (); i ++) {
914
- // TODO: check if doing char[] text.getChars() would be faster than getCharAt(i) ...
915
- char ch = text .charAt (i );
916
- if (ch == ']' ) {
917
- if (seenBracket ) {
918
- seenBracketBracket = true ;
919
- } else {
920
- seenBracket = true ;
921
- }
922
- } else {
857
+ if (value != null ) {
858
+ // .[apostrophe and <, & escaped],
859
+ final char quot = attributeUseApostrophe ? '\'' : '"' ;
860
+ final String quotEntity = attributeUseApostrophe ? "'" : """ ;
861
+
862
+ int pos = 0 ;
863
+ for (int i = 0 ; i < value .length (); i ++) {
864
+ char ch = value .charAt (i );
923
865
if (ch == '&' ) {
924
- if (i > pos ) out .write (text .substring (pos , i ));
866
+ if (i > pos ) out .write (value .substring (pos , i ));
925
867
out .write ("&" );
926
868
pos = i + 1 ;
927
- } else if (ch == '<' ) {
928
- if (i > pos ) out .write (text .substring (pos , i ));
869
+ }
870
+ if (ch == '<' ) {
871
+ if (i > pos ) out .write (value .substring (pos , i ));
929
872
out .write ("<" );
930
873
pos = i + 1 ;
931
- } else if (seenBracketBracket && ch == '>' ) {
932
- if (i > pos ) out .write (text .substring (pos , i ));
933
- out .write (">" );
874
+ } else if (ch == quot ) {
875
+ if (i > pos ) out .write (value .substring (pos , i ));
876
+ out .write (quotEntity );
934
877
pos = i + 1 ;
935
878
} else if (ch < 32 ) {
936
879
// in XML 1.0 only legal character are #x9 | #xA | #xD
937
- if (ch == 9 || ch == 10 || ch == 13 ) {
938
- // pass through
939
-
940
- // } else if(ch == 13) { //escape
880
+ // and they must be escaped otherwise in attribute value they are normalized to spaces
881
+ if (ch == 13 || ch == 10 || ch == 9 ) {
882
+ if (i > pos ) out .write (value .substring (pos , i ));
883
+ out .write ("&#" );
884
+ out .write (Integer .toString (ch ));
885
+ out .write (';' );
886
+ pos = i + 1 ;
887
+ } else {
888
+ throw new IllegalStateException (
889
+ "character " + Integer .toString (ch ) + " is not allowed in output" + getLocation ());
890
+ // in XML 1.1 legal are [#x1-#xD7FF]
891
+ // if(ch > 0) {
941
892
// if(i > pos) out.write(text.substring(pos, i));
942
893
// out.write("&#");
943
894
// out.write(Integer.toString(ch));
944
895
// out.write(';');
945
896
// pos = i + 1;
897
+ // } else {
898
+ // throw new IllegalStateException(
899
+ // "character zero is not allowed in XML 1.1 output"+getLocation());
900
+ // }
901
+ }
902
+ }
903
+ }
904
+ if (pos > 0 ) {
905
+ out .write (value .substring (pos ));
906
+ } else {
907
+ out .write (value ); // this is shortcut to the most common case
908
+ }
909
+ }
910
+ }
911
+
912
+ protected void writeElementContent (String text , Writer out ) throws IOException {
913
+ if (text != null ) {
914
+ // escape '<', '&', ']]>', <32 if necessary
915
+ int pos = 0 ;
916
+ for (int i = 0 ; i < text .length (); i ++) {
917
+ // TODO: check if doing char[] text.getChars() would be faster than getCharAt(i) ...
918
+ char ch = text .charAt (i );
919
+ if (ch == ']' ) {
920
+ if (seenBracket ) {
921
+ seenBracketBracket = true ;
946
922
} else {
947
- // skip special char
923
+ seenBracket = true ;
924
+ }
925
+ } else {
926
+ if (ch == '&' ) {
927
+ if (i > pos ) out .write (text .substring (pos , i ));
928
+ out .write ("&" );
929
+ pos = i + 1 ;
930
+ } else if (ch == '<' ) {
948
931
if (i > pos ) out .write (text .substring (pos , i ));
932
+ out .write ("<" );
949
933
pos = i + 1 ;
934
+ } else if (seenBracketBracket && ch == '>' ) {
935
+ if (i > pos ) out .write (text .substring (pos , i ));
936
+ out .write (">" );
937
+ pos = i + 1 ;
938
+ } else if (ch < 32 ) {
939
+ // in XML 1.0 only legal character are #x9 | #xA | #xD
940
+ if (ch == 9 || ch == 10 || ch == 13 ) {
941
+ // pass through
942
+
943
+ // } else if(ch == 13) { //escape
944
+ // if(i > pos) out.write(text.substring(pos, i));
945
+ // out.write("&#");
946
+ // out.write(Integer.toString(ch));
947
+ // out.write(';');
948
+ // pos = i + 1;
949
+ } else {
950
+ // skip special char
951
+ if (i > pos ) out .write (text .substring (pos , i ));
952
+ pos = i + 1 ;
953
+ }
954
+ }
955
+ if (seenBracket ) {
956
+ seenBracketBracket = seenBracket = false ;
950
957
}
951
- }
952
- if (seenBracket ) {
953
- seenBracketBracket = seenBracket = false ;
954
958
}
955
959
}
956
- }
957
- if ( pos > 0 ) {
958
- out . write ( text . substring ( pos ));
959
- } else {
960
- out . write ( text ); // this is shortcut to the most common case
960
+ if ( pos > 0 ) {
961
+ out . write ( text . substring ( pos ));
962
+ } else {
963
+ out . write ( text ); // this is shortcut to the most common case
964
+ }
961
965
}
962
966
}
963
967
0 commit comments