Skip to content

Package: TypedSAXBufferCreator

TypedSAXBufferCreator

nameinstructionbranchcomplexitylinemethod
TypedSAXBufferCreator(Map, Map)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
TypedSAXBufferCreator(Map, Map, MutableXMLStreamBuffer)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
cacheNamespaceAttribute(String, String)
M: 50 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
characters(char[], int, int)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
comment(char[], int, int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
create(Map, Map, InputStream)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
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: 42 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
createNewBufferFromXMLReader(Map, Map, XMLReader, InputStream)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createNewBufferFromXMLReader(Map, Map, XMLReader, InputStream, String)
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
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: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
endElement(String, String, String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
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: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
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: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
reset()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 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: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
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: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
startElement(String, String, String, Attributes)
M: 27 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
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: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
storeAttributes(Attributes)
M: 71 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
storeNamespaceAttributes()
M: 53 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
storeQualifiedName(int, String, String, String)
M: 28 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
storeTextContent()
M: 63 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 17 C: 0
0%
M: 1 C: 0
0%
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: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3: *
4: * Copyright (c) 2004-2018 Oracle and/or its affiliates. All rights reserved.
5: *
6: * Oracle licenses this file to You under the Apache License, Version 2.0
7: * (the "License"); you may not use this file except in compliance with
8: * the License. You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package com.sun.xml.fastinfoset.streambuffer;
20:
21: import com.sun.xml.fastinfoset.types.XSDataType;
22: import com.sun.xml.fastinfoset.types.LexicalSpaceConvertor;
23: import com.sun.xml.stream.buffer.AbstractCreator;
24: import javax.xml.parsers.ParserConfigurationException;
25: import org.xml.sax.Attributes;
26: import org.xml.sax.SAXException;
27: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
28: import com.sun.xml.stream.buffer.XMLStreamBuffer;
29: import com.sun.xml.stream.buffer.sax.Features;
30: import com.sun.xml.stream.buffer.sax.Properties;
31: import java.io.IOException;
32: import java.io.InputStream;
33: import java.util.Map;
34: import java.util.Set;
35: import javax.xml.parsers.SAXParserFactory;
36: import org.xml.sax.ContentHandler;
37: import org.xml.sax.DTDHandler;
38: import org.xml.sax.EntityResolver;
39: import org.xml.sax.ErrorHandler;
40: import org.xml.sax.InputSource;
41: import org.xml.sax.Locator;
42: import org.xml.sax.SAXParseException;
43: import org.xml.sax.XMLReader;
44: import org.xml.sax.ext.LexicalHandler;
45:
46: /**
47: * A creator of a {@link XMLStreamBuffer} that that produces the XML infoset
48: * using a {@link XMLReader} and converts text content and attribute values from
49: * the lexical space to value space.
50: */
51: public class TypedSAXBufferCreator extends AbstractCreator
52: implements EntityResolver, DTDHandler, ContentHandler, ErrorHandler, LexicalHandler {
53: /**
54: * The map of element local names to sets of types.
55: */
56: protected Map<String, Set<XSDataType>> _elements;
57:
58: /**
59: * The map of attribute local names to sets of types.
60: */
61: protected Map<String, Set<XSDataType>> _attributes;
62:
63: protected String[] _namespaceAttributes = new String[16 * 2];
64:
65: protected int _namespaceAttributesPtr;
66:
67: protected AccessibleStringBuilder _textContent = new AccessibleStringBuilder();
68:
69: protected boolean _storeTextContent;
70:
71: protected Set<XSDataType> _textContentTypes;
72:
73: public static XMLStreamBuffer create(
74: Map<String, Set<XSDataType>> elements,
75: Map<String, Set<XSDataType>> attributes,
76: InputStream in)
77: throws SAXException,IOException, ParserConfigurationException {
78: SAXParserFactory spf = SAXParserFactory.newInstance();
79: spf.setNamespaceAware(true);
80: return createNewBufferFromXMLReader(elements, attributes,
81: spf.newSAXParser().getXMLReader(), in);
82: }
83:
84: public static XMLStreamBuffer createNewBufferFromXMLReader(
85: Map<String, Set<XSDataType>> elements,
86: Map<String, Set<XSDataType>> attributes,
87: XMLReader reader,
88: InputStream in)
89: throws SAXException,IOException, ParserConfigurationException {
90: return createNewBufferFromXMLReader(elements, attributes, reader, in, null);
91: }
92:
93: public static XMLStreamBuffer createNewBufferFromXMLReader(
94: Map<String, Set<XSDataType>> elements,
95: Map<String, Set<XSDataType>> attributes,
96: XMLReader reader,
97: InputStream in,
98: String systemId)
99: throws SAXException,IOException, ParserConfigurationException {
100: TypedSAXBufferCreator t = new TypedSAXBufferCreator(elements, attributes);
101:
102: return t.create(reader, in, systemId);
103: }
104:
105: public TypedSAXBufferCreator(Map<String, Set<XSDataType>> elements,
106: Map<String, Set<XSDataType>> attributes) {
107: _elements = elements;
108: _attributes = attributes;
109: }
110:
111: public TypedSAXBufferCreator(Map<String, Set<XSDataType>> elements,
112: Map<String, Set<XSDataType>> attributes,
113: MutableXMLStreamBuffer buffer) {
114: this(elements, attributes);
115:
116: setBuffer(buffer);
117: }
118:
119: public MutableXMLStreamBuffer create(XMLReader reader, InputStream in) throws IOException, SAXException {
120: return create(reader, in, null);
121: }
122:
123: public MutableXMLStreamBuffer create(XMLReader reader, InputStream in,
124: String systemId) throws IOException, SAXException {
125:• if (_buffer == null) {
126: createBuffer();
127: }
128:
129: reader.setContentHandler(this);
130: reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this);
131:
132: try {
133: setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE));
134: } catch (SAXException e) {
135: }
136:
137:• if (systemId != null) {
138: InputSource s = new InputSource(systemId);
139: s.setByteStream(in);
140: reader.parse(s);
141: } else {
142: reader.parse(new InputSource(in));
143: }
144:
145: return getXMLStreamBuffer();
146: }
147:
148: public void reset() {
149: _buffer = null;
150: _namespaceAttributesPtr = 0;
151: }
152:
153:
154: public void startDocument() throws SAXException {
155: storeStructure(T_DOCUMENT);
156: }
157:
158: public void endDocument() throws SAXException {
159: storeStructure(T_END);
160: }
161:
162: public void startPrefixMapping(String prefix, String uri) throws SAXException {
163: storeTextContent();
164:
165: cacheNamespaceAttribute(prefix, uri);
166: }
167:
168: public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
169: storeTextContent();
170:
171: // Obtain the XS types associated with the local name of this
172: // element
173: _textContentTypes = _elements.get(localName);
174:
175: storeQualifiedName(T_ELEMENT_LN,
176: uri, localName, qName);
177:
178: // Has namespaces attributes
179:• if (_namespaceAttributesPtr > 0) {
180: storeNamespaceAttributes();
181: }
182:
183: // Has attributes
184:• if (attributes.getLength() > 0) {
185: storeAttributes(attributes);
186: }
187: }
188:
189: public void endElement(String uri, String localName, String qName) throws SAXException {
190: storeTextContent();
191:
192: storeStructure(T_END);
193: }
194:
195: public void characters(char ch[], int start, int length) throws SAXException {
196: _storeTextContent = true;
197: _textContent.append(ch, start, length);
198: }
199:
200: public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
201: characters(ch, start, length);
202: }
203:
204: public void processingInstruction(String target, String data) throws SAXException {
205: storeTextContent();
206:
207: storeStructure(T_PROCESSING_INSTRUCTION);
208: storeStructureString(target);
209: storeStructureString(data);
210: }
211:
212: public void comment(char[] ch, int start, int length) throws SAXException {
213: storeTextContent();
214:
215: storeContentCharacters(T_COMMENT_AS_CHAR_ARRAY, ch, start, length);
216: }
217:
218: //
219:
220: private void storeTextContent() {
221:• if (_storeTextContent) {
222: Object o = LexicalSpaceConvertor.convertToValueSpace(_textContentTypes,
223: _textContent.getValue(), 0, _textContent.length(),
224: LexicalSpaceConvertor.LexicalPreference.charArray);
225:
226: final boolean isCharArray = (o instanceof char[]);
227:
228:• if (o != null && !isCharArray) {
229: storeStructure(T_TEXT_AS_OBJECT);
230: storeContentObject(o);
231: } else {
232:• if (isCharArray) {
233: char[] ch = (char[])o;
234: storeContentCharacters(T_TEXT_AS_CHAR_ARRAY,
235: ch, 0, ch.length);
236: } else {
237: storeContentCharacters(T_TEXT_AS_CHAR_ARRAY,
238: _textContent.getValue(), 0, _textContent.length());
239: }
240: }
241:
242: _textContentTypes = null;
243: _storeTextContent = false;
244: _textContent.setLength(0);
245: }
246: }
247:
248: private void cacheNamespaceAttribute(String prefix, String uri) {
249: _namespaceAttributes[_namespaceAttributesPtr++] = prefix;
250: _namespaceAttributes[_namespaceAttributesPtr++] = uri;
251:
252:• if (_namespaceAttributesPtr == _namespaceAttributes.length) {
253: final String[] namespaceAttributes = new String[_namespaceAttributesPtr * 3 / 2 + 1];
254: System.arraycopy(_namespaceAttributes, 0, namespaceAttributes, 0, _namespaceAttributesPtr);
255: _namespaceAttributes = namespaceAttributes;
256: }
257: }
258:
259: private void storeNamespaceAttributes() {
260:• for (int i = 0; i < _namespaceAttributesPtr; i += 2) {
261: int item = T_NAMESPACE_ATTRIBUTE;
262:• if (_namespaceAttributes[i].length() > 0) {
263: item |= FLAG_PREFIX;
264: storeStructureString(_namespaceAttributes[i]);
265: }
266:• if (_namespaceAttributes[i + 1].length() > 0) {
267: item |= FLAG_URI;
268: storeStructureString(_namespaceAttributes[i + 1]);
269: }
270: storeStructure(item);
271: }
272: _namespaceAttributesPtr = 0;
273: }
274:
275: private void storeAttributes(Attributes attributes) {
276:• for (int i = 0; i < attributes.getLength(); i++) {
277: String value = attributes.getValue(i);
278: Object o = LexicalSpaceConvertor.convertToValueSpace(
279: _attributes.get(attributes.getLocalName(i)), value,
280: LexicalSpaceConvertor.LexicalPreference.string);
281:• boolean storeAsString = o == null || (o instanceof String);
282:
283:• storeQualifiedName(storeAsString ? T_ATTRIBUTE_LN : T_ATTRIBUTE_LN_OBJECT,
284: attributes.getURI(i),
285: attributes.getLocalName(i),
286: attributes.getQName(i));
287:
288: storeStructureString(attributes.getType(i));
289:
290:• if (storeAsString) {
291:• if (o instanceof String) {
292: storeContentString((String)o);
293: } else {
294: storeContentString(value);
295: }
296: } else {
297: storeContentObject(o);
298: }
299: }
300: }
301:
302: private void storeQualifiedName(int item, String uri, String localName, String qName) {
303:• if (uri.length() > 0) {
304: item |= FLAG_URI;
305: storeStructureString(uri);
306: }
307:
308: storeStructureString(localName);
309:
310:• if (qName.indexOf(':') >= 0) {
311: item |= FLAG_QUALIFIED_NAME;
312: storeStructureString(qName);
313: }
314:
315: storeStructure(item);
316: }
317:
318:
319: // Empty methods for SAX handlers
320:
321: // Entity resolver handler
322:
323: public InputSource resolveEntity (String publicId, String systemId)
324:         throws IOException, SAXException {
325:         return null;
326: }
327:
328: // DTD handler
329:
330: public void notationDecl (String name, String publicId, String systemId)
331:         throws SAXException { }
332:
333: public void unparsedEntityDecl (String name, String publicId,
334:                                  String systemId, String notationName)
335:         throws SAXException { }
336:
337: // Content handler
338:
339: public void setDocumentLocator (Locator locator) { }
340:
341: public void endPrefixMapping (String prefix) throws SAXException { }
342:
343: public void skippedEntity (String name) throws SAXException { }
344:
345: // Lexical handler
346:
347: public void startDTD(String name, String publicId, String systemId) throws SAXException { }
348:
349: public void endDTD() throws SAXException { }
350:
351: public void startEntity(String name) throws SAXException { }
352:
353: public void endEntity(String name) throws SAXException { }
354:
355: public void startCDATA() throws SAXException { }
356:
357: public void endCDATA() throws SAXException { }
358:
359: // Error handler
360:
361: public void warning(SAXParseException e) throws SAXException { }
362:
363: public void error(SAXParseException e) throws SAXException { }
364:
365: public void fatalError(SAXParseException e) throws SAXException {
366:         throw e;
367: }
368: }