# HG changeset patch # User Jaroslav Tulach # Date 1369740702 -7200 # Node ID cf9ba1d883c5437a7af9f084db6a9397e5a0e372 # Parent 82bb83b955b6f819350dd0a9926f9dc3cedb5706 Moving Context into its own separate module, renaming to BrwsrCtx and making it flexible diff -r 82bb83b955b6 -r cf9ba1d883c5 ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxt.java Thu May 16 14:10:02 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,135 +0,0 @@ -/** - * HTML via Java(tm) Language Bindings - * Copyright (C) 2013 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. apidesign.org - * designates this particular file as subject to the - * "Classpath" exception as provided by apidesign.org - * in the License file that accompanied this code. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException - */ -package org.apidesign.html.ko2brwsr; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.logging.Level; -import java.util.logging.Logger; -import net.java.html.json.Context; -import org.apidesign.html.json.spi.ContextBuilder; -import org.apidesign.html.json.spi.FunctionBinding; -import org.apidesign.html.json.spi.JSONCall; -import org.apidesign.html.json.spi.PropertyBinding; -import org.apidesign.html.json.spi.Technology; -import org.apidesign.html.json.spi.Transfer; - -/** - * - * @author Jaroslav Tulach - */ -final class BrwsrCntxt implements Technology, Transfer { - private BrwsrCntxt() {} - - public static final Context DEFAULT; - static { - BrwsrCntxt c = new BrwsrCntxt(); - DEFAULT = ContextBuilder.create().withTechnology(c).withTransfer(c).build(); - } - - @Override - public void extract(Object obj, String[] props, Object[] values) { - ConvertTypes.extractJSON(obj, props, values); - } - - @Override - public void loadJSON(final JSONCall call) { - class R implements Runnable { - Object[] arr = { null }; - @Override - public void run() { - call.notifySuccess(arr[0]); - } - } - R r = new R(); - if (call.isJSONP()) { - String me = ConvertTypes.createJSONP(r.arr, r); - ConvertTypes.loadJSONP(call.composeURL(me), me); - } else { - String data = null; - if (call.isDoOutput()) { - try { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - call.writeData(bos); - data = new String(bos.toByteArray(), "UTF-8"); - } catch (IOException ex) { - call.notifyError(ex); - } - } - ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data); - } - } - - @Override - public Object wrapModel(Object model) { - return model; - } - - @Override - public void bind(PropertyBinding b, Object model, Object data) { - Knockout.bind(data, b, b.getPropertyName(), - "getValue__Ljava_lang_Object_2", - b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", - false, false - ); - } - - @Override - public void valueHasMutated(Object data, String propertyName) { - Knockout.valueHasMutated(data, propertyName); - } - - @Override - public void expose(FunctionBinding fb, Object model, Object d) { - Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2"); - } - - @Override - public void applyBindings(Object data) { - Knockout.applyBindings(data); - } - - @Override - public Object wrapArray(Object[] arr) { - return arr; - } - - @Override - public M toModel(Class modelClass, Object data) { - return modelClass.cast(data); - } - - @Override - public Object toJSON(InputStream is) throws IOException { - StringBuilder sb = new StringBuilder(); - InputStreamReader r = new InputStreamReader(is); - for (;;) { - int ch = r.read(); - if (ch == -1) { - break; - } - sb.append((char)ch); - } - return ConvertTypes.parse(sb.toString()); - } -} diff -r 82bb83b955b6 -r cf9ba1d883c5 ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxtPrvdr.java --- a/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCntxtPrvdr.java Thu May 16 14:10:02 2013 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -/** - * HTML via Java(tm) Language Bindings - * Copyright (C) 2013 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. apidesign.org - * designates this particular file as subject to the - * "Classpath" exception as provided by apidesign.org - * in the License file that accompanied this code. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException - */ -package org.apidesign.html.ko2brwsr; - -import net.java.html.json.Context; -import org.apidesign.bck2brwsr.core.JavaScriptBody; -import org.apidesign.html.json.spi.ContextProvider; -import org.openide.util.lookup.ServiceProvider; - -/** This is an implementation package - just - * include its JAR on classpath and use official {@link Context} API - * to access the functionality. - *

- * Provides binding between models and - * Bck2Brwsr VM. - * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it. - * - * @author Jaroslav Tulach - */ -@ServiceProvider(service = ContextProvider.class) -public final class BrwsrCntxtPrvdr implements ContextProvider { - @Override - public Context findContext(Class requestor) { - return bck2BrwsrVM() ? BrwsrCntxt.DEFAULT : null; - } - - @JavaScriptBody(args = { }, body = "return true;") - private static boolean bck2BrwsrVM() { - return false; - } -} diff -r 82bb83b955b6 -r cf9ba1d883c5 ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxImpl.java Tue May 28 13:31:42 2013 +0200 @@ -0,0 +1,129 @@ +/** + * HTML via Java(tm) Language Bindings + * Copyright (C) 2013 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. apidesign.org + * designates this particular file as subject to the + * "Classpath" exception as provided by apidesign.org + * in the License file that accompanied this code. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException + */ +package org.apidesign.html.ko2brwsr; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import net.java.html.BrwsrCtx; +import org.apidesign.html.context.spi.Contexts; +import org.apidesign.html.json.spi.FunctionBinding; +import org.apidesign.html.json.spi.JSONCall; +import org.apidesign.html.json.spi.PropertyBinding; +import org.apidesign.html.json.spi.Technology; +import org.apidesign.html.json.spi.Transfer; + +/** + * + * @author Jaroslav Tulach + */ +final class BrwsrCtxImpl implements Technology, Transfer { + private BrwsrCtxImpl() {} + + public static final BrwsrCtxImpl DEFAULT = new BrwsrCtxImpl(); + + @Override + public void extract(Object obj, String[] props, Object[] values) { + ConvertTypes.extractJSON(obj, props, values); + } + + @Override + public void loadJSON(final JSONCall call) { + class R implements Runnable { + Object[] arr = { null }; + @Override + public void run() { + call.notifySuccess(arr[0]); + } + } + R r = new R(); + if (call.isJSONP()) { + String me = ConvertTypes.createJSONP(r.arr, r); + ConvertTypes.loadJSONP(call.composeURL(me), me); + } else { + String data = null; + if (call.isDoOutput()) { + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + call.writeData(bos); + data = new String(bos.toByteArray(), "UTF-8"); + } catch (IOException ex) { + call.notifyError(ex); + } + } + ConvertTypes.loadJSON(call.composeURL(null), r.arr, r, call.getMethod(), data); + } + } + + @Override + public Object wrapModel(Object model) { + return model; + } + + @Override + public void bind(PropertyBinding b, Object model, Object data) { + Knockout.bind(data, b, b.getPropertyName(), + "getValue__Ljava_lang_Object_2", + b.isReadOnly() ? null : "setValue__VLjava_lang_Object_2", + false, false + ); + } + + @Override + public void valueHasMutated(Object data, String propertyName) { + Knockout.valueHasMutated(data, propertyName); + } + + @Override + public void expose(FunctionBinding fb, Object model, Object d) { + Knockout.expose(d, fb, fb.getFunctionName(), "call__VLjava_lang_Object_2Ljava_lang_Object_2"); + } + + @Override + public void applyBindings(Object data) { + Knockout.applyBindings(data); + } + + @Override + public Object wrapArray(Object[] arr) { + return arr; + } + + @Override + public M toModel(Class modelClass, Object data) { + return modelClass.cast(data); + } + + @Override + public Object toJSON(InputStream is) throws IOException { + StringBuilder sb = new StringBuilder(); + InputStreamReader r = new InputStreamReader(is); + for (;;) { + int ch = r.read(); + if (ch == -1) { + break; + } + sb.append((char)ch); + } + return ConvertTypes.parse(sb.toString()); + } +} diff -r 82bb83b955b6 -r cf9ba1d883c5 ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxPrvdr.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ko-bck2brwsr/src/main/java/org/apidesign/html/ko2brwsr/BrwsrCtxPrvdr.java Tue May 28 13:31:42 2013 +0200 @@ -0,0 +1,54 @@ +/** + * HTML via Java(tm) Language Bindings + * Copyright (C) 2013 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. apidesign.org + * designates this particular file as subject to the + * "Classpath" exception as provided by apidesign.org + * in the License file that accompanied this code. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException + */ +package org.apidesign.html.ko2brwsr; + +import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.apidesign.html.context.spi.Contexts; +import org.apidesign.html.json.spi.Technology; +import org.apidesign.html.json.spi.Transfer; +import org.openide.util.lookup.ServiceProvider; + +/** This is an implementation package - just + * include its JAR on classpath and use official {@link Context} API + * to access the functionality. + *

+ * Provides binding between models and + * Bck2Brwsr VM. + * Registers {@link ContextProvider}, so {@link ServiceLoader} can find it. + * + * @author Jaroslav Tulach + */ +@ServiceProvider(service = Contexts.Provider.class) +public final class BrwsrCtxPrvdr implements Contexts.Provider { + + @Override + public void fillContext(Contexts.Builder context, Class requestor) { + if (bck2BrwsrVM()) { + context.register(Technology.class, BrwsrCtxImpl.DEFAULT, 50). + register(Transfer.class, BrwsrCtxImpl.DEFAULT, 50); + } + } + + @JavaScriptBody(args = { }, body = "return true;") + private static boolean bck2BrwsrVM() { + return false; + } +} diff -r 82bb83b955b6 -r cf9ba1d883c5 ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java --- a/ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java Thu May 16 14:10:02 2013 +0200 +++ b/ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java Tue May 28 13:31:42 2013 +0200 @@ -21,9 +21,12 @@ package org.apidesign.html.ko2brwsr; import java.util.Map; -import net.java.html.json.Context; +import net.java.html.BrwsrCtx; import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.apidesign.html.context.spi.Contexts; +import org.apidesign.html.json.spi.Technology; +import org.apidesign.html.json.spi.Transfer; import org.apidesign.html.json.tck.KnockoutTCK; import org.openide.util.lookup.ServiceProvider; import org.testng.annotations.Factory; @@ -42,8 +45,10 @@ } @Override - public Context createContext() { - return BrwsrCntxt.DEFAULT; + public BrwsrCtx createContext() { + return Contexts.newBuilder(). + register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9). + register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build(); }