View Javadoc
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.SAXBufferCreator;
29  import javax.xml.transform.sax.SAXResult;
30  import org.xml.sax.ContentHandler;
31  import org.xml.sax.ext.LexicalHandler;
32  
33  /**
34   * A JAXP Result implementation that supports the serialization to an
35   * {@link MutableXMLStreamBuffer} for use by applications that expect a Result.
36   *
37   * <p>
38   * Reuse of a XMLStreamBufferResult more than once will require that the
39   * MutableXMLStreamBuffer is reset by called
40   * {@link #.getXMLStreamBuffer()}.reset(), or by calling
41   * {@link #.setXMLStreamBuffer()} with a new instance of
42   * {@link MutableXMLStreamBuffer}.
43   *
44   * <p>
45   * The derivation of XMLStreamBufferResult from SAXResult is an implementation
46   * detail.
47   *
48   * <p>General applications shall not call the following methods:
49   * <ul>
50   * <li>setHandler</li>
51   * <li>setLexicalHandler</li>
52   * <li>setSystemId</li>
53   * </ul>
54   */
55  public class XMLStreamBufferResult extends SAXResult {
56      protected MutableXMLStreamBuffer _buffer;
57      protected SAXBufferCreator _bufferCreator;
58  
59      /**
60       * The default XMLStreamBufferResult constructor.
61       *
62       * <p>
63       * A {@link MutableXMLStreamBuffer} is instantiated and used.
64       */
65      public XMLStreamBufferResult() {
66          setXMLStreamBuffer(new MutableXMLStreamBuffer());
67      }
68  
69      /**
70       * XMLStreamBufferResult constructor.
71       *
72       * @param buffer the {@link MutableXMLStreamBuffer} to use.
73       */
74      public XMLStreamBufferResult(MutableXMLStreamBuffer buffer) {
75          setXMLStreamBuffer(buffer);
76      }
77  
78      /**
79       * Get the {@link MutableXMLStreamBuffer} that is used.
80       *
81       * @return the {@link MutableXMLStreamBuffer}.
82       */
83      public MutableXMLStreamBuffer getXMLStreamBuffer() {
84          return _buffer;
85      }
86  
87      /**
88       * Set the {@link MutableXMLStreamBuffer} to use.
89       *
90       * @param buffer the {@link MutableXMLStreamBuffer}.
91       */
92      public void setXMLStreamBuffer(MutableXMLStreamBuffer buffer) {
93          if (buffer == null) {
94              throw new NullPointerException("buffer cannot be null");
95          }
96          _buffer = buffer;
97          setSystemId(_buffer.getSystemId());
98  
99          if (_bufferCreator != null) {
100             _bufferCreator.setXMLStreamBuffer(_buffer);
101         }
102     }
103 
104     public ContentHandler getHandler() {
105         if (_bufferCreator == null) {
106             _bufferCreator = new SAXBufferCreator(_buffer);
107             setHandler(_bufferCreator);
108         } else if (super.getHandler() == null) {
109             setHandler(_bufferCreator);
110         }
111 
112         return _bufferCreator;
113     }
114 
115     public LexicalHandler getLexicalHandler() {
116         return (LexicalHandler) getHandler();
117     }
118 }