2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.json.impl;
23 import net.java.html.BrwsrCtx;
25 /** Super type for those who wish to receive JSON messages.
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 public abstract class RcvrJSON {
30 protected void onOpen(MsgEvnt msg) {}
31 protected abstract void onMessage(MsgEvnt msg);
32 protected void onClose(MsgEvnt msg) {}
33 protected abstract void onError(MsgEvnt msg);
35 public abstract static class MsgEvnt {
39 public Throwable getError() {
43 public final Exception getException() {
44 Throwable t = getError();
45 if (t instanceof Exception) {
51 return new Exception(t);
54 public int dataSize() {
58 public <Data> void dataRead(BrwsrCtx ctx, Class<? extends Data> type, Data[] fillTheArray) {
61 public abstract void dispatch(RcvrJSON r);
63 public static MsgEvnt createError(final Throwable t) {
64 return new MsgEvnt() {
66 public Throwable getError() {
71 public void dispatch(RcvrJSON r) {
77 public static MsgEvnt createMessage(final Object value) {
78 return new MsgEvnt() {
80 public int dataSize() {
81 if (value instanceof Object[]) {
82 return ((Object[])value).length;
89 public <Data> void dataRead(BrwsrCtx context, Class<? extends Data> type, Data[] arr) {
90 if (value instanceof Object[]) {
91 Object[] data = ((Object[]) value);
92 for (int i = 0; i < data.length && i < arr.length; i++) {
93 arr[i] = org.apidesign.html.json.impl.JSON.read(context, type, data[i]);
97 arr[0] = org.apidesign.html.json.impl.JSON.read(context, type, value);
103 public void dispatch(RcvrJSON r) {
109 public static MsgEvnt createOpen() {
110 return new MsgEvnt() {
112 public void dispatch(RcvrJSON r) {
118 public static MsgEvnt createClose() {
119 return new MsgEvnt() {
121 public void dispatch(RcvrJSON r) {