Skip to content

Package: SAXBufferCreator

SAXBufferCreator

nameinstructionbranchcomplexitylinemethod
SAXBufferCreator()
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
SAXBufferCreator(MutableXMLStreamBuffer)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
cacheNamespaceAttribute(String, String)
M: 0 C: 46
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
characters(char[], int, int)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
comment(char[], int, int)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
create(XMLReader, InputStream)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
create(XMLReader, InputStream, String)
M: 15 C: 31
67%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 6 C: 9
60%
M: 0 C: 1
100%
endCDATA()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
endDTD()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
endDocument()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
endElement(String, String, String)
M: 0 C: 14
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
endEntity(String)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
endPrefixMapping(String)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
error(SAXParseException)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
fatalError(SAXParseException)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
ignorableWhitespace(char[], int, int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
notationDecl(String, String, String)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
processingInstruction(String, String)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
reset()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
resolveEntity(String, String)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setDocumentLocator(Locator)
M: 0 C: 1
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
skippedEntity(String)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
startCDATA()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
startDTD(String, String, String)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
startDocument()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
startElement(String, String, String, Attributes)
M: 0 C: 24
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
startEntity(String)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
startPrefixMapping(String, String)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
storeAttributes(Attributes)
M: 0 C: 38
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
storeNamespaceAttributes()
M: 0 C: 53
100%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 0 C: 11
100%
M: 0 C: 1
100%
storeQualifiedName(int, String, String, String)
M: 0 C: 28
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
unparsedEntityDecl(String, String, String, String)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
warning(SAXParseException)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2005, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package com.sun.xml.stream.buffer.sax;
12:
13: import com.sun.xml.stream.buffer.AbstractCreator;
14: import org.xml.sax.Attributes;
15: import org.xml.sax.SAXException;
16: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
17: import java.io.IOException;
18: import java.io.InputStream;
19: import org.xml.sax.ContentHandler;
20: import org.xml.sax.DTDHandler;
21: import org.xml.sax.EntityResolver;
22: import org.xml.sax.ErrorHandler;
23: import org.xml.sax.InputSource;
24: import org.xml.sax.Locator;
25: import org.xml.sax.SAXParseException;
26: import org.xml.sax.XMLReader;
27: import org.xml.sax.ext.LexicalHandler;
28:
29: /**
30: * Writes into {@link MutableXMLStreamBuffer} from SAX.
31: *
32: * TODO
33: * Implement the marking the stream on the element when an ID
34: * attribute on the element is defined
35: */
36: public class SAXBufferCreator extends AbstractCreator
37: implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler, LexicalHandler {
38: protected String[] _namespaceAttributes;
39:
40: protected int _namespaceAttributesPtr;
41:
42: private int depth = 0;
43:
44: public SAXBufferCreator() {
45: _namespaceAttributes = new String[16 * 2];
46: }
47:
48: public SAXBufferCreator(MutableXMLStreamBuffer buffer) {
49: this();
50: setBuffer(buffer);
51: }
52:
53: public MutableXMLStreamBuffer create(XMLReader reader, InputStream in) throws IOException, SAXException {
54: return create(reader, in, null);
55: }
56:
57: public MutableXMLStreamBuffer create(XMLReader reader, InputStream in, String systemId) throws IOException, SAXException {
58:• if (_buffer == null) {
59: createBuffer();
60: }
61: _buffer.setSystemId(systemId);
62: reader.setContentHandler(this);
63: reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this);
64:
65: try {
66: setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE));
67: } catch (SAXException e) {
68: }
69:
70:
71:• if (systemId != null) {
72: InputSource s = new InputSource(systemId);
73: s.setByteStream(in);
74: reader.parse(s);
75: } else {
76: reader.parse(new InputSource(in));
77: }
78:
79: return getXMLStreamBuffer();
80: }
81:
82: public void reset() {
83: _buffer = null;
84: _namespaceAttributesPtr = 0;
85: depth=0;
86: }
87:
88: @Override
89: public void startDocument() throws SAXException {
90: storeStructure(T_DOCUMENT);
91: }
92:
93: @Override
94: public void endDocument() throws SAXException {
95: storeStructure(T_END);
96: }
97:
98: @Override
99: public void startPrefixMapping(String prefix, String uri) throws SAXException {
100: cacheNamespaceAttribute(prefix, uri);
101: }
102:
103: @Override
104: public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
105: storeQualifiedName(T_ELEMENT_LN,
106: uri, localName, qName);
107:
108: // Has namespaces attributes
109:• if (_namespaceAttributesPtr > 0) {
110: storeNamespaceAttributes();
111: }
112:
113: // Has attributes
114:• if (attributes.getLength() > 0) {
115: storeAttributes(attributes);
116: }
117: depth++;
118: }
119:
120: @Override
121: public void endElement(String uri, String localName, String qName) throws SAXException {
122: storeStructure(T_END);
123:• if(--depth==0)
124: increaseTreeCount(); // one tree processed
125: }
126:
127: @Override
128: public void characters(char ch[], int start, int length) throws SAXException {
129: storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, ch, start, length);
130: }
131:
132: @Override
133: public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
134: characters(ch, start, length);
135: }
136:
137: @Override
138: public void processingInstruction(String target, String data) throws SAXException {
139: storeStructure(T_PROCESSING_INSTRUCTION);
140: storeStructureString(target);
141: storeStructureString(data);
142: }
143:
144: @Override
145: public void comment(char[] ch, int start, int length) throws SAXException {
146: storeContentCharacters(T_COMMENT_AS_CHAR_ARRAY, ch, start, length);
147: }
148:
149: //
150:
151: private void cacheNamespaceAttribute(String prefix, String uri) {
152: _namespaceAttributes[_namespaceAttributesPtr++] = prefix;
153: _namespaceAttributes[_namespaceAttributesPtr++] = uri;
154:
155:• if (_namespaceAttributesPtr == _namespaceAttributes.length) {
156: final String[] namespaceAttributes = new String[_namespaceAttributesPtr * 2];
157: System.arraycopy(_namespaceAttributes, 0, namespaceAttributes, 0, _namespaceAttributesPtr);
158: _namespaceAttributes = namespaceAttributes;
159: }
160: }
161:
162: private void storeNamespaceAttributes() {
163:• for (int i = 0; i < _namespaceAttributesPtr; i += 2) {
164: int item = T_NAMESPACE_ATTRIBUTE;
165:• if (_namespaceAttributes[i].length() > 0) {
166: item |= FLAG_PREFIX;
167: storeStructureString(_namespaceAttributes[i]);
168: }
169:• if (_namespaceAttributes[i + 1].length() > 0) {
170: item |= FLAG_URI;
171: storeStructureString(_namespaceAttributes[i + 1]);
172: }
173: storeStructure(item);
174: }
175: _namespaceAttributesPtr = 0;
176: }
177:
178: private void storeAttributes(Attributes attributes) {
179:• for (int i = 0; i < attributes.getLength(); i++) {
180: // Skip NS attributes. Some versions of JDK seem to send wrong local name
181: // Also it is not stored correctly by the following.
182:• if (attributes.getQName(i).startsWith("xmlns"))
183: continue;
184: storeQualifiedName(T_ATTRIBUTE_LN,
185: attributes.getURI(i),
186: attributes.getLocalName(i),
187: attributes.getQName(i));
188:
189: storeStructureString(attributes.getType(i));
190: storeContentString(attributes.getValue(i));
191: }
192: }
193:
194: private void storeQualifiedName(int item, String uri, String localName, String qName) {
195:• if (uri.length() > 0) {
196: item |= FLAG_URI;
197: storeStructureString(uri);
198: }
199:
200: storeStructureString(localName);
201:
202:• if (qName.indexOf(':') >= 0) {
203: item |= FLAG_QUALIFIED_NAME;
204: storeStructureString(qName);
205: }
206:
207: storeStructure(item);
208: }
209:
210:
211: // Empty methods for SAX handlers
212:
213: // Entity resolver handler
214:
215: @Override
216: public InputSource resolveEntity (String publicId, String systemId)
217:         throws IOException, SAXException
218: {
219:         return null;
220: }
221:
222: // DTD handler
223:
224: @Override
225: public void notationDecl (String name, String publicId, String systemId)
226:         throws SAXException
227: { }
228:
229: @Override
230: public void unparsedEntityDecl (String name, String publicId,
231:                                  String systemId, String notationName)
232:         throws SAXException
233: { }
234:
235: // Content handler
236:
237: @Override
238: public void setDocumentLocator (Locator locator) { }
239:
240: @Override
241: public void endPrefixMapping (String prefix) throws SAXException { }
242:
243: @Override
244: public void skippedEntity (String name) throws SAXException { }
245:
246: // Lexical handler
247:
248: @Override
249: public void startDTD(String name, String publicId, String systemId) throws SAXException { }
250:
251: @Override
252: public void endDTD() throws SAXException { }
253:
254: @Override
255: public void startEntity(String name) throws SAXException { }
256:
257: @Override
258: public void endEntity(String name) throws SAXException { }
259:
260: @Override
261: public void startCDATA() throws SAXException { }
262:
263: @Override
264: public void endCDATA() throws SAXException { }
265:
266: // Error handler
267:
268: @Override
269: public void warning(SAXParseException e) throws SAXException { }
270:
271: @Override
272: public void error(SAXParseException e) throws SAXException { }
273:
274: @Override
275: public void fatalError(SAXParseException e) throws SAXException
276: {
277:         throw e;
278: }
279: }