01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20: package com.sun.xml.stream.buffer;
21:
22: import com.sun.xml.stream.buffer.sax.SAXBufferProcessor;
23: import java.io.ByteArrayInputStream;
24: import javax.xml.transform.sax.SAXSource;
25: import org.xml.sax.InputSource;
26: import org.xml.sax.XMLReader;
27:
28: /**
29: * A JAXP Source implementation that supports the parsing
30: * of {@link XMLStreamBuffer} for use by applications that expect a Source.
31: *
32: * <p>
33: * The derivation of XMLStreamBufferSource from SAXSource is an implementation
34: * detail.
35: *
36: * <p>Applications shall obey the following restrictions:
37: * <ul>
38: * <li>The setXMLReader and setInputSource shall not be called.</li>
39: * <li>The XMLReader object obtained by the getXMLReader method shall
40: * be used only for parsing the InputSource object returned by
41: * the getInputSource method.</li>
42: * <li>The InputSource object obtained by the getInputSource method shall
43: * be used only for being parsed by the XMLReader object returned by
44: * the getXMLReader method.</li>
45: * </ul>
46: */
47: public class XMLStreamBufferSource extends SAXSource {
48: protected XMLStreamBuffer _buffer;
49: protected SAXBufferProcessor _bufferProcessor;
50:
51: /**
52: * XMLStreamBufferSource constructor.
53: *
54: * @param buffer the {@link XMLStreamBuffer} to use.
55: */
56: public XMLStreamBufferSource(XMLStreamBuffer buffer) {
57: super (new InputSource(new ByteArrayInputStream(new byte[0])));
58: setXMLStreamBuffer(buffer);
59: }
60:
61: /**
62: * Get the {@link XMLStreamBuffer} that is used.
63: *
64: * @return the {@link XMLStreamBuffer}.
65: */
66: public XMLStreamBuffer getXMLStreamBuffer() {
67: return _buffer;
68: }
69:
70: /**
71: * Set the {@link XMLStreamBuffer} to use.
72: *
73: * @param buffer the {@link XMLStreamBuffer}.
74: */
75: public void setXMLStreamBuffer(XMLStreamBuffer buffer) {
76: if (buffer == null) {
77: throw new NullPointerException("buffer cannot be null");
78: }
79: _buffer = buffer;
80:
81: if (_bufferProcessor != null) {
82: _bufferProcessor.setBuffer(_buffer, false);
83: }
84: }
85:
86: public XMLReader getXMLReader() {
87: if (_bufferProcessor == null) {
88: _bufferProcessor = new SAXBufferProcessor(_buffer, false);
89: setXMLReader(_bufferProcessor);
90: } else if (super.getXMLReader() == null) {
91: setXMLReader(_bufferProcessor);
92: }
93:
94: return _bufferProcessor;
95: }
96: }
|