Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 }