1 |
40 |
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
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 |
|
|
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 |
|
} |