1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/RcvrJSON.java Sun Aug 25 09:36:33 2013 +0200
1.3 @@ -0,0 +1,127 @@
1.4 +/**
1.5 + * HTML via Java(tm) Language Bindings
1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details. apidesign.org
1.16 + * designates this particular file as subject to the
1.17 + * "Classpath" exception as provided by apidesign.org
1.18 + * in the License file that accompanied this code.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 + */
1.24 +package org.apidesign.html.json.impl;
1.25 +
1.26 +import net.java.html.BrwsrCtx;
1.27 +
1.28 +/** Super type for those who wish to receive JSON messages.
1.29 + *
1.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.31 + */
1.32 +public abstract class RcvrJSON {
1.33 + protected void onOpen(MsgEvnt msg) {}
1.34 + protected abstract void onMessage(MsgEvnt msg);
1.35 + protected void onClose(MsgEvnt msg) {}
1.36 + protected abstract void onError(MsgEvnt msg);
1.37 +
1.38 + public abstract static class MsgEvnt {
1.39 + MsgEvnt() {
1.40 + }
1.41 +
1.42 + public Throwable getError() {
1.43 + return null;
1.44 + }
1.45 +
1.46 + public final Exception getException() {
1.47 + Throwable t = getError();
1.48 + if (t instanceof Exception) {
1.49 + return (Exception)t;
1.50 + }
1.51 + if (t == null) {
1.52 + return null;
1.53 + }
1.54 + return new Exception(t);
1.55 + }
1.56 +
1.57 + public int dataSize() {
1.58 + return -1;
1.59 + }
1.60 +
1.61 + public <Data> void dataRead(BrwsrCtx ctx, Class<? extends Data> type, Data[] fillTheArray) {
1.62 + }
1.63 +
1.64 + public abstract void dispatch(RcvrJSON r);
1.65 +
1.66 + public static MsgEvnt createError(final Throwable t) {
1.67 + return new MsgEvnt() {
1.68 + @Override
1.69 + public Throwable getError() {
1.70 + return t;
1.71 + }
1.72 +
1.73 + @Override
1.74 + public void dispatch(RcvrJSON r) {
1.75 + r.onError(this);
1.76 + }
1.77 + };
1.78 + }
1.79 +
1.80 + public static MsgEvnt createMessage(final Object value) {
1.81 + return new MsgEvnt() {
1.82 + @Override
1.83 + public int dataSize() {
1.84 + if (value instanceof Object[]) {
1.85 + return ((Object[])value).length;
1.86 + } else {
1.87 + return 1;
1.88 + }
1.89 + }
1.90 +
1.91 + @Override
1.92 + public <Data> void dataRead(BrwsrCtx context, Class<? extends Data> type, Data[] arr) {
1.93 + if (value instanceof Object[]) {
1.94 + Object[] data = ((Object[]) value);
1.95 + for (int i = 0; i < data.length && i < arr.length; i++) {
1.96 + arr[i] = org.apidesign.html.json.impl.JSON.read(context, type, data[i]);
1.97 + }
1.98 + } else {
1.99 + if (arr.length > 0) {
1.100 + arr[0] = org.apidesign.html.json.impl.JSON.read(context, type, value);
1.101 + }
1.102 + }
1.103 + }
1.104 +
1.105 + @Override
1.106 + public void dispatch(RcvrJSON r) {
1.107 + r.onMessage(this);
1.108 + }
1.109 + };
1.110 + }
1.111 +
1.112 + public static MsgEvnt createOpen() {
1.113 + return new MsgEvnt() {
1.114 + @Override
1.115 + public void dispatch(RcvrJSON r) {
1.116 + r.onOpen(this);
1.117 + }
1.118 + };
1.119 + }
1.120 +
1.121 + public static MsgEvnt createClose() {
1.122 + return new MsgEvnt() {
1.123 + @Override
1.124 + public void dispatch(RcvrJSON r) {
1.125 + r.onClose(this);
1.126 + }
1.127 + };
1.128 + }
1.129 + } // end MsgEvnt
1.130 +}