Home » synapse-1.2-src » org.apache.synapse.format » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with 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,
   13    *  software distributed under the License is distributed on an
   14    *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   18    */
   19   package org.apache.synapse.format;
   20   
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   import java.io.OutputStream;
   24   
   25   import javax.activation.DataSource;
   26   import javax.xml.stream.XMLStreamException;
   27   import javax.xml.stream.XMLStreamReader;
   28   
   29   import org.apache.axiom.om.OMElement;
   30   
   31   /**
   32    * Data source that represents the text of a given {@link OMElement}.
   33    * <p>
   34    * The expression
   35    * <pre>new TextFromElementDataSource(element, charset, contentType)</pre>
   36    * produces a DataSource implementation that is equivalent to
   37    * <pre>new ByteArrayDataSource(element.getText().getBytes(charset), contentType)</pre>
   38    * but that is more efficient.
   39    */
   40   public class TextFromElementDataSource implements DataSource {
   41       private static class InputStreamImpl extends InputStream {
   42           private final XMLStreamReader reader;
   43           private final String charset;
   44           private byte[] buffer;
   45           private int offset;
   46           
   47           public InputStreamImpl(XMLStreamReader reader, String charset) {
   48               this.reader = reader;
   49               this.charset = charset;
   50           }
   51   
   52           @Override
   53           public int read() throws IOException {
   54               byte[] b = new byte[1];
   55               return read(b) == -1 ? -1 : b[0] & 0xFF;
   56           }
   57   
   58           @Override
   59           public int read(byte[] b) throws IOException {
   60               return read(b, 0, b.length);
   61           }
   62   
   63           @Override
   64           public int read(byte[] b, int off, int len) throws IOException {
   65               try {
   66                   int read = 0;
   67                   outer: while (len > 0) {
   68                       if (buffer == null || offset == buffer.length) {
   69                           // Refill buffer
   70                           while (true) {
   71                               if (!reader.hasNext()) {
   72                                   break outer;
   73                               }
   74                               int eventType = reader.next();
   75                               if (eventType == XMLStreamReader.CHARACTERS ||
   76                                       eventType == XMLStreamReader.CDATA) {
   77                                   // Note: this is not entirely correct for encodings such as UTF-16.
   78                                   // Once IO-158 is implemented, we could avoid this by implementing a
   79                                   // Reader and using ReaderInputStream.
   80                                   buffer = reader.getText().getBytes(charset);
   81                                   offset = 0;
   82                                   break;
   83                               }
   84                           }
   85                       }
   86                       int c = Math.min(len, buffer.length-offset);
   87                       System.arraycopy(buffer, offset, b, off, c);
   88                       offset += c;
   89                       off += c;
   90                       len -= c;
   91                       read += c;
   92                   }
   93                   return read == 0 ? -1 : read;
   94               }
   95               catch (XMLStreamException ex) {
   96                   IOException ioException = new IOException("Unable to read from XMLStreamReader");
   97                   ioException.initCause(ex);
   98                   throw ioException;
   99               }
  100           }
  101       }
  102       
  103       private final OMElement element;
  104       private final String charset;
  105       private final String contentType;
  106       
  107       public TextFromElementDataSource(OMElement element, String charset, String contentType) {
  108           this.element = element;
  109           this.charset = charset;
  110           this.contentType = contentType;
  111       }
  112       
  113       public String getContentType() {
  114           return contentType;
  115       }
  116   
  117       public String getName() {
  118           return null;
  119       }
  120   
  121       public InputStream getInputStream() throws IOException {
  122           return new InputStreamImpl(element.getXMLStreamReader(), charset);
  123       }
  124   
  125       public OutputStream getOutputStream() throws IOException {
  126           throw new UnsupportedOperationException();
  127       }
  128   }

Home » synapse-1.2-src » org.apache.synapse.format » [javadoc | source]