Moving Context into its own separate module, renaming to BrwsrCtx and making it flexible context
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 28 May 2013 13:31:42 +0200
branchcontext
changeset 1220cf9ba1d883c5
parent 1219 82bb83b955b6
child 1221 cba2fc5c544b
Moving Context into its own separate module, renaming to BrwsrCtx and making it flexible
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxtPrvdr.java
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java
ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxPrvdr.java
ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java
     1.1 --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java	Thu May 16 14:10:02 2013 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,135 +0,0 @@
     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.ko2brwsr;
    1.25 -
    1.26 -import java.io.ByteArrayOutputStream;
    1.27 -import java.io.IOException;
    1.28 -import java.io.InputStream;
    1.29 -import java.io.InputStreamReader;
    1.30 -import java.util.logging.Level;
    1.31 -import java.util.logging.Logger;
    1.32 -import net.java.html.json.Context;
    1.33 -import org.apidesign.html.json.spi.ContextBuilder;
    1.34 -import org.apidesign.html.json.spi.FunctionBinding;
    1.35 -import org.apidesign.html.json.spi.JSONCall;
    1.36 -import org.apidesign.html.json.spi.PropertyBinding;
    1.37 -import org.apidesign.html.json.spi.Technology;
    1.38 -import org.apidesign.html.json.spi.Transfer;
    1.39 -
    1.40 -/**
    1.41 - *
    1.42 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.43 - */
    1.44 -final class BrwsrCntxt implements Technology<Object>, Transfer {
    1.45 -    private BrwsrCntxt() {}
    1.46 -    
    1.47 -    public static final Context DEFAULT;
    1.48 -    static {
    1.49 -        BrwsrCntxt c = new BrwsrCntxt();
    1.50 -        DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build();
    1.51 -    }
    1.52 -    
    1.53 -    @Override
    1.54 -    public void extract(Object obj, String[] props, Object[] values) {
    1.55 -        ConvertTypes.extractJSON(obj, props, values);
    1.56 -    }
    1.57 -
    1.58 -    @Override
    1.59 -    public void loadJSON(final JSONCall call) {
    1.60 -        class R implements Runnable {
    1.61 -            Object[] arr = { null };
    1.62 -            @Override
    1.63 -            public void run() {
    1.64 -                call.notifySuccess(arr[0]);
    1.65 -            }
    1.66 -        }
    1.67 -        R r = new R();
    1.68 -        if (call.isJSONP()) {
    1.69 -            String me = ConvertTypes.createJSONP(r.arr, r);
    1.70 -            ConvertTypes.loadJSONP(call.composeURL(me), me);
    1.71 -        } else {
    1.72 -            String data = null;
    1.73 -            if (call.isDoOutput()) {
    1.74 -                try {
    1.75 -                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    1.76 -                    call.writeData(bos);
    1.77 -                    data = new String(bos.toByteArray(), "UTF-8");
    1.78 -                } catch (IOException ex) {
    1.79 -                    call.notifyError(ex);
    1.80 -                }
    1.81 -            }
    1.82 -            ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
    1.83 -        }
    1.84 -    }
    1.85 -
    1.86 -    @Override
    1.87 -    public Object wrapModel(Object model) {
    1.88 -        return model;
    1.89 -    }
    1.90 -
    1.91 -    @Override
    1.92 -    public void bind(PropertyBinding b, Object model, Object data) {
    1.93 -        Knockout.bind(data, b, b.getPropertyName(), 
    1.94 -            "getValue__Ljava_lang_Object_2", 
    1.95 -            b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", 
    1.96 -            false, false
    1.97 -        );
    1.98 -    }
    1.99 -
   1.100 -    @Override
   1.101 -    public void valueHasMutated(Object data, String propertyName) {
   1.102 -        Knockout.valueHasMutated(data, propertyName);
   1.103 -    }
   1.104 -
   1.105 -    @Override
   1.106 -    public void expose(FunctionBinding fb, Object model, Object d) {
   1.107 -        Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
   1.108 -    }
   1.109 -
   1.110 -    @Override
   1.111 -    public void applyBindings(Object data) {
   1.112 -        Knockout.applyBindings(data);
   1.113 -    }
   1.114 -
   1.115 -    @Override
   1.116 -    public Object wrapArray(Object[] arr) {
   1.117 -        return arr;
   1.118 -    }
   1.119 -
   1.120 -    @Override
   1.121 -    public <M> M toModel(Class<M> modelClass, Object data) {
   1.122 -        return modelClass.cast(data);
   1.123 -    }
   1.124 -
   1.125 -    @Override
   1.126 -    public Object toJSON(InputStream is) throws IOException {
   1.127 -        StringBuilder sb = new StringBuilder();
   1.128 -        InputStreamReader r = new InputStreamReader(is);
   1.129 -        for (;;) {
   1.130 -            int ch = r.read();
   1.131 -            if (ch == -1) {
   1.132 -                break;
   1.133 -            }
   1.134 -            sb.append((char)ch);
   1.135 -        }
   1.136 -        return ConvertTypes.parse(sb.toString());
   1.137 -    }
   1.138 -}
     2.1 --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxtPrvdr.java	Thu May 16 14:10:02 2013 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,49 +0,0 @@
     2.4 -/**
     2.5 - * HTML via Java(tm) Language Bindings
     2.6 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 - *
     2.8 - * This program is free software: you can redistribute it and/or modify
     2.9 - * it under the terms of the GNU General Public License as published by
    2.10 - * the Free Software Foundation, version 2 of the License.
    2.11 - *
    2.12 - * This program is distributed in the hope that it will be useful,
    2.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 - * GNU General Public License for more details. apidesign.org
    2.16 - * designates this particular file as subject to the
    2.17 - * "Classpath" exception as provided by apidesign.org
    2.18 - * in the License file that accompanied this code.
    2.19 - *
    2.20 - * You should have received a copy of the GNU General Public License
    2.21 - * along with this program. Look for COPYING file in the top folder.
    2.22 - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    2.23 - */
    2.24 -package org.apidesign.html.ko2brwsr;
    2.25 -
    2.26 -import net.java.html.json.Context;
    2.27 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
    2.28 -import org.apidesign.html.json.spi.ContextProvider;
    2.29 -import org.openide.util.lookup.ServiceProvider;
    2.30 -
    2.31 -/** This is an implementation package - just
    2.32 - * include its JAR on classpath and use official {@link Context} API
    2.33 - * to access the functionality.
    2.34 - * <p>
    2.35 - * Provides binding between models and <a href="http://bck2brwsr.apidesign.org">
    2.36 - * Bck2Brwsr</a> VM.
    2.37 - * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
    2.38 - *
    2.39 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.40 - */
    2.41 -@ServiceProvider(service = ContextProvider.class)
    2.42 -public final class BrwsrCntxtPrvdr implements ContextProvider {
    2.43 -    @Override
    2.44 -    public Context findContext(Class<?> requestor) {
    2.45 -        return bck2BrwsrVM() ? BrwsrCntxt.DEFAULT : null;
    2.46 -    }
    2.47 -    
    2.48 -    @JavaScriptBody(args = {  }, body = "return true;")
    2.49 -    private static boolean bck2BrwsrVM() {
    2.50 -        return false;
    2.51 -    }
    2.52 -}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java	Tue May 28 13:31:42 2013 +0200
     3.3 @@ -0,0 +1,129 @@
     3.4 +/**
     3.5 + * HTML via Java(tm) Language Bindings
     3.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details. apidesign.org
    3.16 + * designates this particular file as subject to the
    3.17 + * "Classpath" exception as provided by apidesign.org
    3.18 + * in the License file that accompanied this code.
    3.19 + *
    3.20 + * You should have received a copy of the GNU General Public License
    3.21 + * along with this program. Look for COPYING file in the top folder.
    3.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    3.23 + */
    3.24 +package org.apidesign.html.ko2brwsr;
    3.25 +
    3.26 +import java.io.ByteArrayOutputStream;
    3.27 +import java.io.IOException;
    3.28 +import java.io.InputStream;
    3.29 +import java.io.InputStreamReader;
    3.30 +import net.java.html.BrwsrCtx;
    3.31 +import org.apidesign.html.context.spi.Contexts;
    3.32 +import org.apidesign.html.json.spi.FunctionBinding;
    3.33 +import org.apidesign.html.json.spi.JSONCall;
    3.34 +import org.apidesign.html.json.spi.PropertyBinding;
    3.35 +import org.apidesign.html.json.spi.Technology;
    3.36 +import org.apidesign.html.json.spi.Transfer;
    3.37 +
    3.38 +/**
    3.39 + *
    3.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.41 + */
    3.42 +final class BrwsrCtxImpl implements Technology<Object>, Transfer {
    3.43 +    private BrwsrCtxImpl() {}
    3.44 +    
    3.45 +    public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl();
    3.46 +    
    3.47 +    @Override
    3.48 +    public void extract(Object obj, String[] props, Object[] values) {
    3.49 +        ConvertTypes.extractJSON(obj, props, values);
    3.50 +    }
    3.51 +
    3.52 +    @Override
    3.53 +    public void loadJSON(final JSONCall call) {
    3.54 +        class R implements Runnable {
    3.55 +            Object[] arr = { null };
    3.56 +            @Override
    3.57 +            public void run() {
    3.58 +                call.notifySuccess(arr[0]);
    3.59 +            }
    3.60 +        }
    3.61 +        R r = new R();
    3.62 +        if (call.isJSONP()) {
    3.63 +            String me = ConvertTypes.createJSONP(r.arr, r);
    3.64 +            ConvertTypes.loadJSONP(call.composeURL(me), me);
    3.65 +        } else {
    3.66 +            String data = null;
    3.67 +            if (call.isDoOutput()) {
    3.68 +                try {
    3.69 +                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    3.70 +                    call.writeData(bos);
    3.71 +                    data = new String(bos.toByteArray(), "UTF-8");
    3.72 +                } catch (IOException ex) {
    3.73 +                    call.notifyError(ex);
    3.74 +                }
    3.75 +            }
    3.76 +            ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data);
    3.77 +        }
    3.78 +    }
    3.79 +
    3.80 +    @Override
    3.81 +    public Object wrapModel(Object model) {
    3.82 +        return model;
    3.83 +    }
    3.84 +
    3.85 +    @Override
    3.86 +    public void bind(PropertyBinding b, Object model, Object data) {
    3.87 +        Knockout.bind(data, b, b.getPropertyName(), 
    3.88 +            "getValue__Ljava_lang_Object_2", 
    3.89 +            b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", 
    3.90 +            false, false
    3.91 +        );
    3.92 +    }
    3.93 +
    3.94 +    @Override
    3.95 +    public void valueHasMutated(Object data, String propertyName) {
    3.96 +        Knockout.valueHasMutated(data, propertyName);
    3.97 +    }
    3.98 +
    3.99 +    @Override
   3.100 +    public void expose(FunctionBinding fb, Object model, Object d) {
   3.101 +        Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2");
   3.102 +    }
   3.103 +
   3.104 +    @Override
   3.105 +    public void applyBindings(Object data) {
   3.106 +        Knockout.applyBindings(data);
   3.107 +    }
   3.108 +
   3.109 +    @Override
   3.110 +    public Object wrapArray(Object[] arr) {
   3.111 +        return arr;
   3.112 +    }
   3.113 +
   3.114 +    @Override
   3.115 +    public <M> M toModel(Class<M> modelClass, Object data) {
   3.116 +        return modelClass.cast(data);
   3.117 +    }
   3.118 +
   3.119 +    @Override
   3.120 +    public Object toJSON(InputStream is) throws IOException {
   3.121 +        StringBuilder sb = new StringBuilder();
   3.122 +        InputStreamReader r = new InputStreamReader(is);
   3.123 +        for (;;) {
   3.124 +            int ch = r.read();
   3.125 +            if (ch == -1) {
   3.126 +                break;
   3.127 +            }
   3.128 +            sb.append((char)ch);
   3.129 +        }
   3.130 +        return ConvertTypes.parse(sb.toString());
   3.131 +    }
   3.132 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxPrvdr.java	Tue May 28 13:31:42 2013 +0200
     4.3 @@ -0,0 +1,54 @@
     4.4 +/**
     4.5 + * HTML via Java(tm) Language Bindings
     4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details. apidesign.org
    4.16 + * designates this particular file as subject to the
    4.17 + * "Classpath" exception as provided by apidesign.org
    4.18 + * in the License file that accompanied this code.
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License
    4.21 + * along with this program. Look for COPYING file in the top folder.
    4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    4.23 + */
    4.24 +package org.apidesign.html.ko2brwsr;
    4.25 +
    4.26 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    4.27 +import org.apidesign.html.context.spi.Contexts;
    4.28 +import org.apidesign.html.json.spi.Technology;
    4.29 +import org.apidesign.html.json.spi.Transfer;
    4.30 +import org.openide.util.lookup.ServiceProvider;
    4.31 +
    4.32 +/** This is an implementation package - just
    4.33 + * include its JAR on classpath and use official {@link Context} API
    4.34 + * to access the functionality.
    4.35 + * <p>
    4.36 + * Provides binding between models and <a href="http://bck2brwsr.apidesign.org">
    4.37 + * Bck2Brwsr</a> VM.
    4.38 + * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it.
    4.39 + *
    4.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.41 + */
    4.42 +@ServiceProvider(service = Contexts.Provider.class)
    4.43 +public final class BrwsrCtxPrvdr implements Contexts.Provider {
    4.44 +
    4.45 +    @Override
    4.46 +    public void fillContext(Contexts.Builder context, Class<?> requestor) {
    4.47 +        if (bck2BrwsrVM()) {
    4.48 +            context.register(Technology.class, BrwsrCtxImpl.DEFAULT, 50).
    4.49 +            register(Transfer.class, BrwsrCtxImpl.DEFAULT, 50);
    4.50 +        }
    4.51 +    }
    4.52 +    
    4.53 +    @JavaScriptBody(args = {  }, body = "return true;")
    4.54 +    private static boolean bck2BrwsrVM() {
    4.55 +        return false;
    4.56 +    }
    4.57 +}
     5.1 --- a/ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java	Thu May 16 14:10:02 2013 +0200
     5.2 +++ b/ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java	Tue May 28 13:31:42 2013 +0200
     5.3 @@ -21,9 +21,12 @@
     5.4  package org.apidesign.html.ko2brwsr;
     5.5  
     5.6  import java.util.Map;
     5.7 -import net.java.html.json.Context;
     5.8 +import net.java.html.BrwsrCtx;
     5.9  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    5.10  import org.apidesign.bck2brwsr.vmtest.VMTest;
    5.11 +import org.apidesign.html.context.spi.Contexts;
    5.12 +import org.apidesign.html.json.spi.Technology;
    5.13 +import org.apidesign.html.json.spi.Transfer;
    5.14  import org.apidesign.html.json.tck.KnockoutTCK;
    5.15  import org.openide.util.lookup.ServiceProvider;
    5.16  import org.testng.annotations.Factory;
    5.17 @@ -42,8 +45,10 @@
    5.18      }
    5.19      
    5.20      @Override
    5.21 -    public Context createContext() {
    5.22 -        return BrwsrCntxt.DEFAULT;
    5.23 +    public BrwsrCtx createContext() {
    5.24 +        return Contexts.newBuilder().
    5.25 +            register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9).
    5.26 +            register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build();
    5.27      }
    5.28  
    5.29