ByteArrayParser.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.parser;
00020 
00021 import java.io.ByteArrayOutputStream;
00022 import java.io.IOException;
00023 
00024 import javax.xml.namespace.QName;
00025 
00026 import org.apache.ws.commons.util.Base64;
00027 import org.xml.sax.Attributes;
00028 import org.xml.sax.SAXException;
00029 import org.xml.sax.SAXParseException;
00030 
00031 
00034 public class ByteArrayParser extends TypeParserImpl {
00035         private int level;
00036         private ByteArrayOutputStream baos;
00037         private Base64.Decoder decoder;
00038 
00039         public void startDocument() throws SAXException {
00040                 level = 0;
00041         }
00042 
00043         public void characters(char[] pChars, int pStart, int pLength) throws SAXException {
00044                 if (baos == null) {
00045                         if (!isEmpty(pChars, pStart, pLength)) {
00046                                 throw new SAXParseException("Unexpected non-whitespace characters",
00047                                                                                         getDocumentLocator());
00048                         }
00049                 } else {
00050                         try {
00051                                 decoder.write(pChars, pStart, pLength);
00052                         } catch (IOException e) {
00053                                 throw new SAXParseException("Failed to decode base64 stream.", getDocumentLocator(), e);
00054                         }
00055                 }
00056         }
00057 
00058         public void endElement(String pURI, String pLocalName, String pQName) throws SAXException {
00059                 if (--level == 0) {
00060                         try {
00061                                 decoder.flush();
00062                         } catch (IOException e) {
00063                                 throw new SAXParseException("Failed to decode base64 stream.", getDocumentLocator(), e);
00064                         }
00065                         setResult(baos.toByteArray());
00066                 } else {
00067                         throw new SAXParseException("Unexpected end tag in atomic element: "
00068                                                                                 + new QName(pURI, pLocalName),
00069                                                                                 getDocumentLocator());
00070                 }
00071         }
00072 
00073         public void startElement(String pURI, String pLocalName, String pQName, Attributes pAttrs) throws SAXException {
00074                 if (level++ == 0) {
00075                         baos = new ByteArrayOutputStream();
00076                         decoder = new Base64.Decoder(1024){
00077                                 protected void writeBuffer(byte[] pBytes, int pOffset, int pLen) throws IOException {
00078                                         baos.write(pBytes, pOffset, pLen);
00079                                 }
00080                         };
00081                 } else {
00082                         throw new SAXParseException("Unexpected start tag in atomic element: "
00083                                                                                 + new QName(pURI, pLocalName),
00084                                                                                 getDocumentLocator());
00085                 }
00086         }
00087 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49