Debian Codegraph @

net/sf/saxon/serialize/BinaryTextDecoder.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.serialize;
9
10 import net.sf.saxon.event.ProxyReceiver;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.trans.Err;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.tiny.CharSlice;
16 import net.sf.saxon.value.Base64BinaryValue;
17 import net.sf.saxon.value.HexBinaryValue;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.util.Properties;
23
24 /**
25 * This class generates decodes processing instructions in text output that represent text encoded
26 * in base64 binary or hexBinary
27 *
28 * @author Michael H. Kay
29 */
30
31 public class BinaryTextDecoder extends ProxyReceiver {
32
33 String outputEncoding = "utf8";
34
35 public BinaryTextDecoder(Receiver next, Properties details) throws XPathException {
36 super(next);
37 setOutputProperties(details);
38 }
39
40 /**
41 * Set output properties
42 *
43 * @param details the output serialization properties
44 */
45
46 public void setOutputProperties(Properties details) throws XPathException {
47 outputEncoding = details.getProperty("encoding", "utf8");
48 }
49
50
51 /**
52 * Output a processing instruction. <br>
53 * Does nothing with this output method, unless the saxon:recognize-binary option is set, and this is the
54 * processing instructions hex or b64. The name of the processing instruction may be followed by an encoding
55 * name, for example b64.ascii indicates base64-encoded ASCII strings; if no encoding is present, the encoding
56 * of the output method is assumed.
57 */
58
59 public void processingInstruction(String name, /*@NotNull*/ CharSequence value, Location locationId, int properties)
60 throws XPathException {
61 String encoding;
62 byte[] bytes = null;
63 int dot = name.indexOf('.');
64 if (dot >= 0 && dot != name.length() - 1) {
65 encoding = name.substring(dot + 1);
66 name = name.substring(0, dot);
67 } else {
68 encoding = outputEncoding;
69 }
70 if (name.equals("hex")) {
71 bytes = new HexBinaryValue(value).getBinaryValue();
72 } else if (name.equals("b64")) {
73 bytes = new Base64BinaryValue(value).getBinaryValue();
74 }
75 if (bytes != null) {
76 try {
77 ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
78 InputStreamReader reader = new InputStreamReader(stream, encoding);
79 char[] array = new char[bytes.length];
80 int used = reader.read(array, 0, array.length);
81 nextReceiver.characters(new CharSlice(array, 0, used), locationId, properties);
82 } catch (IOException e) {
83 throw new XPathException(
84 "Text output method: failed to decode binary data " + Err.wrap(value.toString(), Err.VALUE));
85 }
86 }
87 }
88
89
90 }
91