1 /* 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.stream.buffer; 27 28 import com.sun.xml.internal.stream.buffer.sax.SAXBufferProcessor; 29 import java.io.ByteArrayInputStream; 30 import javax.xml.transform.sax.SAXSource; 31 import org.xml.sax.InputSource; 32 import org.xml.sax.XMLReader; 33 34 /** 35 * A JAXP Source implementation that supports the parsing 36 * of {@link XMLStreamBuffer} for use by applications that expect a Source. 37 * 38 * <p> 39 * The derivation of XMLStreamBufferSource from SAXSource is an implementation 40 * detail. 41 * 42 * <p>Applications shall obey the following restrictions: 43 * <ul> 44 * <li>The setXMLReader and setInputSource shall not be called.</li> 45 * <li>The XMLReader object obtained by the getXMLReader method shall 46 * be used only for parsing the InputSource object returned by 47 * the getInputSource method.</li> 48 * <li>The InputSource object obtained by the getInputSource method shall 49 * be used only for being parsed by the XMLReader object returned by 50 * the getXMLReader method.</li> 51 * </ul> 52 */ 53 public class XMLStreamBufferSource extends SAXSource { 54 protected XMLStreamBuffer _buffer; 55 protected SAXBufferProcessor _bufferProcessor; 56 57 /** 58 * XMLStreamBufferSource constructor. 59 * 60 * @param buffer the {@link XMLStreamBuffer} to use. 61 */ 62 public XMLStreamBufferSource(XMLStreamBuffer buffer) { 63 super(new InputSource( 64 new ByteArrayInputStream(new byte[0]))); 65 setXMLStreamBuffer(buffer); 66 } 67 68 /** 69 * Get the {@link XMLStreamBuffer} that is used. 70 * 71 * @return the {@link XMLStreamBuffer}. 72 */ 73 public XMLStreamBuffer getXMLStreamBuffer() { 74 return _buffer; 75 } 76 77 /** 78 * Set the {@link XMLStreamBuffer} to use. 79 * 80 * @param buffer the {@link XMLStreamBuffer}. 81 */ 82 public void setXMLStreamBuffer(XMLStreamBuffer buffer) { 83 if (buffer == null) { 84 throw new NullPointerException("buffer cannot be null"); 85 } 86 _buffer = buffer; 87 88 if (_bufferProcessor != null) { 89 _bufferProcessor.setBuffer(_buffer,false); 90 } 91 } 92 93 public XMLReader getXMLReader() { 94 if (_bufferProcessor == null) { 95 _bufferProcessor = new SAXBufferProcessor(_buffer,false); 96 setXMLReader(_bufferProcessor); 97 } else if (super.getXMLReader() == null) { 98 setXMLReader(_bufferProcessor); 99 } 100 101 return _bufferProcessor; 102 } 103 }