Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.json; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.lang.reflect.*; |
| 5 | import java.security.*; |
| 6 | import java.util.*; |
Stuart McCulloch | d602fe1 | 2012-07-20 16:50:39 +0000 | [diff] [blame] | 7 | import java.util.zip.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 8 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 9 | import aQute.lib.converter.*; |
| 10 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | public class Decoder implements Closeable { |
| 12 | final JSONCodec codec; |
| 13 | Reader reader; |
| 14 | int current; |
| 15 | MessageDigest digest; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 16 | Map<String,Object> extra; |
| 17 | String encoding = "UTF-8"; |
| 18 | |
| 19 | boolean strict; |
Stuart McCulloch | d602fe1 | 2012-07-20 16:50:39 +0000 | [diff] [blame] | 20 | boolean inflate; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | |
| 22 | Decoder(JSONCodec codec) { |
| 23 | this.codec = codec; |
| 24 | } |
| 25 | |
| 26 | public Decoder from(File file) throws Exception { |
| 27 | return from(new FileInputStream(file)); |
| 28 | } |
| 29 | |
| 30 | public Decoder from(InputStream in) throws Exception { |
Stuart McCulloch | d602fe1 | 2012-07-20 16:50:39 +0000 | [diff] [blame] | 31 | |
| 32 | if ( inflate) |
| 33 | in = new InflaterInputStream(in); |
| 34 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | return from(new InputStreamReader(in, encoding)); |
| 36 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 37 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | public Decoder charset(String encoding) { |
| 39 | this.encoding = encoding; |
| 40 | return this; |
| 41 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 42 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 43 | public Decoder strict() { |
| 44 | this.strict = true; |
| 45 | return this; |
| 46 | } |
| 47 | |
| 48 | public Decoder from(Reader in) throws Exception { |
| 49 | reader = in; |
| 50 | read(); |
| 51 | return this; |
| 52 | } |
| 53 | |
| 54 | public Decoder faq(String in) throws Exception { |
| 55 | return from(in.replace('\'', '"')); |
| 56 | } |
| 57 | |
| 58 | public Decoder from(String in) throws Exception { |
| 59 | return from(new StringReader(in)); |
| 60 | } |
| 61 | |
| 62 | public Decoder mark() throws NoSuchAlgorithmException { |
| 63 | if (digest == null) |
| 64 | digest = MessageDigest.getInstance("SHA1"); |
| 65 | digest.reset(); |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | public byte[] digest() { |
| 70 | if (digest == null) |
| 71 | return null; |
| 72 | |
| 73 | return digest.digest(); |
| 74 | } |
| 75 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 76 | public <T> T get(Class<T> clazz) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 77 | return (T) codec.decode(clazz, this); |
| 78 | } |
| 79 | |
| 80 | public Object get(Type type) throws Exception { |
| 81 | return codec.decode(type, this); |
| 82 | } |
| 83 | |
| 84 | public Object get() throws Exception { |
| 85 | return codec.decode(null, this); |
| 86 | } |
| 87 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 88 | public <T> T get(TypeReference<T> ref) throws Exception { |
| 89 | return (T) codec.decode(ref.getType(), this); |
| 90 | } |
| 91 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 92 | int read() throws Exception { |
| 93 | current = reader.read(); |
| 94 | if (digest != null) { |
| 95 | digest.update((byte) (current / 256)); |
| 96 | digest.update((byte) (current % 256)); |
| 97 | } |
| 98 | return current; |
| 99 | } |
| 100 | |
| 101 | int current() { |
| 102 | return current; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Skip any whitespace. |
| 107 | * |
| 108 | * @return |
| 109 | * @throws Exception |
| 110 | */ |
| 111 | int skipWs() throws Exception { |
| 112 | while (Character.isWhitespace(current())) |
| 113 | read(); |
| 114 | return current(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Skip any whitespace. |
| 119 | * |
| 120 | * @return |
| 121 | * @throws Exception |
| 122 | */ |
| 123 | int next() throws Exception { |
| 124 | read(); |
| 125 | return skipWs(); |
| 126 | } |
| 127 | |
| 128 | void expect(String s) throws Exception { |
| 129 | for (int i = 0; i < s.length(); i++) |
| 130 | if (!(s.charAt(i) == read())) |
| 131 | throw new IllegalArgumentException("Expected " + s + " but got something different"); |
| 132 | read(); |
| 133 | } |
| 134 | |
| 135 | public boolean isEof() throws Exception { |
| 136 | int c = skipWs(); |
| 137 | return c < 0; |
| 138 | } |
| 139 | |
| 140 | public void close() throws IOException { |
| 141 | reader.close(); |
| 142 | } |
| 143 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 144 | public Map<String,Object> getExtra() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 145 | if (extra == null) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 146 | extra = new HashMap<String,Object>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 147 | return extra; |
| 148 | } |
Stuart McCulloch | d602fe1 | 2012-07-20 16:50:39 +0000 | [diff] [blame] | 149 | |
| 150 | public Decoder inflate() { |
| 151 | if ( reader != null) |
| 152 | throw new IllegalStateException("Reader already set, inflate must come before from()"); |
| 153 | inflate = true; |
| 154 | return this; |
| 155 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 156 | } |