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, 2018 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: public void startDocument() throws SAXException {
89: storeStructure(T_DOCUMENT);
90: }
91:
92: public void endDocument() throws SAXException {
93: storeStructure(T_END);
94: }
95:
96: public void startPrefixMapping(String prefix, String uri) throws SAXException {
97: cacheNamespaceAttribute(prefix, uri);
98: }
99:
100: public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
101: storeQualifiedName(T_ELEMENT_LN,
102: uri, localName, qName);
103:
104: // Has namespaces attributes
105:• if (_namespaceAttributesPtr > 0) {
106: storeNamespaceAttributes();
107: }
108:
109: // Has attributes
110:• if (attributes.getLength() > 0) {
111: storeAttributes(attributes);
112: }
113: depth++;
114: }
115:
116: public void endElement(String uri, String localName, String qName) throws SAXException {
117: storeStructure(T_END);
118:• if(--depth==0)
119: increaseTreeCount(); // one tree processed
120: }
121:
122: public void characters(char ch[], int start, int length) throws SAXException {
123: storeContentCharacters(T_TEXT_AS_CHAR_ARRAY, ch, start, length);
124: }
125:
126: public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
127: characters(ch, start, length);
128: }
129:
130: public void processingInstruction(String target, String data) throws SAXException {
131: storeStructure(T_PROCESSING_INSTRUCTION);
132: storeStructureString(target);
133: storeStructureString(data);
134: }
135:
136: public void comment(char[] ch, int start, int length) throws SAXException {
137: storeContentCharacters(T_COMMENT_AS_CHAR_ARRAY, ch, start, length);
138: }
139:
140: //
141:
142: private void cacheNamespaceAttribute(String prefix, String uri) {
143: _namespaceAttributes[_namespaceAttributesPtr++] = prefix;
144: _namespaceAttributes[_namespaceAttributesPtr++] = uri;
145:
146:• if (_namespaceAttributesPtr == _namespaceAttributes.length) {
147: final String[] namespaceAttributes = new String[_namespaceAttributesPtr * 2];
148: System.arraycopy(_namespaceAttributes, 0, namespaceAttributes, 0, _namespaceAttributesPtr);
149: _namespaceAttributes = namespaceAttributes;
150: }
151: }
152:
153: private void storeNamespaceAttributes() {
154:• for (int i = 0; i < _namespaceAttributesPtr; i += 2) {
155: int item = T_NAMESPACE_ATTRIBUTE;
156:• if (_namespaceAttributes[i].length() > 0) {
157: item |= FLAG_PREFIX;
158: storeStructureString(_namespaceAttributes[i]);
159: }
160:• if (_namespaceAttributes[i + 1].length() > 0) {
161: item |= FLAG_URI;
162: storeStructureString(_namespaceAttributes[i + 1]);
163: }
164: storeStructure(item);
165: }
166: _namespaceAttributesPtr = 0;
167: }
168:
169: private void storeAttributes(Attributes attributes) {
170:• for (int i = 0; i < attributes.getLength(); i++) {
171: // Skip NS attributes. Some versions of JDK seem to send wrong local name
172: // Also it is not stored correctly by the following.
173:• if (attributes.getQName(i).startsWith("xmlns"))
174: continue;
175: storeQualifiedName(T_ATTRIBUTE_LN,
176: attributes.getURI(i),
177: attributes.getLocalName(i),
178: attributes.getQName(i));
179:
180: storeStructureString(attributes.getType(i));
181: storeContentString(attributes.getValue(i));
182: }
183: }
184:
185: private void storeQualifiedName(int item, String uri, String localName, String qName) {
186:• if (uri.length() > 0) {
187: item |= FLAG_URI;
188: storeStructureString(uri);
189: }
190:
191: storeStructureString(localName);
192:
193:• if (qName.indexOf(':') >= 0) {
194: item |= FLAG_QUALIFIED_NAME;
195: storeStructureString(qName);
196: }
197:
198: storeStructure(item);
199: }
200:
201:
202: // Empty methods for SAX handlers
203:
204: // Entity resolver handler
205:
206: public InputSource resolveEntity (String publicId, String systemId)
207:         throws IOException, SAXException
208: {
209:         return null;
210: }
211:
212: // DTD handler
213:
214: public void notationDecl (String name, String publicId, String systemId)
215:         throws SAXException
216: { }
217:
218: public void unparsedEntityDecl (String name, String publicId,
219:                                  String systemId, String notationName)
220:         throws SAXException
221: { }
222:
223: // Content handler
224:
225: public void setDocumentLocator (Locator locator) { }
226:
227: public void endPrefixMapping (String prefix) throws SAXException { }
228:
229: public void skippedEntity (String name) throws SAXException { }
230:
231: // Lexical handler
232:
233: public void startDTD(String name, String publicId, String systemId) throws SAXException { }
234:
235: public void endDTD() throws SAXException { }
236:
237: public void startEntity(String name) throws SAXException { }
238:
239: public void endEntity(String name) throws SAXException { }
240:
241: public void startCDATA() throws SAXException { }
242:
243: public void endCDATA() throws SAXException { }
244:
245: // Error handler
246:
247: public void warning(SAXParseException e) throws SAXException { }
248:
249: public void error(SAXParseException e) throws SAXException { }
250:
251: public void fatalError(SAXParseException e) throws SAXException
252: {
253:         throw e;
254: }
255: }