1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, |
---|
13 | * software distributed under the License is distributed on an |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
---|
15 | * KIND, either express or implied. See the License for the |
---|
16 | * specific language governing permissions and limitations |
---|
17 | * under the License. |
---|
18 | */ |
---|
19 | |
---|
20 | var Guacamole = Guacamole || {}; |
---|
21 | |
---|
22 | /** |
---|
23 | * A reader which automatically handles the given input stream, assembling all |
---|
24 | * received blobs into a JavaScript object by appending them to each other, in |
---|
25 | * order, and decoding the result as JSON. Note that this object will overwrite |
---|
26 | * any installed event handlers on the given Guacamole.InputStream. |
---|
27 | * |
---|
28 | * @constructor |
---|
29 | * @param {Guacamole.InputStream} stream |
---|
30 | * The stream that JSON will be read from. |
---|
31 | */ |
---|
32 | Guacamole.JSONReader = function guacamoleJSONReader(stream) { |
---|
33 | |
---|
34 | /** |
---|
35 | * Reference to this Guacamole.JSONReader. |
---|
36 | * |
---|
37 | * @private |
---|
38 | * @type {Guacamole.JSONReader} |
---|
39 | */ |
---|
40 | var guacReader = this; |
---|
41 | |
---|
42 | /** |
---|
43 | * Wrapped Guacamole.StringReader. |
---|
44 | * |
---|
45 | * @private |
---|
46 | * @type {Guacamole.StringReader} |
---|
47 | */ |
---|
48 | var stringReader = new Guacamole.StringReader(stream); |
---|
49 | |
---|
50 | /** |
---|
51 | * All JSON read thus far. |
---|
52 | * |
---|
53 | * @private |
---|
54 | * @type {String} |
---|
55 | */ |
---|
56 | var json = ''; |
---|
57 | |
---|
58 | /** |
---|
59 | * Returns the current length of this Guacamole.JSONReader, in characters. |
---|
60 | * |
---|
61 | * @return {Number} |
---|
62 | * The current length of this Guacamole.JSONReader. |
---|
63 | */ |
---|
64 | this.getLength = function getLength() { |
---|
65 | return json.length; |
---|
66 | }; |
---|
67 | |
---|
68 | /** |
---|
69 | * Returns the contents of this Guacamole.JSONReader as a JavaScript |
---|
70 | * object. |
---|
71 | * |
---|
72 | * @return {Object} |
---|
73 | * The contents of this Guacamole.JSONReader, as parsed from the JSON |
---|
74 | * contents of the input stream. |
---|
75 | */ |
---|
76 | this.getJSON = function getJSON() { |
---|
77 | return JSON.parse(json); |
---|
78 | }; |
---|
79 | |
---|
80 | // Append all received text |
---|
81 | stringReader.ontext = function ontext(text) { |
---|
82 | |
---|
83 | // Append received text |
---|
84 | json += text; |
---|
85 | |
---|
86 | // Call handler, if present |
---|
87 | if (guacReader.onprogress) |
---|
88 | guacReader.onprogress(text.length); |
---|
89 | |
---|
90 | }; |
---|
91 | |
---|
92 | // Simply call onend when end received |
---|
93 | stringReader.onend = function onend() { |
---|
94 | if (guacReader.onend) |
---|
95 | guacReader.onend(); |
---|
96 | }; |
---|
97 | |
---|
98 | /** |
---|
99 | * Fired once for every blob of data received. |
---|
100 | * |
---|
101 | * @event |
---|
102 | * @param {Number} length |
---|
103 | * The number of characters received. |
---|
104 | */ |
---|
105 | this.onprogress = null; |
---|
106 | |
---|
107 | /** |
---|
108 | * Fired once this stream is finished and no further data will be written. |
---|
109 | * |
---|
110 | * @event |
---|
111 | */ |
---|
112 | this.onend = null; |
---|
113 | |
---|
114 | }; |
---|