forked from apache/xmlbeans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlOptions.java
More file actions
1544 lines (1336 loc) · 52.2 KB
/
XmlOptions.java
File metadata and controls
1544 lines (1336 loc) · 52.2 KB
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 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.xmlbeans;
import org.apache.xmlbeans.impl.store.Saaj;
import org.xml.sax.EntityResolver;
import org.xml.sax.XMLReader;
import javax.xml.namespace.QName;
import java.net.URI;
import java.util.*;
/**
* Used to supply options for loading, saving, and compiling, and validating.
* <p>
* There are two styles for using XmlOptions: multiline setup, and single-line use.
* Here are two examples. First, multiline style:
* <pre>{@code
* XmlOptions opts = new XmlOptions();
* opts.setSavePrettyPrint();
* opts.setSavePrettyPrintIndent(4);
* System.out.println(xobj.xmlText(opts));
* }</pre>
* <p>
* The alternative is single-line usage:
* <pre>{@code
* System.out.println(xobj.xmlText(
* new XmlOptions().setSavePrettyPrint().setSavePrettyPrintIndent(4)));
* }</pre>
* <p>
* Table showing where each option gets used.
* Note that:
* <ul>
* <li>options available for {@code newInstance} methods will also
* apply for {@code parse} methods</li>
* <li>options used for {@code validate} methods are also used for
* {@code compile} methods, since compilation usually implies
* validation against Schema for Schemas</li>
* </ul>
*
* <table border="1">
* <caption>Option matrix</caption>
* <tr>
* <th>{@code newInstance} methods</th>
* <th>{@code parse} methods</th>
* <th>{@code validate} methods</th>
* <th>{@code compile} methods</th>
* <th>{@code save} and {@code xmlText}methods</th>
* </tr>
* <tr>
* <td>{@code setDocumentType}<br>
* {@code setDocumentSourceName}<br>
* {@code setValidateOnSet}<br>
* {@code setUnsynchronized}</td>
* <td>{@code setLoad***}<br>
* {@code setEntityResolver}</td>
* <td>{@code setErrorListener}<br>
* {@code setValidateTreatLaxAsSkip}
* {@code setValidateStrict}</td>
* <td>{@code setErrorListener}<br>
* {@code setCompile***}<br>
* {@code setEntityResolver}<br>
* {@code setBaseURI}<br>
* {@code setGenerateJavaVersion}</td>
* <td>{@code setSave***}<br>
* {@code setUseDefaultNamespace}<br>
* {@code setCharacterEncoding}</td>
* </tr>
* </table>
*/
public class XmlOptions implements java.io.Serializable {
//
// Complete set of XmlOption's
//
// TODO - Add selectPath option to track the selection (default is to clean selections fast).
public enum XmlOptionsKeys {
SAVE_NAMESPACES_FIRST,
SAVE_SYNTHETIC_DOCUMENT_ELEMENT,
SAVE_PRETTY_PRINT,
SAVE_PRETTY_PRINT_INDENT,
SAVE_PRETTY_PRINT_OFFSET,
SAVE_AGGRESSIVE_NAMESPACES,
SAVE_USE_DEFAULT_NAMESPACE,
SAVE_IMPLICIT_NAMESPACES,
SAVE_SUGGESTED_PREFIXES,
SAVE_FILTER_PROCINST,
SAVE_USE_OPEN_FRAGMENT,
SAVE_OUTER,
SAVE_INNER,
SAVE_NO_XML_DECL,
SAVE_SUBSTITUTE_CHARACTERS,
SAVE_OPTIMIZE_FOR_SPEED,
SAVE_CDATA_LENGTH_THRESHOLD,
SAVE_CDATA_ENTITY_COUNT_THRESHOLD,
SAVE_SAX_NO_NSDECLS_IN_ATTRIBUTES,
LOAD_REPLACE_DOCUMENT_ELEMENT,
LOAD_STRIP_WHITESPACE,
LOAD_STRIP_COMMENTS,
LOAD_STRIP_PROCINSTS,
LOAD_LINE_NUMBERS,
LOAD_LINE_NUMBERS_END_ELEMENT,
LOAD_SAVE_CDATA_BOOKMARKS,
LOAD_SUBSTITUTE_NAMESPACES,
LOAD_TRIM_TEXT_BUFFER,
LOAD_ADDITIONAL_NAMESPACES,
LOAD_MESSAGE_DIGEST,
LOAD_USE_DEFAULT_RESOLVER,
LOAD_USE_XMLREADER,
XQUERY_CURRENT_NODE_VAR,
XQUERY_VARIABLE_MAP,
CHARACTER_ENCODING,
ERROR_LISTENER,
DOCUMENT_TYPE,
DOCUMENT_SOURCE_NAME,
COMPILE_SUBSTITUTE_NAMES,
COMPILE_NO_VALIDATION,
COMPILE_NO_UPA_RULE,
COMPILE_NO_PVR_RULE,
COMPILE_NO_ANNOTATIONS,
COMPILE_DOWNLOAD_URLS,
COMPILE_MDEF_NAMESPACES,
COMPILE_PARTIAL_TYPESYSTEM,
COMPILE_PARTIAL_METHODS,
COMPILE_ANNOTATION_JAVADOC,
VALIDATE_ON_SET,
VALIDATE_TREAT_LAX_AS_SKIP,
VALIDATE_STRICT,
VALIDATE_TEXT_ONLY,
UNSYNCHRONIZED,
ENTITY_RESOLVER,
BASE_URI,
SCHEMA_CODE_PRINTER,
GENERATE_JAVA_VERSION,
USE_SAME_LOCALE,
COPY_USE_NEW_SYNC_DOMAIN,
LOAD_ENTITY_BYTES_LIMIT,
ENTITY_EXPANSION_LIMIT,
LOAD_DTD_GRAMMAR,
LOAD_EXTERNAL_DTD,
DISALLOW_DOCTYPE_DECLARATION,
SAAJ_IMPL,
LOAD_USE_LOCALE_CHAR_UTIL,
XPATH_USE_SAXON,
XPATH_USE_XMLBEANS,
ATTRIBUTE_VALIDATION_COMPAT_MODE,
USE_JAVA_SHORT_NAME
}
public static final int DEFAULT_ENTITY_EXPANSION_LIMIT = 2048;
private static final XmlOptions EMPTY_OPTIONS;
static {
EMPTY_OPTIONS = new XmlOptions();
EMPTY_OPTIONS._map = Collections.unmodifiableMap(EMPTY_OPTIONS._map);
}
private static final long serialVersionUID = 1L;
private Map<XmlOptionsKeys, Object> _map = new HashMap<>();
/**
* Construct a new blank XmlOptions.
*/
public XmlOptions() {
}
/**
* Construct a new XmlOptions, copying the options.
*/
public XmlOptions(XmlOptions other) {
if (other != null) {
_map.putAll(other._map);
}
}
//
// Handy-dandy helper methods for setting some options
//
/**
* This option will cause the saver to save namespace attributes first.
*
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveNamespacesFirst() {
return setSaveNamespacesFirst(true);
}
public XmlOptions setSaveNamespacesFirst(boolean b) {
return set(XmlOptionsKeys.SAVE_NAMESPACES_FIRST, b);
}
public boolean isSaveNamespacesFirst() {
return hasOption(XmlOptionsKeys.SAVE_NAMESPACES_FIRST);
}
/**
* This option will cause the saver to reformat white space for easier reading.
*
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSavePrettyPrint() {
return setSavePrettyPrint(true);
}
public XmlOptions setSavePrettyPrint(boolean b) {
return set(XmlOptionsKeys.SAVE_PRETTY_PRINT, b);
}
public boolean isSavePrettyPrint() {
return hasOption(XmlOptionsKeys.SAVE_PRETTY_PRINT);
}
/**
* When used with {@code setSavePrettyPrint} this sets the indent
* amount to use.
*
* @param indent the indent amount to use
* @see #setSavePrettyPrint
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSavePrettyPrintIndent(int indent) {
return set(XmlOptionsKeys.SAVE_PRETTY_PRINT_INDENT, indent);
}
public Integer getSavePrettyPrintIndent() {
return (Integer) get(XmlOptionsKeys.SAVE_PRETTY_PRINT_INDENT);
}
/**
* When used with {@code setSavePrettyPrint} this sets the offset
* amount to use.
*
* @param offset the offset amount to use
* @see #setSavePrettyPrint
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSavePrettyPrintOffset(int offset) {
return set(XmlOptionsKeys.SAVE_PRETTY_PRINT_OFFSET, offset);
}
public Integer getSavePrettyPrintOffset() {
return (Integer) get(XmlOptionsKeys.SAVE_PRETTY_PRINT_OFFSET);
}
/**
* When writing a document, this sets the character
* encoding to use.
*
* @param encoding the character encoding
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
* @see XmlTokenSource#save(java.io.File, XmlOptions)
*/
public XmlOptions setCharacterEncoding(String encoding) {
return set(XmlOptionsKeys.CHARACTER_ENCODING, encoding);
}
public String getCharacterEncoding() {
return (String) get(XmlOptionsKeys.CHARACTER_ENCODING);
}
/**
* When parsing a document, this sets the type of the root
* element. If this is set, the parser will not try to guess
* the type based on the document's {@code QName}.
*
* @param type The root element's document type.
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setDocumentType(SchemaType type) {
return set(XmlOptionsKeys.DOCUMENT_TYPE, type);
}
public SchemaType getDocumentType() {
return (SchemaType) get(XmlOptionsKeys.DOCUMENT_TYPE);
}
/**
* <p>Sets a collection object for collecting {@link XmlError} objects
* during parsing, validation, and compilation. When set, the collection
* will contain all the errors after the operation takes place. Notice that
* the errors will only have line numbers if the document was
* loaded with line numbers enabled.</p>
*
* <p>The following simple example illustrates using an error listener
* during validation.</p>
*
* <pre>{@code
* // Create an XmlOptions instance and set the error listener.
* XmlOptions validateOptions = new XmlOptions();
* ArrayList<XmlError> errorList = new ArrayList<>();
* validateOptions.setErrorListener(errorList);
*
* // Validate the XML.
* boolean isValid = newEmp.validate(validateOptions);
*
* // If the XML isn't valid, loop through the listener's contents,
* // printing contained messages.
* if (!isValid)
* {
* for (int i = 0; i < errorList.size(); i++)
* {
* XmlError error = (XmlError)errorList.get(i);
*
* System.out.println("\n");
* System.out.println("Message: " + error.getMessage() + "\n");
* System.out.println("Location of invalid XML: " +
* error.getCursorLocation().xmlText() + "\n");
* }
* }
* }</pre>
*
* @param c A collection that will be filled with {@link XmlError} objects
* via {@link Collection#add}
* @see XmlError
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
* @see XmlObject#validate(XmlOptions)
* @see XmlBeans#compileXsd
* @see XmlOptions#setLoadLineNumbers
*/
public XmlOptions setErrorListener(Collection<XmlError> c) {
return set(XmlOptionsKeys.ERROR_LISTENER, c);
}
@SuppressWarnings("unchecked")
public Collection<XmlError> getErrorListener() {
return (Collection<XmlError>) get(XmlOptionsKeys.ERROR_LISTENER);
}
/**
* Causes the saver to reduce the number of namespace prefix declarations.
* The saver will do this by passing over the document twice, first to
* collect the set of needed namespace declarations, and then second
* to actually save the document with the declarations collected
* at the root.
*
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveAggressiveNamespaces() {
return setSaveAggressiveNamespaces(true);
}
public XmlOptions setSaveAggressiveNamespaces(boolean b) {
return set(XmlOptionsKeys.SAVE_AGGRESSIVE_NAMESPACES, b);
}
public boolean isSaveAggressiveNamespaces() {
return hasOption(XmlOptionsKeys.SAVE_AGGRESSIVE_NAMESPACES);
}
/**
* This option causes the saver to wrap the current fragment in
* an element with the given name.
*
* @param name the name to use for the top level element
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveSyntheticDocumentElement(QName name) {
return set(XmlOptionsKeys.SAVE_SYNTHETIC_DOCUMENT_ELEMENT, name);
}
public QName getSaveSyntheticDocumentElement() {
return (QName) get(XmlOptionsKeys.SAVE_SYNTHETIC_DOCUMENT_ELEMENT);
}
/**
* If this option is set, the saver will try to use the default
* namespace for the most commonly used URI. If it is not set
* the saver will always created named prefixes.
*
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setUseDefaultNamespace() {
return setUseDefaultNamespace(true);
}
public XmlOptions setUseDefaultNamespace(boolean b) {
return set(XmlOptionsKeys.SAVE_USE_DEFAULT_NAMESPACE, b);
}
public boolean isUseDefaultNamespace() {
return hasOption(XmlOptionsKeys.SAVE_USE_DEFAULT_NAMESPACE);
}
/**
* If namespaces have already been declared outside the scope of the
* fragment being saved, this allows those mappings to be passed
* down to the saver, so the prefixes are not re-declared.
*
* @param implicitNamespaces a map of prefixes to uris that can be
* used by the saver without being declared
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveImplicitNamespaces(Map<String, String> implicitNamespaces) {
return set(XmlOptionsKeys.SAVE_IMPLICIT_NAMESPACES, implicitNamespaces);
}
@SuppressWarnings("unchecked")
public Map<String, String> getSaveImplicitNamespaces() {
return (Map<String, String>) get(XmlOptionsKeys.SAVE_IMPLICIT_NAMESPACES);
}
/**
* A map of hints to pass to the saver for which prefixes to use
* for which namespace URI.
*
* @param suggestedPrefixes a map from URIs to prefixes
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveSuggestedPrefixes(Map<String, String> suggestedPrefixes) {
return set(XmlOptionsKeys.SAVE_SUGGESTED_PREFIXES, suggestedPrefixes);
}
@SuppressWarnings("unchecked")
public Map<String, String> getSaveSuggestedPrefixes() {
return (Map<String, String>) get(XmlOptionsKeys.SAVE_SUGGESTED_PREFIXES);
}
/**
* This option causes the saver to filter a Processing Instruction
* with the given target
*
* @param filterProcinst the name of a Processing Instruction to filter
* on save
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveFilterProcinst(String filterProcinst) {
return set(XmlOptionsKeys.SAVE_FILTER_PROCINST, filterProcinst);
}
public String getSaveFilterProcinst() {
return (String) get(XmlOptionsKeys.SAVE_FILTER_PROCINST);
}
/**
* This option causes the saver to replace characters with other values in
* the output stream. It is intended to be used for escaping non-standard
* characters during output.
*
* @param characterReplacementMap is an XmlOptionCharEscapeMap containing
* the characters to be escaped.
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
* @see XmlOptionCharEscapeMap
*/
public XmlOptions setSaveSubstituteCharacters(
XmlOptionCharEscapeMap characterReplacementMap) {
return set(XmlOptionsKeys.SAVE_SUBSTITUTE_CHARACTERS, characterReplacementMap);
}
public XmlOptionCharEscapeMap getSaveSubstituteCharacters() {
return (XmlOptionCharEscapeMap) get(XmlOptionsKeys.SAVE_SUBSTITUTE_CHARACTERS);
}
/**
* When saving a fragment, this option changes the qname of the synthesized
* root element. Normally <xml-fragment> is used.
*
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveUseOpenFrag() {
return setSaveUseOpenFrag(true);
}
public XmlOptions setSaveUseOpenFrag(boolean b) {
return set(XmlOptionsKeys.SAVE_USE_OPEN_FRAGMENT, b);
}
public boolean isSaveUseOpenFrag() {
return hasOption(XmlOptionsKeys.SAVE_USE_OPEN_FRAGMENT);
}
/**
* This option controls whether saving begins on the element or its contents
*
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveOuter() {
return setSaveOuter(true);
}
public XmlOptions setSaveOuter(boolean b) {
return set(XmlOptionsKeys.SAVE_OUTER, b);
}
public boolean isSaveOuter() {
return hasOption(XmlOptionsKeys.SAVE_OUTER);
}
/**
* This option controls whether saving begins on the element or its contents
*
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveInner() {
return setSaveInner(true);
}
public XmlOptions setSaveInner(boolean b) {
return set(XmlOptionsKeys.SAVE_INNER, b);
}
public boolean isSaveInner() {
return hasOption(XmlOptionsKeys.SAVE_INNER);
}
/**
* This option controls whether saving saves out the XML
* declaration {@code <?xml ... ?>}
*
* @see XmlTokenSource#save(java.io.File, XmlOptions)
* @see XmlTokenSource#xmlText(XmlOptions)
*/
public XmlOptions setSaveNoXmlDecl() {
return setSaveNoXmlDecl(true);
}
public XmlOptions setSaveNoXmlDecl(boolean b) {
return set(XmlOptionsKeys.SAVE_NO_XML_DECL, b);
}
public boolean isSaveNoXmlDecl() {
return hasOption(XmlOptionsKeys.SAVE_NO_XML_DECL);
}
/**
* This option controls when saving will use CDATA blocks.
* CDATA will be used if the folowing condition is true:
* <br>{@code textLength > cdataLengthThreshold && entityCount > cdataEntityCountThreshold}
* <br>The default value of cdataLengthThreshold is 32.
* <br>
* <br>Use the folowing values for these cases:
* <table border=1>
* <caption>Option matrix</caption>
* <tr><th>Scenario</th> <th>cdataLengthThreshold</th> <th>cdataEntityCountThreshold</th></tr>
* <tr><td>Every text is CDATA</td> <td>0</td> <td>-1</td></tr>
* <tr><td>Only text that has an entity is CDATA</td> <td>0</td> <td>0</td></tr>
* <tr><td>Only text longer than x chars is CDATA</td> <td>x</td> <td>-1</td></tr>
* <tr><td>Only text that has y entitazable chars is CDATA</td> <td>0</td> <td>y</td></tr>
* <tr><td>Only text longer than x chars and has y entitazable chars is CDATA</td> <td>x</td> <td>y</td></tr>
* </table>
*
* @see XmlOptions#setSaveCDataEntityCountThreshold(int)
*/
public XmlOptions setSaveCDataLengthThreshold(int cdataLengthThreshold) {
return set(XmlOptionsKeys.SAVE_CDATA_LENGTH_THRESHOLD, cdataLengthThreshold);
}
public Integer getSaveCDataLengthThreshold() {
return (Integer) get(XmlOptionsKeys.SAVE_CDATA_LENGTH_THRESHOLD);
}
/**
* This option controls when saving will use CDATA blocks.
* CDATA will be used if the folowing condition is true:
* <br>{@code textLength > cdataLengthThreshold && entityCount > cdataEntityCountThreshold}
* <br>The default value of cdataEntityCountThreshold is 5.
*
* @see XmlOptions#setSaveCDataLengthThreshold(int)
*/
public XmlOptions setSaveCDataEntityCountThreshold(int cdataEntityCountThreshold) {
return set(XmlOptionsKeys.SAVE_CDATA_ENTITY_COUNT_THRESHOLD, cdataEntityCountThreshold);
}
public Integer getSaveCDataEntityCountThreshold() {
return (Integer) get(XmlOptionsKeys.SAVE_CDATA_ENTITY_COUNT_THRESHOLD);
}
/**
* <p>Use this option when parsing and saving XML documents.</p>
*
* <p>For parsing this option will annotate the text fields in the store with CDataBookmark.</p>
*
* <p>For saving this option will save the text fields annotated with CDataBookmark as
* CDATA XML text.<br>
* Note: The SaveCDataEntityCountThreshold and SaveCDataLengthThreshold options and
* their default values still apply.</p>
*
* <p><b>Note: Due to the store representation, a CDATA will not be recognized
* if it is imediately after non CDATA text and all text following it will
* be considered CDATA.</b><br>
* Example:<br>
* <pre>{@code
* <a><![CDATA[cdata text]]></a> - is considered as: <a><![CDATA[cdata text]]></a>
* <b><![CDATA[cdata text]]> regular text</b> - is considered as: <b><![CDATA[cdata text regular text]]></b>
* <c>text <![CDATA[cdata text]]></c> - is considered as: <c>text cdata text</c>
* }</pre>
*
* <p>Sample code:
* <pre>{@code
* String xmlText = "<a>\n" +
* "<a><![CDATA[cdata text]]></a>\n" +
* "<b><![CDATA[cdata text]]> regular text</b>\n" +
* "<c>text <![CDATA[cdata text]]></c>\n" +
* "</a>";
* System.out.println(xmlText);
*
* XmlOptions opts = new XmlOptions();
* opts.setUseCDataBookmarks();
*
* XmlObject xo = org.apache.xmlbeans.impl.schema.XmlObjectFactory.parse( xmlText , opts);
*
* System.out.println("xo1:\n" + xo.xmlText(opts));
* System.out.println("\n");
*
* opts.setSavePrettyPrint();
* System.out.println("xo2:\n" + xo.xmlText(opts));
* }</pre>
*
* @see CDataBookmark
* @see CDataBookmark#CDATA_BOOKMARK
*/
public XmlOptions setUseCDataBookmarks() {
return set(XmlOptionsKeys.LOAD_SAVE_CDATA_BOOKMARKS);
}
public boolean isUseCDataBookmarks() {
return hasOption(XmlOptionsKeys.LOAD_SAVE_CDATA_BOOKMARKS);
}
/**
* This option controls whether namespace declarations are included as attributes in the
* startElement event. By default, up to and including XMLBeans 2.3.0 they were included, in
* subsequent versions, they are no longer included.
*/
public XmlOptions setSaveSaxNoNSDeclsInAttributes() {
return setSaveSaxNoNSDeclsInAttributes(true);
}
public XmlOptions setSaveSaxNoNSDeclsInAttributes(boolean b) {
return set(XmlOptionsKeys.SAVE_SAX_NO_NSDECLS_IN_ATTRIBUTES, b);
}
public boolean isSaveSaxNoNSDeclsInAttributes() {
return hasOption(XmlOptionsKeys.SAVE_SAX_NO_NSDECLS_IN_ATTRIBUTES);
}
/**
* If this option is set, the document element is replaced with the
* given QName when parsing. If null is supplied, the document element
* is removed.
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadReplaceDocumentElement(QName replacement) {
return set(XmlOptionsKeys.LOAD_REPLACE_DOCUMENT_ELEMENT, replacement);
}
public QName getLoadReplaceDocumentElement() {
return (QName) get(XmlOptionsKeys.LOAD_REPLACE_DOCUMENT_ELEMENT);
}
/**
* If this option is set, all insignificant whitespace is stripped
* when parsing a document. Can be used to save memory on large
* documents when you know there is no mixed content.
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadStripWhitespace() {
return setLoadStripWhitespace(true);
}
public XmlOptions setLoadStripWhitespace(boolean b) {
return set(XmlOptionsKeys.LOAD_STRIP_WHITESPACE, b);
}
public boolean isSetLoadStripWhitespace() {
return hasOption(XmlOptionsKeys.LOAD_STRIP_WHITESPACE);
}
/**
* If this option is set, all comments are stripped when parsing
* a document.
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadStripComments() {
return setLoadStripComments(true);
}
public XmlOptions setLoadStripComments(boolean b) {
return set(XmlOptionsKeys.LOAD_STRIP_COMMENTS, b);
}
public boolean isLoadStripComments() {
return hasOption(XmlOptionsKeys.LOAD_STRIP_COMMENTS);
}
/**
* If this option is set, all processing instructions
* are stripped when parsing a document.
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadStripProcinsts() {
return setLoadStripProcinsts(true);
}
public XmlOptions setLoadStripProcinsts(boolean b) {
return set(XmlOptionsKeys.LOAD_STRIP_PROCINSTS, b);
}
public boolean isLoadStripProcinsts() {
return hasOption(XmlOptionsKeys.LOAD_STRIP_PROCINSTS);
}
/**
* If this option is set, line number annotations are placed
* in the store when parsing a document. This is particularly
* useful when you want {@link XmlError} objects to contain
* line numbers.
* <br>Note: This adds line numbers info only for start tags.
* For line number info on end tags use:
* {@link XmlOptions#setLoadLineNumbersEndElement()}
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
* @see XmlError
*/
public XmlOptions setLoadLineNumbers() {
return setLoadLineNumbers(true);
}
public XmlOptions setLoadLineNumbers(boolean b) {
return set(XmlOptionsKeys.LOAD_LINE_NUMBERS, b);
}
public boolean isLoadLineNumbers() {
return hasOption(XmlOptionsKeys.LOAD_LINE_NUMBERS);
}
/**
* If this option is set, line number annotations are placed
* in the store when parsing a document. This is particularly
* useful when you want {@link XmlError} objects to contain
* line numbers. Use the option to load line numbers at the end of an element.
*/
public XmlOptions setLoadLineNumbersEndElement() {
return setLoadLineNumbersEndElement(true);
}
public XmlOptions setLoadLineNumbersEndElement(boolean b) {
setLoadLineNumbers(true);
return set(XmlOptionsKeys.LOAD_LINE_NUMBERS_END_ELEMENT, b);
}
public boolean isLoadLineNumbersEndElement() {
return hasOption(XmlOptionsKeys.LOAD_LINE_NUMBERS_END_ELEMENT);
}
/**
* This option sets a map of namespace uri substitutions that happen
* when parsing a document.
* <p>
* This is particularly useful if you
* have documents that use no namespace, but you wish to avoid
* the name collision problems that occur when you introduce
* schema definitions without a target namespace.
* <p>
* By mapping the empty string "" (the absence of a URI) to a specific
* namespace, you can force the parser to behave as if a no-namespace
* document were actually in the specified namespace. This allows you
* to type the instance according to a schema in a nonempty namespace,
* and therefore avoid the problematic practice of using schema
* definitions without a target namespace.
*
* @param substNamespaces a map of document URIs to replacement URIs
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadSubstituteNamespaces(Map<String, String> substNamespaces) {
return set(XmlOptionsKeys.LOAD_SUBSTITUTE_NAMESPACES, substNamespaces);
}
@SuppressWarnings("unchecked")
public Map<String, String> getLoadSubstituteNamespaces() {
return (Map<String, String>) get(XmlOptionsKeys.LOAD_SUBSTITUTE_NAMESPACES);
}
/**
* If this option is set, the underlying xml text buffer is trimmed
* immediately after parsing a document resulting in a smaller memory
* footprint. Use this option if you are loading a large number
* of unchanging documents that will stay in memory for some time.
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadTrimTextBuffer() {
return setLoadTrimTextBuffer(true);
}
public XmlOptions setLoadTrimTextBuffer(boolean b) {
return set(XmlOptionsKeys.LOAD_TRIM_TEXT_BUFFER, b);
}
public boolean isLoadTrimTextBuffer() {
return hasOption(XmlOptionsKeys.LOAD_TRIM_TEXT_BUFFER);
}
/**
* Set additional namespace mappings to be added when parsing
* a document.
*
* @param nses additional namespace mappings
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadAdditionalNamespaces(Map<String, String> nses) {
return set(XmlOptionsKeys.LOAD_ADDITIONAL_NAMESPACES, nses);
}
@SuppressWarnings("unchecked")
public Map<String, String> getLoadAdditionalNamespaces() {
return (Map<String, String>) get(XmlOptionsKeys.LOAD_ADDITIONAL_NAMESPACES);
}
/**
* If this option is set when loading from an InputStream or File, then
* the loader will compute a 160-bit SHA-1 message digest of the XML
* file while loading it and make it available via
* XmlObject.documentProperties().getMessageDigest();
* <br>
* The schema compiler uses message digests to detect and eliminate
* duplicate imported xsd files.
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadMessageDigest() {
return setLoadMessageDigest(true);
}
public XmlOptions setLoadMessageDigest(boolean b) {
return set(XmlOptionsKeys.LOAD_MESSAGE_DIGEST, b);
}
public boolean isLoadMessageDigest() {
return hasOption(XmlOptionsKeys.LOAD_MESSAGE_DIGEST);
}
/**
* By default, XmlBeans does not resolve entities when parsing xml
* documents (unless an explicit entity resolver is specified).
* Use this option to turn on entity resolving by default.
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadUseDefaultResolver() {
return setLoadUseDefaultResolver(true);
}
public XmlOptions setLoadUseDefaultResolver(boolean b) {
return set(XmlOptionsKeys.LOAD_USE_DEFAULT_RESOLVER, b);
}
public boolean isLoadUseDefaultResolver() {
return hasOption(XmlOptionsKeys.LOAD_USE_DEFAULT_RESOLVER);
}
/**
* By default, XmlBeans creates a JAXP parser,
* other parsers can be used by providing an XMLReader.
* For using the default JDK's SAX parser use:
* xmlOptions.setLoadUseXMLReader( SAXParserFactory.newInstance().newSAXParser().getXMLReader() );
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.io.File, XmlOptions)
*/
public XmlOptions setLoadUseXMLReader(XMLReader xmlReader) {
return set(XmlOptionsKeys.LOAD_USE_XMLREADER, xmlReader);
}
public XMLReader getLoadUseXMLReader() {
return (XMLReader) get(XmlOptionsKeys.LOAD_USE_XMLREADER);
}
/**
* Sets the name of the variable that represents
* the current node in a query expression.
*
* @param varName The new variable name to use for the query.
* @see XmlObject#execQuery
* @see XmlCursor#execQuery
*/
public XmlOptions setXqueryCurrentNodeVar(String varName) {
return set(XmlOptionsKeys.XQUERY_CURRENT_NODE_VAR, varName);
}
public String getXqueryCurrentNodeVar() {
return (String) get(XmlOptionsKeys.XQUERY_CURRENT_NODE_VAR);
}
/**
* Map the names and values of external variables in an xquery
* expression. The keys of the map are the variable names
* in the query without the '$' prefix. The values of the map
* are objects and can be any of the primitive wrapper classes,
* String, XmlObject, or XmlCursor. The mapping only applies to
* xquery and has no effect on xpath expressions.
*
* @param varMap a map from Strings to variable instances.
* @see XmlObject#execQuery
* @see XmlCursor#execQuery
*/
public XmlOptions setXqueryVariables(Map<String, Object> varMap) {
return set(XmlOptionsKeys.XQUERY_VARIABLE_MAP, varMap);
}
@SuppressWarnings("unchecked")
public Map<String, Object> getXqueryVariables() {
return (Map<String, Object>) get(XmlOptionsKeys.XQUERY_VARIABLE_MAP);
}
/**
* This option sets the document source name into the xml store
* when parsing a document. If a document is parsed from a
* File or URI, it is automatically set to the URI of the
* source; otherwise, for example, when parsing a String,
* you can use this option to specify the source name yourself.
*
* @see org.apache.xmlbeans.impl.schema.XmlObjectFactory#parse(java.lang.String, XmlOptions)
*/
public XmlOptions setDocumentSourceName(String documentSourceName) {
return set(XmlOptionsKeys.DOCUMENT_SOURCE_NAME, documentSourceName);
}
public String getDocumentSourceName() {
return (String) get(XmlOptionsKeys.DOCUMENT_SOURCE_NAME);
}
/**
* This option allows for {@code QName} substitution during schema compilation.
*
* @param nameMap a map from {@code QName}s to substitute {@code QName}s.
* @see XmlBeans#compileXsd
*/
public XmlOptions setCompileSubstituteNames(Map<QName, QName> nameMap) {
return set(XmlOptionsKeys.COMPILE_SUBSTITUTE_NAMES, nameMap);
}
@SuppressWarnings("unchecked")
public Map<QName, QName> getCompileSubstituteNames() {
return (Map<QName, QName>) get(XmlOptionsKeys.COMPILE_SUBSTITUTE_NAMES);
}
/**
* If this option is set, validation is not done on the Schema XmlBeans
* when building a {@code SchemaTypeSystem}
*
* @see XmlBeans#compileXsd
*/
public XmlOptions setCompileNoValidation() {
return set(XmlOptionsKeys.COMPILE_NO_VALIDATION);
}
public boolean isCompileNoValidation() {
return hasOption(XmlOptionsKeys.COMPILE_NO_VALIDATION);
}
/**
* If this option is set, the unique particle attribution rule is not
* enforced when building a {@code SchemaTypeSystem}. See
* <a target="_blank" href="http://www.w3.org/TR/xmlschema-1/#non-ambig">Appendix H of the XML Schema specification</a>
* for information on the UPA rule.