Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2005-2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.ws.soap.axiom;
 
 
 
Non-caching payload in Axiom.

Author(s):
Jim Cummings
Arjen Poutsma
Since:
1.5.2
 
 class NonCachingPayload extends AbstractPayload {
 
     private static final int BUF_SIZE = 1024;
 
     NonCachingPayload(SOAPBody axiomBodySOAPFactory axiomFactory) {
         super(axiomBodyaxiomFactory);
     }
 
     @Override
     public Result getResultInternal() {
         return StaxUtils.createCustomStaxResult(new DelegatingStreamWriter());
     }
 
     @Override
     protected XMLStreamReader getStreamReader(OMElement payloadElement) {
         return payloadElement.getXMLStreamReaderWithoutCaching();
     }
 
     private class DelegatingStreamWriter implements XMLStreamWriter {
 
         private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
 
         private final XMLStreamWriter delegate;
 
         private QName name;
 
         private String encoding = "UTF-8";
 
         private int elementDepth = 0;
 
         private boolean payloadAdded = false;
 
         private DelegatingStreamWriter() {
             try {
                 this. = StAXUtils.createXMLStreamWriter();
             }
             catch (XMLStreamException ex) {
                 throw new AxiomSoapBodyException("Could not determine payload root element"ex);
             }
         }
 
         public void writeStartDocument() throws XMLStreamException {
             // ignored
         }
 
         public void writeStartDocument(String versionthrows XMLStreamException {
             // ignored
         }
 
         public void writeStartDocument(String encodingString versionthrows XMLStreamException {
             this. = encoding;
         }
 
         public void writeStartElement(String localNamethrows XMLStreamException {
             if ( == null) {
                  = new QName(localName);
            }
            ++;
            .writeStartElement(localName);
        }
        public void writeStartElement(String namespaceURIString localNamethrows XMLStreamException {
            if ( == null) {
                 = new QName(namespaceURIlocalName);
            }
            ++;
            .writeStartElement(namespaceURIlocalName);
        }
        public void writeStartElement(String prefixString localNameString namespaceURIthrows XMLStreamException {
            if ( == null) {
                 = new QName(namespaceURIlocalNameprefix);
            }
            ++;
            .writeStartElement(prefixlocalNamenamespaceURI);
        }
        public void writeEndElement() throws XMLStreamException {
            --;
            .writeEndElement();
            addPayload();
        }
        private void addPayload() throws XMLStreamException {
            if ( <= 0 && !) {
                .flush();
                if (.size() > 0) {
                    byte[] buf = .toByteArray();
                    OMDataSource dataSource = new ByteArrayDataSource(buf);
                    OMNamespace namespace =
                            getAxiomFactory().createOMNamespace(.getNamespaceURI(), .getPrefix());
                    OMElement payloadElement =
                            getAxiomFactory().createOMElement(dataSource.getLocalPart(), namespace);
                    getAxiomBody().addChild(payloadElement);
                     = true;
                }
            }
        }
        public void writeEmptyElement(String localNamethrows XMLStreamException {
            if ( == null) {
                 = new QName(localName);
            }
            .writeEmptyElement(localName);
            addPayload();
        }
        public void writeEmptyElement(String namespaceURIString localNamethrows XMLStreamException {
            if ( == null) {
                 = new QName(namespaceURIlocalName);
            }
            .writeEmptyElement(namespaceURIlocalName);
            addPayload();
        }
        public void writeEmptyElement(String prefixString localNameString namespaceURIthrows XMLStreamException {
            if ( == null) {
                 = new QName(namespaceURIlocalNameprefix);
            }
            .writeEmptyElement(prefixlocalNamenamespaceURI);
            addPayload();
        }
        public void writeEndDocument() throws XMLStreamException {
             = 0;
            .writeEndDocument();
            addPayload();
        }
        // Delegation
        public void close() throws XMLStreamException {
            addPayload();
            .close();
        }
        public void flush() throws XMLStreamException {
            .flush();
        }
        public NamespaceContext getNamespaceContext() {
            return .getNamespaceContext();
        }
        public String getPrefix(String urithrows XMLStreamException {
            return .getPrefix(uri);
        }
        public Object getProperty(String namethrows IllegalArgumentException {
            return .getProperty(name);
        }
        public void setDefaultNamespace(String urithrows XMLStreamException {
            .setDefaultNamespace(uri);
        }
        public void setNamespaceContext(NamespaceContext contextthrows XMLStreamException {
            .setNamespaceContext(context);
        }
        public void setPrefix(String prefixString urithrows XMLStreamException {
            .setPrefix(prefixuri);
        }
        public void writeAttribute(String localNameString valuethrows XMLStreamException {
            .writeAttribute(localNamevalue);
        }
        public void writeAttribute(String namespaceURIString localNameString valuethrows XMLStreamException {
            .writeAttribute(namespaceURIlocalNamevalue);
        }
        public void writeAttribute(String prefixString namespaceURIString localNameString value)
                throws XMLStreamException {
            .writeAttribute(prefixnamespaceURIlocalNamevalue);
        }
        public void writeCData(String datathrows XMLStreamException {
            .writeCData(data);
        }
        public void writeCharacters(char[] textint startint lenthrows XMLStreamException {
            .writeCharacters(textstartlen);
        }
        public void writeCharacters(String textthrows XMLStreamException {
            .writeCharacters(text);
        }
        public void writeComment(String datathrows XMLStreamException {
            .writeComment(data);
        }
        public void writeDefaultNamespace(String namespaceURIthrows XMLStreamException {
            .writeDefaultNamespace(namespaceURI);
        }
        public void writeDTD(String dtdthrows XMLStreamException {
            .writeDTD(dtd);
        }
        public void writeEntityRef(String namethrows XMLStreamException {
            .writeEntityRef(name);
        }
        public void writeNamespace(String prefixString namespaceURIthrows XMLStreamException {
            .writeNamespace(prefixnamespaceURI);
        }
        public void writeProcessingInstruction(String targetthrows XMLStreamException {
            .writeProcessingInstruction(target);
        }
        public void writeProcessingInstruction(String targetString datathrows XMLStreamException {
            .writeProcessingInstruction(targetdata);
        }
    }
New to GrepCode? Check out our FAQ X