Coverage Report - org.apache.xmlrpc.parser.ByteArrayParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ByteArrayParser
59% 
75% 
3,2
 
 1  40
 /*
 2  
  * Copyright 1999,2005 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.xmlrpc.parser;
 17  
 
 18  
 import java.io.ByteArrayOutputStream;
 19  
 import java.io.IOException;
 20  
 
 21  
 import javax.xml.namespace.QName;
 22  
 
 23  
 import org.apache.ws.commons.util.Base64;
 24  
 import org.xml.sax.Attributes;
 25  
 import org.xml.sax.SAXException;
 26  
 import org.xml.sax.SAXParseException;
 27  
 
 28  
 
 29  
 /** A parser for base64 elements.
 30  
  */
 31  40
 public class ByteArrayParser extends TypeParserImpl {
 32  
         private int level;
 33  48
         private ByteArrayOutputStream baos;
 34  
         private Base64.Decoder decoder;
 35  
 
 36  
         public void startDocument() throws SAXException {
 37  40
                 level = 0;
 38  40
         }
 39  
 
 40  
         public void characters(char[] pChars, int pStart, int pLength) throws SAXException {
 41  40
                 if (baos == null) {
 42  0
                         if (!isEmpty(pChars, pStart, pLength)) {
 43  0
                                 throw new SAXParseException("Unexpected non-whitespace characters",
 44  0
                                                                                         getDocumentLocator());
 45  
                         }
 46  
                 } else {
 47  
                         try {
 48  40
                                 decoder.write(pChars, pStart, pLength);
 49  0
                         } catch (IOException e) {
 50  0
                                 throw new SAXParseException("Failed to decode base64 stream.", getDocumentLocator(), e);
 51  
                         }
 52  
                 }
 53  40
         }
 54  
 
 55  
         public void endElement(String pURI, String pLocalName, String pQName) throws SAXException {
 56  40
                 if (--level == 0) {
 57  
                         try {
 58  40
                                 decoder.flush();
 59  0
                         } catch (IOException e) {
 60  0
                                 throw new SAXParseException("Failed to decode base64 stream.", getDocumentLocator(), e);
 61  
                         }
 62  40
                         setResult(baos.toByteArray());
 63  
                 } else {
 64  0
                         throw new SAXParseException("Unexpected end tag in atomic element: "
 65  0
                                                                                 + new QName(pURI, pLocalName),
 66  0
                                                                                 getDocumentLocator());
 67  
                 }
 68  40
         }
 69  
 
 70  
         public void startElement(String pURI, String pLocalName, String pQName, Attributes pAttrs) throws SAXException {
 71  40
                 if (level++ == 0) {
 72  40
                         baos = new ByteArrayOutputStream();
 73  40
                         decoder = new Base64.Decoder(1024){
 74  
                                 protected void writeBuffer(byte[] pBytes, int pOffset, int pLen) throws IOException {
 75  48
                                         baos.write(pBytes, pOffset, pLen);
 76  48
                                 }
 77  
                         };
 78  
                 } else {
 79  0
                         throw new SAXParseException("Unexpected start tag in atomic element: "
 80  0
                                                                                 + new QName(pURI, pLocalName),
 81  0
                                                                                 getDocumentLocator());
 82  
                 }
 83  40
         }
 84  
 }