json/src/main/java/org/apidesign/html/json/impl/RcvrJSON.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 25 Aug 2013 09:36:33 +0200
changeset 255 62f7dafef4cf
child 262 30b03f2c82af
permissions -rw-r--r--
Introducing special JSON receiver rather than using tricks with Runnable and Object[]
jaroslav@255
     1
/**
jaroslav@255
     2
 * HTML via Java(tm) Language Bindings
jaroslav@255
     3
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@255
     4
 *
jaroslav@255
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@255
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@255
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@255
     8
 *
jaroslav@255
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@255
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@255
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@255
    12
 * GNU General Public License for more details. apidesign.org
jaroslav@255
    13
 * designates this particular file as subject to the
jaroslav@255
    14
 * "Classpath" exception as provided by apidesign.org
jaroslav@255
    15
 * in the License file that accompanied this code.
jaroslav@255
    16
 *
jaroslav@255
    17
 * You should have received a copy of the GNU General Public License
jaroslav@255
    18
 * along with this program. Look for COPYING file in the top folder.
jaroslav@255
    19
 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
jaroslav@255
    20
 */
jaroslav@255
    21
package org.apidesign.html.json.impl;
jaroslav@255
    22
jaroslav@255
    23
import net.java.html.BrwsrCtx;
jaroslav@255
    24
jaroslav@255
    25
/** Super type for those who wish to receive JSON messages.
jaroslav@255
    26
 *
jaroslav@255
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@255
    28
 */
jaroslav@255
    29
public abstract class RcvrJSON {
jaroslav@255
    30
    protected void onOpen(MsgEvnt msg) {}
jaroslav@255
    31
    protected abstract void onMessage(MsgEvnt msg);
jaroslav@255
    32
    protected void onClose(MsgEvnt msg) {}
jaroslav@255
    33
    protected abstract void onError(MsgEvnt msg);
jaroslav@255
    34
    
jaroslav@255
    35
    public abstract static class MsgEvnt {
jaroslav@255
    36
        MsgEvnt() {
jaroslav@255
    37
        }
jaroslav@255
    38
        
jaroslav@255
    39
        public Throwable getError() {
jaroslav@255
    40
            return null;
jaroslav@255
    41
        }
jaroslav@255
    42
        
jaroslav@255
    43
        public final Exception getException() {
jaroslav@255
    44
            Throwable t = getError();
jaroslav@255
    45
            if (t instanceof Exception) {
jaroslav@255
    46
                return (Exception)t;
jaroslav@255
    47
            }
jaroslav@255
    48
            if (t == null) {
jaroslav@255
    49
                return null;
jaroslav@255
    50
            }
jaroslav@255
    51
            return new Exception(t);
jaroslav@255
    52
        }
jaroslav@255
    53
        
jaroslav@255
    54
        public int dataSize() {
jaroslav@255
    55
            return -1;
jaroslav@255
    56
        }
jaroslav@255
    57
        
jaroslav@255
    58
        public <Data> void dataRead(BrwsrCtx ctx, Class<? extends Data> type, Data[] fillTheArray) {
jaroslav@255
    59
        }
jaroslav@255
    60
        
jaroslav@255
    61
        public abstract void dispatch(RcvrJSON r);
jaroslav@255
    62
        
jaroslav@255
    63
        public static MsgEvnt createError(final Throwable t) {
jaroslav@255
    64
            return new MsgEvnt() {
jaroslav@255
    65
                @Override
jaroslav@255
    66
                public Throwable getError() {
jaroslav@255
    67
                    return t;
jaroslav@255
    68
                }
jaroslav@255
    69
jaroslav@255
    70
                @Override
jaroslav@255
    71
                public void dispatch(RcvrJSON r) {
jaroslav@255
    72
                    r.onError(this);
jaroslav@255
    73
                }
jaroslav@255
    74
            };
jaroslav@255
    75
        }
jaroslav@255
    76
        
jaroslav@255
    77
        public static MsgEvnt createMessage(final Object value) {
jaroslav@255
    78
            return new MsgEvnt() {
jaroslav@255
    79
                @Override
jaroslav@255
    80
                public int dataSize() {
jaroslav@255
    81
                    if (value instanceof Object[]) {
jaroslav@255
    82
                        return ((Object[])value).length;
jaroslav@255
    83
                    } else {
jaroslav@255
    84
                        return 1;
jaroslav@255
    85
                    }
jaroslav@255
    86
                }
jaroslav@255
    87
                
jaroslav@255
    88
                @Override
jaroslav@255
    89
                public <Data> void dataRead(BrwsrCtx context, Class<? extends Data> type, Data[] arr) {
jaroslav@255
    90
                    if (value instanceof Object[]) {
jaroslav@255
    91
                        Object[] data = ((Object[]) value);
jaroslav@255
    92
                        for (int i = 0; i < data.length && i < arr.length; i++) {
jaroslav@255
    93
                            arr[i] = org.apidesign.html.json.impl.JSON.read(context, type, data[i]);
jaroslav@255
    94
                        }
jaroslav@255
    95
                    } else {
jaroslav@255
    96
                        if (arr.length > 0) {
jaroslav@255
    97
                            arr[0] = org.apidesign.html.json.impl.JSON.read(context, type, value);
jaroslav@255
    98
                        }
jaroslav@255
    99
                    }
jaroslav@255
   100
                }
jaroslav@255
   101
                
jaroslav@255
   102
                @Override
jaroslav@255
   103
                public void dispatch(RcvrJSON r) {
jaroslav@255
   104
                    r.onMessage(this);
jaroslav@255
   105
                }
jaroslav@255
   106
            };
jaroslav@255
   107
        }
jaroslav@255
   108
        
jaroslav@255
   109
        public static MsgEvnt createOpen() {
jaroslav@255
   110
            return new MsgEvnt() {
jaroslav@255
   111
                @Override
jaroslav@255
   112
                public void dispatch(RcvrJSON r) {
jaroslav@255
   113
                    r.onOpen(this);
jaroslav@255
   114
                }
jaroslav@255
   115
            };
jaroslav@255
   116
        }
jaroslav@255
   117
jaroslav@255
   118
        public static MsgEvnt createClose() {
jaroslav@255
   119
            return new MsgEvnt() {
jaroslav@255
   120
                @Override
jaroslav@255
   121
                public void dispatch(RcvrJSON r) {
jaroslav@255
   122
                    r.onClose(this);
jaroslav@255
   123
                }
jaroslav@255
   124
            };
jaroslav@255
   125
        }
jaroslav@255
   126
    } // end MsgEvnt
jaroslav@255
   127
}