| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public void setOutputProperties(Properties details) throws XPathException { |
| 47 |
outputEncoding = details.getProperty("encoding", "utf8"); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public void processingInstruction(String name, 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 |
|