Debian Codegraph @

net/sf/saxon/serialize/XML10ContentChecker.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.serialize;
9
10 import net.sf.saxon.event.ProxyReceiver;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.om.NodeName;
14 import net.sf.saxon.serialize.charcode.UTF16CharacterSet;
15 import net.sf.saxon.serialize.charcode.XMLCharacterData;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.SchemaType;
18 import net.sf.saxon.type.SimpleType;
19
20 /**
21 * This class is used on the serialization pipeline to check that the document conforms
22 * to XML 1.0 rules. It is placed on the pipeline only when the configuration permits
23 * XML 1.1 constructs, but the particular output document is being serialized as XML 1.0
24 *
25 * <p>Simplified in Saxon 9.6 because the rules for XML Names in 1.0 are now the same as
26 * the rules in 1.1; only the rules for valid characters are different.</p>
27 */
28
29 public class XML10ContentChecker extends ProxyReceiver {
30
31
32 public XML10ContentChecker(Receiver next) {
33 super(next);
34 }
35
36 /**
37 * Notify the start of an element
38 * @param elemName integer code identifying the name of the element within the name pool.
39 * @param typeCode integer code identifying the element's type within the name pool.
40 * @param location
41 * @param properties properties of the element node
42 */
43
44 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
45 nextReceiver.startElement(elemName, typeCode, location, properties);
46 }
47
48
49 /**
50 * Notify an attribute. Attributes are notified after the startElement event, and before any
51 * children. Namespaces and attributes may be intermingled.
52 *
53 * @param attName The name of the attribute
54 * @param typeCode The type of the attribute
55 * @param locationId the location of the node in the source, or of the instruction that created it
56 * @param properties Bit significant value. The following bits are defined:
57 * <dl>
58 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
59 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
60 * </dl>
61 * @throws IllegalStateException: attempt to output an attribute when there is no open element
62 * start tag
63 */
64
65 public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
66 checkString(value, locationId);
67 nextReceiver.attribute(attName, typeCode, value, locationId, properties);
68 }
69
70 /**
71 * Character data
72 */
73
74 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
75 checkString(chars, locationId);
76 nextReceiver.characters(chars, locationId, properties);
77 }
78
79 /**
80 * Output a comment
81 */
82
83 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
84 checkString(chars, locationId);
85 nextReceiver.comment(chars, locationId, properties);
86 }
87
88 /**
89 * Processing Instruction
90 */
91
92 public void processingInstruction(String target, /*@NotNull*/ CharSequence data, Location locationId, int properties) throws XPathException {
93 checkString(data, locationId);
94 nextReceiver.processingInstruction(target, data, locationId, properties);
95 }
96
97 /**
98 * Check that a string consists of valid XML 1.0 characters (UTF-16 encoded)
99 *
100 * @param in the string to be checked
101 * @param locationId the location of the string
102 */
103
104 private void checkString(CharSequence in, Location locationId) throws XPathException {
105 final int len = in.length();
106 for (int c = 0; c < len; c++) {
107 int ch32 = in.charAt(c);
108 if (UTF16CharacterSet.isHighSurrogate(ch32)) {
109 char low = in.charAt(++c);
110 ch32 = UTF16CharacterSet.combinePair((char) ch32, low);
111 }
112 if (!XMLCharacterData.isValid10(ch32)) {
113 XPathException err = new XPathException("The result tree contains a character not allowed by XML 1.0 (hex " +
114 Integer.toHexString(ch32) + ')');
115 err.setErrorCode("SERE0006");
116 err.setLocator(locationId);
117 throw err;
118 }
119 }
120 }
121
122 }
123